feat(acl): add FORCE_RESOLVE_COUNTRY option to resolve country

This commit is contained in:
yusing 2025-04-26 09:48:43 +08:00
parent 59bc342a40
commit c72f66d64b
2 changed files with 8 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/puzpuzpuz/xsync/v3" "github.com/puzpuzpuz/xsync/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
acl "github.com/yusing/go-proxy/internal/acl/types" acl "github.com/yusing/go-proxy/internal/acl/types"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/logging/accesslog" "github.com/yusing/go-proxy/internal/logging/accesslog"
@ -153,7 +154,10 @@ func (c *Config) Start(parent *task.Task) gperr.Error {
return nil return nil
} }
func (c *config) cacheRecord(info *acl.IPInfo, allow bool) { func (c *Config) cacheRecord(info *acl.IPInfo, allow bool) {
if common.ForceResolveCountry && info.City == nil {
c.MaxMind.lookupCity(info)
}
c.ipCache.Store(info.Str, &checkCache{ c.ipCache.Store(info.Str, &checkCache{
IPInfo: info, IPInfo: info,
allow: allow, allow: allow,
@ -175,7 +179,7 @@ func (c *Config) IPAllowed(ip net.IP) bool {
return false return false
} }
// always allow private and loopback // always allow loopback
// loopback is not logged // loopback is not logged
if ip.IsLoopback() { if ip.IsLoopback() {
return true return true

View file

@ -58,6 +58,8 @@ var (
MetricsDisableDisk = GetEnvBool("METRICS_DISABLE_DISK", false) MetricsDisableDisk = GetEnvBool("METRICS_DISABLE_DISK", false)
MetricsDisableNetwork = GetEnvBool("METRICS_DISABLE_NETWORK", false) MetricsDisableNetwork = GetEnvBool("METRICS_DISABLE_NETWORK", false)
MetricsDisableSensors = GetEnvBool("METRICS_DISABLE_SENSORS", false) MetricsDisableSensors = GetEnvBool("METRICS_DISABLE_SENSORS", false)
ForceResolveCountry = GetEnvBool("FORCE_RESOLVE_COUNTRY", false)
) )
func GetEnv[T any](key string, defaultValue T, parser func(string) (T, error)) T { func GetEnv[T any](key string, defaultValue T, parser func(string) (T, error)) T {