Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: refine geodata loader #323

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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