Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
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
package rule_test
 
import (
    "context"
    "errors"
    "io/fs"
    "os"
    "path/filepath"
    "testing"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/platform"
    "github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
    "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
    "github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
    _ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/standard"
    "github.com/v2fly/v2ray-core/v5/infra/conf/rule"
)
 
func init() {
    const geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
 
    wd, err := os.Getwd()
    common.Must(err)
 
    tempPath := filepath.Join(wd, "..", "..", "..", "testing", "temp")
    geoipPath := filepath.Join(tempPath, "geoip.dat")
 
    os.Setenv("v2ray.location.asset", tempPath)
 
    if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
        common.Must(os.MkdirAll(tempPath, 0o755))
        geoipBytes, err := common.FetchHTTPContent(geoipURL)
        common.Must(err)
        common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
    }
}
 
func TestToCidrList(t *testing.T) {
    t.Log(os.Getenv("v2ray.location.asset"))
 
    common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat")))
 
    ips := cfgcommon.StringList([]string{
        "geoip:us",
        "geoip:cn",
        "geoip:!cn",
        "ext:geoiptestrouter.dat:!cn",
        "ext:geoiptestrouter.dat:ca",
        "ext-ip:geoiptestrouter.dat:!cn",
        "ext-ip:geoiptestrouter.dat:!ca",
    })
 
    cfgctx := cfgcommon.NewConfigureLoadingContext(context.Background())
 
    if loader, err := geodata.GetGeoDataLoader("standard"); err == nil {
        cfgcommon.SetGeoDataLoader(cfgctx, loader)
    } else {
        t.Fatal(err)
    }
 
    _, err := rule.ToCidrList(cfgctx, ips)
    if err != nil {
        t.Fatalf("Failed to parse geoip list, got %s", err)
    }
}