diff --git a/infra/conf/jsonpb/jsonpb.go b/infra/conf/jsonpb/jsonpb.go index f059ae620..0d15de6da 100644 --- a/infra/conf/jsonpb/jsonpb.go +++ b/infra/conf/jsonpb/jsonpb.go @@ -37,11 +37,11 @@ func DumpJsonPb(config proto.Message, w io.Writer) error { return dumpJsonPb(config, w) } -const FormatProtobufJSON = "jsonpb" +const FormatProtobufJSONPB = "jsonpb" func init() { common.Must(core.RegisterConfigLoader(&core.ConfigFormat{ - Name: []string{FormatProtobufJSON}, + Name: []string{FormatProtobufJSONPB}, Extension: []string{".pb.json", ".pbjson"}, Loader: func(input interface{}) (*core.Config, error) { switch v := input.(type) { diff --git a/main/commands/all/jsonv4/convert.go b/main/commands/all/jsonv4/convert.go index 5924b4395..39da7704c 100644 --- a/main/commands/all/jsonv4/convert.go +++ b/main/commands/all/jsonv4/convert.go @@ -3,6 +3,7 @@ package jsonv4 import ( "bytes" "encoding/json" + "github.com/v2fly/v2ray-core/v4/infra/conf/jsonpb" "os" "strings" @@ -123,6 +124,26 @@ func executeConvert(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to convert to protobuf: %s", err) } + case jsonpb.FormatProtobufJSONPB: + data, err := json.Marshal(m) + if err != nil { + base.Fatalf("failed to marshal json: %s", err) + } + r := bytes.NewReader(data) + cf, err := serial.DecodeJSONConfig(r) + if err != nil { + base.Fatalf("failed to decode json: %s", err) + } + pbConfig, err := cf.Build() + if err != nil { + base.Fatalf(err.Error()) + } + w := bytes.NewBuffer(nil) + err = jsonpb.DumpJsonPb(pbConfig, w) + if err != nil { + base.Fatalf(err.Error()) + } + out = w.Bytes() default: base.Errorf("invalid output format: %s", outputFormat) base.Errorf("Run '%s help %s' for details.", base.CommandEnv.Exec, cmd.LongName())