move more to pb

This commit is contained in:
Meo597
2026-10-27 01:31:55 +08:00
parent 092159313a
commit 92a7a528d1
4 changed files with 16 additions and 65 deletions
+12
View File
@@ -76,6 +76,16 @@ func TestDownloadOneLuaGuard(t *testing.T) {
} }
func TestProtobufConfigValidation(t *testing.T) { func TestProtobufConfigValidation(t *testing.T) {
dir := t.TempDir()
t.Setenv("xray.location.asset", dir)
for _, name := range []string{"asset.dat", "asset.DAT"} {
if err := os.WriteFile(filepath.Join(dir, name), nil, 0o600); err != nil {
t.Fatal(err)
}
}
if err := os.Mkdir(filepath.Join(dir, "directory.dat"), 0o700); err != nil {
t.Fatal(err)
}
server, err := core.New(&core.Config{}) server, err := core.New(&core.Config{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -95,6 +105,8 @@ func TestProtobufConfigValidation(t *testing.T) {
{"uppercase extension", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "asset.DAT"}}}, ""}, {"uppercase extension", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "asset.DAT"}}}, ""},
{"invalid extension", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "asset.lua"}}}, "extension must be .dat"}, {"invalid extension", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "asset.lua"}}}, "extension must be .dat"},
{"missing extension", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "asset"}}}, "extension must be .dat"}, {"missing extension", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "asset"}}}, "extension must be .dat"},
{"missing file", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "missing.dat"}}}, "invalid geodata asset file"},
{"directory", &Config{Cron: "@daily", Assets: []*Asset{{Url: "https://example.com/asset.dat", File: "directory.dat"}}}, "asset is not a regular file"},
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
config, err := serial.ToTypedMessage(tc.config).GetInstance() config, err := serial.ToTypedMessage(tc.config).GetInstance()
+4
View File
@@ -11,6 +11,7 @@ import (
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
commongeodata "github.com/xtls/xray-core/common/geodata" commongeodata "github.com/xtls/xray-core/common/geodata"
"github.com/xtls/xray-core/common/platform/filesystem"
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
) )
@@ -35,6 +36,9 @@ func New(ctx context.Context, config *Config) (*Instance, error) {
if !strings.EqualFold(filepath.Ext(asset.GetFile()), ".dat") { if !strings.EqualFold(filepath.Ext(asset.GetFile()), ".dat") {
return nil, errors.New("geodata asset file extension must be .dat") return nil, errors.New("geodata asset file extension must be .dat")
} }
if _, err := filesystem.StatAsset(asset.GetFile()); err != nil {
return nil, errors.New("invalid geodata asset file: ", asset.GetFile()).Base(err)
}
} }
schedule, err := cron.ParseStandard(config.Cron) schedule, err := cron.ParseStandard(config.Cron)
if err != nil { if err != nil {
-9
View File
@@ -2,13 +2,10 @@ package conf
import ( import (
"net/url" "net/url"
"path/filepath"
"strings"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/xtls/xray-core/app/geodata" "github.com/xtls/xray-core/app/geodata"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/platform/filesystem"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
@@ -21,12 +18,6 @@ func (c *GeodataAssetConfig) Build() (*geodata.Asset, error) {
if err := validateHTTPS(c.URL); err != nil { if err := validateHTTPS(c.URL); err != nil {
return nil, errors.New("invalid geodata asset url: ", c.URL).Base(err) return nil, errors.New("invalid geodata asset url: ", c.URL).Base(err)
} }
if !strings.EqualFold(filepath.Ext(c.File), ".dat") {
return nil, errors.New("geodata asset file extension must be .dat")
}
if _, err := filesystem.StatAsset(c.File); err != nil {
return nil, errors.New("invalid geodata asset file: ", c.File).Base(err)
}
return &geodata.Asset{ return &geodata.Asset{
Url: c.URL, Url: c.URL,
File: c.File, File: c.File,
-56
View File
@@ -1,9 +1,6 @@
package conf_test package conf_test
import ( import (
"os"
"path/filepath"
"strings"
"testing" "testing"
"github.com/xtls/xray-core/app/geodata" "github.com/xtls/xray-core/app/geodata"
@@ -11,8 +8,6 @@ import (
) )
func TestGeodataConfig(t *testing.T) { func TestGeodataConfig(t *testing.T) {
t.Setenv("xray.location.asset", filepath.Join("..", "..", "resources"))
creator := func() Buildable { creator := func() Buildable {
return new(GeodataConfig) return new(GeodataConfig)
} }
@@ -40,27 +35,7 @@ func TestGeodataConfig(t *testing.T) {
}) })
} }
func TestGeodataAssetConfig(t *testing.T) {
t.Setenv("xray.location.asset", filepath.Join("..", "..", "resources"))
if _, err := (&GeodataAssetConfig{
URL: "https://example.com/geoip.dat",
File: "geoip.dat",
}).Build(); err != nil {
t.Fatal(err)
}
if _, err := (&GeodataAssetConfig{
URL: "https://example.com/geoip.dat",
File: "missing.dat",
}).Build(); err == nil {
t.Fatal("expected error")
}
}
func TestGeodataAssetConfigInvalidURL(t *testing.T) { func TestGeodataAssetConfigInvalidURL(t *testing.T) {
t.Setenv("xray.location.asset", filepath.Join("..", "..", "resources"))
for _, rawURL := range []string{ for _, rawURL := range []string{
"", "",
"http://example.com/geoip.dat", "http://example.com/geoip.dat",
@@ -75,34 +50,3 @@ func TestGeodataAssetConfigInvalidURL(t *testing.T) {
} }
} }
} }
func TestGeodataAssetConfigFileExtension(t *testing.T) {
dir := t.TempDir()
t.Setenv("xray.location.asset", dir)
for _, tc := range []struct {
name string
valid bool
}{
{"custom.dat", true},
{"custom.DAT", true},
{"launcher.cmd", false},
{"script.lua", false},
{"config.json", false},
{"asset", false},
} {
if err := os.WriteFile(filepath.Join(dir, tc.name), nil, 0o600); err != nil {
t.Fatal(err)
}
_, err := (&GeodataAssetConfig{
URL: "https://example.com/download",
File: tc.name,
}).Build()
if tc.valid {
if err != nil {
t.Fatalf("Build(%q): %v", tc.name, err)
}
} else if err == nil || !strings.Contains(err.Error(), "extension must be .dat") {
t.Fatalf("Build(%q) = %v; want .dat extension error", tc.name, err)
}
}
}