Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands: Add convert with two sub-commands #3661

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main/commands/all/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package all

import (
"github.com/xtls/xray-core/main/commands/all/api"
"github.com/xtls/xray-core/main/commands/all/convert"
"github.com/xtls/xray-core/main/commands/all/tls"
"github.com/xtls/xray-core/main/commands/base"
)
Expand All @@ -12,7 +13,7 @@ func init() {
base.RootCommand.Commands = append(
base.RootCommand.Commands,
api.CmdAPI,
// cmdConvert,
convert.CmdConvert,
tls.CmdTLS,
cmdUUID,
cmdX25519,
Expand Down
126 changes: 0 additions & 126 deletions main/commands/all/convert.go

This file was deleted.

17 changes: 17 additions & 0 deletions main/commands/all/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package convert

import (
"github.com/xtls/xray-core/main/commands/base"
)

// CmdConvert do config convertion
var CmdConvert = &base.Command{
UsageLine: "{{.Exec}} convert",
Short: "Convert configs",
Long: `{{.Exec}} {{.LongName}} provides tools to convert config.
`,
Commands: []*base.Command{
cmdProtobuf,
cmdJson,
},
}
71 changes: 71 additions & 0 deletions main/commands/all/convert/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package convert

import (
"encoding/json"
"fmt"
"io"

creflect "github.com/xtls/xray-core/common/reflect"
cserial "github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/main/commands/base"
"github.com/xtls/xray-core/main/confloader"
)

var cmdJson = &base.Command{
CustomFlags: true,
UsageLine: "{{.Exec}} convert json [-type] [stdin:] [typedMessage file] ",
Short: "Convert typedMessage to json",
Long: `
Convert ONE typedMessage to json.

Where typedMessage file need to be in the following format:

{
"type": "xray.proxy.shadowsocks.Account",
"value": "CgMxMTEQBg=="
}

Arguments:

-t, -type
Inject type infomation.

Examples:

{{.Exec}} convert json user.tmsg
`,
Run: executeTypedMessageToJson,
}

func executeTypedMessageToJson(cmd *base.Command, args []string) {

var injectTypeInfo bool
cmd.Flag.BoolVar(&injectTypeInfo, "t", false, "")
cmd.Flag.BoolVar(&injectTypeInfo, "type", false, "")
cmd.Flag.Parse(args)

if cmd.Flag.NArg() < 1 {
base.Fatalf("empty input list")
}

reader, err := confloader.LoadConfig(cmd.Flag.Arg(0))
if err != nil {
base.Fatalf(err.Error())
}

b, err := io.ReadAll(reader)
if err != nil {
base.Fatalf(err.Error())
}

tm := cserial.TypedMessage{}
if err = json.Unmarshal(b, &tm); err != nil {
base.Fatalf(err.Error())
}

if j, ok := creflect.MarshalToJson(&tm, injectTypeInfo); ok {
fmt.Println(j)
} else {
base.Fatalf("marshal TypedMessage to json failed")
}
}
81 changes: 81 additions & 0 deletions main/commands/all/convert/protobuf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package convert

import (
"fmt"
"os"

"github.com/xtls/xray-core/common/cmdarg"
creflect "github.com/xtls/xray-core/common/reflect"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/main/commands/base"

"google.golang.org/protobuf/proto"
)

var cmdProtobuf = &base.Command{
CustomFlags: true,
UsageLine: "{{.Exec}} convert pb [-debug] [-type] [json file] [json file] ...",
Short: "Convert multiple json configs to protobuf",
Long: `
Convert multiple json configs to protobuf.

Arguments:

-d, -debug
Show mix.pb as json.
FOR DEBUGGING ONLY!
DO NOT PASS THIS OUTPUT TO XRAY-CORE!

-t, -type
Inject type information into debug output.

Examples:

{{.Exec}} convert pb config.json c1.json c2.json c3.json > mix.pb
`,
Run: executeConvertConfigsToProtobuf,
}

func executeConvertConfigsToProtobuf(cmd *base.Command, args []string) {

var optDump bool
var optType bool

cmd.Flag.BoolVar(&optDump, "d", false, "")
cmd.Flag.BoolVar(&optDump, "debug", false, "")
cmd.Flag.BoolVar(&optType, "t", false, "")
cmd.Flag.BoolVar(&optType, "type", false, "")
cmd.Flag.Parse(args)

unnamedArgs := cmdarg.Arg{}
for _, v := range cmd.Flag.Args() {
unnamedArgs.Set(v)
}

if len(unnamedArgs) < 1 {
base.Fatalf("empty config list")
}

pbConfig, err := core.LoadConfig("auto", unnamedArgs)
if err != nil {
base.Fatalf(err.Error())
}

if optDump {
if j, ok := creflect.MarshalToJson(pbConfig, optType); ok {
fmt.Println(j)
return
} else {
base.Fatalf("failed to marshal proto config to json.")
}
}

bytesConfig, err := proto.Marshal(pbConfig)
if err != nil {
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)
}
}