source_ip_block.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. routerService "github.com/xtls/xray-core/app/router/command"
  7. cserial "github.com/xtls/xray-core/common/serial"
  8. "github.com/xtls/xray-core/infra/conf/serial"
  9. "github.com/xtls/xray-core/main/commands/base"
  10. )
  11. var cmdSourceIpBlock = &base.Command{
  12. CustomFlags: true,
  13. UsageLine: "{{.Exec}} api sib [--server=127.0.0.1:8080] -outbound=blocked -inbound=socks 1.2.3.4",
  14. Short: "Block connections by source IP",
  15. Long: `
  16. Block connections by source IP address.
  17. Arguments:
  18. -s, -server <server:port>
  19. The API server address. Default 127.0.0.1:8080
  20. -t, -timeout <seconds>
  21. Timeout in seconds for calling API. Default 3
  22. -outbound
  23. Specifies the outbound tag.
  24. -inbound
  25. Specifies the inbound tag.
  26. -ruletag
  27. The ruleTag. Default sourceIpBlock
  28. -reset
  29. remove ruletag and apply new source IPs. Default false
  30. Example:
  31. {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -outbound=blocked -inbound=socks 1.2.3.4
  32. {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -outbound=blocked -inbound=socks 1.2.3.4 -reset
  33. `,
  34. Run: executeSourceIpBlock,
  35. }
  36. func executeSourceIpBlock(cmd *base.Command, args []string) {
  37. var (
  38. inbound string
  39. outbound string
  40. ruletag string
  41. reset bool
  42. )
  43. setSharedFlags(cmd)
  44. cmd.Flag.StringVar(&inbound, "inbound", "", "")
  45. cmd.Flag.StringVar(&outbound, "outbound", "", "")
  46. cmd.Flag.StringVar(&ruletag, "ruletag", "sourceIpBlock", "")
  47. cmd.Flag.BoolVar(&reset, "reset", false, "")
  48. cmd.Flag.Parse(args)
  49. unnamedArgs := cmd.Flag.Args()
  50. if len(unnamedArgs) == 0 {
  51. fmt.Println("reading from stdin:")
  52. unnamedArgs = []string{"stdin:"}
  53. }
  54. conn, ctx, close := dialAPIServer()
  55. defer close()
  56. client := routerService.NewRoutingServiceClient(conn)
  57. jsonIps, err := json.Marshal(unnamedArgs)
  58. if err != nil {
  59. fmt.Println("Error marshaling JSON:", err)
  60. return
  61. }
  62. jsonInbound, err := json.Marshal([]string{inbound})
  63. if inbound == "" {
  64. jsonInbound, err = json.Marshal([]string{})
  65. }
  66. if err != nil {
  67. fmt.Println("Error marshaling JSON:", err)
  68. return
  69. }
  70. stringConfig := fmt.Sprintf(`
  71. {
  72. "routing": {
  73. "rules": [
  74. {
  75. "ruleTag" : "%s",
  76. "inboundTag": %s,
  77. "outboundTag": "%s",
  78. "type": "field",
  79. "source": %s
  80. }
  81. ]
  82. }
  83. }
  84. `, ruletag, string(jsonInbound), outbound, string(jsonIps))
  85. conf, err := serial.DecodeJSONConfig(strings.NewReader(stringConfig))
  86. if err != nil {
  87. base.Fatalf("failed to decode : %s", err)
  88. }
  89. rc := *conf.RouterConfig
  90. config, err := rc.Build()
  91. if err != nil {
  92. base.Fatalf("failed to build conf: %s", err)
  93. }
  94. tmsg := cserial.ToTypedMessage(config)
  95. if tmsg == nil {
  96. base.Fatalf("failed to format config to TypedMessage.")
  97. }
  98. if reset {
  99. rr := &routerService.RemoveRuleRequest{
  100. RuleTag: ruletag,
  101. }
  102. resp, err := client.RemoveRule(ctx, rr)
  103. if err != nil {
  104. base.Fatalf("failed to perform RemoveRule: %s", err)
  105. }
  106. showJSONResponse(resp)
  107. }
  108. ra := &routerService.AddRuleRequest{
  109. Config: tmsg,
  110. ShouldAppend: true,
  111. }
  112. resp, err := client.AddRule(ctx, ra)
  113. if err != nil {
  114. base.Fatalf("failed to perform AddRule: %s", err)
  115. }
  116. showJSONResponse(resp)
  117. }