inbounds_add.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package api
  2. import (
  3. "fmt"
  4. handlerService "github.com/xtls/xray-core/app/proxyman/command"
  5. "github.com/xtls/xray-core/infra/conf"
  6. "github.com/xtls/xray-core/infra/conf/serial"
  7. "github.com/xtls/xray-core/main/commands/base"
  8. )
  9. var cmdAddInbounds = &base.Command{
  10. CustomFlags: true,
  11. UsageLine: "{{.Exec}} api adi [--server=127.0.0.1:8080] <c1.json> [c2.json]...",
  12. Short: "Add inbounds",
  13. Long: `
  14. Add inbounds to Xray.
  15. Arguments:
  16. -s, -server
  17. The API server address. Default 127.0.0.1:8080
  18. -t, -timeout
  19. Timeout seconds to call API. Default 3
  20. Example:
  21. {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json c2.json
  22. `,
  23. Run: executeAddInbounds,
  24. }
  25. func executeAddInbounds(cmd *base.Command, args []string) {
  26. setSharedFlags(cmd)
  27. cmd.Flag.Parse(args)
  28. unnamedArgs := cmd.Flag.Args()
  29. if len(unnamedArgs) == 0 {
  30. fmt.Println("reading from stdin:")
  31. unnamedArgs = []string{"stdin:"}
  32. }
  33. ins := make([]conf.InboundDetourConfig, 0)
  34. for _, arg := range unnamedArgs {
  35. r, err := loadArg(arg)
  36. if err != nil {
  37. base.Fatalf("failed to load %s: %s", arg, err)
  38. }
  39. conf, err := serial.DecodeJSONConfig(r)
  40. if err != nil {
  41. base.Fatalf("failed to decode %s: %s", arg, err)
  42. }
  43. ins = append(ins, conf.InboundConfigs...)
  44. }
  45. if len(ins) == 0 {
  46. base.Fatalf("no valid inbound found")
  47. }
  48. conn, ctx, close := dialAPIServer()
  49. defer close()
  50. client := handlerService.NewHandlerServiceClient(conn)
  51. for _, in := range ins {
  52. fmt.Println("adding:", in.Tag)
  53. i, err := in.Build()
  54. if err != nil {
  55. base.Fatalf("failed to build conf: %s", err)
  56. }
  57. r := &handlerService.AddInboundRequest{
  58. Inbound: i,
  59. }
  60. resp, err := client.AddInbound(ctx, r)
  61. if err != nil {
  62. base.Fatalf("failed to add inbound: %s", err)
  63. }
  64. showJSONResponse(resp)
  65. }
  66. }