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("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)

View file

@ -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)
}

View file

@ -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"`

View file

@ -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")

View file

@ -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")

View file

@ -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
}

View file

@ -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)

View file

@ -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', '<br>');
}
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 = `<a href="http://${addr}:${port}">${port}</a>&nbsp;&nbsp;&nbsp;`;
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 {
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)
}
}

View file

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

View file

@ -1,10 +1,10 @@
{{ define "host.html" }}
<script src="/fs/public/js/scan.js"></script>
<script src="/fs/public/js/host-scan.js"></script>
<body>
<div class="container-lg">
<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-header">Host</div>
<div class="card-body table-responsive">
@ -17,6 +17,10 @@
<td>Name</td>
<td>{{ .Host.Name }}</td>
</tr>
<tr>
<td>DNS</td>
<td>{{ .Host.DNS }}</td>
</tr>
<tr>
<td>Iface</td>
<td>{{ .Host.Iface }}</td>
@ -50,6 +54,12 @@
{{ end }}
</td>
</tr>
<tr>
<td>
<button onclick="delHost('{{ .Host.ID }}')" class="btn btn-danger">Delete host</button>
</td>
<td></td>
</tr>
</table>
</div>
</div>
@ -77,6 +87,16 @@
</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>
{{ 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/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