Ignore IP change Issue #25

This commit is contained in:
aceberg 2023-01-02 13:05:44 +07:00
parent 524313845f
commit 2497cd12da
8 changed files with 21 additions and 5 deletions

View file

@ -1 +1 @@
VERSION=0.8.1
VERSION=0.8.2

View file

@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [v0.8.2] - 2023-01-02
### Added
- Option to ignore IP change [Issue #25](https://github.com/aceberg/WatchYourLAN/issues/25)
## [v0.8.1] - 2022-12-29
### Changed
- Full code refactoring

View file

@ -57,6 +57,7 @@ Configuration can be done through config file or environment variables
| TIMEOUT | Time between scans (seconds) | 60 (1 minute) |
| SHOUTRRR_URL | Url to any notification service supported by [Shoutrrr](https://github.com/containrrr/shoutrrr) (gotify, email, telegram and others) | "" |
| THEME | Any theme name from https://bootswatch.com in lowcase | solar |
| IGNOREIP | If you want to detect unknown hosts by MAC only, set this wariable to "yes" | no |
## Config file

View file

@ -16,6 +16,7 @@ func Get(path string) (config models.Conf) {
viper.SetDefault("TIMEOUT", "60")
viper.SetDefault("SHOUTRRR_URL", "")
viper.SetDefault("THEME", "solar")
viper.SetDefault("IGNOREIP", "no")
viper.SetConfigFile(path)
viper.SetConfigType("env")
@ -31,6 +32,7 @@ func Get(path string) (config models.Conf) {
config.Timeout = viper.GetInt("TIMEOUT")
config.ShoutURL = viper.Get("SHOUTRRR_URL").(string)
config.Theme = viper.Get("THEME").(string)
config.IgnoreIP = viper.Get("IGNOREIP").(string)
return config
}
@ -47,6 +49,7 @@ func Write(path string, config models.Conf) {
viper.Set("TIMEOUT", config.Timeout)
viper.Set("SHOUTRRR_URL", config.ShoutURL)
viper.Set("THEME", config.Theme)
viper.Set("IGNOREIP", config.IgnoreIP)
err := viper.WriteConfig()
check.IfError(err)

View file

@ -21,6 +21,7 @@ type Conf struct {
Timeout int
ShoutURL string
Theme string
IgnoreIP string
}
// GuiData - all data sent to html page

View file

@ -9,12 +9,13 @@ import (
"github.com/aceberg/WatchYourLAN/internal/notify"
)
func hostInDB(path string, host models.Host, dbHosts []models.Host) bool { // Check if host is already in DB
func hostInDB(appConfig models.Conf, host models.Host, dbHosts []models.Host) bool { // Check if host is already in DB
for _, oneHost := range dbHosts {
if host.IP == oneHost.IP && host.Mac == oneHost.Mac && host.Hw == oneHost.Hw {
if host.Mac == oneHost.Mac && (appConfig.IgnoreIP == "yes" || host.IP == oneHost.IP) {
oneHost.IP = host.IP
oneHost.Date = host.Date
oneHost.Now = 1
db.Update(path, oneHost)
db.Update(appConfig.DbPath, oneHost)
return true
}
}
@ -23,7 +24,7 @@ func hostInDB(path string, host models.Host, dbHosts []models.Host) bool { // Ch
func hostsCompare(appConfig models.Conf, foundHosts, dbHosts []models.Host) {
for _, oneHost := range foundHosts {
if !(hostInDB(appConfig.DbPath, oneHost, dbHosts)) {
if !(hostInDB(appConfig, oneHost, dbHosts)) {
oneHost.Now = 1 // Mark host online
msg := fmt.Sprintf("UNKNOWN HOST IP: '%s', MAC: '%s', Hw: '%s'", oneHost.IP, oneHost.Mac, oneHost.Hw)
log.Println("WARN:", msg)

View file

@ -35,6 +35,7 @@ func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
AppConfig.GuiPort = r.FormValue("port")
AppConfig.ShoutURL = r.FormValue("shout")
AppConfig.Theme = r.FormValue("theme")
AppConfig.IgnoreIP = r.FormValue("ignoreip")
timeout := r.FormValue("timeout")
AppConfig.Timeout, err = strconv.Atoi(timeout)

View file

@ -35,6 +35,10 @@
{{ end }}
</select></td>
</tr>
<tr>
<td>Ignore IP</td>
<td><input name="ignoreip" type="text" class="form-control" value="{{ .Config.IgnoreIP }}"></td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
<td></td>