diff --git a/internal/arp/arpscan.go b/internal/arp/arpscan.go index 579becb..669e988 100644 --- a/internal/arp/arpscan.go +++ b/internal/arp/arpscan.go @@ -21,7 +21,6 @@ func parseOutput(text, iface string) []models.Host { var foundHosts = []models.Host{} perString := strings.Split(text, "\n") - currentTime := time.Now() for _, host := range perString { if host != "" { @@ -31,7 +30,7 @@ func parseOutput(text, iface string) []models.Host { oneHost.IP = p[0] oneHost.Mac = p[1] oneHost.Hw = p[2] - oneHost.Date = currentTime.Format("2006-01-02 15:04:05") + oneHost.Date = time.Now().Format("2006-01-02 15:04:05") oneHost.Now = 1 foundHosts = append(foundHosts, oneHost) } diff --git a/internal/web/api.go b/internal/web/api.go index e929de0..9e2d2e0 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -1,12 +1,13 @@ package web import ( - // "log" + "log" "net/http" "github.com/gin-gonic/gin" "github.com/aceberg/WatchYourLAN/internal/db" + "github.com/aceberg/WatchYourLAN/internal/models" "github.com/aceberg/WatchYourLAN/internal/portscan" ) @@ -18,8 +19,19 @@ func apiAll(c *gin.Context) { } func apiHistory(c *gin.Context) { + var hosts []models.Host - c.IndentedJSON(http.StatusOK, histHosts) + mac := c.Param("mac") + + if mac != "/" { + mac = mac[1:] + log.Println("MAC", mac) + hosts = getHostsByMAC(mac, histHosts) + } else { + hosts = histHosts + } + + c.IndentedJSON(http.StatusOK, hosts) } func apiHost(c *gin.Context) { diff --git a/internal/web/functions.go b/internal/web/functions.go index 4ef3b5f..87c116d 100644 --- a/internal/web/functions.go +++ b/internal/web/functions.go @@ -33,3 +33,15 @@ func updateDNS(host models.Host) (name, dns string) { return name, dns } + +func getHostsByMAC(mac string, hosts []models.Host) (foundHosts []models.Host) { + + for _, host := range hosts { + if host.Mac == mac { + + foundHosts = append(foundHosts, host) + } + } + + return foundHosts +} diff --git a/internal/web/public/js/history.js b/internal/web/public/js/history.js index f67c16e..1e8b12b 100644 --- a/internal/web/public/js/history.js +++ b/internal/web/public/js/history.js @@ -20,6 +20,9 @@ function createHTML(addr, i) { ${addr.IP} ${addr.Mac} + ${addr.Hw} + ${addr.Date} + ${addr.Known} ${now} `; diff --git a/internal/web/public/js/host-scan.js b/internal/web/public/js/host-scan.js index 4219f65..835bc49 100644 --- a/internal/web/public/js/host-scan.js +++ b/internal/web/public/js/host-scan.js @@ -49,13 +49,4 @@ async function scanAddr() { function genHTML(addr, port) { html = `${port}   `; return html; -} - -async function delHost(id) { - - const url = '/api/host/del/'+id; - - await fetch(url); - - window.location.href = '/'; } \ No newline at end of file diff --git a/internal/web/public/js/host.js b/internal/web/public/js/host.js new file mode 100644 index 0000000..ab8cfce --- /dev/null +++ b/internal/web/public/js/host.js @@ -0,0 +1,41 @@ + +async function delHost(id) { + + const url = '/api/host/del/'+id; + + await fetch(url); + + window.location.href = '/'; +} + +async function loadHistory(mac) { + + const url = '/api/history/'+mac; + + let hist = await (await fetch(url)).json(); + + // console.log("HIST", hist); + displayHistory(hist); +} + +function displayHistory(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 = ` + + Sorry, your browser does not support inline SVG. + `; + + // html = ``; + document.getElementById('showHist').insertAdjacentHTML('beforeend', html); + } +} \ No newline at end of file diff --git a/internal/web/scan.go b/internal/web/scan.go index a9e0b4d..dec5f08 100644 --- a/internal/web/scan.go +++ b/internal/web/scan.go @@ -47,17 +47,16 @@ func startScan() { func compareHosts(foundHosts []models.Host) { - // Make map and Insert history + // Make map of found hosts foundHostsMap := make(map[string]models.Host) for _, fHost := range foundHosts { foundHostsMap[fHost.Mac] = fHost - db.Insert(appConfig.DBPath, "history", fHost) } for _, aHost := range allHosts { fHost, exists := foundHostsMap[aHost.Mac] - if exists && (appConfig.IgnoreIP == "yes" || aHost.IP == fHost.IP) { + if exists { aHost.Iface = fHost.Iface aHost.IP = fHost.IP @@ -68,9 +67,11 @@ func compareHosts(foundHosts []models.Host) { } else { aHost.Now = 0 - db.Insert(appConfig.DBPath, "history", aHost) } db.Update(appConfig.DBPath, "now", aHost) + + aHost.Date = time.Now().Format("2006-01-02 15:04:05") + db.Insert(appConfig.DBPath, "history", aHost) } for _, fHost := range foundHostsMap { diff --git a/internal/web/templates/history.html b/internal/web/templates/history.html index bc83c40..6af5775 100644 --- a/internal/web/templates/history.html +++ b/internal/web/templates/history.html @@ -15,7 +15,10 @@ Iface IP MAC - History + Hardware + Date + Known + Online diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index c1f3e9a..2516da5 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -1,6 +1,7 @@ {{ define "host.html" }} +
@@ -43,7 +44,7 @@ Known - {{ .Host.Known }} + {{ if eq .Host.Known 1 }}Yes{{ else }}No{{ end }} Online @@ -92,12 +93,15 @@
History
- +
+ {{ template "footer.html" }} {{ end }} \ No newline at end of file diff --git a/internal/web/webgui.go b/internal/web/webgui.go index f974400..eac4d2d 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -44,7 +44,7 @@ func Gui(dirPath, nodePath string) { router.GET("/api/all", apiAll) // api.go router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go - router.GET("/api/history", apiHistory) // api.go + router.GET("/api/history/*mac", apiHistory) // api.go router.GET("/api/host", apiHost) // api.go router.GET("/api/host/del/:id", apiHostDel) // api.go router.GET("/api/port/:addr/:port", apiPort) // api.go