[PR #6320] [MERGED] feat(amneziawg): add AmneziaWG as an outbound protocol #6218

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6320
Author: @rqzbeh
Created: 8/25/2026
Status: ✅ Merged
Merged: 9/10/2026
Merged by: @MHSanaei

Base: main ← Head: feat/amneziawg-outbound-v2


📝 Commits (9)

  • 43be40c feat(amneziawg): add AmneziaWG as an outbound protocol
  • b6df3f6 Merge branch 'main' into feat/amneziawg-outbound-v2
  • 458499a fix(amneziawg): resolve IPv6-only DNS default fallback and validate required keys
  • b79e2f9 Merge branch 'main' into feat/amneziawg-outbound-v2
  • c259a19 Merge remote-tracking branch 'upstream/main' into feat/amneziawg-outbound-v2
  • 5973f41 Merge remote-tracking branch 'origin/feat/amneziawg-outbound-v2' into feat/amneziawg-outbound-v2
  • 15b8494 Merge branch 'main' into feat/amneziawg-outbound-v2
  • 4ac2fd3 Merge branch 'main' into feat/amneziawg-outbound-v2
  • 47f29b6 Merge branch 'main' into feat/amneziawg-outbound-v2

📊 Changes

50 files changed (+4169 additions, -33 deletions)

View changed files

📝 docs/architecture.md (+27 -0)
📝 frontend/.storybook/preview-head.html (+3 -0)
📝 frontend/src/hooks/useXraySetting.ts (+8 -1)
📝 frontend/src/lib/xray/outbound-form-adapter.ts (+106 -0)
📝 frontend/src/pages/xray/outbounds/OutboundFormModal.tsx (+2 -0)
➕ frontend/src/pages/xray/outbounds/protocols/amneziawg.tsx (+228 -0)
📝 frontend/src/pages/xray/outbounds/protocols/index.ts (+1 -0)
📝 frontend/src/schemas/forms/outbound-form.ts (+7 -0)
📝 frontend/src/schemas/primitives/outbound-protocol.ts (+1 -0)
➕ frontend/src/schemas/protocols/outbound/amneziawg.ts (+49 -0)
📝 frontend/src/schemas/protocols/outbound/index.ts (+3 -0)
➕ frontend/src/test/amneziawg-outbound-adapter.test.ts (+95 -0)
➕ internal/amneziawg/outbound.go (+323 -0)
➕ internal/amneziawg/outbound_test.go (+312 -0)
➕ internal/amneziawgnet/client_device.go (+139 -0)
➕ internal/amneziawgnet/client_device_test.go (+117 -0)
📝 internal/amneziawgnet/device.go (+9 -3)
➕ internal/amneziawgnet/dns.go (+216 -0)
➕ internal/amneziawgnet/egress.go (+652 -0)
➕ internal/amneziawgnet/egress_domain_test.go (+741 -0)

...and 30 more files

📄 Description

Summary

Adds AmneziaWG (AWG) as an outbound option — the outbound counterpart of the embedded AmneziaWG inbound support. An admin can declare an amneziawg outbound in the Xray settings; the panel runs an amneziawg-go client device for it and routes Xray's traffic through that tunnel.

Supersedes #6309 (same feature, one squashed commit, all review findings fixed).

Why

Closes #6302. Hardened cascading panel-to-panel tunnels need AmneziaWG on the egress side; today AWG is inbound-only (#6105), and hanging the obfuscation fields off the stock WireGuard outbound cannot work because Xray-core has no AWG proxy — outbound JSON is handed to the core verbatim.

The design mirrors the inbound engine one-for-one: an embedded amneziawg-go device over a gVisor netstack inside the panel process (no kernel module, nothing new to install). The generated config swaps each amneziawg outbound to a loopback SOCKS5 bridge pointed at the panel's own egress server, which dials into that outbound's device.

Review fixes baked in

Every finding from the #6309 review is fixed here, with tests:

  • socks settings key: flat-form address/port/user/pass (addr is not a key xray-core understands; nil Address refuses the whole generated config)
  • UAPI peer key: persistent_keepalive_interval, not _seconds (unknown keys make IpcSet hard-error, so peers never installed); exact emitted line pinned by a table test
  • RFC 1929 sub-negotiation: VER byte consumed before ULEN (username was mis-parsed, deadlocking both sides)
  • SOCKS password enforced via hmac.Equal against the process-wide secret instead of being read and discarded
  • UDP ASSOCIATE bidirectional: per-destination connected gonet.DialUDP endpoints with stack-assigned source ports + reply pump wrapping responses in SOCKS5 UDP headers; associated client's source address pinned per RFC 1928
  • listener shutdown races: accept loop takes the listener as an argument (no unlocked field read), closing is signalled before ln.Close(), accepted conns tracked and closed on Close() (no hang on healthy flows), Listen re-arms its own channel
  • stale stack registrations: failed rebuilds and StopAll deregister from the egress server, so closed devices are never served
  • allowedIPs normalization at the parse boundary; validation no longer trims (a stray \n silently truncates IpcSet's peer set); i1–i5 pass ValidateConfigValue
  • frontend hint uses Form.Item extra instead of inert Tailwind utility classes

How was this tested?

Backend:

  • New internal/amneziawgnet/client_device_test.go: UAPI keepalive key asserted exactly (fails without the fix — verified by temporary revert)
  • Extended internal/amneziawg/outbound_test.go: newline-in-allowedIPs rejected at validation, normalized at parse boundary, unparseable entries rejected, i1–i5 control-char matrix
  • Updated xray_amneziawg_outbound_test.go: asserts address key + non-empty password
  • Full go test -shuffle=on -count=1 ./internal/... green locally (one pre-existing flake in TestAcquireUpdateSlotConcurrency reproduced on a clean tree too) and targeted suites green on a second machine (Go 1.27 linux/amd64)
  • go test -race ./internal/amneziawgnet/... clean; go vet clean

Frontend:

  • typecheck / oxlint / oxfmt green; vitest suites incl. adapter + i18n-dead-keys green

Type of change

  • New feature
  • Bug fix
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

🔄 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/6320 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 8/25/2026 **Status:** ✅ Merged **Merged:** 9/10/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/amneziawg-outbound-v2` --- ### 📝 Commits (9) - [`43be40c`](https://github.com/MHSanaei/3x-ui/commit/43be40c5f9bc4e2eabafb204b2f32bd8d0774020) feat(amneziawg): add AmneziaWG as an outbound protocol - [`b6df3f6`](https://github.com/MHSanaei/3x-ui/commit/b6df3f641da56ed5f7534f40435314fdaccfcdb9) Merge branch 'main' into feat/amneziawg-outbound-v2 - [`458499a`](https://github.com/MHSanaei/3x-ui/commit/458499a357b428ff450a7a228f63fdda0d2ebea7) fix(amneziawg): resolve IPv6-only DNS default fallback and validate required keys - [`b79e2f9`](https://github.com/MHSanaei/3x-ui/commit/b79e2f9eb2c5ab743eea6795b54e6837ba17f9aa) Merge branch 'main' into feat/amneziawg-outbound-v2 - [`c259a19`](https://github.com/MHSanaei/3x-ui/commit/c259a19647593eff8b9eb9cb4b8248a5e7397b56) Merge remote-tracking branch 'upstream/main' into feat/amneziawg-outbound-v2 - [`5973f41`](https://github.com/MHSanaei/3x-ui/commit/5973f411c2e5e6094489b8e54ddd9354af7d734f) Merge remote-tracking branch 'origin/feat/amneziawg-outbound-v2' into feat/amneziawg-outbound-v2 - [`15b8494`](https://github.com/MHSanaei/3x-ui/commit/15b84948af3452b8e3dc9d85b889607cafdfbbbc) Merge branch 'main' into feat/amneziawg-outbound-v2 - [`4ac2fd3`](https://github.com/MHSanaei/3x-ui/commit/4ac2fd3c13175733649ef7ce566be75e873f8a97) Merge branch 'main' into feat/amneziawg-outbound-v2 - [`47f29b6`](https://github.com/MHSanaei/3x-ui/commit/47f29b6ca7a1c7e4cfacc43a6df6f1b5598ae9ae) Merge branch 'main' into feat/amneziawg-outbound-v2 ### 📊 Changes **50 files changed** (+4169 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `docs/architecture.md` (+27 -0) 📝 `frontend/.storybook/preview-head.html` (+3 -0) 📝 `frontend/src/hooks/useXraySetting.ts` (+8 -1) 📝 `frontend/src/lib/xray/outbound-form-adapter.ts` (+106 -0) 📝 `frontend/src/pages/xray/outbounds/OutboundFormModal.tsx` (+2 -0) ➕ `frontend/src/pages/xray/outbounds/protocols/amneziawg.tsx` (+228 -0) 📝 `frontend/src/pages/xray/outbounds/protocols/index.ts` (+1 -0) 📝 `frontend/src/schemas/forms/outbound-form.ts` (+7 -0) 📝 `frontend/src/schemas/primitives/outbound-protocol.ts` (+1 -0) ➕ `frontend/src/schemas/protocols/outbound/amneziawg.ts` (+49 -0) 📝 `frontend/src/schemas/protocols/outbound/index.ts` (+3 -0) ➕ `frontend/src/test/amneziawg-outbound-adapter.test.ts` (+95 -0) ➕ `internal/amneziawg/outbound.go` (+323 -0) ➕ `internal/amneziawg/outbound_test.go` (+312 -0) ➕ `internal/amneziawgnet/client_device.go` (+139 -0) ➕ `internal/amneziawgnet/client_device_test.go` (+117 -0) 📝 `internal/amneziawgnet/device.go` (+9 -3) ➕ `internal/amneziawgnet/dns.go` (+216 -0) ➕ `internal/amneziawgnet/egress.go` (+652 -0) ➕ `internal/amneziawgnet/egress_domain_test.go` (+741 -0) _...and 30 more files_ </details> ### 📄 Description ## Summary Adds AmneziaWG (AWG) as an **outbound** option — the outbound counterpart of the embedded AmneziaWG inbound support. An admin can declare an `amneziawg` outbound in the Xray settings; the panel runs an amneziawg-go client device for it and routes Xray's traffic through that tunnel. Supersedes #6309 (same feature, one squashed commit, all review findings fixed). ## Why Closes #6302. Hardened cascading panel-to-panel tunnels need AmneziaWG on the egress side; today AWG is inbound-only (#6105), and hanging the obfuscation fields off the stock WireGuard outbound cannot work because Xray-core has no AWG proxy — outbound JSON is handed to the core verbatim. The design mirrors the inbound engine one-for-one: an embedded amneziawg-go device over a gVisor netstack inside the panel process (no kernel module, nothing new to install). The generated config swaps each `amneziawg` outbound to a loopback SOCKS5 bridge pointed at the panel's own egress server, which dials into that outbound's device. ## Review fixes baked in Every finding from the #6309 review is fixed here, with tests: - **socks settings key**: flat-form `address`/`port`/`user`/`pass` (`addr` is not a key xray-core understands; nil `Address` refuses the whole generated config) - **UAPI peer key**: `persistent_keepalive_interval`, not `_seconds` (unknown keys make `IpcSet` hard-error, so peers never installed); exact emitted line pinned by a table test - **RFC 1929 sub-negotiation**: VER byte consumed before ULEN (username was mis-parsed, deadlocking both sides) - **SOCKS password enforced** via `hmac.Equal` against the process-wide secret instead of being read and discarded - **UDP ASSOCIATE bidirectional**: per-destination connected `gonet.DialUDP` endpoints with stack-assigned source ports + reply pump wrapping responses in SOCKS5 UDP headers; associated client's source address pinned per RFC 1928 - **listener shutdown races**: accept loop takes the listener as an argument (no unlocked field read), closing is signalled before `ln.Close()`, accepted conns tracked and closed on `Close()` (no hang on healthy flows), `Listen` re-arms its own channel - **stale stack registrations**: failed rebuilds and `StopAll` deregister from the egress server, so closed devices are never served - **allowedIPs normalization** at the parse boundary; validation no longer trims (a stray `\n` silently truncates IpcSet's peer set); `i1`–`i5` pass `ValidateConfigValue` - **frontend hint** uses `Form.Item extra` instead of inert Tailwind utility classes ## How was this tested? Backend: - New `internal/amneziawgnet/client_device_test.go`: UAPI keepalive key asserted exactly (fails without the fix — verified by temporary revert) - Extended `internal/amneziawg/outbound_test.go`: newline-in-allowedIPs rejected at validation, normalized at parse boundary, unparseable entries rejected, i1–i5 control-char matrix - Updated `xray_amneziawg_outbound_test.go`: asserts `address` key + non-empty password - Full `go test -shuffle=on -count=1 ./internal/...` green locally (one pre-existing flake in `TestAcquireUpdateSlotConcurrency` reproduced on a clean tree too) and targeted suites green on a second machine (Go 1.27 linux/amd64) - `go test -race ./internal/amneziawgnet/...` clean; `go vet` clean Frontend: - typecheck / oxlint / oxfmt green; vitest suites incl. adapter + i18n-dead-keys green ## Type of change - [x] New feature - [ ] Bug fix - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [x] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:11 +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#6218
No description provided.