diff --git a/common/geodata/cache.go b/common/geodata/cache.go index 65ffd066f..9a654e909 100644 --- a/common/geodata/cache.go +++ b/common/geodata/cache.go @@ -11,27 +11,27 @@ import ( "github.com/p4gefau1t/trojan-go/log" ) -type GeoIPCache map[string]*v2router.GeoIP +type geoipCache map[string]*v2router.GeoIP -func (g GeoIPCache) Has(key string) bool { +func (g geoipCache) Has(key string) bool { return !(g.Get(key) == nil) } -func (g GeoIPCache) Get(key string) *v2router.GeoIP { +func (g geoipCache) Get(key string) *v2router.GeoIP { if g == nil { return nil } return g[key] } -func (g GeoIPCache) Set(key string, value *v2router.GeoIP) { +func (g geoipCache) Set(key string, value *v2router.GeoIP) { if g == nil { g = make(map[string]*v2router.GeoIP) } g[key] = value } -func (g GeoIPCache) Unmarshal(filename, code string) (*v2router.GeoIP, error) { +func (g geoipCache) Unmarshal(filename, code string) (*v2router.GeoIP, error) { asset := common.GetAssetLocation(filename) idx := strings.ToLower(asset + ":" + code) if g.Has(idx) { @@ -77,27 +77,27 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*v2router.GeoIP, error) { return nil, common.NewError("country code " + code + " not found in " + filename) } -type GeoSiteCache map[string]*v2router.GeoSite +type geositeCache map[string]*v2router.GeoSite -func (g GeoSiteCache) Has(key string) bool { +func (g geositeCache) Has(key string) bool { return !(g.Get(key) == nil) } -func (g GeoSiteCache) Get(key string) *v2router.GeoSite { +func (g geositeCache) Get(key string) *v2router.GeoSite { if g == nil { return nil } return g[key] } -func (g GeoSiteCache) Set(key string, value *v2router.GeoSite) { +func (g geositeCache) Set(key string, value *v2router.GeoSite) { if g == nil { g = make(map[string]*v2router.GeoSite) } g[key] = value } -func (g GeoSiteCache) Unmarshal(filename, code string) (*v2router.GeoSite, error) { +func (g geositeCache) Unmarshal(filename, code string) (*v2router.GeoSite, error) { asset := common.GetAssetLocation(filename) idx := strings.ToLower(asset + ":" + code) if g.Has(idx) { diff --git a/common/geodata/decode_test.go b/common/geodata/decode_test.go index f210aee7a..d4b84d389 100644 --- a/common/geodata/decode_test.go +++ b/common/geodata/decode_test.go @@ -72,7 +72,7 @@ func BenchmarkLoadGeoIP(b *testing.B) { m1 := runtime.MemStats{} m2 := runtime.MemStats{} - loader := geodata.GetGeodataLoader() + loader := geodata.NewGeodataLoader() runtime.ReadMemStats(&m1) cn, _ := loader.LoadGeoIP("cn") @@ -89,7 +89,7 @@ func BenchmarkLoadGeoSite(b *testing.B) { m3 := runtime.MemStats{} m4 := runtime.MemStats{} - loader := geodata.GetGeodataLoader() + loader := geodata.NewGeodataLoader() runtime.ReadMemStats(&m3) cn, _ := loader.LoadGeoSite("cn") diff --git a/common/geodata/interface.go b/common/geodata/interface.go new file mode 100644 index 000000000..f40dac66e --- /dev/null +++ b/common/geodata/interface.go @@ -0,0 +1,10 @@ +package geodata + +import v2router "github.com/v2fly/v2ray-core/v4/app/router" + +type GeodataLoader interface { + LoadIP(filename, country string) ([]*v2router.CIDR, error) + LoadSite(filename, list string) ([]*v2router.Domain, error) + LoadGeoIP(country string) ([]*v2router.CIDR, error) + LoadGeoSite(list string) ([]*v2router.Domain, error) +} diff --git a/common/geodata/loader.go b/common/geodata/loader.go index 62838de53..bf6f10548 100644 --- a/common/geodata/loader.go +++ b/common/geodata/loader.go @@ -6,27 +6,20 @@ import ( v2router "github.com/v2fly/v2ray-core/v4/app/router" ) -type geodataLoader interface { - LoadIP(filename, country string) ([]*v2router.CIDR, error) - LoadSite(filename, list string) ([]*v2router.Domain, error) - LoadGeoIP(country string) ([]*v2router.CIDR, error) - LoadGeoSite(list string) ([]*v2router.Domain, error) +type geodataCache struct { + geoipCache + geositeCache } -func GetGeodataLoader() geodataLoader { +func NewGeodataLoader() GeodataLoader { return &geodataCache{ make(map[string]*v2router.GeoIP), make(map[string]*v2router.GeoSite), } } -type geodataCache struct { - GeoIPCache - GeoSiteCache -} - func (g *geodataCache) LoadIP(filename, country string) ([]*v2router.CIDR, error) { - geoip, err := g.GeoIPCache.Unmarshal(filename, country) + geoip, err := g.geoipCache.Unmarshal(filename, country) if err != nil { return nil, err } @@ -35,7 +28,7 @@ func (g *geodataCache) LoadIP(filename, country string) ([]*v2router.CIDR, error } func (g *geodataCache) LoadSite(filename, list string) ([]*v2router.Domain, error) { - geosite, err := g.GeoSiteCache.Unmarshal(filename, list) + geosite, err := g.geositeCache.Unmarshal(filename, list) if err != nil { return nil, err } diff --git a/tunnel/router/client.go b/tunnel/router/client.go index 265ecf497..33ed00a49 100644 --- a/tunnel/router/client.go +++ b/tunnel/router/client.go @@ -312,7 +312,7 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) { runtime.ReadMemStats(&m1) - geodataLoader := geodata.GetGeodataLoader() + geodataLoader := geodata.NewGeodataLoader() ipCode := loadCode(cfg, "geoip:") for _, c := range ipCode {