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
package standard
 
import (
    "strings"
 
    "google.golang.org/protobuf/proto"
 
    "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
    "github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
    "github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
)
 
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
 
func loadIP(filename, country string) ([]*routercommon.CIDR, error) {
    geoipBytes, err := filesystem.ReadAsset(filename)
    if err != nil {
        return nil, newError("failed to open file: ", filename).Base(err)
    }
    var geoipList routercommon.GeoIPList
    if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
        return nil, err
    }
 
    for _, geoip := range geoipList.Entry {
        if strings.EqualFold(geoip.CountryCode, country) {
            return geoip.Cidr, nil
        }
    }
 
    return nil, newError("country not found in ", filename, ": ", country)
}
 
func loadSite(filename, list string) ([]*routercommon.Domain, error) {
    geositeBytes, err := filesystem.ReadAsset(filename)
    if err != nil {
        return nil, newError("failed to open file: ", filename).Base(err)
    }
    var geositeList routercommon.GeoSiteList
    if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
        return nil, err
    }
 
    for _, site := range geositeList.Entry {
        if strings.EqualFold(site.CountryCode, list) {
            return site.Domain, nil
        }
    }
 
    return nil, newError("list not found in ", filename, ": ", list)
}
 
type standardLoader struct{}
 
func (d standardLoader) LoadSite(filename, list string) ([]*routercommon.Domain, error) {
    return loadSite(filename, list)
}
 
func (d standardLoader) LoadIP(filename, country string) ([]*routercommon.CIDR, error) {
    return loadIP(filename, country)
}
 
func init() {
    geodata.RegisterGeoDataLoaderImplementationCreator("standard", func() geodata.LoaderImplementation {
        return standardLoader{}
    })
}