Skip to content

Commit

Permalink
Host history
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Aug 24, 2024
1 parent 0000b54 commit 0d10123
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 21 deletions.
3 changes: 1 addition & 2 deletions internal/arp/arpscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
}
Expand Down
16 changes: 14 additions & 2 deletions internal/web/api.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions internal/web/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions internal/web/public/js/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>
`;
Expand Down
9 changes: 0 additions & 9 deletions internal/web/public/js/host-scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,4 @@ 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 = '/';
}
41 changes: 41 additions & 0 deletions internal/web/public/js/host.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 5 additions & 4 deletions internal/web/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion internal/web/templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
8 changes: 6 additions & 2 deletions internal/web/templates/host.html
Original file line number Diff line number Diff line change
@@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion internal/web/webgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d10123

Please sign in to comment.