Trim history

This commit is contained in:
aceberg 2023-10-06 22:17:06 +07:00
parent b8f7d3bfb3
commit a180ae3b92
8 changed files with 90 additions and 3 deletions

View file

@ -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 |

View file

@ -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)

View file

@ -19,6 +19,7 @@ type Conf struct {
NodePath string
Icon string
Auth bool
HistDays string
}
// Host - one host

View file

@ -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)
}

View file

@ -5,7 +5,7 @@
<div class="container mt-3">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Host ID</th>
<th>Name</th>
<th>IP</th>
<th>Mac</th>

View file

@ -98,6 +98,44 @@
{{ end }}
</div>
</div>
<div class="row">
<table class="table table-striped">
<tr>
<th>Host ID</th>
<th>Name</th>
<th>IP</th>
<th>Mac</th>
<th>Hardware</th>
<th>Last seen</th>
<th>Known</th>
<th>State</th>
</tr>
{{ range .Hist }}
<tr>
<td><a href="/host?id={{ .Host }}">{{ .Host }}</a></td>
<td>{{ .Name }}</td>
<td><a href="http://{{ .IP }}" target="_blank">{{ .IP }}</a></td>
<td><a href="/host?id={{ .Host }}">{{ .Mac }}</a></td>
<td>{{ .Hw }}</td>
<td>{{ .Date }}</td>
<td>
{{ if eq .Known 1 }}
Yes
{{ else }}
No
{{ end }}
</td>
<td>
{{ if eq .State 1 }}
<i class="bi bi-circle-fill text-success"></i>
{{ else }}
<i class="bi bi-circle-fill text-danger"></i>
{{ end }}
</td>
</tr>
{{ end }}
</table>
</div>
</div>

View file

@ -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)
}
}
}

View file

@ -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