From 8afe90afbd79f2b03bb1c8b268163389d6af2acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Fri, 22 Aug 2025 22:01:26 +0800 Subject: [PATCH 1/7] convert pb -outpbfile --- main/commands/all/convert/protobuf.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index c77df963eec3..4b346594363a 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -14,13 +14,16 @@ import ( var cmdProtobuf = &base.Command{ CustomFlags: true, - UsageLine: "{{.Exec}} convert pb [-debug] [-type] [json file] [json file] ...", + UsageLine: "{{.Exec}} convert pb [-outpbfile file] [-debug] [-type] [json file] [json file] ...", Short: "Convert multiple json configs to protobuf", Long: ` Convert multiple json configs to protobuf. Arguments: + -o file, -outpbfile file + Write the ProtoBuf file (eg. mix.pb) to specified location. + -d, -debug Show mix.pb as json. FOR DEBUGGING ONLY! @@ -31,16 +34,20 @@ Arguments: Examples: - {{.Exec}} convert pb config.json c1.json c2.json c3.json > mix.pb + {{.Exec}} convert pb config.json c1.json c2.json c3.json + {{.Exec}} convert pb -outpbfile output.pb config.json c1.json c2.json c3.json `, Run: executeConvertConfigsToProtobuf, } func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { + var optFile string var optDump bool var optType bool + cmd.Flag.StringVar(&optFile, "o", "", "") + cmd.Flag.StringVar(&optFile, "outpbfile", "", "") cmd.Flag.BoolVar(&optDump, "d", false, "") cmd.Flag.BoolVar(&optDump, "debug", false, "") cmd.Flag.BoolVar(&optType, "t", false, "") @@ -75,7 +82,19 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { base.Fatalf("failed to marshal proto config: %s", err) } - if _, err := os.Stdout.Write(bytesConfig); err != nil { - base.Fatalf("failed to write proto config: %s", err) + if len(optFile) > 0 { + f, err := os.Create(optFile) + if err != nil { + base.Fatalf("failed to create proto file: %s", err) + } + defer f.Close() + + if _, err := f.Write(bytesConfig); err!= nil { + base.Fatalf("failed to write proto file: %s", err) + } + } else { + if _, err := os.Stdout.Write(bytesConfig); err != nil { + base.Fatalf("failed to write proto config: %s", err) + } } } From 7d0266148950ac452c7780854c9aa672c98686d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Fri, 22 Aug 2025 22:42:46 +0800 Subject: [PATCH 2/7] fmt --- main/commands/all/convert/protobuf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index 4b346594363a..873836bbd312 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -89,7 +89,7 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { } defer f.Close() - if _, err := f.Write(bytesConfig); err!= nil { + if _, err := f.Write(bytesConfig); err != nil { base.Fatalf("failed to write proto file: %s", err) } } else { From 6aa13f7dc8eb6dfbd4a1f02bda39232e899c4adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sat, 23 Aug 2025 11:16:48 +0800 Subject: [PATCH 3/7] Remove the ability to output proto to STDOUT; Description clarity --- main/commands/all/convert/protobuf.go | 40 ++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index 873836bbd312..1f5a9ffd6a4c 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -3,6 +3,7 @@ package convert import ( "fmt" "os" + "strings" "github.com/xtls/xray-core/common/cmdarg" creflect "github.com/xtls/xray-core/common/reflect" @@ -17,15 +18,15 @@ var cmdProtobuf = &base.Command{ UsageLine: "{{.Exec}} convert pb [-outpbfile file] [-debug] [-type] [json file] [json file] ...", Short: "Convert multiple json configs to protobuf", Long: ` -Convert multiple json configs to protobuf. +Convert multiple configs to protobuf. JSON, YAML and TOML can be used. Arguments: -o file, -outpbfile file - Write the ProtoBuf file (eg. mix.pb) to specified location. + Write the ProtoBuf output (eg. mix.pb) to specified file location. -d, -debug - Show mix.pb as json. + Show mix.pb as JSON format. FOR DEBUGGING ONLY! DO NOT PASS THIS OUTPUT TO XRAY-CORE! @@ -34,8 +35,8 @@ Arguments: Examples: - {{.Exec}} convert pb config.json c1.json c2.json c3.json {{.Exec}} convert pb -outpbfile output.pb config.json c1.json c2.json c3.json + {{.Exec}} convert pb -debug mix.pb `, Run: executeConvertConfigsToProtobuf, } @@ -63,6 +64,15 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { base.Fatalf("invalid config list length: %d", len(unnamedArgs)) } + if len(optFile) > 0 { + fileFormat := core.GetFormatByExtension(getFileExtension(optFile)) + if fileFormat != "protobuf" || fileFormat != "" { + base.Fatalf("-outpbfile followed by a possible original config.") + } + } else if !optDump { + base.Fatalf("-outpbfile not specified") + } + pbConfig, err := core.LoadConfig("auto", unnamedArgs) if err != nil { base.Fatalf("failed to load config: %s", err) @@ -77,12 +87,12 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { } } - bytesConfig, err := proto.Marshal(pbConfig) - if err != nil { - base.Fatalf("failed to marshal proto config: %s", err) - } - if len(optFile) > 0 { + bytesConfig, err := proto.Marshal(pbConfig) + if err != nil { + base.Fatalf("failed to marshal proto config: %s", err) + } + f, err := os.Create(optFile) if err != nil { base.Fatalf("failed to create proto file: %s", err) @@ -92,9 +102,13 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { if _, err := f.Write(bytesConfig); err != nil { base.Fatalf("failed to write proto file: %s", err) } - } else { - if _, err := os.Stdout.Write(bytesConfig); err != nil { - base.Fatalf("failed to write proto config: %s", err) - } } } + +func getFileExtension(filename string) string { + idx := strings.LastIndexByte(filename, '.') + if idx == -1 { + return "" + } + return filename[idx+1:] +} From f79da26dc8f3ce6d9e77aaa17ee939f0149fa374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sat, 23 Aug 2025 11:23:16 +0800 Subject: [PATCH 4/7] Fix test error (ambigous) --- main/commands/all/convert/protobuf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index 1f5a9ffd6a4c..08d36592c677 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -66,7 +66,7 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { if len(optFile) > 0 { fileFormat := core.GetFormatByExtension(getFileExtension(optFile)) - if fileFormat != "protobuf" || fileFormat != "" { + if (fileFormat != "protobuf") || (fileFormat != "") { base.Fatalf("-outpbfile followed by a possible original config.") } } else if !optDump { From 710a117489b8f176e967e283233ef8162a184890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sat, 23 Aug 2025 11:39:44 +0800 Subject: [PATCH 5/7] Use switch case --- main/commands/all/convert/protobuf.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index 08d36592c677..4b4a351a6613 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -18,7 +18,7 @@ var cmdProtobuf = &base.Command{ UsageLine: "{{.Exec}} convert pb [-outpbfile file] [-debug] [-type] [json file] [json file] ...", Short: "Convert multiple json configs to protobuf", Long: ` -Convert multiple configs to protobuf. JSON, YAML and TOML can be used. +Convert multiple configs to ProtoBuf. JSON, YAML and TOML can be used. Arguments: @@ -65,8 +65,10 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { } if len(optFile) > 0 { - fileFormat := core.GetFormatByExtension(getFileExtension(optFile)) - if (fileFormat != "protobuf") || (fileFormat != "") { + switch core.GetFormatByExtension(getFileExtension(optFile)){ + case "protobuf", "": + fmt.Printf("Output ProtoBuf file is %s.", optFile) + default: base.Fatalf("-outpbfile followed by a possible original config.") } } else if !optDump { From 4108f30cb7db004c6cc7c2d7ffe789fdb8bd61e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sat, 23 Aug 2025 12:04:26 +0800 Subject: [PATCH 6/7] Should be done --- main/commands/all/convert/protobuf.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index 4b4a351a6613..69788c23e9b6 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -60,14 +60,10 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { unnamedArgs.Set(v) } - if len(unnamedArgs) < 1 { - base.Fatalf("invalid config list length: %d", len(unnamedArgs)) - } - if len(optFile) > 0 { switch core.GetFormatByExtension(getFileExtension(optFile)){ case "protobuf", "": - fmt.Printf("Output ProtoBuf file is %s.", optFile) + fmt.Println("Output ProtoBuf file is %s.", optFile) default: base.Fatalf("-outpbfile followed by a possible original config.") } @@ -75,6 +71,10 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { base.Fatalf("-outpbfile not specified") } + if len(unnamedArgs) < 1 { + base.Fatalf("invalid config list length: %d", len(unnamedArgs)) + } + pbConfig, err := core.LoadConfig("auto", unnamedArgs) if err != nil { base.Fatalf("failed to load config: %s", err) From 571c71fdbd9625a5264956c473e880e9bc21e811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=90=B2=93=F0=90=B3=9B=F0=90=B3=AA=F0=90=B3=82?= =?UTF-8?q?=F0=90=B3=90=20=F0=90=B2=80=F0=90=B3=A2=F0=90=B3=A6=F0=90=B3=AB?= =?UTF-8?q?=F0=90=B3=A2=20=F0=90=B2=A5=F0=90=B3=94=F0=90=B3=9B=F0=90=B3=AA?= =?UTF-8?q?=F0=90=B3=8C=F0=90=B3=91=F0=90=B3=96=F0=90=B3=87?= <26771058+KobeArthurScofield@users.noreply.github.com> Date: Sat, 23 Aug 2025 12:14:47 +0800 Subject: [PATCH 7/7] Done --- main/commands/all/convert/protobuf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/commands/all/convert/protobuf.go b/main/commands/all/convert/protobuf.go index 69788c23e9b6..74272c57b006 100644 --- a/main/commands/all/convert/protobuf.go +++ b/main/commands/all/convert/protobuf.go @@ -63,7 +63,7 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) { if len(optFile) > 0 { switch core.GetFormatByExtension(getFileExtension(optFile)){ case "protobuf", "": - fmt.Println("Output ProtoBuf file is %s.", optFile) + fmt.Println("Output ProtoBuf file is ", optFile) default: base.Fatalf("-outpbfile followed by a possible original config.") }