Skip to content

Commit 730a144

Browse files
committed
HTTP/3 fix UDP port availability check
1 parent e27d3d6 commit 730a144

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

internal/net/net.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ func IsPortAvailable(p int) bool {
3939
return err == nil
4040
}
4141

42+
// IsUDPPortAvailable checks if a UDP port is available or not
43+
func IsUDPPortAvailable(p int) bool {
44+
ln, err := _net.ListenPacket("udp", fmt.Sprintf(":%v", p))
45+
defer func() {
46+
if ln != nil {
47+
ln.Close()
48+
}
49+
}()
50+
return err == nil
51+
}
52+
4253
// IsIPv6Enabled checks if IPV6 is enabled or not and we have
4354
// at least one configured in the pod
4455
func IsIPv6Enabled() bool {

internal/net/net_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,20 @@ func TestIsPortAvailable(t *testing.T) {
5858
t.Fatalf("expected port %v to not be available", p)
5959
}
6060
}
61+
62+
func TestIsUDPPortAvailable(t *testing.T) {
63+
if !IsUDPPortAvailable(0) {
64+
t.Fatal("expected port 0 to be available (random port) but returned false")
65+
}
66+
67+
ln, err := net.ListenPacket("udp", ":0") //nolint:gosec // Ignore the gosec error in testing
68+
if err != nil {
69+
t.Fatalf("unexpected error: %v", err)
70+
}
71+
defer ln.Close()
72+
73+
p := ln.LocalAddr().(*net.UDPAddr).Port
74+
if IsUDPPortAvailable(p) {
75+
t.Fatalf("expected port %v to not be available", p)
76+
}
77+
}

pkg/flags/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ https://blog.maxmind.com/2019/12/significant-changes-to-accessing-and-using-geol
275275
return false, nil, fmt.Errorf("port %v is already in use. Please check the flag --https-port", *httpsPort)
276276
}
277277

278-
if !ing_net.IsPortAvailable(*quicPort) {
278+
if !ing_net.IsUDPPortAvailable(*quicPort) {
279279
return false, nil, fmt.Errorf("port %v is already in use. Please check the flag --quic-port", *quicPort)
280280
}
281281

0 commit comments

Comments
 (0)