[PR #6654] [MERGED] fix(amneziawg): sniff the relay with routeOnly; scope the v6 egress to IPv6 #6399

Closed
opened 2026-11-02 04:08:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6654
Author: @rudenko-ks
Created: 9/26/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @MHSanaei

Base: main ← Head: fix/amneziawg-relay-routeonly


📝 Commits (3)

  • 3ca9c20 fix(amneziawg): sniff the relay with routeOnly
  • e200037 fix(amneziawg): send only IPv6 targets through the peer's v6 egress
  • 5adae14 Merge branch 'main' into fix/amneziawg-relay-routeonly

📊 Changes

2 files changed (+34 additions, -12 deletions)

View changed files

📝 internal/web/service/xray.go (+5 -12)
📝 internal/web/service/xray_config_inject_test.go (+29 -0)

📄 Description

Summary

Two small fixes to the embedded AmneziaWG relay's Xray config:

  1. Sniff the relay inbound with routeOnly: true, so the sniffed domain drives routing only and the connection still goes to the IP the peer asked for.
  2. Limit the per-peer IPv6 egress rule to IPv6 destinations ("ip": ["::/0"]), because its sendThrough is an IPv6 address and cannot dial an IPv4 target.

Why

1. Sniffing rewrote the destination. amneziawgEgressSniffingSettings (added in #6105) turns on sniffing so that domain-based Routing rules can match traffic that arrives as bare IPs. Without routeOnly, it also replaces the dial target with the sniffed SNI/Host. That breaks any client that connects to one IP while presenting another name.

Telegram does exactly this. When its main connection drops, its anti-censorship recovery opens FakeTLS to 194.221.250.50:443 with SNI www.google.com. The relay re-addressed that connection to www.google.com, so it reached real Google. Telegram then looped on it: its net log showed 450 TLS hash mismatch in 20 minutes while the app sat on "Connecting…" until it was killed.

2. The IPv6 egress took IPv4 flows it cannot dial. For a peer with an IPv6 AllowedIP and aliases active, injectAmneziawgV6Egress routes all of that peer's traffic (inboundTag + user) to a freedom outbound with sendThrough: <peer v6>. An IPv4 target then fails with dial tcp: address [v6]: mismatched local address type. This already broke every IPv4 flow that sniffing does not recognise, such as DNS to 1.1.1.1 over UDP or TCP, and SSH. Sniffed TLS/HTTP only worked because of the rewrite in (1): the domain was dialed over IPv6. With (1) fixed, IPv4 HTTPS broke as well. That is why both changes ship together.

fakedns stays in destOverride: the dispatcher still maps fake-pool IPs back to their domain regardless of routeOnly.

Type of change

  • Bug fix

Areas affected

  • Xray config generation

How was this tested?

Unit tests. TestInjectAmneziawgnetSocks_SniffingRouteOnly (new) asserts routeOnly: true and the exact destOverride list. TestInjectAmneziawgV6Egress_CreatesOutboundAndRuleForV6Peer now asserts ip: ["::/0"]. Each assertion fails without its fix and passes with it. go build ./..., go test ./internal/web/service -shuffle=on, go vet, gofumpt and golangci-lint are clean.

Live test. Setup:

  • An isolated instance of this branch on an Ubuntu 24.04 server with a routed IPv6 /64.
  • An AmneziaWG inbound with IPv6 enabled on ens3; one peer with 10.77.0.2/32 plus a public /128 alias from that /64.
  • A kernel AmneziaWG client on a second host, using source-based policy routing.
  • The same checks run against three builds: main, commit 1 only, and both commits. Dial targets were read from Xray's info log.
Check from the peer main + routeOnly only + routeOnly + ::/0
IPv4 HTTPS (dual-stack site) works, but dialed by domain over the v6 alias fails: v4 target from v6 source works, exits from host IPv4
IPv6 HTTPS works, v6 alias works, v6 alias works, v6 alias
IPv4 DNS @1.1.1.1 UDP / TCP fails fails works
FakeTLS 194.221.250.50:443, SNI www.google.com dialed www.google.com over v6, reaches real Google dial fails dialed 194.221.250.50:443 from host IPv4

Xray log for the FakeTLS check with both commits:

app/dispatcher: sniffed domain: www.google.com
app/dispatcher: default route for tcp:www.google.com:443
proxy/freedom: connection opened to tcp:194.221.250.50:443, local endpoint <host v4>:45214, remote endpoint 194.221.250.50:443

In production: commit 1 on a server behind which the Telegram hang was observed (no IPv6 peers there). Before the change, every net log had hundreds of TLS hash mismatch. After it, the next hour of logs had none, and the same FakeTLS connection completed (TLS hello complete, connected to 194.221.250.50:443).

Breaking changes

None expected. Flows now go to the IP the client resolved, and routing decisions are unchanged.

  • As with any routeOnly inbound, a freedom outbound's domainStrategy no longer applies to these flows, since their target is an IP.
  • Peers with an IPv6 identity now leave from the server's regular IPv4 for IPv4 targets. Before, those flows either failed or were silently moved to IPv6. Their IPv4 flows now also pass through the admin's own routing rules. Their IPv6 flows still hit the prepended egress rule first, as before.
  • FakeDNS flows are unaffected by routeOnly, because the dispatcher rewrites fake-pool targets to the domain in any case. With domainStrategy: AsIs, such a flow does not match ::/0 and takes the regular outbound. On main, the same flow failed whenever the domain was IPv4-only.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass. (N/A)
  • I updated the Wiki / README / API docs if user-facing behavior changed. (N/A)
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6654 **Author:** [@rudenko-ks](https://github.com/rudenko-ks) **Created:** 9/26/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/amneziawg-relay-routeonly` --- ### 📝 Commits (3) - [`3ca9c20`](https://github.com/MHSanaei/3x-ui/commit/3ca9c2069699788e9e08739785385ef9ebb3b86e) fix(amneziawg): sniff the relay with routeOnly - [`e200037`](https://github.com/MHSanaei/3x-ui/commit/e2000372b3010a9502877f63ca892273f0e079b8) fix(amneziawg): send only IPv6 targets through the peer's v6 egress - [`5adae14`](https://github.com/MHSanaei/3x-ui/commit/5adae14303b892aded00ec746826ca49e5eff0bd) Merge branch 'main' into fix/amneziawg-relay-routeonly ### 📊 Changes **2 files changed** (+34 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/xray.go` (+5 -12) 📝 `internal/web/service/xray_config_inject_test.go` (+29 -0) </details> ### 📄 Description ## Summary Two small fixes to the embedded AmneziaWG relay's Xray config: 1. Sniff the relay inbound with `routeOnly: true`, so the sniffed domain drives routing only and the connection still goes to the IP the peer asked for. 2. Limit the per-peer IPv6 egress rule to IPv6 destinations (`"ip": ["::/0"]`), because its `sendThrough` is an IPv6 address and cannot dial an IPv4 target. ## Why **1. Sniffing rewrote the destination.** `amneziawgEgressSniffingSettings` (added in #6105) turns on sniffing so that domain-based Routing rules can match traffic that arrives as bare IPs. Without `routeOnly`, it also replaces the dial target with the sniffed SNI/Host. That breaks any client that connects to one IP while presenting another name. Telegram does exactly this. When its main connection drops, its anti-censorship recovery opens FakeTLS to `194.221.250.50:443` with SNI `www.google.com`. The relay re-addressed that connection to `www.google.com`, so it reached real Google. Telegram then looped on it: its net log showed **450 `TLS hash mismatch`** in 20 minutes while the app sat on "Connecting…" until it was killed. **2. The IPv6 egress took IPv4 flows it cannot dial.** For a peer with an IPv6 AllowedIP and aliases active, `injectAmneziawgV6Egress` routes *all* of that peer's traffic (`inboundTag` + `user`) to a freedom outbound with `sendThrough: <peer v6>`. An IPv4 target then fails with `dial tcp: address [v6]: mismatched local address type`. This already broke every IPv4 flow that sniffing does not recognise, such as DNS to `1.1.1.1` over UDP or TCP, and SSH. Sniffed TLS/HTTP only worked because of the rewrite in (1): the domain was dialed over IPv6. With (1) fixed, IPv4 HTTPS broke as well. That is why both changes ship together. `fakedns` stays in `destOverride`: the dispatcher still maps fake-pool IPs back to their domain regardless of `routeOnly`. ## Type of change - [x] Bug fix ## Areas affected - [x] Xray config generation ## How was this tested? **Unit tests.** `TestInjectAmneziawgnetSocks_SniffingRouteOnly` (new) asserts `routeOnly: true` and the exact `destOverride` list. `TestInjectAmneziawgV6Egress_CreatesOutboundAndRuleForV6Peer` now asserts `ip: ["::/0"]`. Each assertion fails without its fix and passes with it. `go build ./...`, `go test ./internal/web/service -shuffle=on`, `go vet`, `gofumpt` and `golangci-lint` are clean. **Live test.** Setup: - An isolated instance of this branch on an Ubuntu 24.04 server with a routed IPv6 /64. - An AmneziaWG inbound with IPv6 enabled on `ens3`; one peer with `10.77.0.2/32` plus a public `/128` alias from that /64. - A kernel AmneziaWG client on a second host, using source-based policy routing. - The same checks run against three builds: `main`, commit 1 only, and both commits. Dial targets were read from Xray's `info` log. | Check from the peer | `main` | + routeOnly only | + routeOnly + `::/0` | |---|---|---|---| | IPv4 HTTPS (dual-stack site) | works, but dialed **by domain over the v6 alias** | **fails**: v4 target from v6 source | works, exits from host IPv4 | | IPv6 HTTPS | works, v6 alias | works, v6 alias | works, v6 alias | | IPv4 DNS `@1.1.1.1` UDP / TCP | **fails** | **fails** | works | | FakeTLS `194.221.250.50:443`, SNI `www.google.com` | dialed `www.google.com` over v6, reaches real Google | dial fails | dialed `194.221.250.50:443` from host IPv4 | Xray log for the FakeTLS check with both commits: ``` app/dispatcher: sniffed domain: www.google.com app/dispatcher: default route for tcp:www.google.com:443 proxy/freedom: connection opened to tcp:194.221.250.50:443, local endpoint <host v4>:45214, remote endpoint 194.221.250.50:443 ``` **In production:** commit 1 on a server behind which the Telegram hang was observed (no IPv6 peers there). Before the change, every net log had hundreds of `TLS hash mismatch`. After it, the next hour of logs had none, and the same FakeTLS connection completed (`TLS hello complete`, `connected to 194.221.250.50:443`). ## Breaking changes None expected. Flows now go to the IP the client resolved, and routing decisions are unchanged. - As with any `routeOnly` inbound, a freedom outbound's `domainStrategy` no longer applies to these flows, since their target is an IP. - Peers with an IPv6 identity now leave from the server's regular IPv4 for IPv4 targets. Before, those flows either failed or were silently moved to IPv6. Their IPv4 flows now also pass through the admin's own routing rules. Their IPv6 flows still hit the prepended egress rule first, as before. - FakeDNS flows are unaffected by `routeOnly`, because the dispatcher rewrites fake-pool targets to the domain in any case. With `domainStrategy: AsIs`, such a flow does not match `::/0` and takes the regular outbound. On `main`, the same flow failed whenever the domain was IPv4-only. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. (N/A) - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. (N/A) - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:28 +07:00
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#6399
No description provided.