From b86ee2ba73788205dc8c9a967707dafc3d132382 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:15:10 +0700 Subject: [PATCH] History js --- internal/conf/getconfig.go | 12 ++-- internal/models/models.go | 2 +- internal/web/config.go | 7 ++- internal/web/public/js/hist-html.js | 20 +++++++ internal/web/public/js/history.js | 86 ++++++++++++++++----------- internal/web/public/js/host.js | 39 ++++++------ internal/web/public/js/index.js | 2 +- internal/web/public/js/old_history.js | 45 -------------- internal/web/templates/config.html | 12 ++-- internal/web/templates/history.html | 30 ++++++---- internal/web/templates/host.html | 15 ++++- 11 files changed, 145 insertions(+), 125 deletions(-) create mode 100644 internal/web/public/js/hist-html.js delete mode 100644 internal/web/public/js/old_history.js diff --git a/internal/conf/getconfig.go b/internal/conf/getconfig.go index e52a659..30bbc62 100644 --- a/internal/conf/getconfig.go +++ b/internal/conf/getconfig.go @@ -16,10 +16,10 @@ func Get(path string) (config models.Conf) { viper.SetDefault("COLOR", "dark") viper.SetDefault("NODEPATH", "") // viper.SetDefault("SCANER", "arpscan") - viper.SetDefault("ARPARGS", "") + viper.SetDefault("ARP_ARGS", "") viper.SetDefault("IFACES", "") viper.SetDefault("TIMEOUT", 60) - viper.SetDefault("IGNOREIP", "no") + viper.SetDefault("TRIM_HIST", 48) viper.SetConfigFile(path) viper.SetConfigType("yaml") @@ -34,10 +34,10 @@ func Get(path string) (config models.Conf) { config.Color = viper.Get("COLOR").(string) config.NodePath = viper.Get("NODEPATH").(string) // config.Scaner = viper.Get("SCANER").(string) - config.ArpArgs = viper.Get("ARPARGS").(string) + config.ArpArgs = viper.Get("ARP_ARGS").(string) config.Ifaces = viper.Get("IFACES").(string) config.Timeout = viper.GetInt("TIMEOUT") - config.IgnoreIP = viper.Get("IGNOREIP").(string) + config.TrimHist = viper.GetInt("TRIM_HIST") return config } @@ -54,10 +54,10 @@ func Write(config models.Conf) { viper.Set("COLOR", config.Color) viper.Set("NODEPATH", config.NodePath) // viper.Set("SCANER", config.Scaner) - viper.Set("ARPARGS", config.ArpArgs) + viper.Set("ARP_ARGS", config.ArpArgs) viper.Set("IFACES", config.Ifaces) viper.Set("TIMEOUT", config.Timeout) - viper.Set("IGNOREIP", config.IgnoreIP) + viper.Set("TRIM_HIST", config.TrimHist) err := viper.WriteConfig() check.IfError(err) diff --git a/internal/models/models.go b/internal/models/models.go index c03a11b..743ac28 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -14,7 +14,7 @@ type Conf struct { // Scaner string ArpArgs string Timeout int - IgnoreIP string + TrimHist int } // Host - one host diff --git a/internal/web/config.go b/internal/web/config.go index 2b88402..bfe8e69 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -3,6 +3,7 @@ package web import ( "log/slog" "net/http" + "strconv" "github.com/gin-gonic/gin" @@ -34,10 +35,14 @@ func saveConfigHandler(c *gin.Context) { appConfig.Theme = c.PostForm("theme") appConfig.Color = c.PostForm("color") appConfig.NodePath = c.PostForm("node") - appConfig.IgnoreIP = c.PostForm("ignore") appConfig.ArpArgs = c.PostForm("arpargs") appConfig.Ifaces = c.PostForm("ifaces") + timeout := c.PostForm("timeout") + trimHist := c.PostForm("trim") + appConfig.Timeout, _ = strconv.Atoi(timeout) + appConfig.TrimHist, _ = strconv.Atoi(trimHist) + conf.Write(appConfig) slog.Info("writing new config to " + appConfig.ConfPath) diff --git a/internal/web/public/js/hist-html.js b/internal/web/public/js/hist-html.js new file mode 100644 index 0000000..2bd97d2 --- /dev/null +++ b/internal/web/public/js/hist-html.js @@ -0,0 +1,20 @@ + +function getHistHTML(hist) { + + let html = '', col, title; + + for (let h of hist) { + if (h.Now != 0) { + col = `fill:var(--bs-success);stroke:var(--bs-primary);`; + } else { + col = `fill:var(--bs-gray-500);stroke:var(--bs-primary);`; + } + title = `title="Date: ${h.Date}\nIface: ${h.Iface}\nIP: ${h.IP}\nKnown: ${h.Known}"`; + + html = html + ` + + Sorry, your browser does not support inline SVG. + `; + } + return html; +} \ No newline at end of file diff --git a/internal/web/public/js/history.js b/internal/web/public/js/history.js index 1e8b12b..a776216 100644 --- a/internal/web/public/js/history.js +++ b/internal/web/public/js/history.js @@ -1,46 +1,60 @@ -var addrsArray = {}; +let show = 500; +let addrsArray; loadAddrs(); -function createHTML(addr, i) { - let now = ''; - - if (addr.Now == 0) { - now = ``; - } else { - now = ``; - } - - let html = ` - - ${i}. - ${addr.Name} - ${addr.Iface} - - ${addr.IP} - - ${addr.Mac} - ${addr.Hw} - ${addr.Date} - ${addr.Known} - ${now} - - `; - - return html; -} - async function loadAddrs() { - let url = '/api/history'; - let addrsMap = await (await fetch(url)).json(); - if (addrsMap != null) { - addrsArray = Object.values(addrsMap); + const n = localStorage.getItem("histShow"); + if (n != null) { + show = n; } - displayArrayData(addrsArray); + const url = '/api/all'; + addrsArray = await (await fetch(url)).json(); + + loadHistory(); } -function sortBy(field) { - sortByAny(addrsArray, field); +async function loadHistory() { + + let tr, td, url, hist; + let i = 0; + + document.getElementById('showHist').innerHTML = ''; + + for (let a of addrsArray) { + url = '/api/history/'+a.Mac; + hist = await (await fetch(url)).json(); + if (show > 0) { + hist = hist.slice(0, show); + } + + td = getHistHTML(hist); // hist-html.js + i = i + 1; + + tr = ` + + ${i}. + +

${a.Name}

+

${a.IP}

+

${a.Mac}

+ + ${td} + `; + + document.getElementById('showHist').insertAdjacentHTML('beforeend', tr); + } +} + +function showHist(n) { + show = n; + localStorage.setItem("histShow", show); + loadHistory(); +} + +function manualShow() { + const n = document.getElementById('man-show').value; + showHist(n); } \ No newline at end of file diff --git a/internal/web/public/js/host.js b/internal/web/public/js/host.js index ab8cfce..bf64ccd 100644 --- a/internal/web/public/js/host.js +++ b/internal/web/public/js/host.js @@ -1,3 +1,5 @@ +let show = 500; +let mac; async function delHost(id) { @@ -8,11 +10,20 @@ async function delHost(id) { window.location.href = '/'; } -async function loadHistory(mac) { +async function loadHistory(m) { + + const n = localStorage.getItem("hostShow"); + if (n != null) { + show = n; + } + mac = m; const url = '/api/history/'+mac; let hist = await (await fetch(url)).json(); + if (show > 0) { + hist = hist.slice(0, show); + } // console.log("HIST", hist); displayHistory(hist); @@ -20,22 +31,16 @@ async function loadHistory(mac) { function displayHistory(hist) { - let html, col, title; + document.getElementById('showHist').innerHTML = getHistHTML(hist); // hist-html.js +} - for (let h of hist) { - if (h.Now != 0) { - col = `fill:var(--bs-success);stroke:var(--bs-primary);`; - } else { - col = `fill:var(--bs-gray-500);stroke:var(--bs-primary);`; - } - title = `title="Date: ${h.Date}\nIface: ${h.Iface}\nIP: ${h.IP}\nKnown: ${h.Known}"`; +function showHist(n) { + show = n; + localStorage.setItem("hostShow", show); + loadHistory(mac); +} - html = ` - - Sorry, your browser does not support inline SVG. - `; - - // html = ``; - document.getElementById('showHist').insertAdjacentHTML('beforeend', html); - } +function manualShow() { + const n = document.getElementById('man-show').value; + showHist(n); } \ No newline at end of file diff --git a/internal/web/public/js/index.js b/internal/web/public/js/index.js index d48c548..dfde66a 100644 --- a/internal/web/public/js/index.js +++ b/internal/web/public/js/index.js @@ -26,7 +26,7 @@ function createHTML(addr, i) { ${name} ${addr.Iface} - ${addr.IP} + ${addr.IP} ${addr.Mac} ${addr.Hw} diff --git a/internal/web/public/js/old_history.js b/internal/web/public/js/old_history.js deleted file mode 100644 index 128a7cc..0000000 --- a/internal/web/public/js/old_history.js +++ /dev/null @@ -1,45 +0,0 @@ -var histArray = {}; - -loadHistory(); - -function createHTML(hist, i) { - - let allState = ""; - let color = ""; - - for (let state of hist.State){ - if (state.State) { - color = `bi-check-circle-fill" style="color:var(--bs-success);"`; - } else { - color = `bi-dash-circle-fill" style="color:var(--bs-danger);"`; - } - allState = allState + ``; - } - - let html = ` - - ${i}. - ${hist.Name} - ${hist.Addr} - ${hist.Port} - ${hist.PortName} - ${allState} - `; - - return html; -} - -async function loadHistory() { - - let url = '/api/history'; - let histMap = await (await fetch(url)).json(); - if (histMap != null) { - histArray = Object.values(histMap); - } - - displayArrayData(histArray); -} - -function sortBy(field) { - sortByAny(histArray, field); -} \ No newline at end of file diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index 6af5e96..779a23a 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -43,17 +43,17 @@ - Ignore IP - + Timeout + Args for arpscan + + Trim History (hours) + + diff --git a/internal/web/templates/history.html b/internal/web/templates/history.html index 6af5775..291aa80 100644 --- a/internal/web/templates/history.html +++ b/internal/web/templates/history.html @@ -1,27 +1,35 @@ {{ define "history.html" }} - + +
+
+
+ + + +
+
- - - - - - - - + + - - + +
Name Iface IP MAC Hardware Date Known Online HostHistory
diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index 2516da5..879238f 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -2,6 +2,7 @@ +
@@ -91,7 +92,19 @@
-
History
+
+
+ + + +
+