From 2497cd12da7f7ce83c33365417b801f9134eb626 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Mon, 2 Jan 2023 13:05:44 +0700 Subject: [PATCH] Ignore IP change Issue #25 --- .version | 2 +- CHANGELOG.md | 5 +++++ README.md | 1 + internal/conf/get-config.go | 3 +++ internal/models/models.go | 1 + internal/scan/compare.go | 9 +++++---- internal/web/config.go | 1 + internal/web/templates/config.html | 4 ++++ 8 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.version b/.version index 5409a84..31f28ae 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -VERSION=0.8.1 \ No newline at end of file +VERSION=0.8.2 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a74efd8..b776ff5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 4a28947..07aac5a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/internal/conf/get-config.go b/internal/conf/get-config.go index a758c4b..bd4ac44 100644 --- a/internal/conf/get-config.go +++ b/internal/conf/get-config.go @@ -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) diff --git a/internal/models/models.go b/internal/models/models.go index 7b2cb58..64f6e19 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -21,6 +21,7 @@ type Conf struct { Timeout int ShoutURL string Theme string + IgnoreIP string } // GuiData - all data sent to html page diff --git a/internal/scan/compare.go b/internal/scan/compare.go index 3fee4ef..16c0249 100644 --- a/internal/scan/compare.go +++ b/internal/scan/compare.go @@ -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) diff --git a/internal/web/config.go b/internal/web/config.go index 1130c67..c996017 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -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) diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index b19b84b..5aacd38 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -35,6 +35,10 @@ {{ end }} + + Ignore IP + +