|
|
@ -2,6 +2,7 @@ package conf_test |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
@ -18,21 +19,44 @@ import ( |
|
|
|
"google.golang.org/protobuf/proto" |
|
|
|
"google.golang.org/protobuf/proto" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
func getAssetPath(file string) (string, error) { |
|
|
|
wd, err := os.Getwd() |
|
|
|
path := platform.GetAssetLocation(file) |
|
|
|
common.Must(err) |
|
|
|
_, err := os.Stat(path) |
|
|
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) { |
|
|
|
path := filepath.Join("..", "..", "resources", file) |
|
|
|
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat"))) |
|
|
|
_, err := os.Stat(path) |
|
|
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
|
|
|
return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", fmt.Errorf("can't stat %s: %v", path, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return path, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", fmt.Errorf("can't stat %s: %v", path, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
os.Setenv("xray.location.asset", wd) |
|
|
|
return path, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestToCidrList(t *testing.T) { |
|
|
|
func TestToCidrList(t *testing.T) { |
|
|
|
t.Log(os.Getenv("xray.location.asset")) |
|
|
|
tempDir, err := os.MkdirTemp("", "test-") |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatalf("can't create temp dir: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
defer os.RemoveAll(tempDir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
geoipPath, err := getAssetPath("geoip.dat") |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
common.Must(filesystem.CopyFile(filepath.Join(tempDir, "geoip.dat"), geoipPath)) |
|
|
|
|
|
|
|
common.Must(filesystem.CopyFile(filepath.Join(tempDir, "geoiptestrouter.dat"), geoipPath)) |
|
|
|
|
|
|
|
|
|
|
|
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), "geoip.dat")) |
|
|
|
os.Setenv("xray.location.asset", tempDir) |
|
|
|
|
|
|
|
defer os.Unsetenv("xray.location.asset") |
|
|
|
|
|
|
|
|
|
|
|
ips := StringList([]string{ |
|
|
|
ips := StringList([]string{ |
|
|
|
"geoip:us", |
|
|
|
"geoip:us", |
|
|
@ -44,7 +68,7 @@ func TestToCidrList(t *testing.T) { |
|
|
|
"ext-ip:geoiptestrouter.dat:!ca", |
|
|
|
"ext-ip:geoiptestrouter.dat:!ca", |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
_, err := ToCidrList(ips) |
|
|
|
_, err = ToCidrList(ips) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("Failed to parse geoip list, got %s", err) |
|
|
|
t.Fatalf("Failed to parse geoip list, got %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|