From 0000b54fb8996451d0fb68f1dbd538e4f9b297dc Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sat, 24 Aug 2024 02:30:04 +0700 Subject: [PATCH] Names from DNS --- internal/conf/getconfig.go | 6 ++--- internal/db/edit.go | 12 ++++++---- internal/models/models.go | 3 ++- internal/web/api.go | 10 ++++++++ internal/web/config.go | 2 +- internal/web/functions.go | 14 +++++++++++ internal/web/host.go | 5 ++-- .../web/public/js/{scan.js => host-scan.js} | 15 ++++++++++++ internal/web/scan.go | 2 ++ internal/web/templates/config.html | 10 ++++---- internal/web/templates/host.html | 24 +++++++++++++++++-- internal/web/webgui.go | 1 + 12 files changed, 85 insertions(+), 19 deletions(-) rename internal/web/public/js/{scan.js => host-scan.js} (77%) diff --git a/internal/conf/getconfig.go b/internal/conf/getconfig.go index 979a47d..e52a659 100644 --- a/internal/conf/getconfig.go +++ b/internal/conf/getconfig.go @@ -15,7 +15,7 @@ func Get(path string) (config models.Conf) { viper.SetDefault("THEME", "solar") viper.SetDefault("COLOR", "dark") viper.SetDefault("NODEPATH", "") - viper.SetDefault("SCANER", "arpscan") + // viper.SetDefault("SCANER", "arpscan") viper.SetDefault("ARPARGS", "") viper.SetDefault("IFACES", "") viper.SetDefault("TIMEOUT", 60) @@ -33,7 +33,7 @@ func Get(path string) (config models.Conf) { config.Theme = viper.Get("THEME").(string) config.Color = viper.Get("COLOR").(string) config.NodePath = viper.Get("NODEPATH").(string) - config.Scaner = viper.Get("SCANER").(string) + // config.Scaner = viper.Get("SCANER").(string) config.ArpArgs = viper.Get("ARPARGS").(string) config.Ifaces = viper.Get("IFACES").(string) config.Timeout = viper.GetInt("TIMEOUT") @@ -53,7 +53,7 @@ func Write(config models.Conf) { viper.Set("THEME", config.Theme) viper.Set("COLOR", config.Color) viper.Set("NODEPATH", config.NodePath) - viper.Set("SCANER", config.Scaner) + // viper.Set("SCANER", config.Scaner) viper.Set("ARPARGS", config.ArpArgs) viper.Set("IFACES", config.Ifaces) viper.Set("TIMEOUT", config.Timeout) diff --git a/internal/db/edit.go b/internal/db/edit.go index a6cfe8a..12d62e8 100644 --- a/internal/db/edit.go +++ b/internal/db/edit.go @@ -12,6 +12,7 @@ func Create(path string) { sqlStatement := `CREATE TABLE IF NOT EXISTS "now" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "NAME" TEXT NOT NULL, + "DNS" TEXT NOT NULL, "IFACE" TEXT, "IP" TEXT, "MAC" TEXT, @@ -25,6 +26,7 @@ func Create(path string) { sqlStatement = `CREATE TABLE IF NOT EXISTS "history" ( "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "NAME" TEXT NOT NULL, + "DNS" TEXT NOT NULL, "IFACE" TEXT, "IP" TEXT, "MAC" TEXT, @@ -40,9 +42,9 @@ func Create(path string) { func Insert(path, table string, oneHost models.Host) { oneHost.Name = quoteStr(oneHost.Name) oneHost.Hw = quoteStr(oneHost.Hw) - sqlStatement := `INSERT INTO '%s' (NAME, IFACE, IP, MAC, HW, DATE, KNOWN, NOW) - VALUES ('%s','%s','%s','%s','%s','%s','%d','%d');` - sqlStatement = fmt.Sprintf(sqlStatement, table, oneHost.Name, oneHost.Iface, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now) + sqlStatement := `INSERT INTO '%s' (NAME, DNS, IFACE, IP, MAC, HW, DATE, KNOWN, NOW) + VALUES ('%s','%s','%s','%s','%s','%s','%s','%d','%d');` + sqlStatement = fmt.Sprintf(sqlStatement, table, oneHost.Name, oneHost.DNS, oneHost.Iface, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now) dbExec(path, sqlStatement) } @@ -52,10 +54,10 @@ func Update(path, table string, oneHost models.Host) { oneHost.Name = quoteStr(oneHost.Name) oneHost.Hw = quoteStr(oneHost.Hw) sqlStatement := `UPDATE '%s' set - NAME = '%s', IFACE = '%s', IP = '%s', MAC = '%s', HW = '%s', DATE = '%s', + NAME = '%s', DNS = '%s', IFACE = '%s', IP = '%s', MAC = '%s', HW = '%s', DATE = '%s', KNOWN = '%d', NOW = '%d' WHERE ID = '%d';` - sqlStatement = fmt.Sprintf(sqlStatement, table, oneHost.Name, oneHost.Iface, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now, oneHost.ID) + sqlStatement = fmt.Sprintf(sqlStatement, table, oneHost.Name, oneHost.DNS, oneHost.Iface, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now, oneHost.ID) dbExec(path, sqlStatement) } diff --git a/internal/models/models.go b/internal/models/models.go index dfbbf4a..c03a11b 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -11,7 +11,7 @@ type Conf struct { DBPath string NodePath string Ifaces string - Scaner string + // Scaner string ArpArgs string Timeout int IgnoreIP string @@ -21,6 +21,7 @@ type Conf struct { type Host struct { ID int `db:"ID"` Name string `db:"NAME"` + DNS string `db:"DNS"` Iface string `db:"IFACE"` IP string `db:"IP"` Mac string `db:"MAC"` diff --git a/internal/web/api.go b/internal/web/api.go index 58b79ea..e929de0 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -31,6 +31,16 @@ func apiHost(c *gin.Context) { c.IndentedJSON(http.StatusOK, host) } +func apiHostDel(c *gin.Context) { + + idStr := c.Param("id") + host := getHostByID(idStr, allHosts) // functions.go + db.Delete(appConfig.DBPath, "now", host.ID) + allHosts = db.Select(appConfig.DBPath, "now") + + c.IndentedJSON(http.StatusOK, "OK") +} + func apiPort(c *gin.Context) { addr := c.Param("addr") diff --git a/internal/web/config.go b/internal/web/config.go index 91beec1..2b88402 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -34,7 +34,7 @@ func saveConfigHandler(c *gin.Context) { appConfig.Theme = c.PostForm("theme") appConfig.Color = c.PostForm("color") appConfig.NodePath = c.PostForm("node") - appConfig.Scaner = c.PostForm("scaner") + appConfig.IgnoreIP = c.PostForm("ignore") appConfig.ArpArgs = c.PostForm("arpargs") appConfig.Ifaces = c.PostForm("ifaces") diff --git a/internal/web/functions.go b/internal/web/functions.go index 0e81c29..4ef3b5f 100644 --- a/internal/web/functions.go +++ b/internal/web/functions.go @@ -1,7 +1,9 @@ package web import ( + "net" "strconv" + "strings" "github.com/aceberg/WatchYourLAN/internal/models" ) @@ -19,3 +21,15 @@ func getHostByID(idStr string, hosts []models.Host) (oneHost models.Host) { return oneHost } + +func updateDNS(host models.Host) (name, dns string) { + + dnsNames, _ := net.LookupAddr(host.IP) + + if len(dnsNames) > 0 { + name = dnsNames[0] + dns = strings.Join(dnsNames, " ") + } + + return name, dns +} diff --git a/internal/web/host.go b/internal/web/host.go index 2f5d213..bc22bf5 100644 --- a/internal/web/host.go +++ b/internal/web/host.go @@ -13,8 +13,9 @@ func hostHandler(c *gin.Context) { guiData.Config = appConfig idStr := c.Param("id") - - guiData.Host = getHostByID(idStr, allHosts) + host := getHostByID(idStr, allHosts) + _, host.DNS = updateDNS(host) + guiData.Host = host c.HTML(http.StatusOK, "header.html", guiData) c.HTML(http.StatusOK, "host.html", guiData) diff --git a/internal/web/public/js/scan.js b/internal/web/public/js/host-scan.js similarity index 77% rename from internal/web/public/js/scan.js rename to internal/web/public/js/host-scan.js index 0cd2da1..4219f65 100644 --- a/internal/web/public/js/scan.js +++ b/internal/web/public/js/host-scan.js @@ -17,6 +17,7 @@ async function scanAddr() { } let portOpen = false; stop = false; + found = 0; document.getElementById('stopBtn').style.visibility = "visible"; @@ -25,6 +26,10 @@ async function scanAddr() { if (stop) { break; } + if (found > 9) { + found = 0; + document.getElementById('foundPorts').insertAdjacentHTML('beforeend', '
'); + } let url = '/api/port/'+addr+'/'+i; portOpen = await (await fetch(url)).json(); @@ -32,6 +37,7 @@ async function scanAddr() { document.getElementById("curPort").innerHTML = "Scanning port "+i; if (portOpen) { + found = found + 1; let html = genHTML(addr, i); document.getElementById('foundPorts').insertAdjacentHTML('beforeend', html); } @@ -43,4 +49,13 @@ 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/scan.go b/internal/web/scan.go index 308ffd4..a9e0b4d 100644 --- a/internal/web/scan.go +++ b/internal/web/scan.go @@ -68,6 +68,7 @@ func compareHosts(foundHosts []models.Host) { } else { aHost.Now = 0 + db.Insert(appConfig.DBPath, "history", aHost) } db.Update(appConfig.DBPath, "now", aHost) } @@ -75,6 +76,7 @@ func compareHosts(foundHosts []models.Host) { for _, fHost := range foundHostsMap { // NOTIFY, LOG + fHost.Name, fHost.DNS = updateDNS(fHost) db.Insert(appConfig.DBPath, "now", fHost) } } diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index 7d39a52..6af5e96 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -43,11 +43,11 @@ - Scaner - + + + diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index 37d85f0..c1f3e9a 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -1,10 +1,10 @@ {{ define "host.html" }} - +
-
+
Host
@@ -17,6 +17,10 @@ Name {{ .Host.Name }} + + DNS + {{ .Host.DNS }} + Iface {{ .Host.Iface }} @@ -50,6 +54,12 @@ {{ end }} + + + + + +
@@ -77,6 +87,16 @@
+
+
+
+
History
+
+ +
+
+
+
{{ template "footer.html" }} diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 4a17c2f..f974400 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -46,6 +46,7 @@ func Gui(dirPath, nodePath string) { router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go router.GET("/api/history", 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 router.GET("/", indexHandler) // index.go