Skip to content

Commit b86ee2b

Browse files
committed
History js
1 parent 0d10123 commit b86ee2b

File tree

11 files changed

+143
-123
lines changed

11 files changed

+143
-123
lines changed

internal/conf/getconfig.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ func Get(path string) (config models.Conf) {
1616
viper.SetDefault("COLOR", "dark")
1717
viper.SetDefault("NODEPATH", "")
1818
// viper.SetDefault("SCANER", "arpscan")
19-
viper.SetDefault("ARPARGS", "")
19+
viper.SetDefault("ARP_ARGS", "")
2020
viper.SetDefault("IFACES", "")
2121
viper.SetDefault("TIMEOUT", 60)
22-
viper.SetDefault("IGNOREIP", "no")
22+
viper.SetDefault("TRIM_HIST", 48)
2323

2424
viper.SetConfigFile(path)
2525
viper.SetConfigType("yaml")
@@ -34,10 +34,10 @@ func Get(path string) (config models.Conf) {
3434
config.Color = viper.Get("COLOR").(string)
3535
config.NodePath = viper.Get("NODEPATH").(string)
3636
// config.Scaner = viper.Get("SCANER").(string)
37-
config.ArpArgs = viper.Get("ARPARGS").(string)
37+
config.ArpArgs = viper.Get("ARP_ARGS").(string)
3838
config.Ifaces = viper.Get("IFACES").(string)
3939
config.Timeout = viper.GetInt("TIMEOUT")
40-
config.IgnoreIP = viper.Get("IGNOREIP").(string)
40+
config.TrimHist = viper.GetInt("TRIM_HIST")
4141

4242
return config
4343
}
@@ -54,10 +54,10 @@ func Write(config models.Conf) {
5454
viper.Set("COLOR", config.Color)
5555
viper.Set("NODEPATH", config.NodePath)
5656
// viper.Set("SCANER", config.Scaner)
57-
viper.Set("ARPARGS", config.ArpArgs)
57+
viper.Set("ARP_ARGS", config.ArpArgs)
5858
viper.Set("IFACES", config.Ifaces)
5959
viper.Set("TIMEOUT", config.Timeout)
60-
viper.Set("IGNOREIP", config.IgnoreIP)
60+
viper.Set("TRIM_HIST", config.TrimHist)
6161

6262
err := viper.WriteConfig()
6363
check.IfError(err)

internal/models/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Conf struct {
1414
// Scaner string
1515
ArpArgs string
1616
Timeout int
17-
IgnoreIP string
17+
TrimHist int
1818
}
1919

2020
// Host - one host

internal/web/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package web
33
import (
44
"log/slog"
55
"net/http"
6+
"strconv"
67

78
"github.com/gin-gonic/gin"
89

@@ -34,10 +35,14 @@ func saveConfigHandler(c *gin.Context) {
3435
appConfig.Theme = c.PostForm("theme")
3536
appConfig.Color = c.PostForm("color")
3637
appConfig.NodePath = c.PostForm("node")
37-
appConfig.IgnoreIP = c.PostForm("ignore")
3838
appConfig.ArpArgs = c.PostForm("arpargs")
3939
appConfig.Ifaces = c.PostForm("ifaces")
4040

41+
timeout := c.PostForm("timeout")
42+
trimHist := c.PostForm("trim")
43+
appConfig.Timeout, _ = strconv.Atoi(timeout)
44+
appConfig.TrimHist, _ = strconv.Atoi(trimHist)
45+
4146
conf.Write(appConfig)
4247

4348
slog.Info("writing new config to " + appConfig.ConfPath)

internal/web/public/js/hist-html.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
function getHistHTML(hist) {
3+
4+
let html = '', col, title;
5+
6+
for (let h of hist) {
7+
if (h.Now != 0) {
8+
col = `fill:var(--bs-success);stroke:var(--bs-primary);`;
9+
} else {
10+
col = `fill:var(--bs-gray-500);stroke:var(--bs-primary);`;
11+
}
12+
title = `title="Date: ${h.Date}\nIface: ${h.Iface}\nIP: ${h.IP}\nKnown: ${h.Known}"`;
13+
14+
html = html + `<i ${title}><svg width="10" height="20">
15+
<rect width="10" height="20" style="${col}"/>
16+
Sorry, your browser does not support inline SVG.
17+
</svg></i>`;
18+
}
19+
return html;
20+
}

internal/web/public/js/history.js

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
1-
var addrsArray = {};
1+
let show = 500;
2+
let addrsArray;
23

34
loadAddrs();
45

5-
function createHTML(addr, i) {
6-
let now = '';
7-
8-
if (addr.Now == 0) {
9-
now = `<i class="bi bi-circle-fill" style="color:var(--bs-gray-500);"></i>`;
10-
} else {
11-
now = `<i class="bi bi-check-circle-fill" style="color:var(--bs-success);"></i>`;
12-
}
13-
14-
let html = `
15-
<tr>
16-
<td style="opacity: 45%;">${i}.</td>
17-
<td>${addr.Name}</td>
18-
<td>${addr.Iface}</td>
19-
<td>
20-
<a href="http://${addr.IP}">${addr.IP}</a>
21-
</td>
22-
<td>${addr.Mac}</td>
23-
<td>${addr.Hw}</td>
24-
<td>${addr.Date}</td>
25-
<td>${addr.Known}</td>
26-
<td>${now}</td>
27-
</tr>
28-
`;
6+
async function loadAddrs() {
297

30-
return html;
8+
const n = localStorage.getItem("histShow");
9+
if (n != null) {
10+
show = n;
11+
}
12+
13+
const url = '/api/all';
14+
addrsArray = await (await fetch(url)).json();
15+
16+
loadHistory();
3117
}
3218

33-
async function loadAddrs() {
19+
async function loadHistory() {
3420

35-
let url = '/api/history';
36-
let addrsMap = await (await fetch(url)).json();
37-
if (addrsMap != null) {
38-
addrsArray = Object.values(addrsMap);
21+
let tr, td, url, hist;
22+
let i = 0;
23+
24+
document.getElementById('showHist').innerHTML = '';
25+
26+
for (let a of addrsArray) {
27+
url = '/api/history/'+a.Mac;
28+
hist = await (await fetch(url)).json();
29+
if (show > 0) {
30+
hist = hist.slice(0, show);
31+
}
32+
33+
td = getHistHTML(hist); // hist-html.js
34+
i = i + 1;
35+
36+
tr = `
37+
<tr>
38+
<td style="opacity: 45%;">${i}.</td>
39+
<td>
40+
<p>${a.Name}</p>
41+
<p><a href="http://${a.IP}" target="blank">${a.IP}</a></p>
42+
<p><a href="/host/${a.ID}">${a.Mac}</a></p>
43+
</td>
44+
<td>${td}</td>
45+
</tr>`;
46+
47+
document.getElementById('showHist').insertAdjacentHTML('beforeend', tr);
3948
}
49+
}
4050

41-
displayArrayData(addrsArray);
51+
function showHist(n) {
52+
show = n;
53+
localStorage.setItem("histShow", show);
54+
loadHistory();
4255
}
4356

44-
function sortBy(field) {
45-
sortByAny(addrsArray, field);
57+
function manualShow() {
58+
const n = document.getElementById('man-show').value;
59+
showHist(n);
4660
}

internal/web/public/js/host.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
let show = 500;
2+
let mac;
13

24
async function delHost(id) {
35

@@ -8,34 +10,37 @@ async function delHost(id) {
810
window.location.href = '/';
911
}
1012

11-
async function loadHistory(mac) {
13+
async function loadHistory(m) {
14+
15+
const n = localStorage.getItem("hostShow");
16+
if (n != null) {
17+
show = n;
18+
}
1219

20+
mac = m;
1321
const url = '/api/history/'+mac;
1422

1523
let hist = await (await fetch(url)).json();
24+
if (show > 0) {
25+
hist = hist.slice(0, show);
26+
}
1627

1728
// console.log("HIST", hist);
1829
displayHistory(hist);
1930
}
2031

2132
function displayHistory(hist) {
2233

23-
let html, col, title;
24-
25-
for (let h of hist) {
26-
if (h.Now != 0) {
27-
col = `fill:var(--bs-success);stroke:var(--bs-primary);`;
28-
} else {
29-
col = `fill:var(--bs-gray-500);stroke:var(--bs-primary);`;
30-
}
31-
title = `title="Date: ${h.Date}\nIface: ${h.Iface}\nIP: ${h.IP}\nKnown: ${h.Known}"`;
34+
document.getElementById('showHist').innerHTML = getHistHTML(hist); // hist-html.js
35+
}
3236

33-
html = `<i ${title}><svg width="10" height="20">
34-
<rect width="10" height="20" style="${col}"/>
35-
Sorry, your browser does not support inline SVG.
36-
</svg></i>`;
37+
function showHist(n) {
38+
show = n;
39+
localStorage.setItem("hostShow", show);
40+
loadHistory(mac);
41+
}
3742

38-
// html = `<i class="bi bi-file-fill" style="${col}" ${title}></i>`;
39-
document.getElementById('showHist').insertAdjacentHTML('beforeend', html);
40-
}
43+
function manualShow() {
44+
const n = document.getElementById('man-show').value;
45+
showHist(n);
4146
}

internal/web/public/js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function createHTML(addr, i) {
2626
<td>${name}</td>
2727
<td>${addr.Iface}</td>
2828
<td>
29-
<a href="http://${addr.IP}">${addr.IP}</a>
29+
<a href="http://${addr.IP}" target="blank">${addr.IP}</a>
3030
</td>
3131
<td><a href="/host/${addr.ID}">${addr.Mac}</a></td>
3232
<td>${addr.Hw}</td>

internal/web/public/js/old_history.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

internal/web/templates/config.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@
4343
<td><input name="ifaces" type="text" class="form-control" value="{{ .Config.Ifaces }}"></td>
4444
</tr>
4545
<tr>
46-
<td>Ignore IP</td>
47-
<td><select name="ignore" class="form-select">
48-
<option selected>{{ .Config.IgnoreIP }}</option>
49-
<option value="yes">yes</option>
50-
<option value="no">no</option>
51-
</select></td>
46+
<td>Timeout</td>
47+
<td><input name="timeout" type="number" class="form-control" value="{{ .Config.Timeout }}"></td>
5248
</tr>
5349
<tr>
5450
<td>Args for arpscan</td>
5551
<td><input name="arpargs" type="text" class="form-control" value="{{ .Config.ArpArgs }}"></td>
5652
</tr>
53+
<tr>
54+
<td>Trim History (hours)</td>
55+
<td><input name="trim" type="number" class="form-control" value="{{ .Config.TrimHist }}"></td>
56+
</tr>
5757
<tr>
5858
<td><button type="submit" class="btn btn-primary">Save</button></td>
5959
<td></td>

internal/web/templates/history.html

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
{{ define "history.html" }}
22
</head>
33
<script src="/fs/public/js/history.js"></script>
4-
<script src="/fs/public/js/sort.js"></script>
4+
<script src="/fs/public/js/hist-html.js"></script>
5+
56
<body>
67
<div class="container-lg">
78
<div class="row">
89
<div class="col-md mt-4 mb-4">
910
<div class="card border-primary">
11+
<div class="card-header">
12+
<div class="input-group">
13+
<button class="btn btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Show</button>
14+
<ul class="dropdown-menu">
15+
<li><a class="dropdown-item" onclick="showHist(500)">500</a></li>
16+
<li><a class="dropdown-item" onclick="showHist(1000)">1000</a></li>
17+
<li><a class="dropdown-item" onclick="showHist(2000)">2000</a></li>
18+
<li><a class="dropdown-item" onclick="showHist(5000)">5000</a></li>
19+
<li><a class="dropdown-item" onclick="showHist(0)">All</a></li>
20+
</ul>
21+
<input class="form-control" id="man-show" onchange="manualShow()" placeholder="Show elements" style="max-width: 10em;">
22+
</div>
23+
</div>
1024
<div class="card-body table-responsive">
1125
<table class="table table-striped">
1226
<thead>
1327
<th style="width: 3em;"></th>
14-
<th>Name <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Name')"></i></th>
15-
<th>Iface <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Iface')"></i></th>
16-
<th>IP <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('IP')"></i></th>
17-
<th>MAC <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Mac')"></i></th>
18-
<th>Hardware <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Hw')"></i></th>
19-
<th>Date <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Date')"></i></th>
20-
<th>Known <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Known')"></i></th>
21-
<th>Online <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Now')"></i></th>
28+
<th>Host</th>
29+
<th>History</th>
2230
</thead>
23-
<tbody id="tBody"></tbody>
24-
<!-- index.js -->
31+
<tbody id="showHist"></tbody>
32+
<!-- history.js -->
2533
</table>
2634
</div>
2735
</div>

0 commit comments

Comments
 (0)