2021-05-02 16:40:11 -04:00
|
|
|
package jsonv4
|
2020-11-30 07:08:08 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-02 17:11:24 -05:00
|
|
|
handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command"
|
2021-06-04 19:42:53 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/all/api"
|
2020-12-04 09:32:55 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/base"
|
2021-02-21 10:02:42 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/helpers"
|
2020-11-30 07:08:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdAddOutbounds = &base.Command{
|
|
|
|
CustomFlags: true,
|
2021-03-10 16:03:26 -05:00
|
|
|
UsageLine: "{{.Exec}} api ado [--server=127.0.0.1:8080] [c1.json] [dir1]...",
|
2021-02-21 10:02:42 -05:00
|
|
|
Short: "add outbounds",
|
2020-11-30 07:08:08 -05:00
|
|
|
Long: `
|
|
|
|
Add outbounds to V2Ray.
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
> Make sure you have "HandlerService" set in "config.api.services"
|
|
|
|
of server config.
|
|
|
|
|
2020-11-30 07:08:08 -05:00
|
|
|
Arguments:
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-format <format>
|
2021-03-10 16:03:26 -05:00
|
|
|
The input format.
|
2021-02-21 10:02:42 -05:00
|
|
|
Available values: "auto", "json", "toml", "yaml"
|
|
|
|
Default: "auto"
|
|
|
|
|
|
|
|
-r
|
|
|
|
Load folders recursively.
|
|
|
|
|
|
|
|
-s, -server <server:port>
|
2020-11-30 07:08:08 -05:00
|
|
|
The API server address. Default 127.0.0.1:8080
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-t, -timeout <seconds>
|
2020-11-30 07:08:08 -05:00
|
|
|
Timeout seconds to call API. Default 3
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
{{.Exec}} {{.LongName}} dir
|
|
|
|
{{.Exec}} {{.LongName}} c1.json c2.yaml
|
2020-11-30 07:08:08 -05:00
|
|
|
`,
|
|
|
|
Run: executeAddOutbounds,
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeAddOutbounds(cmd *base.Command, args []string) {
|
2021-05-02 16:40:11 -04:00
|
|
|
api.SetSharedFlags(cmd)
|
|
|
|
api.SetSharedConfigFlags(cmd)
|
2020-11-30 07:08:08 -05:00
|
|
|
cmd.Flag.Parse(args)
|
2021-06-04 19:42:53 -04:00
|
|
|
c, err := helpers.LoadConfig(cmd.Flag.Args(), api.APIConfigFormat, api.APIConfigRecursively)
|
2021-02-21 10:02:42 -05:00
|
|
|
if err != nil {
|
2021-03-10 16:03:26 -05:00
|
|
|
base.Fatalf("failed to load: %s", err)
|
2020-11-30 07:08:08 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
if len(c.OutboundConfigs) == 0 {
|
2020-11-30 07:08:08 -05:00
|
|
|
base.Fatalf("no valid outbound found")
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:40:11 -04:00
|
|
|
conn, ctx, close := api.DialAPIServer()
|
2020-11-30 07:08:08 -05:00
|
|
|
defer close()
|
|
|
|
|
|
|
|
client := handlerService.NewHandlerServiceClient(conn)
|
2021-02-21 10:02:42 -05:00
|
|
|
for _, out := range c.OutboundConfigs {
|
2020-11-30 07:08:08 -05:00
|
|
|
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,
|
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
_, err = client.AddOutbound(ctx, r)
|
2020-11-30 07:08:08 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to add outbound: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|