From cb5a8e7b9d22a17406ac106bb49f3f9e42b1bb26 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 11 Oct 2025 17:03:08 +0800 Subject: [PATCH] fix(acl): correct acl log handling and add country to summary --- internal/acl/config.go | 57 ++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/internal/acl/config.go b/internal/acl/config.go index 23079ea5..85ccc738 100644 --- a/internal/acl/config.go +++ b/internal/acl/config.go @@ -109,6 +109,10 @@ func (c *Config) Validate() gperr.Error { c.allowLocal = true } + if c.Notify.Interval < 0 { + c.Notify.Interval = defaultNotifyInterval + } + if c.Log != nil { c.logAllowed = c.Log.LogAllowed } @@ -120,24 +124,6 @@ func (c *Config) Validate() gperr.Error { c.ipCache = xsync.NewMap[string, *checkCache]() - if c.needLogOrNotify() { - c.logNotifyCh = make(chan ipLog, 100) - } - - if c.needNotify() { - c.allowedCount = make(map[string]uint32) - c.blockedCount = make(map[string]uint32) - } - - if c.Notify.Interval < 0 { - c.Notify.Interval = defaultNotifyInterval - } - if c.needNotify() { - c.notifyTicker = time.NewTicker(c.Notify.Interval) - } else { - c.notifyTicker = time.NewTicker(time.Duration(math.MaxInt64)) // never tick - } - if c.Notify.IncludeAllowed != nil { c.notifyAllowed = *c.Notify.IncludeAllowed } else { @@ -162,6 +148,18 @@ func (c *Config) Start(parent task.Parent) gperr.Error { return c.valErr } + if c.needLogOrNotify() { + c.logNotifyCh = make(chan ipLog, 100) + } + + if c.needNotify() { + c.allowedCount = make(map[string]uint32) + c.blockedCount = make(map[string]uint32) + c.notifyTicker = time.NewTicker(c.Notify.Interval) + } else { + c.notifyTicker = time.NewTicker(time.Duration(math.MaxInt64)) // never tick + } + if c.needLogOrNotify() { go c.logNotifyLoop(parent) } @@ -187,13 +185,30 @@ func (c *Config) cacheRecord(info *maxmind.IPInfo, allow bool) { } func (c *Config) needLogOrNotify() bool { - return c.logger != nil || c.needNotify() + return c.needLog() || c.needNotify() +} + +func (c *Config) needLog() bool { + return c.logger != nil } func (c *Config) needNotify() bool { return len(c.Notify.To) > 0 } +func (c *Config) getCachedCity(ip string) string { + record, ok := c.ipCache.Load(ip) + if ok { + if record.City != nil { + if record.City.Country.IsoCode != "" { + return record.City.Country.IsoCode + } + return record.City.Location.TimeZone + } + } + return "unknown location" +} + func (c *Config) logNotifyLoop(parent task.Parent) { defer c.notifyTicker.Stop() @@ -229,11 +244,11 @@ func (c *Config) logNotifyLoop(parent task.Parent) { fieldsBody[i] = fmt.Sprintf("Total: allowed %d, blocked %d", c.totalAllowedCount, c.totalBlockedCount) i++ for ip, count := range c.allowedCount { - fieldsBody[i] = fmt.Sprintf("%s: allowed %d times", ip, count) + fieldsBody[i] = fmt.Sprintf("%s (%s): allowed %d times", ip, c.getCachedCity(ip), count) i++ } for ip, count := range c.blockedCount { - fieldsBody[i] = fmt.Sprintf("%s: blocked %d times", ip, count) + fieldsBody[i] = fmt.Sprintf("%s (%s): blocked %d times", ip, c.getCachedCity(ip), count) i++ } notif.Notify(¬if.LogMessage{