mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-19 03:25:01 +03:00
fix(amneziawg): account for S4 junk in the default tunnel MTU (#6376)
* fix(amneziawg): account for S4 junk in the default tunnel MTU
amneziawg prepends S4 random bytes to every transport packet
(device.NewOutboundElement) and, unlike content padding and random trailers,
never clamps them against the tunnel MTU. A full-size packet therefore lands on
the wire at MTU + 60 + S4 bytes: 20 IPv4 + 8 UDP + S4 + 16 transport header +
16 poly1305 tag.
With the 1420 default that overflows a 1500-byte link once S4 exceeds 20, and
GenerateObfuscation31 draws S4 from 12..27 inclusive -- so roughly 44% of newly
created inbounds fragment every full-size packet they send.
Measured on a live pair of interfaces, predicted against observed:
MTU 1380 S4 12 -> 1452 on the wire (fits)
MTU 1420 S4 12 -> 1492 (fits)
MTU 1420 S4 20 -> 1500 (exactly at the limit)
MTU 1420 S4 21 -> 1501 (fragments)
MTU 1420 S4 27 -> 1507 (fragments)
EffectiveMTU now subtracts S4 from the default; an explicit MTU is untouched.
Client configs carry the same number. They previously omitted the MTU line
whenever the server had no explicit value, which left the client on its own
1420 default and fragmented the client-to-server direction even after the
server side was fixed -- silently, and only in one direction. All three
emitters (the Go subscription text and the two TypeScript ones) now agree,
which is what the existing parity test exists to protect.
* fix(amneziawg): rebuild the device when S4 changes the derived MTU
Addresses review feedback on the previous commit.
Deriving the default MTU from S4 made a construction-time-only property depend
on a hot-reloadable input, but addressFingerprint -- ensureLocked's only rebuild
trigger -- still hashed the raw inst.MTU. S4 is a UAPI field, so an S4-only edit
took the in-place IpcSet branch and the gVisor netstack kept the MTU derived
from the old S4 while all three client emitters already advertised the new one.
Every panel-created inbound leaves mtu unset, so that was the normal case, not
an edge one: with S4 raised far enough the fragmentation this fix exists to
remove came straight back, and stayed until a panel restart or an unrelated
address edit.
Folding EffectiveMTU into the fingerprint fixes it. An explicit MTU still takes
the in-place branch on an S4 edit, since it does not move the interface MTU.
Also trims four comment blocks to the 2-line cap in CLAUDE.md, and points
NewDevice's doc comment at EffectiveMTU instead of the deleted defaultMTU.
This commit is contained in:
@@ -13,11 +13,6 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
|
||||
)
|
||||
|
||||
// defaultMTU matches internal/amneziawg's own kernel-module interface
|
||||
// default -- 1420, WireGuard/AmneziaWG's usual accounting for tunnel
|
||||
// encapsulation overhead on a standard 1500-byte-MTU host link.
|
||||
const defaultMTU = 1420
|
||||
|
||||
// DeviceOptions carries AmneziaWG 3.0's device-wide fields (header
|
||||
// protection, content padding, and the five session-timing knobs) --
|
||||
// mirrored from amneziawg.Instance's identically named fields by every
|
||||
@@ -78,7 +73,7 @@ type Device struct {
|
||||
|
||||
// NewDevice constructs, configures, and brings up an embedded AmneziaWG
|
||||
// interface for inst in one call: a gVisor-backed tun.Device sized to
|
||||
// inst.MTU (or defaultMTU), addressed with inst.Address, configured via
|
||||
// amneziawg.EffectiveMTU, addressed with inst.Address, configured via
|
||||
// UAPI with inst.Obfuscation, inst.PrivateKey, opts' AWG 3.0 fields, and one
|
||||
// UAPI peer per inst.Peers entry. It does not attach a forwarder or start
|
||||
// relaying traffic -- that's the caller's job (see AttachTCPForwarder /
|
||||
@@ -122,10 +117,7 @@ func newUnconfiguredDevice(inst amneziawg.Instance, opts DeviceOptions) (*Device
|
||||
return nil, fmt.Errorf("amneziawgnet: %w", err)
|
||||
}
|
||||
|
||||
mtu := inst.MTU
|
||||
if mtu <= 0 {
|
||||
mtu = defaultMTU
|
||||
}
|
||||
mtu := amneziawg.EffectiveMTU(inst.MTU, inst.Obfuscation.S4)
|
||||
|
||||
tun, gstack, err := createNetTUNWithStack(addrs, mtu)
|
||||
if err != nil {
|
||||
|
||||
@@ -126,9 +126,10 @@ func (m *Manager) Ensure(d Desired) error {
|
||||
// tearing down every peer's live handshake/session state on every single
|
||||
// reconcile, so no connection could ever survive past one tick); only
|
||||
// peers/obfuscation/keys/listen_port changed (reconfigure the existing
|
||||
// Device in place via IpcSet); or the interface's own address(es)/MTU
|
||||
// changed (these are fixed at netstack-construction time, so the only
|
||||
// option is closing the old Device and building a fresh one).
|
||||
// Device in place via IpcSet); or the interface's own address(es)/effective
|
||||
// MTU changed -- S4 counts, the default MTU derives from it (these are fixed
|
||||
// at netstack-construction time, so the only option is closing the old
|
||||
// Device and building a fresh one).
|
||||
func (m *Manager) ensureLocked(d Desired) error {
|
||||
inst, opts := d.Instance, d.Options
|
||||
if opts.Logger == nil {
|
||||
@@ -256,12 +257,12 @@ func socksRelayForInstance(inst amneziawg.Instance) SocksRelay {
|
||||
}
|
||||
}
|
||||
|
||||
// addressFingerprint captures the two Instance fields that can't be changed
|
||||
// on a running Device via IpcSet alone (they're fixed when the gVisor
|
||||
// netstack is built) -- everything else (keys, listen port, obfuscation,
|
||||
// AWG 3.0 options, peers) amneziawg-go's own UAPI can hot-reconfigure.
|
||||
// addressFingerprint captures what IpcSet can't change on a running Device,
|
||||
// fixed when the netstack is built: address, and the S4-derived effective MTU.
|
||||
func addressFingerprint(inst amneziawg.Instance) string {
|
||||
return fmt.Sprintf("%d|%s", inst.MTU, strings.Join(inst.Address, ","))
|
||||
return fmt.Sprintf("%d|%s",
|
||||
amneziawg.EffectiveMTU(inst.MTU, inst.Obfuscation.S4),
|
||||
strings.Join(inst.Address, ","))
|
||||
}
|
||||
|
||||
// Reconcile brings every desired instance's embedded interface up to date
|
||||
|
||||
@@ -90,6 +90,69 @@ func TestManagerLifecycle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// An inbound with no explicit MTU derives it from S4, so an S4-only edit is
|
||||
// structural: leave it out of the fingerprint and the netstack keeps the old MTU
|
||||
// while every client emitter already advertises the new one.
|
||||
func TestEnsureRebuildsWhenS4ChangesTheDerivedMTU(t *testing.T) {
|
||||
priv, pub, err := wireguard.GenerateWireguardKeypair()
|
||||
if err != nil {
|
||||
t.Fatalf("generate keypair: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mtu int
|
||||
wantRebuild bool
|
||||
}{
|
||||
{"derived MTU", 0, true},
|
||||
{"explicit MTU", 1420, false},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m := &Manager{ifaces: map[int]*managed{}}
|
||||
defer m.StopAll()
|
||||
|
||||
inst := amneziawg.Instance{
|
||||
Id: 9 + i,
|
||||
InterfaceName: fmt.Sprintf("awgtest%d", 9+i),
|
||||
ListenPort: 58719 + i,
|
||||
PrivateKey: priv,
|
||||
PublicKey: pub,
|
||||
Address: []string{"10.209.0.1/24"},
|
||||
MTU: tt.mtu,
|
||||
Obfuscation: amneziawg.Obfuscation31{
|
||||
Jc: 4, Jmin: 40, Jmax: 70,
|
||||
S1: 20, S2: 30, S3: 20, S4: 5,
|
||||
},
|
||||
}
|
||||
if err := m.Ensure(Desired{Instance: inst}); err != nil {
|
||||
t.Fatalf("Ensure (create): %v", err)
|
||||
}
|
||||
before, _, ok := m.Lookup(inst.Id)
|
||||
if !ok {
|
||||
t.Fatal("Lookup after create: not found")
|
||||
}
|
||||
|
||||
edited := inst
|
||||
edited.Obfuscation.S4 = 27
|
||||
if err := m.Ensure(Desired{Instance: edited}); err != nil {
|
||||
t.Fatalf("Ensure (S4 changed): %v", err)
|
||||
}
|
||||
after, _, ok := m.Lookup(inst.Id)
|
||||
if !ok {
|
||||
t.Fatal("Lookup after S4 edit: not found")
|
||||
}
|
||||
|
||||
if rebuilt := before != after; rebuilt != tt.wantRebuild {
|
||||
t.Errorf("S4 5->27 rebuilt the Device = %v, want %v (MTU %d -> %d)",
|
||||
rebuilt, tt.wantRebuild,
|
||||
amneziawg.EffectiveMTU(inst.MTU, inst.Obfuscation.S4),
|
||||
amneziawg.EffectiveMTU(edited.MTU, edited.Obfuscation.S4))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedUDPHandlerDoesNotWaitForManagerLock(t *testing.T) {
|
||||
cur := &managed{udpRelay: NewUDPRelay(SocksRelay{Addr: "invalid"}, nil)}
|
||||
cur.peers.Store(NewPeerIndex([]amneziawg.Peer{{
|
||||
|
||||
Reference in New Issue
Block a user