XTLS / Xray-core

Xray, Penetrates Everything. Also the best v2ray-core, with XTLS support. Fully compatible configuration.
https://t.me/projectXray
Mozilla Public License 2.0
25.57k stars 3.95k forks source link

Bug Report: Inbound Addition Failure in Xray API #3806

Closed EchoVRC closed 1 day ago

EchoVRC commented 2 months ago

Integrity requirements

Description

Summary: When removing and immediately re-adding an inbound with the same tag using the Xray API, the system throws errors. The initial attempt results in a DeadlineExceeded error, and subsequent attempts return an existing tag found error. This indicates a race condition or incomplete state clearance in the Xray API when handling rapid inbound changes.

img

Reproduction Method

./xray api rmi --server 127.0.0.1:9000 wireguard-7; ./xray api adi --server 127.0.0.1:9000 generated_config/wireguard.json

generated_config/wireguard.json

{
  "inbounds": [
    {
      "tag": "wireguard-7",
      "listen": "0.0.0.0",
      "port": 51820,
      "protocol": "wireguard",
      "settings": {
        "secretKey": "iBL/rcU9IrJHkohqPTVSHH2XytBO96nhpIPk7PxiOW0=",
        "peers": [
          {
            "publicKey": "dB53utuSg2/89INZGtFdAEtZ6rGDkPF5kXYFtepIkQg=",
            "allowedIPs": [
              "169.254.0.2/32"
            ]
          }
        ],
        "kernelMode": false
      }
    }
  ]
}

Client config


N/A

Server config


{
  "log": {
    "loglevel": "debug"
  },
  "stats": {},
  "api": {
    "listen": "127.0.0.1:9000",
    "services": [
      "HandlerService",
      "LoggerService",
      "StatsService",
      "RoutingService",
      "ReflectionService",
      "DNSService",
      "TransportService",
      "PolicyService",
      "ReverseService"
    ],
    "tag": "api"
  },
  "policy": {
    "levels": {
      "0": {
        "statsUserUplink": true,
        "statsUserDownlink": true
      }
    }
  },
  "inbounds": [
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    },
    {
      "protocol": "blackhole",
      "tag": "blocked"
    }
  ],
  "routing": {
    "domainStrategy": "AsIs",
    "rules": [
      {
        "type": "field",
        "inboundTag": ["api"],
        "outboundTag": "api"
      },
      {
        "type": "field",
        "outboundTag": "blocked",
        "ip": ["geoip:private"]
      },
      {
        "type": "field",
        "outboundTag": "blocked",
        "protocol": ["bittorrent"]
      }
    ]
  }
}

Client log


N/A

Server log


root@v1655092:/opt/app# ./xray -c generated_config/xray_config.json
Xray 1.8.24 (Xray, Penetrates Everything.) 6baad79 (go1.23.0 linux/amd64)
A unified platform for anti-censorship.
2024/09/13 04:36:08 [Info] infra/conf/serial: Reading config: &{Name:generated_config/xray_config.json Format:json}
2024/09/13 04:36:08 [Debug] app/log: Logger started
2024/09/13 04:36:08 [Info] app/commander: API server listening on 127.0.0.1:9000
2024/09/13 04:36:08 [Warning] core: Xray 1.8.24 started
2024/09/13 04:36:17 [Debug] Routine: decryption worker 1 - started
2024/09/13 04:36:17 [Debug] UAPI: Updating private key
2024/09/13 04:36:17 [Debug] Routine: handshake worker 2 - started
2024/09/13 04:36:17 [Debug] Routine: handshake worker 1 - started
2024/09/13 04:36:17 [Debug] Routine: encryption worker 2 - started
2024/09/13 04:36:17 [Debug] Routine: encryption worker 1 - started
2024/09/13 04:36:17 [Debug] Routine: decryption worker 2 - started
2024/09/13 04:36:17 [Debug] Routine: encryption worker 3 - started
2024/09/13 04:36:17 [Debug] UAPI: Updating listen port
2024/09/13 04:36:17 [Debug] Routine: decryption worker 3 - started
2024/09/13 04:36:17 [Debug] Routine: handshake worker 3 - started
2024/09/13 04:36:17 [Debug] Routine: TUN reader - started
2024/09/13 04:36:17 [Debug] Routine: event worker - started
2024/09/13 04:36:17 [Debug] Interface up requested
2024/09/13 04:36:17 [Debug] peer(dB53…IkQg) - UAPI: Created
2024/09/13 04:36:17 [Debug] peer(dB53…IkQg) - UAPI: Adding allowedip
2024/09/13 04:36:17 [Debug] peer(dB53…IkQg) - Starting
2024/09/13 04:36:17 [Debug] UDP bind has been updated
2024/09/13 04:36:17 [Debug] Interface state was Down, requested Up, now Up
2024/09/13 04:36:17 [Debug] peer(dB53…IkQg) - Routine: sequential receiver - started
2024/09/13 04:36:17 [Debug] peer(dB53…IkQg) - Routine: sequential sender - started
2024/09/13 04:36:17 [Debug] Routine: receive incoming Open - started
2024/09/13 04:36:17 [Info] transport/internet/udp: listening UDP on 0.0.0.0:51820
2024/09/13 04:36:25 [Info] transport/internet/udp: failed to read UDP msg > read udp [::]:51820: use of closed network connection
2024/09/13 04:36:25 [Debug] UAPI: Updating private key
2024/09/13 04:36:25 [Debug] Routine: handshake worker 1 - started
2024/09/13 04:36:25 [Debug] Routine: encryption worker 1 - started
2024/09/13 04:36:25 [Debug] Routine: decryption worker 1 - started
2024/09/13 04:36:25 [Debug] Routine: encryption worker 3 - started
2024/09/13 04:36:25 [Debug] Routine: encryption worker 2 - started
2024/09/13 04:36:25 [Debug] Routine: decryption worker 2 - started
2024/09/13 04:36:25 [Debug] Routine: handshake worker 2 - started
2024/09/13 04:36:25 [Debug] Routine: decryption worker 3 - started
2024/09/13 04:36:25 [Debug] Routine: TUN reader - started
2024/09/13 04:36:25 [Debug] Routine: handshake worker 3 - started
2024/09/13 04:36:25 [Debug] UAPI: Updating listen port
2024/09/13 04:36:25 [Debug] Routine: event worker - started
2024/09/13 04:36:25 [Debug] Interface up requested
2024/09/13 04:36:25 [Debug] peer(dB53…IkQg) - UAPI: Created
2024/09/13 04:36:25 [Debug] peer(dB53…IkQg) - UAPI: Adding allowedip
2024/09/13 04:36:25 [Debug] peer(dB53…IkQg) - Starting
2024/09/13 04:36:25 [Debug] Routine: receive incoming Open - started
2024/09/13 04:36:25 [Info] transport/internet/udp: listening UDP on 0.0.0.0:51820
2024/09/13 04:36:27 [Info] transport/internet/udp: failed to read UDP msg > read udp [::]:51820: use of closed network connection
2024/09/13 04:36:27 [Debug] UAPI: Updating private key
2024/09/13 04:36:27 [Debug] Routine: encryption worker 2 - started
2024/09/13 04:36:27 [Debug] Routine: decryption worker 3 - started
2024/09/13 04:36:27 [Debug] Routine: decryption worker 2 - started
2024/09/13 04:36:27 [Debug] Routine: encryption worker 1 - started
2024/09/13 04:36:27 [Debug] Routine: decryption worker 1 - started
2024/09/13 04:36:27 [Debug] Routine: handshake worker 2 - started
2024/09/13 04:36:27 [Debug] Routine: encryption worker 3 - started
2024/09/13 04:36:27 [Debug] Routine: handshake worker 1 - started
2024/09/13 04:36:27 [Debug] Routine: TUN reader - started
2024/09/13 04:36:27 [Debug] Routine: handshake worker 3 - started
2024/09/13 04:36:27 [Debug] Routine: event worker - started
2024/09/13 04:36:27 [Debug] Interface up requested
2024/09/13 04:36:27 [Debug] UAPI: Updating listen port
2024/09/13 04:36:27 [Debug] UDP bind has been updated
2024/09/13 04:36:27 [Debug] Routine: receive incoming Open - started
2024/09/13 04:36:27 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:27 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:27 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:28 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:28 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:28 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:29 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:29 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:29 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:30 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:30 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:30 [Debug] Routine: receive incoming Open - stopped
2024/09/13 04:36:30 [Debug] UDP bind has been updated
2024/09/13 04:36:30 [Debug] Routine: receive incoming Open - started
2024/09/13 04:36:30 [Debug] peer(dB53…IkQg) - UAPI: Created
2024/09/13 04:36:30 [Debug] peer(dB53…IkQg) - UAPI: Adding allowedip
2024/09/13 04:36:30 [Debug] peer(dB53…IkQg) - Starting
2024/09/13 04:36:30 [Debug] Interface state was Down, requested Up, now Up
2024/09/13 04:36:30 [Debug] peer(dB53…IkQg) - Routine: sequential sender - started
2024/09/13 04:36:30 [Debug] peer(dB53…IkQg) - Routine: sequential receiver - started
2024/09/13 04:36:30 [Info] transport/internet/udp: listening UDP on 0.0.0.0:51820
2024/09/13 04:36:50 [Info] transport/internet/udp: failed to read UDP msg > read udp [::]:51820: use of closed network connection
2024/09/13 04:36:50 [Debug] UAPI: Updating private key
2024/09/13 04:36:50 [Debug] Routine: handshake worker 2 - started
2024/09/13 04:36:50 [Debug] Routine: encryption worker 1 - started
2024/09/13 04:36:50 [Debug] Routine: decryption worker 1 - started
2024/09/13 04:36:50 [Debug] Routine: handshake worker 1 - started
2024/09/13 04:36:50 [Debug] Routine: encryption worker 2 - started
2024/09/13 04:36:50 [Debug] Routine: decryption worker 2 - started
2024/09/13 04:36:50 [Debug] Routine: TUN reader - started
2024/09/13 04:36:50 [Debug] Routine: event worker - started
2024/09/13 04:36:50 [Debug] Interface up requested
2024/09/13 04:36:50 [Debug] Routine: handshake worker 3 - started
2024/09/13 04:36:50 [Debug] Routine: encryption worker 3 - started
2024/09/13 04:36:50 [Debug] Routine: decryption worker 3 - started
2024/09/13 04:36:50 [Debug] UAPI: Updating listen port
2024/09/13 04:36:50 [Debug] UDP bind has been updated
2024/09/13 04:36:50 [Debug] Routine: receive incoming Open - started
2024/09/13 04:36:50 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:51 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:51 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:51 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:52 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:52 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:52 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:53 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:53 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:53 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:54 [Debug] Failed to receive Open packet: channel closed
2024/09/13 04:36:54 [Debug] Routine: receive incoming Open - stopped
2024/09/13 04:36:54 [Debug] UDP bind has been updated
2024/09/13 04:36:54 [Debug] peer(dB53…IkQg) - UAPI: Created
2024/09/13 04:36:54 [Debug] peer(dB53…IkQg) - UAPI: Adding allowedip
2024/09/13 04:36:54 [Debug] peer(dB53…IkQg) - Starting
2024/09/13 04:36:54 [Debug] Interface state was Down, requested Up, now Up
2024/09/13 04:36:54 [Debug] Routine: receive incoming Open - started
2024/09/13 04:36:54 [Debug] peer(dB53…IkQg) - Routine: sequential sender - started
2024/09/13 04:36:54 [Debug] peer(dB53…IkQg) - Routine: sequential receiver - started
2024/09/13 04:36:54 [Info] transport/internet/udp: listening UDP on 0.0.0.0:51820

yuhan6665 commented 2 months ago

I think this is not a serious problem. (Not priority) The inbound might takes a while to up, you can retry

EchoVRC commented 2 months ago

The main problem is that if the json contains multiple inbound, it crashes on the first one and the others don't apply.

yuhan6665 commented 1 month ago

I think you should do one at a time. Can you post the crash log?

EchoVRC commented 1 month ago

I've already done that. See Server log in the original post.

Another interesting point. If you remove wireguard inboond under load, the server keeps trying to send keepalive... Something seems to remain after removal((( image

Fangliding commented 1 month ago

To some extent, the implementation of Wireguard inbound is not standardized. I think we should declare which protocols the API is applicable to

EchoVRC commented 1 month ago

Check out https://github.com/XTLS/Xray-core/blob/a35d5719f425f061a3274e2ba51481299853ea82/proxy/wireguard/gvisortun/tun.go#L176 this part I'm not an expert, but if you rewrite this part so that it closes all connections/goroutine normally, the problem will be solved

moh3nasiyat commented 1 month ago

hi in new ver xray i get this error when xray restart

"Failed to start: main: failed to load config files: [bin/config.json] > infra/conf: Failed to build SplitHTTP config. > infra/conf: maxConnections cannot be specified together with maxConcurrency"

Fangliding commented 1 day ago

image