Names from DNS

This commit is contained in:
aceberg 2024-08-24 02:30:04 +07:00
parent 54513cddd9
commit 0000b54fb8
12 changed files with 85 additions and 19 deletions

View file

@ -15,7 +15,7 @@ func Get(path string) (config models.Conf) {
viper.SetDefault("THEME", "solar") viper.SetDefault("THEME", "solar")
viper.SetDefault("COLOR", "dark") viper.SetDefault("COLOR", "dark")
viper.SetDefault("NODEPATH", "") viper.SetDefault("NODEPATH", "")
viper.SetDefault("SCANER", "arpscan") // viper.SetDefault("SCANER", "arpscan")
viper.SetDefault("ARPARGS", "") viper.SetDefault("ARPARGS", "")
viper.SetDefault("IFACES", "") viper.SetDefault("IFACES", "")
viper.SetDefault("TIMEOUT", 60) viper.SetDefault("TIMEOUT", 60)
@ -33,7 +33,7 @@ func Get(path string) (config models.Conf) {
config.Theme = viper.Get("THEME").(string) config.Theme = viper.Get("THEME").(string)
config.Color = viper.Get("COLOR").(string) config.Color = viper.Get("COLOR").(string)
config.NodePath = viper.Get("NODEPATH").(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.ArpArgs = viper.Get("ARPARGS").(string)
config.Ifaces = viper.Get("IFACES").(string) config.Ifaces = viper.Get("IFACES").(string)
config.Timeout = viper.GetInt("TIMEOUT") config.Timeout = viper.GetInt("TIMEOUT")
@ -53,7 +53,7 @@ func Write(config models.Conf) {
viper.Set("THEME", config.Theme) viper.Set("THEME", config.Theme)
viper.Set("COLOR", config.Color) viper.Set("COLOR", config.Color)
viper.Set("NODEPATH", config.NodePath) viper.Set("NODEPATH", config.NodePath)
viper.Set("SCANER", config.Scaner) // viper.Set("SCANER", config.Scaner)
viper.Set("ARPARGS", config.ArpArgs) viper.Set("ARPARGS", config.ArpArgs)
viper.Set("IFACES", config.Ifaces) viper.Set("IFACES", config.Ifaces)
viper.Set("TIMEOUT", config.Timeout) viper.Set("TIMEOUT", config.Timeout)

View file

@ -12,6 +12,7 @@ func Create(path string) {
sqlStatement := `CREATE TABLE IF NOT EXISTS "now" ( sqlStatement := `CREATE TABLE IF NOT EXISTS "now" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"NAME" TEXT NOT NULL, "NAME" TEXT NOT NULL,
"DNS" TEXT NOT NULL,
"IFACE" TEXT, "IFACE" TEXT,
"IP" TEXT, "IP" TEXT,
"MAC" TEXT, "MAC" TEXT,
@ -25,6 +26,7 @@ func Create(path string) {
sqlStatement = `CREATE TABLE IF NOT EXISTS "history" ( sqlStatement = `CREATE TABLE IF NOT EXISTS "history" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"NAME" TEXT NOT NULL, "NAME" TEXT NOT NULL,
"DNS" TEXT NOT NULL,
"IFACE" TEXT, "IFACE" TEXT,
"IP" TEXT, "IP" TEXT,
"MAC" TEXT, "MAC" TEXT,
@ -40,9 +42,9 @@ func Create(path string) {
func Insert(path, table string, oneHost models.Host) { func Insert(path, table string, oneHost models.Host) {
oneHost.Name = quoteStr(oneHost.Name) oneHost.Name = quoteStr(oneHost.Name)
oneHost.Hw = quoteStr(oneHost.Hw) oneHost.Hw = quoteStr(oneHost.Hw)
sqlStatement := `INSERT INTO '%s' (NAME, IFACE, IP, MAC, HW, DATE, KNOWN, NOW) sqlStatement := `INSERT INTO '%s' (NAME, DNS, IFACE, IP, MAC, HW, DATE, KNOWN, NOW)
VALUES ('%s','%s','%s','%s','%s','%s','%d','%d');` VALUES ('%s','%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 = 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) dbExec(path, sqlStatement)
} }
@ -52,10 +54,10 @@ func Update(path, table string, oneHost models.Host) {
oneHost.Name = quoteStr(oneHost.Name) oneHost.Name = quoteStr(oneHost.Name)
oneHost.Hw = quoteStr(oneHost.Hw) oneHost.Hw = quoteStr(oneHost.Hw)
sqlStatement := `UPDATE '%s' set 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' KNOWN = '%d', NOW = '%d'
WHERE ID = '%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) dbExec(path, sqlStatement)
} }

View file

@ -11,7 +11,7 @@ type Conf struct {
DBPath string DBPath string
NodePath string NodePath string
Ifaces string Ifaces string
Scaner string // Scaner string
ArpArgs string ArpArgs string
Timeout int Timeout int
IgnoreIP string IgnoreIP string
@ -21,6 +21,7 @@ type Conf struct {
type Host struct { type Host struct {
ID int `db:"ID"` ID int `db:"ID"`
Name string `db:"NAME"` Name string `db:"NAME"`
DNS string `db:"DNS"`
Iface string `db:"IFACE"` Iface string `db:"IFACE"`
IP string `db:"IP"` IP string `db:"IP"`
Mac string `db:"MAC"` Mac string `db:"MAC"`

View file

@ -31,6 +31,16 @@ func apiHost(c *gin.Context) {
c.IndentedJSON(http.StatusOK, host) 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) { func apiPort(c *gin.Context) {
addr := c.Param("addr") addr := c.Param("addr")

View file

@ -34,7 +34,7 @@ func saveConfigHandler(c *gin.Context) {
appConfig.Theme = c.PostForm("theme") appConfig.Theme = c.PostForm("theme")
appConfig.Color = c.PostForm("color") appConfig.Color = c.PostForm("color")
appConfig.NodePath = c.PostForm("node") appConfig.NodePath = c.PostForm("node")
appConfig.Scaner = c.PostForm("scaner") appConfig.IgnoreIP = c.PostForm("ignore")
appConfig.ArpArgs = c.PostForm("arpargs") appConfig.ArpArgs = c.PostForm("arpargs")
appConfig.Ifaces = c.PostForm("ifaces") appConfig.Ifaces = c.PostForm("ifaces")

View file

@ -1,7 +1,9 @@
package web package web
import ( import (
"net"
"strconv" "strconv"
"strings"
"github.com/aceberg/WatchYourLAN/internal/models" "github.com/aceberg/WatchYourLAN/internal/models"
) )
@ -19,3 +21,15 @@ func getHostByID(idStr string, hosts []models.Host) (oneHost models.Host) {
return oneHost 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
}

View file

@ -13,8 +13,9 @@ func hostHandler(c *gin.Context) {
guiData.Config = appConfig guiData.Config = appConfig
idStr := c.Param("id") idStr := c.Param("id")
host := getHostByID(idStr, allHosts)
guiData.Host = getHostByID(idStr, allHosts) _, host.DNS = updateDNS(host)
guiData.Host = host
c.HTML(http.StatusOK, "header.html", guiData) c.HTML(http.StatusOK, "header.html", guiData)
c.HTML(http.StatusOK, "host.html", guiData) c.HTML(http.StatusOK, "host.html", guiData)

View file

@ -17,6 +17,7 @@ async function scanAddr() {
} }
let portOpen = false; let portOpen = false;
stop = false; stop = false;
found = 0;
document.getElementById('stopBtn').style.visibility = "visible"; document.getElementById('stopBtn').style.visibility = "visible";
@ -25,6 +26,10 @@ async function scanAddr() {
if (stop) { if (stop) {
break; break;
} }
if (found > 9) {
found = 0;
document.getElementById('foundPorts').insertAdjacentHTML('beforeend', '<br>');
}
let url = '/api/port/'+addr+'/'+i; let url = '/api/port/'+addr+'/'+i;
portOpen = await (await fetch(url)).json(); portOpen = await (await fetch(url)).json();
@ -32,6 +37,7 @@ async function scanAddr() {
document.getElementById("curPort").innerHTML = "Scanning port "+i; document.getElementById("curPort").innerHTML = "Scanning port "+i;
if (portOpen) { if (portOpen) {
found = found + 1;
let html = genHTML(addr, i); let html = genHTML(addr, i);
document.getElementById('foundPorts').insertAdjacentHTML('beforeend', html); document.getElementById('foundPorts').insertAdjacentHTML('beforeend', html);
} }
@ -43,4 +49,13 @@ async function scanAddr() {
function genHTML(addr, port) { function genHTML(addr, port) {
html = `<a href="http://${addr}:${port}">${port}</a>&nbsp;&nbsp;&nbsp;`; html = `<a href="http://${addr}:${port}">${port}</a>&nbsp;&nbsp;&nbsp;`;
return html; return html;
}
async function delHost(id) {
const url = '/api/host/del/'+id;
await fetch(url);
window.location.href = '/';
} }

View file

@ -68,6 +68,7 @@ func compareHosts(foundHosts []models.Host) {
} else { } else {
aHost.Now = 0 aHost.Now = 0
db.Insert(appConfig.DBPath, "history", aHost)
} }
db.Update(appConfig.DBPath, "now", aHost) db.Update(appConfig.DBPath, "now", aHost)
} }
@ -75,6 +76,7 @@ func compareHosts(foundHosts []models.Host) {
for _, fHost := range foundHostsMap { for _, fHost := range foundHostsMap {
// NOTIFY, LOG // NOTIFY, LOG
fHost.Name, fHost.DNS = updateDNS(fHost)
db.Insert(appConfig.DBPath, "now", fHost) db.Insert(appConfig.DBPath, "now", fHost)
} }
} }

View file

@ -43,11 +43,11 @@
<td><input name="ifaces" type="text" class="form-control" value="{{ .Config.Ifaces }}"></td> <td><input name="ifaces" type="text" class="form-control" value="{{ .Config.Ifaces }}"></td>
</tr> </tr>
<tr> <tr>
<td>Scaner</td> <td>Ignore IP</td>
<td><select name="scaner" class="form-select"> <td><select name="ignore" class="form-select">
<option selected>{{ .Config.Scaner }}</option> <option selected>{{ .Config.IgnoreIP }}</option>
<option value="arpscan">arpscan</option> <option value="yes">yes</option>
<option value="golang">golang (experimental)</option> <option value="no">no</option>
</select></td> </select></td>
</tr> </tr>
<tr> <tr>

View file

@ -1,10 +1,10 @@
{{ define "host.html" }} {{ define "host.html" }}
<script src="/fs/public/js/scan.js"></script> <script src="/fs/public/js/host-scan.js"></script>
<body> <body>
<div class="container-lg"> <div class="container-lg">
<div class="row"> <div class="row">
<div class="col-md mt-4"> <div class="col-md mt-4 mb-4">
<div class="card border-primary"> <div class="card border-primary">
<div class="card-header">Host</div> <div class="card-header">Host</div>
<div class="card-body table-responsive"> <div class="card-body table-responsive">
@ -17,6 +17,10 @@
<td>Name</td> <td>Name</td>
<td>{{ .Host.Name }}</td> <td>{{ .Host.Name }}</td>
</tr> </tr>
<tr>
<td>DNS</td>
<td>{{ .Host.DNS }}</td>
</tr>
<tr> <tr>
<td>Iface</td> <td>Iface</td>
<td>{{ .Host.Iface }}</td> <td>{{ .Host.Iface }}</td>
@ -50,6 +54,12 @@
{{ end }} {{ end }}
</td> </td>
</tr> </tr>
<tr>
<td>
<button onclick="delHost('{{ .Host.ID }}')" class="btn btn-danger">Delete host</button>
</td>
<td></td>
</tr>
</table> </table>
</div> </div>
</div> </div>
@ -77,6 +87,16 @@
</div> </div>
</div> </div>
</div> </div>
<div class="row">
<div class="col-md mb-4">
<div class="card border-primary">
<div class="card-header">History</div>
<div class="card-body">
</div>
</div>
</div>
</div>
</div> </div>
{{ template "footer.html" }} {{ template "footer.html" }}

View file

@ -46,6 +46,7 @@ func Gui(dirPath, nodePath string) {
router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go
router.GET("/api/history", apiHistory) // api.go router.GET("/api/history", apiHistory) // api.go
router.GET("/api/host", apiHost) // 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("/api/port/:addr/:port", apiPort) // api.go
router.GET("/", indexHandler) // index.go router.GET("/", indexHandler) // index.go