github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/main/commands/all/api/inbounds_add.go (about) 1 package api 2 3 import ( 4 "fmt" 5 6 handlerService "github.com/xmplusdev/xmcore/app/proxyman/command" 7 "github.com/xmplusdev/xmcore/infra/conf" 8 "github.com/xmplusdev/xmcore/infra/conf/serial" 9 "github.com/xmplusdev/xmcore/main/commands/base" 10 ) 11 12 var cmdAddInbounds = &base.Command{ 13 CustomFlags: true, 14 UsageLine: "{{.Exec}} api adi [--server=127.0.0.1:8080] <c1.json> [c2.json]...", 15 Short: "Add inbounds", 16 Long: ` 17 Add inbounds 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: executeAddInbounds, 27 } 28 29 func executeAddInbounds(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 ins := make([]conf.InboundDetourConfig, 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 ins = append(ins, conf.InboundConfigs...) 49 } 50 if len(ins) == 0 { 51 base.Fatalf("no valid inbound found") 52 } 53 54 conn, ctx, close := dialAPIServer() 55 defer close() 56 57 client := handlerService.NewHandlerServiceClient(conn) 58 for _, in := range ins { 59 fmt.Println("adding:", in.Tag) 60 i, err := in.Build() 61 if err != nil { 62 base.Fatalf("failed to build conf: %s", err) 63 } 64 r := &handlerService.AddInboundRequest{ 65 Inbound: i, 66 } 67 resp, err := client.AddInbound(ctx, r) 68 if err != nil { 69 base.Fatalf("failed to add inbound: %s", err) 70 } 71 showJSONResponse(resp) 72 } 73 }