mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-12-23 22:19:49 +00:00
Adjust JSON & TOML & YAML
This commit is contained in:
parent
3d7e86efba
commit
ffd8fd1d8a
@ -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))
|
||||
|
@ -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"
|
||||
)
|
||||
|
@ -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")
|
||||
}
|
||||
},
|
||||
}))
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package jsonem
|
||||
package json
|
||||
|
||||
import (
|
||||
"io"
|
@ -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{})
|
||||
}
|
14
main/run.go
14
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:
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user