1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/main/commands/all/api/outbounds_add.go
Jebbs 7a0af318df
refine api, run command and merger (#766)
* mergers code optimize

* api log work with pipe

* remove inbounds/outbounds by tags
*fix rmo flag parse

* cmds description and message optimize
2021-09-04 11:11:45 +01:00

76 lines
1.7 KiB
Go

package api
import (
"fmt"
handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command"
"github.com/v2fly/v2ray-core/v4/main/commands/base"
"github.com/v2fly/v2ray-core/v4/main/commands/helpers"
)
var cmdAddOutbounds = &base.Command{
CustomFlags: true,
UsageLine: "{{.Exec}} api ado [--server=127.0.0.1:8080] [c1.json] [dir1]...",
Short: "add outbounds",
Long: `
Add outbounds to V2Ray.
> Make sure you have "HandlerService" set in "config.api.services"
of server config.
Arguments:
-format <format>
The input format.
Available values: "auto", "json", "toml", "yaml"
Default: "auto"
-r
Load folders recursively.
-s, -server <server:port>
The API server address. Default 127.0.0.1:8080
-t, -timeout <seconds>
Timeout seconds to call API. Default 3
Example:
{{.Exec}} {{.LongName}} dir
{{.Exec}} {{.LongName}} c1.json c2.yaml
`,
Run: executeAddOutbounds,
}
func executeAddOutbounds(cmd *base.Command, args []string) {
setSharedFlags(cmd)
setSharedConfigFlags(cmd)
cmd.Flag.Parse(args)
c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively)
if err != nil {
base.Fatalf("failed to load: %s", err)
}
if len(c.OutboundConfigs) == 0 {
base.Fatalf("no valid outbound found")
}
conn, ctx, close := dialAPIServer()
defer close()
client := handlerService.NewHandlerServiceClient(conn)
for _, out := range c.OutboundConfigs {
fmt.Println("adding:", out.Tag)
o, err := out.Build()
if err != nil {
base.Fatalf("failed to build conf: %s", err)
}
r := &handlerService.AddOutboundRequest{
Outbound: o,
}
_, err = client.AddOutbound(ctx, r)
if err != nil {
base.Fatalf("failed to add outbound: %s", err)
}
}
}