diff --git a/cmd/sing-box/cmd_convert.go b/cmd/sing-box/cmd_convert.go deleted file mode 100644 index a88fe50c..00000000 --- a/cmd/sing-box/cmd_convert.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "os" - "path/filepath" - - "github.com/sagernet/sing-box/common/conf" - "github.com/sagernet/sing-box/common/conf/mergers" - "github.com/sagernet/sing-box/log" - "github.com/sagernet/sing-box/option" - E "github.com/sagernet/sing/common/exceptions" - - "github.com/spf13/cobra" -) - -var commandConvertOutput string -var commandConvertOutputFormat string - -var commandConvert = &cobra.Command{ - Use: "convert", - Short: "Convert configuration", - Long: `Convert and merge configuration files between different formats. - -Note: comments will be lost after conversion.`, - Run: func(cmd *cobra.Command, args []string) { - err := convert() - if err != nil { - log.Fatal(err) - } - }, - Args: cobra.NoArgs, -} - -func init() { - commandConvert.Flags().StringVarP(&commandConvertOutput, "output", "o", "", "output file") - commandConvert.Flags().StringVarP(&commandConvertOutputFormat, "output-format", "f", string(mergers.FormatJSON), "output format: json, yaml, toml") - mainCommand.AddCommand(commandConvert) -} - -func convert() error { - var ( - configContent []byte - err error - ) - format := mergers.ParseFormat(configFormat) - outFormat := mergers.ParseFormat(commandConvertOutputFormat) - switch outFormat { - case mergers.FormatJSON, - mergers.FormatYAML, - mergers.FormatTOML: - // ok - default: - return E.New("unsupported output format: ", commandConvertOutputFormat) - } - if len(configPaths) == 1 && configPaths[0] == "stdin" { - configContent, err = conf.ReaderToJSON(os.Stdin, format) - } else { - configContent, err = conf.FilesToJSON(configPaths, format, configRecursive) - } - if err != nil { - return E.Cause(err, "read config") - } - - var options option.Options - err = options.UnmarshalJSON(configContent) - if err != nil { - return E.Cause(err, "decode config") - } - content, err := conf.Sprint(options, outFormat) - if err != nil { - return E.Cause(err, "encode config") - } - - if commandConvertOutput == "" { - os.Stdout.WriteString(content + "\n") - return nil - } - - output, err := os.Create(commandConvertOutput) - if err != nil { - return E.Cause(err, "open output") - } - _, err = output.WriteString(content) - output.Close() - if err != nil { - return E.Cause(err, "write output") - } - outputPath, _ := filepath.Abs(commandConvertOutput) - os.Stderr.WriteString(outputPath + "\n") - return nil -} diff --git a/cmd/sing-box/cmd_format.go b/cmd/sing-box/cmd_format.go index 01ab1381..6b5640f3 100644 --- a/cmd/sing-box/cmd_format.go +++ b/cmd/sing-box/cmd_format.go @@ -6,7 +6,6 @@ import ( "path/filepath" "strings" - "github.com/sagernet/sing-box/common/conf/mergers" "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" @@ -19,7 +18,7 @@ var commandFormatFlagWrite bool var commandFormat = &cobra.Command{ Use: "format", - Short: "Format configuration (json only)", + Short: "Format configuration", Run: func(cmd *cobra.Command, args []string) { err := format() if err != nil { @@ -39,16 +38,9 @@ func format() error { return E.New("only one file can be formatted at a time") } configPath := configPaths[0] - format := mergers.ParseFormat(configFormat) - isJSON := strings.EqualFold(filepath.Ext(configPath), ".json") - // only json is supported, since: - // 1. all other foramts will lost comments after formatting - // 2. fields order is shuffled for yaml and toml, because of the nature of the go map - if format != mergers.FormatJSON && - (format == mergers.FormatAuto && !isJSON) { + if strings.EqualFold(filepath.Ext(configPath), ".json") { return E.New("only json format is supported") } - configContent, err := os.ReadFile(configPath) if err != nil { return E.Cause(err, "read config") diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index 7415acd4..90f36cf0 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -37,7 +37,7 @@ func readConfig() (option.Options, error) { configContent []byte err error ) - format := mergers.ParseFormat(configFormat) + format := mergers.FormatAuto if len(configPaths) == 1 && configPaths[0] == "stdin" { configContent, err = conf.ReaderToJSON(os.Stdin, format) } else { diff --git a/cmd/sing-box/main.go b/cmd/sing-box/main.go index e2a19276..f4b4eef3 100644 --- a/cmd/sing-box/main.go +++ b/cmd/sing-box/main.go @@ -3,7 +3,6 @@ package main import ( "os" - "github.com/sagernet/sing-box/common/conf/mergers" _ "github.com/sagernet/sing-box/include" "github.com/sagernet/sing-box/log" @@ -12,7 +11,6 @@ import ( var ( configPaths []string - configFormat string configRecursive bool workingDir string disableColor bool @@ -25,7 +23,6 @@ var mainCommand = &cobra.Command{ func init() { mainCommand.PersistentFlags().StringArrayVarP(&configPaths, "config", "c", []string{"config.json"}, "set configuration files / directories") - mainCommand.PersistentFlags().StringVarP(&configFormat, "config-format", "", string(mergers.FormatAuto), "configuration files format: auto, json, jsonc, yaml, toml") mainCommand.PersistentFlags().BoolVarP(&configRecursive, "config-recursive", "r", false, "load configuration directories recursively") mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory") mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output") diff --git a/common/conf/mergers/mergers.go b/common/conf/mergers/mergers.go index a795270f..b389ba68 100644 --- a/common/conf/mergers/mergers.go +++ b/common/conf/mergers/mergers.go @@ -6,11 +6,8 @@ import ( "fmt" "strings" - "github.com/pelletier/go-toml" "github.com/sagernet/sing-box/common/conf/jsonc" - "github.com/sagernet/sing-box/common/conf/merge" "github.com/sagernet/sing/common" - "gopkg.in/yaml.v2" ) func init() { @@ -36,30 +33,6 @@ func init() { return m, nil }, ))) - common.Must(registerMerger(makeMerger( - FormatTOML, - []string{".toml"}, - func(v []byte) (map[string]interface{}, error) { - m := make(map[string]interface{}) - if err := toml.Unmarshal(v, &m); err != nil { - return nil, err - } - return m, nil - }, - ))) - common.Must(registerMerger(makeMerger( - FormatYAML, - []string{".yml", ".yaml"}, - func(v []byte) (map[string]interface{}, error) { - m1 := make(map[interface{}]interface{}) - err := yaml.Unmarshal(v, &m1) - if err != nil { - return nil, err - } - m2 := merge.Convert(m1) - return m2, nil - }, - ))) common.Must(registerMerger( &Merger{ Name: FormatAuto, diff --git a/common/conf/output.go b/common/conf/output.go deleted file mode 100644 index a93c513b..00000000 --- a/common/conf/output.go +++ /dev/null @@ -1,50 +0,0 @@ -package conf - -import ( - "bytes" - "encoding/json" - "fmt" - - "github.com/pelletier/go-toml" - "github.com/sagernet/sing-box/common/conf/mergers" - "gopkg.in/yaml.v2" -) - -// Sprint returns the text of give format for v -func Sprint(v interface{}, format mergers.Format) (string, error) { - var ( - out []byte - err error - ) - buffer := new(bytes.Buffer) - encoder := json.NewEncoder(buffer) - encoder.SetIndent("", " ") - err = encoder.Encode(v) - if err != nil { - return "", fmt.Errorf("failed to convert to json: %s", err) - } - if format == mergers.FormatJSON || format == mergers.FormatJSONC { - return string(buffer.Bytes()), nil - } - - m := make(map[string]interface{}) - err = json.Unmarshal(buffer.Bytes(), &m) - if err != nil { - return "", err - } - switch format { - case mergers.FormatTOML: - out, err = toml.Marshal(m) - if err != nil { - return "", fmt.Errorf("failed to convert to toml: %s", err) - } - case mergers.FormatYAML: - out, err = yaml.Marshal(m) - if err != nil { - return "", fmt.Errorf("failed to convert to yaml: %s", err) - } - default: - return "", fmt.Errorf("invalid output format: %s", format) - } - return string(out), nil -} diff --git a/go.mod b/go.mod index 8c641a38..bb30bf31 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,6 @@ require ( github.com/mholt/acmez v1.0.4 github.com/miekg/dns v1.1.50 github.com/oschwald/maxminddb-golang v1.10.0 - github.com/pelletier/go-toml v1.9.5 github.com/pires/go-proxyproto v0.6.2 github.com/refraction-networking/utls v1.1.2 github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb @@ -42,7 +41,6 @@ require ( golang.zx2c4.com/wireguard v0.0.0-20220829161405-d1d08426b27b google.golang.org/grpc v1.49.0 google.golang.org/protobuf v1.28.1 - gopkg.in/yaml.v2 v2.4.0 gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c ) diff --git a/go.sum b/go.sum index b269d984..82e173b9 100644 --- a/go.sum +++ b/go.sum @@ -128,8 +128,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak= github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd9181uj2MQ5Vndg= github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0= -github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= -github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pires/go-proxyproto v0.6.2 h1:KAZ7UteSOt6urjme6ZldyFm4wDe/z0ZUP0Yv0Dos0d8= github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=