Skip to content

Commit

Permalink
Trim history
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Oct 6, 2023
1 parent b8f7d3b commit a180ae3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
5 changes: 4 additions & 1 deletion internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Conf struct {
NodePath string
Icon string
Auth bool
HistDays string
}

// Host - one host
Expand Down
7 changes: 7 additions & 0 deletions internal/web/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/web/templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
38 changes: 38 additions & 0 deletions internal/web/templates/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>


Expand Down
37 changes: 37 additions & 0 deletions internal/web/trim-history.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
1 change: 1 addition & 0 deletions internal/web/webgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a180ae3

Please sign in to comment.