diff --git a/main/commands/all/api/api.go b/main/commands/all/api/api.go index 1e7493bfe..dc0fa5e49 100644 --- a/main/commands/all/api/api.go +++ b/main/commands/all/api/api.go @@ -16,9 +16,5 @@ var CmdAPI = &base.Command{ cmdBalancerCheck, cmdBalancerInfo, cmdBalancerOverride, - cmdAddInbounds, - cmdAddOutbounds, - cmdRemoveInbounds, - cmdRemoveOutbounds, }, } diff --git a/main/commands/all/api/inbounds_add.go b/main/commands/all/api/jsonv4/inbounds_add.go similarity index 85% rename from main/commands/all/api/inbounds_add.go rename to main/commands/all/api/jsonv4/inbounds_add.go index f4a16b48b..9dbd8b1c3 100644 --- a/main/commands/all/api/inbounds_add.go +++ b/main/commands/all/api/jsonv4/inbounds_add.go @@ -1,7 +1,8 @@ -package api +package jsonv4 import ( "fmt" + "github.com/v2fly/v2ray-core/v4/main/commands/all/api" handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command" "github.com/v2fly/v2ray-core/v4/main/commands/base" @@ -43,10 +44,10 @@ Example: } func executeAddInbounds(cmd *base.Command, args []string) { - setSharedFlags(cmd) - setSharedConfigFlags(cmd) + api.SetSharedFlags(cmd) + api.SetSharedConfigFlags(cmd) cmd.Flag.Parse(args) - c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively) + c, err := helpers.LoadConfig(cmd.Flag.Args(), api.ApiConfigFormat, api.ApiConfigRecursively) if err != nil { base.Fatalf("failed to load: %s", err) } @@ -54,7 +55,7 @@ func executeAddInbounds(cmd *base.Command, args []string) { base.Fatalf("no valid inbound found") } - conn, ctx, close := dialAPIServer() + conn, ctx, close := api.DialAPIServer() defer close() client := handlerService.NewHandlerServiceClient(conn) diff --git a/main/commands/all/api/inbounds_remove.go b/main/commands/all/api/jsonv4/inbounds_remove.go similarity index 86% rename from main/commands/all/api/inbounds_remove.go rename to main/commands/all/api/jsonv4/inbounds_remove.go index 63291c8b7..f262eabf3 100644 --- a/main/commands/all/api/inbounds_remove.go +++ b/main/commands/all/api/jsonv4/inbounds_remove.go @@ -1,7 +1,8 @@ -package api +package jsonv4 import ( "fmt" + "github.com/v2fly/v2ray-core/v4/main/commands/all/api" handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command" "github.com/v2fly/v2ray-core/v4/main/commands/base" @@ -47,8 +48,8 @@ Example: } func executeRemoveInbounds(cmd *base.Command, args []string) { - setSharedFlags(cmd) - setSharedConfigFlags(cmd) + api.SetSharedFlags(cmd) + api.SetSharedConfigFlags(cmd) isTags := cmd.Flag.Bool("tags", false, "") cmd.Flag.Parse(args) @@ -56,7 +57,7 @@ func executeRemoveInbounds(cmd *base.Command, args []string) { if *isTags { tags = cmd.Flag.Args() } else { - c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively) + c, err := helpers.LoadConfig(cmd.Flag.Args(), api.ApiConfigFormat, api.ApiConfigRecursively) if err != nil { base.Fatalf("failed to load: %s", err) } @@ -69,7 +70,7 @@ func executeRemoveInbounds(cmd *base.Command, args []string) { base.Fatalf("no inbound to remove") } - conn, ctx, close := dialAPIServer() + conn, ctx, close := api.DialAPIServer() defer close() client := handlerService.NewHandlerServiceClient(conn) diff --git a/main/commands/all/api/jsonv4/init.go b/main/commands/all/api/jsonv4/init.go new file mode 100644 index 000000000..dd0c9aa8a --- /dev/null +++ b/main/commands/all/api/jsonv4/init.go @@ -0,0 +1,12 @@ +package jsonv4 + +import "github.com/v2fly/v2ray-core/v4/main/commands/all/api" + +func init() { + api.CmdAPI.Commands = append(api.CmdAPI.Commands, + cmdAddInbounds, + cmdAddOutbounds, + cmdRemoveInbounds, + cmdRemoveOutbounds) + +} diff --git a/main/commands/all/api/outbounds_add.go b/main/commands/all/api/jsonv4/outbounds_add.go similarity index 85% rename from main/commands/all/api/outbounds_add.go rename to main/commands/all/api/jsonv4/outbounds_add.go index c70bdcca0..0b791a2c8 100644 --- a/main/commands/all/api/outbounds_add.go +++ b/main/commands/all/api/jsonv4/outbounds_add.go @@ -1,7 +1,8 @@ -package api +package jsonv4 import ( "fmt" + "github.com/v2fly/v2ray-core/v4/main/commands/all/api" handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command" "github.com/v2fly/v2ray-core/v4/main/commands/base" @@ -43,10 +44,10 @@ Example: } func executeAddOutbounds(cmd *base.Command, args []string) { - setSharedFlags(cmd) - setSharedConfigFlags(cmd) + api.SetSharedFlags(cmd) + api.SetSharedConfigFlags(cmd) cmd.Flag.Parse(args) - c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively) + c, err := helpers.LoadConfig(cmd.Flag.Args(), api.ApiConfigFormat, api.ApiConfigRecursively) if err != nil { base.Fatalf("failed to load: %s", err) } @@ -54,7 +55,7 @@ func executeAddOutbounds(cmd *base.Command, args []string) { base.Fatalf("no valid outbound found") } - conn, ctx, close := dialAPIServer() + conn, ctx, close := api.DialAPIServer() defer close() client := handlerService.NewHandlerServiceClient(conn) diff --git a/main/commands/all/api/outbounds_remove.go b/main/commands/all/api/jsonv4/outbounds_remove.go similarity index 87% rename from main/commands/all/api/outbounds_remove.go rename to main/commands/all/api/jsonv4/outbounds_remove.go index 2a536c548..e41a058a0 100644 --- a/main/commands/all/api/outbounds_remove.go +++ b/main/commands/all/api/jsonv4/outbounds_remove.go @@ -1,7 +1,8 @@ -package api +package jsonv4 import ( "fmt" + "github.com/v2fly/v2ray-core/v4/main/commands/all/api" handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command" "github.com/v2fly/v2ray-core/v4/main/commands/base" @@ -47,8 +48,8 @@ Example: } func executeRemoveOutbounds(cmd *base.Command, args []string) { - setSharedFlags(cmd) - setSharedConfigFlags(cmd) + api.SetSharedFlags(cmd) + api.SetSharedConfigFlags(cmd) isTags := cmd.Flag.Bool("tags", false, "") cmd.Flag.Parse(args) @@ -56,7 +57,7 @@ func executeRemoveOutbounds(cmd *base.Command, args []string) { if *isTags { tags = cmd.Flag.Args() } else { - c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively) + c, err := helpers.LoadConfig(cmd.Flag.Args(), api.ApiConfigFormat, api.ApiConfigRecursively) if err != nil { base.Fatalf("failed to load: %s", err) } @@ -69,7 +70,7 @@ func executeRemoveOutbounds(cmd *base.Command, args []string) { base.Fatalf("no outbound to remove") } - conn, ctx, close := dialAPIServer() + conn, ctx, close := api.DialAPIServer() defer close() client := handlerService.NewHandlerServiceClient(conn) diff --git a/main/commands/all/api/shared.go b/main/commands/all/api/shared.go index 0bd6913ad..a525385ae 100644 --- a/main/commands/all/api/shared.go +++ b/main/commands/all/api/shared.go @@ -16,13 +16,20 @@ import ( ) var ( - apiServerAddrPtr string - apiTimeout int - apiJSON bool - apiConfigFormat string - apiConfigRecursively bool + apiServerAddrPtr string + apiTimeout int + apiJSON bool + // ApiConfigFormat is an internal variable + ApiConfigFormat string + // ApiConfigRecursively is an internal variable + ApiConfigRecursively bool ) +// SetSharedFlags is an internal API +func SetSharedFlags(cmd *base.Command) { + setSharedFlags(cmd) +} + func setSharedFlags(cmd *base.Command) { cmd.Flag.StringVar(&apiServerAddrPtr, "s", "127.0.0.1:8080", "") cmd.Flag.StringVar(&apiServerAddrPtr, "server", "127.0.0.1:8080", "") @@ -31,9 +38,19 @@ func setSharedFlags(cmd *base.Command) { cmd.Flag.BoolVar(&apiJSON, "json", false, "") } +// SetSharedConfigFlags is an internal API +func SetSharedConfigFlags(cmd *base.Command) { + setSharedConfigFlags(cmd) +} + func setSharedConfigFlags(cmd *base.Command) { - cmd.Flag.StringVar(&apiConfigFormat, "format", core.FormatAuto, "") - cmd.Flag.BoolVar(&apiConfigRecursively, "r", false, "") + cmd.Flag.StringVar(&ApiConfigFormat, "format", core.FormatAuto, "") + cmd.Flag.BoolVar(&ApiConfigRecursively, "r", false, "") +} + +// SetSharedFlags is an internal API +func DialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) { + return dialAPIServer() } func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) { diff --git a/main/commands/all/commands.go b/main/commands/all/commands.go index 321557736..afa00354c 100644 --- a/main/commands/all/commands.go +++ b/main/commands/all/commands.go @@ -12,7 +12,6 @@ func init() { base.RootCommand.Commands = append( base.RootCommand.Commands, api.CmdAPI, - cmdConvert, cmdLove, tls.CmdTLS, cmdUUID, diff --git a/main/commands/all/convert.go b/main/commands/all/jsonv4/convert.go similarity index 99% rename from main/commands/all/convert.go rename to main/commands/all/jsonv4/convert.go index 317c5b67d..223ad4c28 100644 --- a/main/commands/all/convert.go +++ b/main/commands/all/jsonv4/convert.go @@ -1,12 +1,12 @@ -package all +package jsonv4 import ( "bytes" "encoding/json" + "github.com/pelletier/go-toml" "os" "strings" - "github.com/pelletier/go-toml" "google.golang.org/protobuf/proto" core "github.com/v2fly/v2ray-core/v4" diff --git a/main/commands/all/jsonv4/init.go b/main/commands/all/jsonv4/init.go new file mode 100644 index 000000000..7bdd0d93e --- /dev/null +++ b/main/commands/all/jsonv4/init.go @@ -0,0 +1,7 @@ +package jsonv4 + +import "github.com/v2fly/v2ray-core/v4/main/commands/base" + +func init() { + base.RegisterCommand(cmdConvert) +}