fix: allow IPv4 and IPv6 inbounds to share a port (#6603)

* fix: distinguish IPv4 and IPv6 listen conflicts

* fix(ports): let an IPv4 address share a port only with a v6only wildcard

xray listens on tcp/udp, and Go opens every wildcard listen, 0.0.0.0
included, as one dual-stack socket unless sockopt.v6only is set. Treating
:: and 0.0.0.0 as separate families let the panel save pairs the core
then fails to bind, and it broke main's own TestListenOverlaps.

listenOverlaps now takes the inbound's sockopt.v6only: a wildcard claims
both families, or only IPv6 with v6only, so :: with v6only may share its
port with an IPv4 address while a plain :: or 0.0.0.0 still may not.

---------

Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
Xinny Lin
2026-09-27 04:02:22 +08:00
committed by GitHub
parent bd01f923fb
commit f3b100282a
4 changed files with 121 additions and 11 deletions
+27 -1
View File
@@ -114,7 +114,7 @@ func TestListenOverlaps(t *testing.T) {
{"1.2.3.4", "::1", false},
}
for _, c := range cases {
if got := listenOverlaps(c.a, c.b); got != c.want {
if got := listenOverlaps(bindAddr{listen: c.a}, bindAddr{listen: c.b}); got != c.want {
t.Errorf("listenOverlaps(%q, %q) = %v, want %v", c.a, c.b, got, c.want)
}
}
@@ -927,3 +927,29 @@ func TestCheckPortConflict_AmneziawgnetSocksRelayReverseDirectionBlockedOnUpdate
t.Fatalf("awg-1's own derived relay port %d collides with vless-1's real port; must be rejected", relayPort)
}
}
// xray binds "::" dual-stack unless sockopt.v6only is set, so only then may an
// IPv4 address share its port; the flag is read from the saved streamSettings.
func TestCheckPortConflict_V6OnlyWildcardLeavesIPv4AddressFree(t *testing.T) {
for _, tc := range []struct {
name string
stream string
want bool
}{
{"dual-stack", `{"network":"tcp"}`, true},
{"v6only", `{"network":"tcp","sockopt":{"v6only":true}}`, false},
} {
t.Run(tc.name, func(t *testing.T) {
setupConflictDB(t)
seedInboundConflict(t, "vless-v6", "::", 443, model.VLESS, tc.stream, `{}`)
v4 := &model.Inbound{Tag: "vless-v4", Listen: "10.5.0.200", Port: 443, Protocol: model.VLESS, StreamSettings: `{"network":"tcp"}`}
exist, err := (&InboundService{}).checkPortConflict(v4, 0)
if err != nil {
t.Fatalf("checkPortConflict: %v", err)
}
if got := exist != nil; got != tc.want {
t.Fatalf("conflict = %v, want %v", got, tc.want)
}
})
}
}