This commit is contained in:
aceberg 2024-08-25 23:39:26 +07:00
parent 17b6545eaa
commit 8542417d40
5 changed files with 20 additions and 13 deletions

View file

@ -2,7 +2,9 @@ package main
import ( import (
"flag" "flag"
// "net/http"
// _ "net/http/pprof"
_ "time/tzdata" _ "time/tzdata"
"github.com/aceberg/WatchYourLAN/internal/web" "github.com/aceberg/WatchYourLAN/internal/web"
@ -16,5 +18,14 @@ func main() {
nodePtr := flag.String("n", nodePath, "Path to node modules") nodePtr := flag.String("n", nodePath, "Path to node modules")
flag.Parse() flag.Parse()
// pprof - memory leak detect
// go tool pprof -alloc_space http://localhost:8085/debug/pprof/heap
// (pprof) web
// (pprof) list db.Select
//
// go func() {
// http.ListenAndServe("localhost:8085", nil)
// }()
web.Gui(*dirPtr, *nodePtr) // webgui.go web.Gui(*dirPtr, *nodePtr) // webgui.go
} }

View file

@ -21,6 +21,8 @@ func apiAll(c *gin.Context) {
func apiHistory(c *gin.Context) { func apiHistory(c *gin.Context) {
var hosts []models.Host var hosts []models.Host
histHosts := db.Select(appConfig.DBPath, "history")
mac := c.Param("mac") mac := c.Param("mac")
if mac != "/" { if mac != "/" {

View file

@ -10,8 +10,8 @@ var (
// appConfig - config for Web Gui // appConfig - config for Web Gui
appConfig models.Conf appConfig models.Conf
allHosts []models.Host allHosts []models.Host
histHosts []models.Host // histHosts []models.Host
quitScan chan bool quitScan chan bool
) )

View file

@ -16,7 +16,6 @@ func updateRoutines() {
db.Create(appConfig.DBPath) db.Create(appConfig.DBPath)
allHosts = db.Select(appConfig.DBPath, "now") allHosts = db.Select(appConfig.DBPath, "now")
histHosts = db.Select(appConfig.DBPath, "history")
quitScan = make(chan bool) quitScan = make(chan bool)
go startScan(quitScan) // scan-routine.go go startScan(quitScan) // scan-routine.go

View file

@ -12,10 +12,6 @@ import (
"github.com/aceberg/WatchYourLAN/internal/notify" "github.com/aceberg/WatchYourLAN/internal/notify"
) )
var foundHostsMap map[string]models.Host
var aHost, fHost models.Host
var exists bool
func startScan(quit chan bool) { func startScan(quit chan bool) {
var lastDate, nowDate, plusDate time.Time var lastDate, nowDate, plusDate time.Time
var foundHosts []models.Host var foundHosts []models.Host
@ -33,7 +29,6 @@ func startScan(quit chan bool) {
foundHosts = arp.Scan(appConfig.Ifaces) foundHosts = arp.Scan(appConfig.Ifaces)
compareHosts(foundHosts) compareHosts(foundHosts)
allHosts = db.Select(appConfig.DBPath, "now") allHosts = db.Select(appConfig.DBPath, "now")
histHosts = db.Select(appConfig.DBPath, "history")
lastDate = time.Now() lastDate = time.Now()
} }
@ -46,14 +41,14 @@ func startScan(quit chan bool) {
func compareHosts(foundHosts []models.Host) { func compareHosts(foundHosts []models.Host) {
// Make map of found hosts // 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
} }
for _, aHost = range allHosts { for _, aHost := range allHosts {
fHost, exists = foundHostsMap[aHost.Mac] fHost, exists := foundHostsMap[aHost.Mac]
if exists { if exists {
aHost.Iface = fHost.Iface aHost.Iface = fHost.Iface
@ -75,7 +70,7 @@ func compareHosts(foundHosts []models.Host) {
} }
} }
for _, fHost = range foundHostsMap { for _, fHost := range foundHostsMap {
msg := fmt.Sprintf("Unknown host IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface) msg := fmt.Sprintf("Unknown host IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
slog.Warn(msg) slog.Warn(msg)