Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package memconservative
 
import (
    "io/ioutil"
    "strings"
 
    "google.golang.org/protobuf/proto"
 
    "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
    "github.com/v2fly/v2ray-core/v5/common/platform"
)
 
type GeoIPCache map[string]*routercommon.GeoIP
 
func (g GeoIPCache) Has(key string) bool {
    return !(g.Get(key) == nil)
}
 
func (g GeoIPCache) Get(key string) *routercommon.GeoIP {
    if g == nil {
        return nil
    }
    return g[key]
}
 
func (g GeoIPCache) Set(key string, value *routercommon.GeoIP) {
    if g == nil {
        g = make(map[string]*routercommon.GeoIP)
    }
    g[key] = value
}
 
func (g GeoIPCache) Unmarshal(filename, code string) (*routercommon.GeoIP, error) {
    asset := platform.GetAssetLocation(filename)
    idx := strings.ToLower(asset + ":" + code)
    if g.Has(idx) {
        return g.Get(idx), nil
    }
 
    geoipBytes, err := Decode(asset, code)
    switch err {
    case nil:
        var geoip routercommon.GeoIP
        if err := proto.Unmarshal(geoipBytes, &geoip); err != nil {
            return nil, err
        }
        g.Set(idx, &geoip)
        return &geoip, nil
 
    case errCodeNotFound:
        return nil, newError("country code ", code, " not found in ", filename)
 
    case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
        errInvalidGeodataFile, errInvalidGeodataVarintLength:
        newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method")
        geoipBytes, err = ioutil.ReadFile(asset)
        if err != nil {
            return nil, err
        }
        var geoipList routercommon.GeoIPList
        if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
            return nil, err
        }
        for _, geoip := range geoipList.GetEntry() {
            if strings.EqualFold(code, geoip.GetCountryCode()) {
                g.Set(idx, geoip)
                return geoip, nil
            }
        }
 
    default:
        return nil, err
    }
 
    return nil, newError("country code ", code, " not found in ", filename)
}
 
type GeoSiteCache map[string]*routercommon.GeoSite
 
func (g GeoSiteCache) Has(key string) bool {
    return !(g.Get(key) == nil)
}
 
func (g GeoSiteCache) Get(key string) *routercommon.GeoSite {
    if g == nil {
        return nil
    }
    return g[key]
}
 
func (g GeoSiteCache) Set(key string, value *routercommon.GeoSite) {
    if g == nil {
        g = make(map[string]*routercommon.GeoSite)
    }
    g[key] = value
}
 
func (g GeoSiteCache) Unmarshal(filename, code string) (*routercommon.GeoSite, error) {
    asset := platform.GetAssetLocation(filename)
    idx := strings.ToLower(asset + ":" + code)
    if g.Has(idx) {
        return g.Get(idx), nil
    }
 
    geositeBytes, err := Decode(asset, code)
    switch err {
    case nil:
        var geosite routercommon.GeoSite
        if err := proto.Unmarshal(geositeBytes, &geosite); err != nil {
            return nil, err
        }
        g.Set(idx, &geosite)
        return &geosite, nil
 
    case errCodeNotFound:
        return nil, newError("list ", code, " not found in ", filename)
 
    case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
        errInvalidGeodataFile, errInvalidGeodataVarintLength:
        newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method")
        geositeBytes, err = ioutil.ReadFile(asset)
        if err != nil {
            return nil, err
        }
        var geositeList routercommon.GeoSiteList
        if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
            return nil, err
        }
        for _, geosite := range geositeList.GetEntry() {
            if strings.EqualFold(code, geosite.GetCountryCode()) {
                g.Set(idx, geosite)
                return geosite, nil
            }
        }
 
    default:
        return nil, err
    }
 
    return nil, newError("list ", code, " not found in ", filename)
}