From 107e29ee2034b133c73659d6ee3c174b3f9f34f3 Mon Sep 17 00:00:00 2001 From: Toby Date: Wed, 10 Apr 2024 21:30:37 -0700 Subject: [PATCH] fix: do not reload geoip/geosite when reloading ruleset to prevent leaking references to streams --- cmd/root.go | 4 ++-- ruleset/expr.go | 14 ++++++-------- ruleset/interface.go | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2ea21eb..288e3d7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,7 @@ import ( "github.com/apernet/OpenGFW/modifier" modUDP "github.com/apernet/OpenGFW/modifier/udp" "github.com/apernet/OpenGFW/ruleset" + "github.com/apernet/OpenGFW/ruleset/builtins/geo" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -259,8 +260,7 @@ func runMain(cmd *cobra.Command, args []string) { } rsConfig := &ruleset.BuiltinConfig{ Logger: &rulesetLogger{}, - GeoSiteFilename: config.Ruleset.GeoSite, - GeoIpFilename: config.Ruleset.GeoIp, + GeoMatcher: geo.NewGeoMatcher(config.Ruleset.GeoSite, config.Ruleset.GeoIp), ProtectedDialContext: engineConfig.IO.ProtectedDialContext, } rs, err := ruleset.CompileExprRules(rawRs, analyzers, modifiers, rsConfig) diff --git a/ruleset/expr.go b/ruleset/expr.go index 868a115..373d0d3 100644 --- a/ruleset/expr.go +++ b/ruleset/expr.go @@ -20,7 +20,6 @@ import ( "github.com/apernet/OpenGFW/analyzer" "github.com/apernet/OpenGFW/modifier" "github.com/apernet/OpenGFW/ruleset/builtins" - "github.com/apernet/OpenGFW/ruleset/builtins/geo" ) // ExprRule is the external representation of an expression rule. @@ -302,23 +301,22 @@ type Function struct { } func buildFunctionMap(config *BuiltinConfig) map[string]*Function { - geoMatcher := geo.NewGeoMatcher(config.GeoSiteFilename, config.GeoIpFilename) return map[string]*Function{ "geoip": { - InitFunc: geoMatcher.LoadGeoIP, + InitFunc: config.GeoMatcher.LoadGeoIP, PatchFunc: nil, Func: func(params ...any) (any, error) { - return geoMatcher.MatchGeoIp(params[0].(string), params[1].(string)), nil + return config.GeoMatcher.MatchGeoIp(params[0].(string), params[1].(string)), nil }, - Types: []reflect.Type{reflect.TypeOf(geoMatcher.MatchGeoIp)}, + Types: []reflect.Type{reflect.TypeOf(config.GeoMatcher.MatchGeoIp)}, }, "geosite": { - InitFunc: geoMatcher.LoadGeoSite, + InitFunc: config.GeoMatcher.LoadGeoSite, PatchFunc: nil, Func: func(params ...any) (any, error) { - return geoMatcher.MatchGeoSite(params[0].(string), params[1].(string)), nil + return config.GeoMatcher.MatchGeoSite(params[0].(string), params[1].(string)), nil }, - Types: []reflect.Type{reflect.TypeOf(geoMatcher.MatchGeoSite)}, + Types: []reflect.Type{reflect.TypeOf(config.GeoMatcher.MatchGeoSite)}, }, "cidr": { InitFunc: nil, diff --git a/ruleset/interface.go b/ruleset/interface.go index 535c2a4..a6df363 100644 --- a/ruleset/interface.go +++ b/ruleset/interface.go @@ -7,6 +7,7 @@ import ( "github.com/apernet/OpenGFW/analyzer" "github.com/apernet/OpenGFW/modifier" + "github.com/apernet/OpenGFW/ruleset/builtins/geo" ) type Action int @@ -102,7 +103,6 @@ type Logger interface { type BuiltinConfig struct { Logger Logger - GeoSiteFilename string - GeoIpFilename string + GeoMatcher *geo.GeoMatcher ProtectedDialContext func(ctx context.Context, network, address string) (net.Conn, error) }