Host history

This commit is contained in:
aceberg 2024-08-24 15:44:20 +07:00
parent 0000b54fb8
commit 0d10123d12
10 changed files with 87 additions and 21 deletions

View file

@ -21,7 +21,6 @@ func parseOutput(text, iface string) []models.Host {
var foundHosts = []models.Host{} var foundHosts = []models.Host{}
perString := strings.Split(text, "\n") perString := strings.Split(text, "\n")
currentTime := time.Now()
for _, host := range perString { for _, host := range perString {
if host != "" { if host != "" {
@ -31,7 +30,7 @@ func parseOutput(text, iface string) []models.Host {
oneHost.IP = p[0] oneHost.IP = p[0]
oneHost.Mac = p[1] oneHost.Mac = p[1]
oneHost.Hw = p[2] 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 oneHost.Now = 1
foundHosts = append(foundHosts, oneHost) foundHosts = append(foundHosts, oneHost)
} }

View file

@ -1,12 +1,13 @@
package web package web
import ( import (
// "log" "log"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/aceberg/WatchYourLAN/internal/db" "github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
"github.com/aceberg/WatchYourLAN/internal/portscan" "github.com/aceberg/WatchYourLAN/internal/portscan"
) )
@ -18,8 +19,19 @@ func apiAll(c *gin.Context) {
} }
func apiHistory(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) { func apiHost(c *gin.Context) {

View file

@ -33,3 +33,15 @@ func updateDNS(host models.Host) (name, dns string) {
return name, dns 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
}

View file

@ -20,6 +20,9 @@ function createHTML(addr, i) {
<a href="http://${addr.IP}">${addr.IP}</a> <a href="http://${addr.IP}">${addr.IP}</a>
</td> </td>
<td>${addr.Mac}</td> <td>${addr.Mac}</td>
<td>${addr.Hw}</td>
<td>${addr.Date}</td>
<td>${addr.Known}</td>
<td>${now}</td> <td>${now}</td>
</tr> </tr>
`; `;

View file

@ -50,12 +50,3 @@ 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

@ -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 = `<i ${title}><svg width="10" height="20">
<rect width="10" height="20" style="${col}"/>
Sorry, your browser does not support inline SVG.
</svg></i>`;
// html = `<i class="bi bi-file-fill" style="${col}" ${title}></i>`;
document.getElementById('showHist').insertAdjacentHTML('beforeend', html);
}
}

View file

@ -47,17 +47,16 @@ func startScan() {
func compareHosts(foundHosts []models.Host) { func compareHosts(foundHosts []models.Host) {
// Make map and Insert history // Make map of found hosts
foundHostsMap := make(map[string]models.Host) foundHostsMap := make(map[string]models.Host)
for _, fHost := range foundHosts { for _, fHost := range foundHosts {
foundHostsMap[fHost.Mac] = fHost foundHostsMap[fHost.Mac] = fHost
db.Insert(appConfig.DBPath, "history", fHost)
} }
for _, aHost := range allHosts { for _, aHost := range allHosts {
fHost, exists := foundHostsMap[aHost.Mac] fHost, exists := foundHostsMap[aHost.Mac]
if exists && (appConfig.IgnoreIP == "yes" || aHost.IP == fHost.IP) { if exists {
aHost.Iface = fHost.Iface aHost.Iface = fHost.Iface
aHost.IP = fHost.IP aHost.IP = fHost.IP
@ -68,9 +67,11 @@ 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)
aHost.Date = time.Now().Format("2006-01-02 15:04:05")
db.Insert(appConfig.DBPath, "history", aHost)
} }
for _, fHost := range foundHostsMap { for _, fHost := range foundHostsMap {

View file

@ -15,7 +15,10 @@
<th>Iface <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Iface')"></i></th> <th>Iface <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Iface')"></i></th>
<th>IP <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('IP')"></i></th> <th>IP <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('IP')"></i></th>
<th>MAC <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Mac')"></i></th> <th>MAC <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Mac')"></i></th>
<th>History <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Now')"></i></th> <th>Hardware <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Hw')"></i></th>
<th>Date <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Date')"></i></th>
<th>Known <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Known')"></i></th>
<th>Online <i class="bi bi-sort-down-alt my-btn" onclick="sortBy('Now')"></i></th>
</thead> </thead>
<tbody id="tBody"></tbody> <tbody id="tBody"></tbody>
<!-- index.js --> <!-- index.js -->

View file

@ -1,6 +1,7 @@
{{ define "host.html" }} {{ define "host.html" }}
<script src="/fs/public/js/host-scan.js"></script> <script src="/fs/public/js/host-scan.js"></script>
<script src="/fs/public/js/host.js"></script>
<body> <body>
<div class="container-lg"> <div class="container-lg">
<div class="row"> <div class="row">
@ -43,7 +44,7 @@
</tr> </tr>
<tr> <tr>
<td>Known</td> <td>Known</td>
<td>{{ .Host.Known }}</td> <td>{{ if eq .Host.Known 1 }}Yes{{ else }}No{{ end }}</td>
</tr> </tr>
<tr> <tr>
<td>Online</td> <td>Online</td>
@ -92,12 +93,15 @@
<div class="card border-primary"> <div class="card border-primary">
<div class="card-header">History</div> <div class="card-header">History</div>
<div class="card-body"> <div class="card-body">
<div id="showHist"></div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<script>
loadHistory('{{ .Host.Mac }}');
</script>
{{ template "footer.html" }} {{ template "footer.html" }}
{{ end }} {{ end }}

View file

@ -44,7 +44,7 @@ func Gui(dirPath, nodePath string) {
router.GET("/api/all", apiAll) // api.go router.GET("/api/all", apiAll) // api.go
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/*mac", 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/host/del/:id", apiHostDel) // api.go
router.GET("/api/port/:addr/:port", apiPort) // api.go router.GET("/api/port/:addr/:port", apiPort) // api.go