From a180ae3b921bb2dae917cfb11eee503debf5ea6c Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:17:06 +0700 Subject: [PATCH] Trim history --- README.md | 2 +- internal/conf/config.go | 5 +++- internal/models/models.go | 1 + internal/web/host.go | 7 ++++++ internal/web/templates/history.html | 2 +- internal/web/templates/host.html | 38 +++++++++++++++++++++++++++++ internal/web/trim-history.go | 37 ++++++++++++++++++++++++++++ internal/web/webgui.go | 1 + 8 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 internal/web/trim-history.go diff --git a/README.md b/README.md index 6ab8982..312e1a1 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Configuration can be done through config file or environment variables | AUTH_PASSWORD | Encrypted password (bcrypt). [How to encrypt password with bcrypt?](docs/BCRYPT.md) | "" | | COLOR | Background color: light or dark | light | | DBPATH | Path to Database | /data/db.sqlite | -| GUIIP | Address for web GUI | localhost (127.0.0.1) | +| GUIIP | Address for web GUI | 0.0.0.0 | | GUIPORT | Port for web GUI | 8840 | | IFACE | Interface to scan. Could be one or more, separated by space. Currently `docker0` is not allowed, as arp-scan wouldn't work with it correctly | enp1s0 | | IGNOREIP | If you want to detect unknown hosts by MAC only, set this wariable to "yes" | no | diff --git a/internal/conf/config.go b/internal/conf/config.go index fb4316e..51b478b 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -13,7 +13,7 @@ func Get(path string) (config models.Conf, authConf auth.Conf) { viper.SetDefault("IFACE", "enp1s0") viper.SetDefault("DBPATH", "/data/db.sqlite") - viper.SetDefault("GUIIP", "localhost") + viper.SetDefault("GUIIP", "0.0.0.0") viper.SetDefault("GUIPORT", "8840") viper.SetDefault("TIMEOUT", "60") viper.SetDefault("SHOUTRRR_URL", "") @@ -24,6 +24,7 @@ func Get(path string) (config models.Conf, authConf auth.Conf) { viper.SetDefault("AUTH_USER", "") viper.SetDefault("AUTH_PASSWORD", "") viper.SetDefault("AUTH_EXPIRE", "7d") + viper.SetDefault("HISTORY_DAYS", "30") viper.SetConfigFile(path) viper.SetConfigType("yaml") @@ -42,6 +43,7 @@ func Get(path string) (config models.Conf, authConf auth.Conf) { config.Color = viper.Get("COLOR").(string) config.IgnoreIP = viper.Get("IGNOREIP").(string) config.LogLevel = viper.Get("LOGLEVEL").(string) + config.HistDays = viper.Get("HISTORY_DAYS").(string) authConf.Auth = viper.GetBool("AUTH") authConf.User, _ = viper.Get("AUTH_USER").(string) authConf.Password, _ = viper.Get("AUTH_PASSWORD").(string) @@ -69,6 +71,7 @@ func Write(path string, config models.Conf, authConf auth.Conf) { viper.Set("COLOR", config.Color) viper.Set("IGNOREIP", config.IgnoreIP) viper.Set("LOGLEVEL", config.LogLevel) + viper.Set("HISTORY_DAYS", config.HistDays) viper.Set("auth", authConf.Auth) viper.Set("auth_user", authConf.User) diff --git a/internal/models/models.go b/internal/models/models.go index 57a0acf..e4f89b9 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -19,6 +19,7 @@ type Conf struct { NodePath string Icon string Auth bool + HistDays string } // Host - one host diff --git a/internal/web/host.go b/internal/web/host.go index d46e157..ea2e710 100644 --- a/internal/web/host.go +++ b/internal/web/host.go @@ -49,5 +49,12 @@ func hostHandler(w http.ResponseWriter, r *http.Request) { guiData.Themes = addr + history := db.SelectHist(AppConfig.DbPath) + for _, hist := range history { + if hist.Host == id { + guiData.Hist = append(guiData.Hist, hist) + } + } + execTemplate(w, "host", guiData) } diff --git a/internal/web/templates/history.html b/internal/web/templates/history.html index be2316e..f19f680 100644 --- a/internal/web/templates/history.html +++ b/internal/web/templates/history.html @@ -5,7 +5,7 @@
- + diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index 82158e8..2ba1182 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -98,6 +98,44 @@ {{ end }} +
+
IDHost ID Name IP Mac
+ + + + + + + + + + + {{ range .Hist }} + + + + + + + + + + + {{ end }} +
Host IDNameIPMacHardwareLast seenKnownState
{{ .Host }}{{ .Name }}{{ .IP }}{{ .Mac }}{{ .Hw }}{{ .Date }} + {{ if eq .Known 1 }} + Yes + {{ else }} + No + {{ end }} + + {{ if eq .State 1 }} + + {{ else }} + + {{ end }} +
+
diff --git a/internal/web/trim-history.go b/internal/web/trim-history.go new file mode 100644 index 0000000..82fbb30 --- /dev/null +++ b/internal/web/trim-history.go @@ -0,0 +1,37 @@ +package web + +import ( + // "log" + "strconv" + "time" + + "github.com/aceberg/WatchYourLAN/internal/db" +) + +func trimHistoryRoutine() { + + for { + trimHistory() + + time.Sleep(time.Duration(1) * time.Hour) + } +} + +func trimHistory() { + + days, _ := strconv.Atoi(AppConfig.HistDays) + now := time.Now() + history := db.SelectHist(AppConfig.DbPath) + + for _, hist := range history { + date, _ := time.Parse("2006-01-02 15:04:05", hist.Date) + datePlus := date.Add(time.Duration(days) * 24 * time.Hour) + + if now.After(datePlus) { + + db.DeleteHist(AppConfig.DbPath, hist.ID) + + // log.Println("REMOVED DATE =", hist.Date) + } + } +} diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 3f433ed..e8542e5 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -24,6 +24,7 @@ func Gui(configPath, nodePath string) { QuitScan = make(chan bool) go scan.Start(AppConfig, QuitScan) + go trimHistoryRoutine() // trim-history.go updateAllHosts() // webgui.go