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/) The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). 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 ## [v0.8.1] - 2022-12-29
### Changed ### Changed
- Full code refactoring - 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) | | 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) | "" | | 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 | | 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 ## Config file

View file

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

View file

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

View file

@ -9,12 +9,13 @@ import (
"github.com/aceberg/WatchYourLAN/internal/notify" "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 { 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.Date = host.Date
oneHost.Now = 1 oneHost.Now = 1
db.Update(path, oneHost) db.Update(appConfig.DbPath, oneHost)
return true 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) { func hostsCompare(appConfig models.Conf, foundHosts, dbHosts []models.Host) {
for _, oneHost := range foundHosts { for _, oneHost := range foundHosts {
if !(hostInDB(appConfig.DbPath, oneHost, dbHosts)) { if !(hostInDB(appConfig, oneHost, dbHosts)) {
oneHost.Now = 1 // Mark host online oneHost.Now = 1 // Mark host online
msg := fmt.Sprintf("UNKNOWN HOST IP: '%s', MAC: '%s', Hw: '%s'", oneHost.IP, oneHost.Mac, oneHost.Hw) msg := fmt.Sprintf("UNKNOWN HOST IP: '%s', MAC: '%s', Hw: '%s'", oneHost.IP, oneHost.Mac, oneHost.Hw)
log.Println("WARN:", msg) log.Println("WARN:", msg)

View file

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

View file

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