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