Commit 8d7de62c authored by vicotor's avatar vicotor

add ipbanlist watcher

parent 1be0814d
......@@ -190,6 +190,7 @@ func main() {
}
banList = loadBanList(banListFile)
log.Printf("loaded ban list entries: %d", len(banList))
startIpBanListWatcher(banListFile)
// Load Local Address Blacklist
localBlacklistFile = os.Getenv("LOCAL_BLACKLIST_FILE")
......@@ -1029,6 +1030,53 @@ func startLocalBlacklistWatcher(path string) {
}()
}
func startIpBanListWatcher(path string) {
// Start fsnotify watcher
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("create ip banlist watcher error: %v", err)
return
}
defer watcher.Close()
dir := filepath.Dir(path)
if err := watcher.Add(dir); err != nil {
log.Printf("add ip banlist watch dir error: %v", err)
return
}
for {
select {
case ev, ok := <-watcher.Events:
if !ok {
return
}
if ev.Name == path {
if ev.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Rename) != 0 {
bl := loadBanList(path)
banListMu.Lock()
banList = bl
banListMu.Unlock()
log.Printf("ip banlist reloaded (%d entries) due to event: %s", len(bl), ev.Op.String())
}
if ev.Op&fsnotify.Remove != 0 {
banListMu.Lock()
banList = map[string]struct{}{}
banListMu.Unlock()
log.Printf("ip banlist file removed, cleared entries")
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("ip banlist watcher error: %v", err)
}
}
}()
}
// ===== Ban List helper functions =====
func loadBanList(path string) map[string]struct{} {
result := make(map[string]struct{})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment