2020-11-30 07:08:08 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-02 17:11:24 -05:00
|
|
|
handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command"
|
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 cmdAddInbounds = &base.Command{
|
|
|
|
CustomFlags: true,
|
2021-03-10 16:03:26 -05:00
|
|
|
UsageLine: "{{.Exec}} api adi [--server=127.0.0.1:8080] [c1.json] [dir1]...",
|
2021-02-21 10:02:42 -05:00
|
|
|
Short: "add inbounds",
|
2020-11-30 07:08:08 -05:00
|
|
|
Long: `
|
|
|
|
Add inbounds 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: executeAddInbounds,
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeAddInbounds(cmd *base.Command, args []string) {
|
|
|
|
setSharedFlags(cmd)
|
2021-02-21 10:02:42 -05:00
|
|
|
setSharedConfigFlags(cmd)
|
2020-11-30 07:08:08 -05:00
|
|
|
cmd.Flag.Parse(args)
|
2021-02-21 10:02:42 -05:00
|
|
|
c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively)
|
|
|
|
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.InboundConfigs) == 0 {
|
2020-11-30 07:08:08 -05:00
|
|
|
base.Fatalf("no valid inbound found")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, ctx, close := dialAPIServer()
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
client := handlerService.NewHandlerServiceClient(conn)
|
2021-02-21 10:02:42 -05:00
|
|
|
for _, in := range c.InboundConfigs {
|
2020-11-30 07:08:08 -05:00
|
|
|
fmt.Println("adding:", in.Tag)
|
|
|
|
i, err := in.Build()
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to build conf: %s", err)
|
|
|
|
}
|
|
|
|
r := &handlerService.AddInboundRequest{
|
|
|
|
Inbound: i,
|
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
_, err = client.AddInbound(ctx, r)
|
2020-11-30 07:08:08 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to add inbound: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|