mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
Commands: Add convert with two sub-commands (#3661)
* Add back convert-configs-to-protobuf command. * Add convert-typedMessage-to-json command. * Add -debug and -type arguments into convert.pb sub-command. --------- Co-authored-by: nobody <nobody@nowhere.mars>
This commit is contained in:
parent
85e2ebc6f7
commit
f650d87083
5 changed files with 171 additions and 127 deletions
71
main/commands/all/convert/json.go
Normal file
71
main/commands/all/convert/json.go
Normal file
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue