Commit 572b8a73 authored by vicotor's avatar vicotor

update file watcher

parent 1ae4f985
......@@ -695,19 +695,21 @@ func compilePattern(p string) *regexp.Regexp {
}
func startWhitelistWatcher(path string) {
// Start fsnotify watcher
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("create whitelist watcher error: %v", err)
return
}
defer watcher.Close()
dir := filepath.Dir(path)
if err := watcher.Add(dir); err != nil {
log.Printf("add whitelist watch dir error: %v", err)
watcher.Close()
return
}
go func() {
defer watcher.Close()
for {
select {
case ev, ok := <-watcher.Events:
......@@ -739,6 +741,34 @@ func startWhitelistWatcher(path string) {
}
}
}()
// Start Polling as fallback for Docker mounts
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var lastModTime time.Time
if info, err := os.Stat(path); err == nil {
lastModTime = info.ModTime()
}
for range ticker.C {
info, err := os.Stat(path)
if err != nil {
continue
}
// Check if modified
if !info.ModTime().Equal(lastModTime) {
lastModTime = info.ModTime()
exact, pats := loadWhitelist(path)
whitelistMu.Lock()
whitelist = exact
whitelistPatterns = pats
whitelistMu.Unlock()
log.Printf("whitelist reloaded (%d exact, %d patterns) due to polling change", len(exact), len(pats))
}
}
}()
}
// ===== IP Blacklist helper functions (dynamic reload) =====
......@@ -820,19 +850,21 @@ func isIPBlacklisted(v string) bool {
}
func startIPBlacklistWatcher(path string) {
// Start fsnotify watcher
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("create ip blacklist watcher error: %v", err)
return
}
defer watcher.Close()
dir := filepath.Dir(path)
if err := watcher.Add(dir); err != nil {
log.Printf("add ip blacklist watch dir error: %v", err)
watcher.Close()
return
}
go func() {
defer watcher.Close()
for {
select {
case ev, ok := <-watcher.Events:
......@@ -864,6 +896,34 @@ func startIPBlacklistWatcher(path string) {
}
}
}()
// Start Polling as fallback for Docker mounts
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var lastModTime time.Time
if info, err := os.Stat(path); err == nil {
lastModTime = info.ModTime()
}
for range ticker.C {
info, err := os.Stat(path)
if err != nil {
continue
}
// Check if modified
if !info.ModTime().Equal(lastModTime) {
lastModTime = info.ModTime()
exact, pats := loadIPBlacklist(path)
ipBlacklistMu.Lock()
ipBlacklist = exact
ipBlacklistPatterns = pats
ipBlacklistMu.Unlock()
log.Printf("ip blacklist reloaded (%d exact, %d patterns) due to polling change", len(exact), len(pats))
}
}
}()
}
// ===== Local Address Blacklist helper functions =====
......@@ -896,19 +956,21 @@ func isLocalBlacklisted(addr string) bool {
}
func startLocalBlacklistWatcher(path string) {
// Start fsnotify watcher
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("create local blacklist watcher error: %v", err)
return
}
defer watcher.Close()
dir := filepath.Dir(path)
if err := watcher.Add(dir); err != nil {
log.Printf("add local blacklist watch dir error: %v", err)
watcher.Close()
return
}
go func() {
defer watcher.Close()
for {
select {
case ev, ok := <-watcher.Events:
......@@ -938,6 +1000,33 @@ func startLocalBlacklistWatcher(path string) {
}
}
}()
// Start Polling as fallback for Docker mounts
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var lastModTime time.Time
if info, err := os.Stat(path); err == nil {
lastModTime = info.ModTime()
}
for range ticker.C {
info, err := os.Stat(path)
if err != nil {
continue
}
// Check if modified
if !info.ModTime().Equal(lastModTime) {
lastModTime = info.ModTime()
bl := loadLocalBlacklist(path)
localBlacklistMu.Lock()
localBlacklist = bl
localBlacklistMu.Unlock()
log.Printf("local blacklist reloaded (%d entries) due to polling change", len(bl))
}
}
}()
}
// ===== Ban List helper functions =====
......
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