fix(amneziawg): fall back to a free egress port when 64900 is refused

The panel's SOCKS5 egress for AmneziaWG outbounds bound the fixed
127.0.0.1:64900, and every generated socks bridge dialed that constant.
64900 sits inside Windows' dynamic port range, where the OS can reserve
whole blocks (this host excludes 64885-64984), so on the Windows builds
release.yml ships the listener could stay down and every AmneziaWG
outbound with it. Listen now tries 64900 first and falls back to any
free loopback port; bridges, the outbound probe and the port-conflict
check use EgressPort(), the port actually held. Bridges are generated
apart from the listener, so BuildSocksBridge records the port it wrote
and the AmneziaWG job requests an Xray restart while the listener holds
a different one. Where 64900 is free nothing changes.

The job's restart request is two lines of wiring no test reaches; the
staleness it acts on is pinned by
TestBridgesStaleUntilRegeneratedForTheBoundPort.
This commit is contained in:
MHSanaei
2026-09-27 16:09:54 +02:00
parent 8f47b53879
commit 75f3702dd3
10 changed files with 185 additions and 22 deletions
@@ -10,10 +10,10 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
)
// egressPortBound reports whether 127.0.0.1:<EgressBasePort> accepts TCP.
// egressPortBound reports whether the egress server's Port accepts TCP.
func egressPortBound(t *testing.T) bool {
t.Helper()
conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", itoa(int(EgressBasePort))), 500*time.Millisecond)
conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", itoa(GetEgressServer().Port())), 500*time.Millisecond)
if err != nil {
return false
}
@@ -54,7 +54,7 @@ func newTestOutboundDesired(t *testing.T, tag string) OutboundDesired {
}
// TestOutboundManagerReconcileEmptyDesiredClosesEgress verifies that an empty
// desired set tears down interfaces and releases 127.0.0.1:64900.
// desired set tears down interfaces and releases the egress port.
func TestOutboundManagerReconcileEmptyDesiredClosesEgress(t *testing.T) {
m := &OutboundManager{iface: map[string]*managedOutbound{}}
defer m.Reconcile(nil)
@@ -72,13 +72,14 @@ func TestOutboundManagerReconcileEmptyDesiredClosesEgress(t *testing.T) {
if !egressPortBound(t) {
t.Fatal("egress port not bound after Reconcile with a desired outbound")
}
held := GetEgressServer().Port()
// Empty: listener must be released so other listeners can take the port.
m.Reconcile(nil)
if egressPortBound(t) {
t.Fatal("egress port still bound after Reconcile(nil)")
}
ln, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", itoa(int(EgressBasePort))))
ln, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", itoa(held)))
if err != nil {
t.Fatalf("egress port must be free after Reconcile(nil): %v", err)
}
@@ -102,6 +103,7 @@ func TestEgressServerCloseDuringConcurrentAccepts(t *testing.T) {
if err := srv.Listen(); err != nil {
t.Fatal(err)
}
addr := net.JoinHostPort("127.0.0.1", itoa(srv.Port()))
stop := make(chan struct{})
done := make(chan struct{})
@@ -113,7 +115,7 @@ func TestEgressServerCloseDuringConcurrentAccepts(t *testing.T) {
case <-stop:
return
default:
c, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", itoa(int(EgressBasePort))), 50*time.Millisecond)
c, err := net.DialTimeout("tcp", addr, 50*time.Millisecond)
if err == nil {
clientWg.Add(1)
go func(conn net.Conn) {