mirror of https://github.com/XTLS/Xray-core
Commands: Add `-outpbfile` for `convert pb` (#5048)
parent
b65da77267
commit
11f0513bce
|
@ -3,6 +3,7 @@ package convert
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common/cmdarg"
|
"github.com/xtls/xray-core/common/cmdarg"
|
||||||
creflect "github.com/xtls/xray-core/common/reflect"
|
creflect "github.com/xtls/xray-core/common/reflect"
|
||||||
|
@ -14,15 +15,18 @@ import (
|
||||||
|
|
||||||
var cmdProtobuf = &base.Command{
|
var cmdProtobuf = &base.Command{
|
||||||
CustomFlags: true,
|
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",
|
Short: "Convert multiple json configs to protobuf",
|
||||||
Long: `
|
Long: `
|
||||||
Convert multiple json configs to protobuf.
|
Convert multiple configs to ProtoBuf. JSON, YAML and TOML can be used.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
||||||
|
-o file, -outpbfile file
|
||||||
|
Write the ProtoBuf output (eg. mix.pb) to specified file location.
|
||||||
|
|
||||||
-d, -debug
|
-d, -debug
|
||||||
Show mix.pb as json.
|
Show mix.pb as JSON format.
|
||||||
FOR DEBUGGING ONLY!
|
FOR DEBUGGING ONLY!
|
||||||
DO NOT PASS THIS OUTPUT TO XRAY-CORE!
|
DO NOT PASS THIS OUTPUT TO XRAY-CORE!
|
||||||
|
|
||||||
|
@ -31,16 +35,20 @@ Arguments:
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
{{.Exec}} convert pb config.json c1.json c2.json c3.json > mix.pb
|
{{.Exec}} convert pb -outpbfile output.pb config.json c1.json c2.json c3.json
|
||||||
|
{{.Exec}} convert pb -debug mix.pb
|
||||||
`,
|
`,
|
||||||
Run: executeConvertConfigsToProtobuf,
|
Run: executeConvertConfigsToProtobuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) {
|
func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) {
|
||||||
|
|
||||||
|
var optFile string
|
||||||
var optDump bool
|
var optDump bool
|
||||||
var optType bool
|
var optType bool
|
||||||
|
|
||||||
|
cmd.Flag.StringVar(&optFile, "o", "", "")
|
||||||
|
cmd.Flag.StringVar(&optFile, "outpbfile", "", "")
|
||||||
cmd.Flag.BoolVar(&optDump, "d", false, "")
|
cmd.Flag.BoolVar(&optDump, "d", false, "")
|
||||||
cmd.Flag.BoolVar(&optDump, "debug", false, "")
|
cmd.Flag.BoolVar(&optDump, "debug", false, "")
|
||||||
cmd.Flag.BoolVar(&optType, "t", false, "")
|
cmd.Flag.BoolVar(&optType, "t", false, "")
|
||||||
|
@ -52,6 +60,17 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) {
|
||||||
unnamedArgs.Set(v)
|
unnamedArgs.Set(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(optFile) > 0 {
|
||||||
|
switch core.GetFormatByExtension(getFileExtension(optFile)){
|
||||||
|
case "protobuf", "":
|
||||||
|
fmt.Println("Output ProtoBuf file is ", optFile)
|
||||||
|
default:
|
||||||
|
base.Fatalf("-outpbfile followed by a possible original config.")
|
||||||
|
}
|
||||||
|
} else if !optDump {
|
||||||
|
base.Fatalf("-outpbfile not specified")
|
||||||
|
}
|
||||||
|
|
||||||
if len(unnamedArgs) < 1 {
|
if len(unnamedArgs) < 1 {
|
||||||
base.Fatalf("invalid config list length: %d", len(unnamedArgs))
|
base.Fatalf("invalid config list length: %d", len(unnamedArgs))
|
||||||
}
|
}
|
||||||
|
@ -70,12 +89,28 @@ func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesConfig, err := proto.Marshal(pbConfig)
|
if len(optFile) > 0 {
|
||||||
if err != nil {
|
bytesConfig, err := proto.Marshal(pbConfig)
|
||||||
base.Fatalf("failed to marshal proto config: %s", err)
|
if err != nil {
|
||||||
}
|
base.Fatalf("failed to marshal proto config: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := os.Stdout.Write(bytesConfig); err != nil {
|
f, err := os.Create(optFile)
|
||||||
base.Fatalf("failed to write proto config: %s", err)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getFileExtension(filename string) string {
|
||||||
|
idx := strings.LastIndexByte(filename, '.')
|
||||||
|
if idx == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return filename[idx+1:]
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue