From ffd8fd1d8ab75f583f9825971aa74a4e48bc2176 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+rprx@users.noreply.github.com> Date: Fri, 25 Dec 2020 18:53:17 +0800 Subject: [PATCH] Adjust JSON & TOML & YAML --- infra/conf/serial/loader.go | 14 ++++----- main/distro/all/all.go | 12 +++----- main/json/config_json.go | 38 ------------------------- main/{jsonem/jsonem.go => json/json.go} | 2 +- main/jsonem/errors.generated.go | 9 ------ main/run.go | 14 +++++---- main/toml/toml.go | 8 ++++-- main/yaml/yaml.go | 8 ++++-- testing/scenarios/transport_test.go | 2 +- 9 files changed, 32 insertions(+), 75 deletions(-) delete mode 100644 main/json/config_json.go rename main/{jsonem/jsonem.go => json/json.go} (98%) delete mode 100644 main/jsonem/errors.generated.go diff --git a/infra/conf/serial/loader.go b/infra/conf/serial/loader.go index 61ee8077..5774889e 100644 --- a/infra/conf/serial/loader.go +++ b/infra/conf/serial/loader.go @@ -85,6 +85,8 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) { return pbConfig, nil } +// DecodeTOMLConfig reads from reader and decode the config into *conf.Config +// using github.com/BurntSushi/toml and map to convert toml to json. func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) { tomlFile, err := ioutil.ReadAll(reader) if err != nil { @@ -92,15 +94,13 @@ func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) { } configMap := make(map[string]interface{}) - if _, err := toml.Decode(string(tomlFile), &configMap); err != nil { - return nil, newError("failed to convert TOML to Map").Base(err) + return nil, newError("failed to convert toml to map").Base(err) } jsonFile, err := json.Marshal(&configMap) - if err != nil { - return nil, newError("failed to convert Map to JSON").Base(err) + return nil, newError("failed to convert map to json").Base(err) } return DecodeJSONConfig(bytes.NewReader(jsonFile)) @@ -121,10 +121,8 @@ func LoadTOMLConfig(reader io.Reader) (*core.Config, error) { } // DecodeYAMLConfig reads from reader and decode the config into *conf.Config -// using github.com/ghodss/yaml to convert yaml to json -// syntax error could be detected. +// using github.com/ghodss/yaml to convert yaml to json. func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) { - yamlFile, err := ioutil.ReadAll(reader) if err != nil { return nil, newError("failed to read config file").Base(err) @@ -132,7 +130,7 @@ func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) { jsonFile, err := yaml.YAMLToJSON(yamlFile) if err != nil { - return nil, newError("failed to read config file").Base(err) + return nil, newError("failed to convert yaml to json").Base(err) } return DecodeJSONConfig(bytes.NewReader(jsonFile)) diff --git a/main/distro/all/all.go b/main/distro/all/all.go index 197ae627..68e36e5c 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -57,18 +57,14 @@ import ( _ "github.com/xtls/xray-core/transport/internet/headers/wechat" _ "github.com/xtls/xray-core/transport/internet/headers/wireguard" - // JSON config support. Choose only one from the two below. - // The following line loads JSON from xctl - // _ "github.com/xtls/xray-core/main/json" - // The following line loads JSON internally - _ "github.com/xtls/xray-core/main/jsonem" - _ "github.com/xtls/xray-core/main/yaml" - + // JSON & TOML & YAML + _ "github.com/xtls/xray-core/main/json" _ "github.com/xtls/xray-core/main/toml" + _ "github.com/xtls/xray-core/main/yaml" // Load config from file or http(s) _ "github.com/xtls/xray-core/main/confloader/external" - // commands + // Commands _ "github.com/xtls/xray-core/main/commands/all" ) diff --git a/main/json/config_json.go b/main/json/config_json.go deleted file mode 100644 index 90a169a0..00000000 --- a/main/json/config_json.go +++ /dev/null @@ -1,38 +0,0 @@ -package json - -//go:generate go run github.com/xtls/xray-core/common/errors/errorgen - -import ( - "io" - "os" - - "github.com/xtls/xray-core/common" - "github.com/xtls/xray-core/common/cmdarg" - core "github.com/xtls/xray-core/core" - "github.com/xtls/xray-core/main/confloader" -) - -func init() { - common.Must(core.RegisterConfigLoader(&core.ConfigFormat{ - Name: "JSON", - Extension: []string{"json"}, - Loader: func(input interface{}) (*core.Config, error) { - switch v := input.(type) { - case cmdarg.Arg: - r, err := confloader.LoadExtConfig(v, os.Stdin) - if err != nil { - return nil, newError("failed to execute xctl to convert config file.").Base(err).AtWarning() - } - return core.LoadConfig("protobuf", "", r) - case io.Reader: - r, err := confloader.LoadExtConfig([]string{"stdin:"}, os.Stdin) - if err != nil { - return nil, newError("failed to execute xctl to convert config file.").Base(err).AtWarning() - } - return core.LoadConfig("protobuf", "", r) - default: - return nil, newError("unknown type") - } - }, - })) -} diff --git a/main/jsonem/jsonem.go b/main/json/json.go similarity index 98% rename from main/jsonem/jsonem.go rename to main/json/json.go index fb4cb5d8..2e4168c3 100644 --- a/main/jsonem/jsonem.go +++ b/main/json/json.go @@ -1,4 +1,4 @@ -package jsonem +package json import ( "io" diff --git a/main/jsonem/errors.generated.go b/main/jsonem/errors.generated.go deleted file mode 100644 index 1c3f60e9..00000000 --- a/main/jsonem/errors.generated.go +++ /dev/null @@ -1,9 +0,0 @@ -package jsonem - -import "github.com/xtls/xray-core/common/errors" - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} diff --git a/main/run.go b/main/run.go index f5cfb94f..4d307f05 100644 --- a/main/run.go +++ b/main/run.go @@ -66,17 +66,19 @@ func executeRun(cmd *base.Command, args []string) { printVersion() server, err := startXray() if err != nil { - base.Fatalf("Failed to start: %s", err) + fmt.Println("Failed to start:", err) + // Configuration error. Exit with a special value to prevent systemd from restarting. + os.Exit(23) } if *test { fmt.Println("Configuration OK.") - base.SetExitStatus(0) - base.Exit() + os.Exit(0) } if err := server.Start(); err != nil { - base.Fatalf("Failed to start: %s", err) + fmt.Println("Failed to start:", err) + os.Exit(-1) } defer server.Close() @@ -152,10 +154,10 @@ func getConfigFilePath() cmdarg.Arg { func getConfigFormat() string { switch strings.ToLower(*format) { - case "yaml", "yml": - return "yaml" case "pb", "protobuf": return "protobuf" + case "yaml", "yml": + return "yaml" case "toml": return "toml" default: diff --git a/main/toml/toml.go b/main/toml/toml.go index f6fbfefc..e52077f3 100644 --- a/main/toml/toml.go +++ b/main/toml/toml.go @@ -22,9 +22,13 @@ func init() { for i, arg := range v { newError("Reading config: ", arg).AtInfo().WriteToLog() r, err := confloader.LoadConfig(arg) - common.Must(err) + if err != nil { + return nil, newError("failed to read config: ", arg).Base(err) + } c, err := serial.DecodeTOMLConfig(r) - common.Must(err) + if err != nil { + return nil, newError("failed to decode config: ", arg).Base(err) + } if i == 0 { // This ensure even if the muti-json parser do not support a setting, // It is still respected automatically for the first configure file diff --git a/main/yaml/yaml.go b/main/yaml/yaml.go index 4a647756..760a5717 100644 --- a/main/yaml/yaml.go +++ b/main/yaml/yaml.go @@ -22,9 +22,13 @@ func init() { for i, arg := range v { newError("Reading config: ", arg).AtInfo().WriteToLog() r, err := confloader.LoadConfig(arg) - common.Must(err) + if err != nil { + return nil, newError("failed to read config: ", arg).Base(err) + } c, err := serial.DecodeYAMLConfig(r) - common.Must(err) + if err != nil { + return nil, newError("failed to decode config: ", arg).Base(err) + } if i == 0 { // This ensure even if the muti-json parser do not support a setting, // It is still respected automatically for the first configure file diff --git a/testing/scenarios/transport_test.go b/testing/scenarios/transport_test.go index ad447d9d..41f7cf29 100644 --- a/testing/scenarios/transport_test.go +++ b/testing/scenarios/transport_test.go @@ -137,7 +137,7 @@ func TestHTTPConnectionHeader(t *testing.T) { func TestDomainSocket(t *testing.T) { if runtime.GOOS == "windows" || runtime.GOOS == "android" { - t.Skip("Not supported on windows and android") + t.Skip("Not supported on windows or android") return } tcpServer := tcp.Server{