github.com/xraypb/Xray-core@v1.8.1/main/commands/all/api/outbounds_add.go (about) 1 package api 2 3 import ( 4 "fmt" 5 6 handlerService "github.com/xraypb/Xray-core/app/proxyman/command" 7 "github.com/xraypb/Xray-core/infra/conf" 8 "github.com/xraypb/Xray-core/infra/conf/serial" 9 "github.com/xraypb/Xray-core/main/commands/base" 10 ) 11 12 var cmdAddOutbounds = &base.Command{ 13 CustomFlags: true, 14 UsageLine: "{{.Exec}} api ado [--server=127.0.0.1:8080] <c1.json> [c2.json]...", 15 Short: "Add outbounds", 16 Long: ` 17 Add outbounds to Xray. 18 Arguments: 19 -s, -server 20 The API server address. Default 127.0.0.1:8080 21 -t, -timeout 22 Timeout seconds to call API. Default 3 23 Example: 24 {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json c2.json 25 `, 26 Run: executeAddOutbounds, 27 } 28 29 func executeAddOutbounds(cmd *base.Command, args []string) { 30 setSharedFlags(cmd) 31 cmd.Flag.Parse(args) 32 unnamedArgs := cmd.Flag.Args() 33 if len(unnamedArgs) == 0 { 34 fmt.Println("Reading from STDIN") 35 unnamedArgs = []string{"stdin:"} 36 } 37 38 outs := make([]conf.OutboundDetourConfig, 0) 39 for _, arg := range unnamedArgs { 40 r, err := loadArg(arg) 41 if err != nil { 42 base.Fatalf("failed to load %s: %s", arg, err) 43 } 44 conf, err := serial.DecodeJSONConfig(r) 45 if err != nil { 46 base.Fatalf("failed to decode %s: %s", arg, err) 47 } 48 outs = append(outs, conf.OutboundConfigs...) 49 } 50 if len(outs) == 0 { 51 base.Fatalf("no valid outbound found") 52 } 53 54 conn, ctx, close := dialAPIServer() 55 defer close() 56 57 client := handlerService.NewHandlerServiceClient(conn) 58 for _, out := range outs { 59 fmt.Println("adding:", out.Tag) 60 o, err := out.Build() 61 if err != nil { 62 base.Fatalf("failed to build conf: %s", err) 63 } 64 r := &handlerService.AddOutboundRequest{ 65 Outbound: o, 66 } 67 resp, err := client.AddOutbound(ctx, r) 68 if err != nil { 69 base.Fatalf("failed to add outbound: %s", err) 70 } 71 showJSONResponse(resp) 72 } 73 }