Skip to content

Commit

Permalink
Feat: refine geodata loader (p4gefau1t#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier authored May 11, 2021
1 parent a9ce6d4 commit 531bc2b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
20 changes: 10 additions & 10 deletions common/geodata/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions common/geodata/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions common/geodata/interface.go
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 6 additions & 13 deletions common/geodata/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tunnel/router/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 531bc2b

Please sign in to comment.