fix(acl): maxmind error even if configured, refactor

This commit is contained in:
yusing
2025-05-14 13:44:43 +08:00
parent 8e27886235
commit b490e8c475
5 changed files with 47 additions and 19 deletions

View File

@@ -1,12 +1,29 @@
package maxmind
import (
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/notif"
"github.com/yusing/go-proxy/internal/task"
)
var instance *MaxMind
var warnOnce sync.Once
func warnNotConfigured() {
log.Warn().Msg("MaxMind not configured, geo lookup will fail")
notif.Notify(&notif.LogMessage{
Level: zerolog.WarnLevel,
Title: "MaxMind not configured",
Body: notif.MessageBody("MaxMind is not configured, geo lookup will fail"),
Color: notif.ColorError,
})
}
func SetInstance(parent task.Parent, cfg *Config) gperr.Error {
newInstance := &MaxMind{Config: cfg}
if err := newInstance.LoadMaxMindDB(parent); err != nil {
@@ -22,6 +39,7 @@ func HasInstance() bool {
func LookupCity(ip *IPInfo) (*City, bool) {
if instance == nil {
warnOnce.Do(warnNotConfigured)
return nil, false
}
return instance.lookupCity(ip)