diff --git a/internal/web/api.go b/internal/web/api.go index f3b2f5e..4f5eb9b 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -4,9 +4,15 @@ import ( "net/http" "github.com/gin-gonic/gin" + // "github.com/aceberg/WatchYourLAN/internal/models" ) func apiAll(c *gin.Context) { c.IndentedJSON(http.StatusOK, allHosts) } + +func apiHistory(c *gin.Context) { + + c.IndentedJSON(http.StatusOK, histHosts) +} diff --git a/internal/web/const-var.go b/internal/web/const-var.go index e8cbf92..7cc526e 100644 --- a/internal/web/const-var.go +++ b/internal/web/const-var.go @@ -10,8 +10,8 @@ var ( // appConfig - config for Web Gui appConfig models.Conf - allHosts []models.Host - // histHosts []models.Host + allHosts []models.Host + histHosts []models.Host quitScan chan bool ) diff --git a/internal/web/index.go b/internal/web/index.go index b3f0424..8fa3532 100644 --- a/internal/web/index.go +++ b/internal/web/index.go @@ -15,3 +15,11 @@ func indexHandler(c *gin.Context) { c.HTML(http.StatusOK, "header.html", guiData) c.HTML(http.StatusOK, "index.html", guiData) } + +func historyHandler(c *gin.Context) { + var guiData models.GuiData + guiData.Config = appConfig + + c.HTML(http.StatusOK, "header.html", guiData) + c.HTML(http.StatusOK, "history.html", guiData) +} diff --git a/internal/web/public/js/history.js b/internal/web/public/js/history.js index 128a7cc..f67c16e 100644 --- a/internal/web/public/js/history.js +++ b/internal/web/public/js/history.js @@ -1,45 +1,43 @@ -var histArray = {}; +var addrsArray = {}; -loadHistory(); +loadAddrs(); -function createHTML(hist, i) { - - let allState = ""; - let color = ""; +function createHTML(addr, i) { + let now = ''; - 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 + ``; + if (addr.Now == 0) { + now = ``; + } else { + now = ``; } - + let html = ` ${i}. - ${hist.Name} - ${hist.Addr} - ${hist.Port} - ${hist.PortName} - ${allState} - `; + ${addr.Name} + ${addr.Iface} + + ${addr.IP} + + ${addr.Mac} + ${now} + + `; return html; } -async function loadHistory() { +async function loadAddrs() { let url = '/api/history'; - let histMap = await (await fetch(url)).json(); - if (histMap != null) { - histArray = Object.values(histMap); + let addrsMap = await (await fetch(url)).json(); + if (addrsMap != null) { + addrsArray = Object.values(addrsMap); } - displayArrayData(histArray); + displayArrayData(addrsArray); } function sortBy(field) { - sortByAny(histArray, field); + sortByAny(addrsArray, field); } \ No newline at end of file diff --git a/internal/web/public/js/old_history.js b/internal/web/public/js/old_history.js new file mode 100644 index 0000000..128a7cc --- /dev/null +++ b/internal/web/public/js/old_history.js @@ -0,0 +1,45 @@ +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/public/js/scan.js b/internal/web/public/js/scan.js deleted file mode 100644 index 4c62cbb..0000000 --- a/internal/web/public/js/scan.js +++ /dev/null @@ -1,125 +0,0 @@ -var addr = ''; -var stop = false; -var portMap = {}; -var portArray = []; - -// Run, when page loadad -window.addEventListener('load', function() { - loadSavedPorts(); -}); - -function delRow(id) { - document.getElementById(id).innerHTML = ""; -} - -function stopScan() { - stop = true; -} - -async function scanAddr() { - let begin = document.getElementById("begin").value; - let end = document.getElementById("end").value; - let savedPorts = []; - - if (begin == "") { - begin = 1 - } - if (end == "") { - end = 65535 - } - let port = {}; - stop = false; - - if (portMap != null) { - savedPorts = Object.keys(portMap); - } - // console.log("Saved ports:", savedPorts); - - document.getElementById('stopBtn').style.visibility = "visible"; - - for (let i = begin ; i <= end; i++) { - - if (stop) { - break; - } - - let url = '/api/port/'+addr+'/'+i; - port = await (await fetch(url)).json(); - - document.getElementById("curPort").innerHTML = "Scanning port "+i; - - if ((port.State) && (!savedPorts.includes(port.Port.toString()))) { - html = createHTML(port, '', true); - document.getElementById('tBody').insertAdjacentHTML('afterbegin', html); - } - } - - document.getElementById('stopBtn').style.visibility = "hidden"; -} - -function createHTML(port, i, found) { - let state = ``; - let checked = ``; - let sup = ``; - - if (found) { - sup = ` new`; - } - - if (port.Watch) { - checked = `checked`; - - if (port.State) { - state = ``; - } else { - state = ``; - } - } else { - state = ``; - } - - let html = ` - - ${i}. - - - - - ${port.Port}${sup} - - - - - ${state} - - -
- -
- - - - - - - `; - - return html; -} - -async function loadSavedPorts() { - - addr = document.getElementById("pageAddr").value; - - let url = '/api/port/'+addr; - portMap = await (await fetch(url)).json(); - if (portMap != null) { - portArray = Object.values(portMap); - } - - displayArrayData(portArray); // sort.js -} - -function sortBy(field) { - sortByAny(portArray, field); // sort.js -} \ No newline at end of file diff --git a/internal/web/scan.go b/internal/web/scan.go index e018016..308ffd4 100644 --- a/internal/web/scan.go +++ b/internal/web/scan.go @@ -13,7 +13,7 @@ func updateScan() { db.Create(appConfig.DBPath) allHosts = db.Select(appConfig.DBPath, "now") - // histHosts = db.Select(appConfig.DBPath, "history") + histHosts = db.Select(appConfig.DBPath, "history") quitScan = make(chan bool) go startScan() @@ -35,7 +35,7 @@ func startScan() { foundHosts := arp.Scan(appConfig.Ifaces) compareHosts(foundHosts) allHosts = db.Select(appConfig.DBPath, "now") - // histHosts = db.Select(appConfig.DBPath, "history") + histHosts = db.Select(appConfig.DBPath, "history") lastDate = time.Now() } diff --git a/internal/web/templates/history.html b/internal/web/templates/history.html new file mode 100644 index 0000000..bc83c40 --- /dev/null +++ b/internal/web/templates/history.html @@ -0,0 +1,30 @@ +{{ define "history.html" }} + + + + +
+
+
+
+
+ + + + + + + + + + + +
Name Iface IP MAC History
+
+
+
+
+
+ +{{ template "footer.html" }} +{{ end }} \ No newline at end of file diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 1e15ccc..cd29b19 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -42,10 +42,12 @@ func Gui(dirPath, nodePath string) { router.StaticFS("/fs/", http.FS(pubFS)) // public - router.GET("/api/all", apiAll) // api.go + router.GET("/api/all", apiAll) // api.go + router.GET("/api/history", apiHistory) // api.go - router.GET("/", indexHandler) // index.go - router.GET("/config/", configHandler) // config.go + router.GET("/", indexHandler) // index.go + router.GET("/history/", historyHandler) // index.go + router.GET("/config/", configHandler) // config.go router.POST("/config/", saveConfigHandler) // config.go