Host history
This commit is contained in:
parent
0000b54fb8
commit
0d10123d12
10 changed files with 87 additions and 21 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ function createHTML(addr, i) {
|
|||
<a href="http://${addr.IP}">${addr.IP}</a>
|
||||
</td>
|
||||
<td>${addr.Mac}</td>
|
||||
<td>${addr.Hw}</td>
|
||||
<td>${addr.Date}</td>
|
||||
<td>${addr.Known}</td>
|
||||
<td>${now}</td>
|
||||
</tr>
|
||||
`;
|
||||
|
|
|
|||
|
|
@ -49,13 +49,4 @@ async function scanAddr() {
|
|||
function genHTML(addr, port) {
|
||||
html = `<a href="http://${addr}:${port}">${port}</a> `;
|
||||
return html;
|
||||
}
|
||||
|
||||
async function delHost(id) {
|
||||
|
||||
const url = '/api/host/del/'+id;
|
||||
|
||||
await fetch(url);
|
||||
|
||||
window.location.href = '/';
|
||||
}
|
||||
41
internal/web/public/js/host.js
Normal file
41
internal/web/public/js/host.js
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,10 @@
|
|||
<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>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>
|
||||
<tbody id="tBody"></tbody>
|
||||
<!-- index.js -->
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
{{ define "host.html" }}
|
||||
|
||||
<script src="/fs/public/js/host-scan.js"></script>
|
||||
<script src="/fs/public/js/host.js"></script>
|
||||
<body>
|
||||
<div class="container-lg">
|
||||
<div class="row">
|
||||
|
|
@ -43,7 +44,7 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<td>Known</td>
|
||||
<td>{{ .Host.Known }}</td>
|
||||
<td>{{ if eq .Host.Known 1 }}Yes{{ else }}No{{ end }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Online</td>
|
||||
|
|
@ -92,12 +93,15 @@
|
|||
<div class="card border-primary">
|
||||
<div class="card-header">History</div>
|
||||
<div class="card-body">
|
||||
|
||||
<div id="showHist"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
loadHistory('{{ .Host.Mac }}');
|
||||
</script>
|
||||
|
||||
{{ template "footer.html" }}
|
||||
{{ end }}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue