1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00

v2ray jsonpb engineering support

This commit is contained in:
Shelikhoo 2021-06-19 23:50:12 +01:00
parent 95ddd2a313
commit 8566ecee1d
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
5 changed files with 121 additions and 1 deletions

View File

@ -6,7 +6,7 @@ import (
"github.com/v2fly/v2ray-core/v4/main/commands/base"
)
// go:generate go run v2ray.com/core/common/errors/errorgen
//go:generate go run v2ray.com/core/common/errors/errorgen
func init() {
base.RootCommand.Commands = append(

View File

@ -0,0 +1,57 @@
package engineering
import (
"bytes"
"fmt"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common/cmdarg"
"github.com/v2fly/v2ray-core/v4/main/commands/base"
"google.golang.org/protobuf/proto"
"io"
"os"
)
var (
configFiles cmdarg.Arg
configDirs cmdarg.Arg
configFormat *string
configDirRecursively *bool
)
func setConfigFlags(cmd *base.Command) {
configFormat = cmd.Flag.String("format", core.FormatAuto, "")
configDirRecursively = cmd.Flag.Bool("r", false, "")
cmd.Flag.Var(&configFiles, "config", "")
cmd.Flag.Var(&configFiles, "c", "")
cmd.Flag.Var(&configDirs, "confdir", "")
cmd.Flag.Var(&configDirs, "d", "")
}
var cmdConvertPb = &base.Command{
UsageLine: "{{.Exec}} engineering convertpb [-c config.json] [-d dir]",
CustomFlags: true,
Run: func(cmd *base.Command, args []string) {
setConfigFlags(cmd)
cmd.Flag.Parse(args)
config, err := core.LoadConfig(*configFormat, configFiles)
if err != nil {
if len(configFiles) == 0 {
base.Fatalf("%s", newError("failed to load config").Base(err))
return
} else {
base.Fatalf("%s", newError(fmt.Sprintf("failed to load config: %s", configFiles)).Base(err))
return
}
}
bytew, err := proto.Marshal(config)
if err != nil {
base.Fatalf("%s", newError("failed to marshal config").Base(err))
return
}
io.Copy(os.Stdout, bytes.NewReader(bytew))
},
}

View File

@ -0,0 +1,18 @@
package engineering
import "github.com/v2fly/v2ray-core/v4/main/commands/base"
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
var cmdEngineering = &base.Command{
UsageLine: "{{.Exec}} engineering",
Commands: []*base.Command{
cmdConvertPb,
cmdReversePb,
},
}
func init() {
base.RegisterCommand(cmdEngineering)
}

View File

@ -0,0 +1,42 @@
package engineering
import (
"bytes"
"flag"
"github.com/golang/protobuf/proto"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/infra/conf/jsonpb"
"github.com/v2fly/v2ray-core/v4/infra/conf/v2jsonpb"
"github.com/v2fly/v2ray-core/v4/main/commands/base"
"io"
"os"
)
var cmdReversePb = &base.Command{
UsageLine: "{{.Exec}} engineering reversepb [-f format]",
Flag: func() flag.FlagSet {
fs := flag.NewFlagSet("", flag.ExitOnError)
configFormat = fs.String("f", "v2jsonpb", "")
return *fs
}(),
Run: func(cmd *base.Command, args []string) {
cmd.Flag.Parse(args)
configIn := bytes.NewBuffer(nil)
io.Copy(configIn, os.Stdin)
var conf core.Config
if err := proto.Unmarshal(configIn.Bytes(), &conf); err != nil {
base.Fatalf("%s", err)
}
switch *configFormat {
case "jsonpb":
if err := jsonpb.DumpJsonPb(&conf, os.Stdout); err != nil {
base.Fatalf("%s", err)
}
case "v2jsonpb":
if value, err := v2jsonpb.DumpV2JsonPb(&conf); err != nil {
base.Fatalf("%s", err)
} else {
io.Copy(os.Stdout, bytes.NewReader(value))
}
}
}}

View File

@ -76,6 +76,9 @@ import (
// commands
_ "github.com/v2fly/v2ray-core/v4/main/commands/all"
//engineering commands
_ "github.com/v2fly/v2ray-core/v4/main/commands/all/engineering"
// Commands that rely on jsonv4 format This disable selective compile
_ "github.com/v2fly/v2ray-core/v4/main/commands/all/api/jsonv4"
_ "github.com/v2fly/v2ray-core/v4/main/commands/all/jsonv4"