[GH-ISSUE #4437] [Bug] v3.0.2: freedom-outbound sendThrough keeps re-adding stale ip rules, breaking custom policy routing #3026

Closed
opened 2026-10-31 20:29:35 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @krugloff89 on GitHub (May 16, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4437

Environment

  • 3x-ui version: v3.0.2
  • xray-core: 26.4.25
  • OS: Ubuntu 24.04.4 LTS, kernel 6.8.0-111-generic

Setup context

On a host running 3x-ui, I have multiple WireGuard interfaces:

  • wg-chain-X-0 with 172.16.X.1/30 (inbound side of a multi-hop chain)
  • wg-cX-ext (separate outbound interface to an external WG provider)

I manage policy routing myself with explicit priority:

ip rule add from 172.16.X.0/24 lookup 200X prio 100
ip route replace default dev wg-cX-ext table 200X

In 3x-ui I create a freedom outbound with sendThrough: "172.16.X.1" so xray binds outbound sockets to that source IP, expecting my own prio 100 rule to direct traffic to table 200X → wg-cX-ext.

Steps to reproduce

  1. Create a freedom outbound in xrayTemplateConfig with sendThrough: "172.16.X.1".
  2. Trigger any of:
    • Click "Apply routing rules" in the GUI
    • Click "Restart Xray"
    • Save any inbound (any edit in the UI)
    • Auto-disable of an expired client (restartXrayOnClientDisable=true)
    • System link-change events (e.g. another userspace tool rebinding interfaces — Tailscale, NetworkManager, etc.)
  3. Run ip rule list.

Actual behavior

3x-ui injects (via netlink, not ip CLI) a new policy-routing rule for every freedom-outbound:

0:  from 172.16.X.1 lookup X

— with no explicit pref (kernel default = 0), and into the native routing table number X (the chain ID), with a corresponding stale route:

ip route add default dev wg-chain-X-0 table X

Each retrigger stacks a new duplicate with a different random pref. After dozens of triggers the routing table looks like:

0:    from 172.16.123.1 lookup 123
3:    from 172.16.123.1 lookup 123
8:    from 172.16.123.1 lookup 123
12:   from 172.16.123.1 lookup 123
...
5193: from 172.16.123.1 lookup 123
100:  from 172.16.123.0/24 lookup 20123   ← my correct rule, completely shadowed

Because prio 0 < prio 100, the injected rule always wins. Traffic goes into wg-chain-X-0, which has no 0.0.0.0/0 peer (it's an inbound-only interface), so packets are dropped.

Expected behavior

Either of:

  1. sendThrough should only bind the socket, not modify host policy routing. Source-IP binding is a userspace concept; pushing ip rule/ip route into kernel without the user's knowledge violates least surprise.
  2. OR use an explicit pref ≥ 32000 (kernel default for main table), make it deterministic, and clean up old rules before adding new ones to prevent stacking.
  3. OR make this behavior opt-in via a clear setting in xray-config or panel settings, defaulting to off for sendThrough.

Why this is critical

For users who run 3x-ui as part of a larger network setup (multi-hop WG, transit gateways, dual-VPN routing) and have their own policy routing on the host:

  • The injected prio 0 rule silently wins over every user rule with prio > 0
  • Freedom-outbounds break on every Xray restart or trigger event
  • Stale duplicates accumulate unbounded in the routing table
  • Kernel policy-routing decision becomes O(N) over hundreds of stale rules — measurable CPU overhead

Reproduction logs

$ ip rule list | head
0:    from all lookup local
0:    from 172.16.123.1 lookup 123          ← injected by 3x-ui (no pref → 0)
3:    from 172.16.123.1 lookup 123          ← duplicate from another trigger
8:    from 172.16.123.1 lookup 123
...
100:  from 172.16.123.0/24 lookup 20123     ← my correct rule (prio 100, shadowed)

$ ip route show table 123
default dev wg-chain-123-0 scope link        ← injected, dead-end

$ ip route show table 20123
default dev wg-c123-ext scope link           ← my correct route, never reached

$ curl --interface 172.16.123.1 https://api.ipify.org
# timeout — packets enter wg-chain-123-0 with no 0.0.0.0/0 peer

$ curl --interface 10.8.123.99 https://api.ipify.org   # different src, no GUI-rule match
194.126.177.116                                          # works, my rule matches

Workaround

I had to deploy a userspace daemon on the host that listens on ip monitor rule (kernel netlink stream) and deletes the stale rules in realtime (~50ms latency). Without it, freedom-outbounds break on every Xray restart and the routing table fills up with duplicate rules.

This shouldn't be necessary — sendThrough should not touch host policy routing.

Suspected source in code

Likely in web/service/xray.go (or wherever freedom.settings.sendThrough is processed during config-apply): a call to netlink.RuleAdd() (or equivalent via vishvananda/netlink / x/sys/unix) is made without specifying Priority and without idempotency check or cleanup of previous additions.

Severity

High for users with multi-hop / transit / dual-VPN setups. Silent loss of VPN egress on every Xray restart, plus unbounded growth of stale entries in kernel routing tables.

Logs: 2026-05-16 14:13:03 watcher start, kernel=6.8.0-111-generic
2026-05-16 14:13:17 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:13:17 deleted: pref=5 from=172.16.123.1 lookup=123
2026-05-16 14:13:17 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:20:36 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:20:36 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:20:36 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:20:36 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:21:00 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:21:00 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:21:00 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:21:00 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:39:47 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:39:47 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:39:47 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:39:47 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:39:50 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:39:50 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:39:50 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:39:50 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:40:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:40:25 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:40:25 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:40:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:40:27 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:40:27 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:40:27 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:40:28 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:59:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:59:25 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:59:25 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:59:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:59:27 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 14:59:27 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 14:59:27 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 14:59:27 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 15:04:41 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 15:04:41 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 15:04:41 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 15:04:41 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 15:04:55 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 15:04:55 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 15:04:55 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 15:04:55 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 15:05:20 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
2026-05-16 15:05:20 deleted: pref=0 from=172.16.123.1 lookup=123
2026-05-16 15:05:20 deleted stale default route: dev wg-chain-123-0 table 123
2026-05-16 15:05:20 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing

Originally created by @krugloff89 on GitHub (May 16, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4437 ## Environment - **3x-ui version**: v3.0.2 - **xray-core**: 26.4.25 - **OS**: Ubuntu 24.04.4 LTS, kernel 6.8.0-111-generic ## Setup context On a host running 3x-ui, I have multiple WireGuard interfaces: - `wg-chain-X-0` with `172.16.X.1/30` (inbound side of a multi-hop chain) - `wg-cX-ext` (separate outbound interface to an external WG provider) I manage policy routing **myself** with explicit priority: ``` ip rule add from 172.16.X.0/24 lookup 200X prio 100 ip route replace default dev wg-cX-ext table 200X ``` In 3x-ui I create a `freedom` outbound with `sendThrough: "172.16.X.1"` so xray binds outbound sockets to that source IP, expecting my own `prio 100` rule to direct traffic to `table 200X` → `wg-cX-ext`. ## Steps to reproduce 1. Create a `freedom` outbound in `xrayTemplateConfig` with `sendThrough: "172.16.X.1"`. 2. Trigger **any** of: - Click "Apply routing rules" in the GUI - Click "Restart Xray" - Save any inbound (any edit in the UI) - Auto-disable of an expired client (`restartXrayOnClientDisable=true`) - System link-change events (e.g. another userspace tool rebinding interfaces — Tailscale, NetworkManager, etc.) 3. Run `ip rule list`. ## Actual behavior 3x-ui injects (via netlink, not `ip` CLI) a new policy-routing rule for every freedom-outbound: ``` 0: from 172.16.X.1 lookup X ``` — **with no explicit `pref`** (kernel default = 0), and into the **native** routing table number `X` (the chain ID), with a corresponding stale route: ``` ip route add default dev wg-chain-X-0 table X ``` Each retrigger stacks a new duplicate with a different random pref. After dozens of triggers the routing table looks like: ``` 0: from 172.16.123.1 lookup 123 3: from 172.16.123.1 lookup 123 8: from 172.16.123.1 lookup 123 12: from 172.16.123.1 lookup 123 ... 5193: from 172.16.123.1 lookup 123 100: from 172.16.123.0/24 lookup 20123 ← my correct rule, completely shadowed ``` Because **prio 0 < prio 100**, the injected rule **always wins**. Traffic goes into `wg-chain-X-0`, which has no `0.0.0.0/0` peer (it's an inbound-only interface), so packets are dropped. ## Expected behavior Either of: 1. **`sendThrough` should only bind the socket, not modify host policy routing.** Source-IP binding is a userspace concept; pushing `ip rule`/`ip route` into kernel without the user's knowledge violates least surprise. 2. **OR** use an explicit `pref` ≥ 32000 (kernel default for `main` table), make it deterministic, and **clean up old rules before adding new ones** to prevent stacking. 3. **OR** make this behavior **opt-in** via a clear setting in xray-config or panel settings, defaulting to **off** for sendThrough. ## Why this is critical For users who run 3x-ui as part of a larger network setup (multi-hop WG, transit gateways, dual-VPN routing) and have **their own** policy routing on the host: - The injected `prio 0` rule silently wins over every user rule with `prio > 0` - Freedom-outbounds **break** on every Xray restart or trigger event - Stale duplicates accumulate **unbounded** in the routing table - Kernel policy-routing decision becomes O(N) over hundreds of stale rules — measurable CPU overhead ## Reproduction logs ```bash $ ip rule list | head 0: from all lookup local 0: from 172.16.123.1 lookup 123 ← injected by 3x-ui (no pref → 0) 3: from 172.16.123.1 lookup 123 ← duplicate from another trigger 8: from 172.16.123.1 lookup 123 ... 100: from 172.16.123.0/24 lookup 20123 ← my correct rule (prio 100, shadowed) $ ip route show table 123 default dev wg-chain-123-0 scope link ← injected, dead-end $ ip route show table 20123 default dev wg-c123-ext scope link ← my correct route, never reached $ curl --interface 172.16.123.1 https://api.ipify.org # timeout — packets enter wg-chain-123-0 with no 0.0.0.0/0 peer $ curl --interface 10.8.123.99 https://api.ipify.org # different src, no GUI-rule match 194.126.177.116 # works, my rule matches ``` ## Workaround I had to deploy a userspace daemon on the host that listens on `ip monitor rule` (kernel netlink stream) and deletes the stale rules in realtime (~50ms latency). Without it, freedom-outbounds break on every Xray restart and the routing table fills up with duplicate rules. This shouldn't be necessary — `sendThrough` should not touch host policy routing. ## Suspected source in code Likely in `web/service/xray.go` (or wherever `freedom.settings.sendThrough` is processed during config-apply): a call to `netlink.RuleAdd()` (or equivalent via `vishvananda/netlink` / `x/sys/unix`) is made without specifying `Priority` and without idempotency check or cleanup of previous additions. ## Severity **High** for users with multi-hop / transit / dual-VPN setups. Silent loss of VPN egress on every Xray restart, plus unbounded growth of stale entries in kernel routing tables. Logs: 2026-05-16 14:13:03 watcher start, kernel=6.8.0-111-generic 2026-05-16 14:13:17 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:13:17 deleted: pref=5 from=172.16.123.1 lookup=123 2026-05-16 14:13:17 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:20:36 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:20:36 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:20:36 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:20:36 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:21:00 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:21:00 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:21:00 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:21:00 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:39:47 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:39:47 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:39:47 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:39:47 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:39:50 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:39:50 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:39:50 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:39:50 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:40:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:40:25 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:40:25 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:40:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:40:27 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:40:27 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:40:27 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:40:28 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:59:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:59:25 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:59:25 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:59:25 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:59:27 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 14:59:27 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 14:59:27 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 14:59:27 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 15:04:41 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 15:04:41 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 15:04:41 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 15:04:41 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 15:04:55 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 15:04:55 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 15:04:55 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 15:04:55 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 15:05:20 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing 2026-05-16 15:05:20 deleted: pref=0 from=172.16.123.1 lookup=123 2026-05-16 15:05:20 deleted stale default route: dev wg-chain-123-0 table 123 2026-05-16 15:05:20 STALE-DETECTED: from 172.16.123.1 table=123 pref=0 — removing
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#3026
No description provided.