Skip to content

Commit

Permalink
v2ray jsonpb engineering support
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang committed Sep 4, 2021
1 parent 95ddd2a commit 8566ece
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main/commands/all/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
57 changes: 57 additions & 0 deletions main/commands/all/engineering/convertpb.go
Original file line number Diff line number Diff line change
@@ -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))

},
}
18 changes: 18 additions & 0 deletions main/commands/all/engineering/engineering.go
Original file line number Diff line number Diff line change
@@ -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)

}
42 changes: 42 additions & 0 deletions main/commands/all/engineering/reversepb.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
}}
3 changes: 3 additions & 0 deletions main/distro/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8566ece

Please sign in to comment.