mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 02:09:01 +02:00
fix(acl): correct acl log handling and add country to summary
This commit is contained in:
@@ -109,6 +109,10 @@ func (c *Config) Validate() gperr.Error {
|
|||||||
c.allowLocal = true
|
c.allowLocal = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Notify.Interval < 0 {
|
||||||
|
c.Notify.Interval = defaultNotifyInterval
|
||||||
|
}
|
||||||
|
|
||||||
if c.Log != nil {
|
if c.Log != nil {
|
||||||
c.logAllowed = c.Log.LogAllowed
|
c.logAllowed = c.Log.LogAllowed
|
||||||
}
|
}
|
||||||
@@ -120,24 +124,6 @@ func (c *Config) Validate() gperr.Error {
|
|||||||
|
|
||||||
c.ipCache = xsync.NewMap[string, *checkCache]()
|
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 {
|
if c.Notify.IncludeAllowed != nil {
|
||||||
c.notifyAllowed = *c.Notify.IncludeAllowed
|
c.notifyAllowed = *c.Notify.IncludeAllowed
|
||||||
} else {
|
} else {
|
||||||
@@ -162,6 +148,18 @@ func (c *Config) Start(parent task.Parent) gperr.Error {
|
|||||||
return c.valErr
|
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() {
|
if c.needLogOrNotify() {
|
||||||
go c.logNotifyLoop(parent)
|
go c.logNotifyLoop(parent)
|
||||||
}
|
}
|
||||||
@@ -187,13 +185,30 @@ func (c *Config) cacheRecord(info *maxmind.IPInfo, allow bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) needLogOrNotify() 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 {
|
func (c *Config) needNotify() bool {
|
||||||
return len(c.Notify.To) > 0
|
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) {
|
func (c *Config) logNotifyLoop(parent task.Parent) {
|
||||||
defer c.notifyTicker.Stop()
|
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)
|
fieldsBody[i] = fmt.Sprintf("Total: allowed %d, blocked %d", c.totalAllowedCount, c.totalBlockedCount)
|
||||||
i++
|
i++
|
||||||
for ip, count := range c.allowedCount {
|
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++
|
i++
|
||||||
}
|
}
|
||||||
for ip, count := range c.blockedCount {
|
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++
|
i++
|
||||||
}
|
}
|
||||||
notif.Notify(¬if.LogMessage{
|
notif.Notify(¬if.LogMessage{
|
||||||
|
|||||||
Reference in New Issue
Block a user