2021-05-03 22:15:11 -04:00
|
|
|
package rule_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
|
|
|
|
_ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
|
2021-05-04 09:52:35 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/rule"
|
2021-05-03 22:15:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-05-04 09:51:38 -04:00
|
|
|
const geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
|
|
|
|
|
2021-05-03 22:15:11 -04:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
tempPath := filepath.Join(wd, "..", "..", "..", "testing", "temp")
|
2021-05-03 22:15:11 -04:00
|
|
|
geoipPath := filepath.Join(tempPath, "geoip.dat")
|
|
|
|
|
|
|
|
os.Setenv("v2ray.location.asset", tempPath)
|
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-05-19 17:28:52 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0o755))
|
2021-05-04 09:51:38 -04:00
|
|
|
geoipBytes, err := common.FetchHTTPContent(geoipURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
|
2021-05-03 22:15:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|