Skip to content

Commit 7558c8e

Browse files
committed
url: try local port on both IP stacks
Signed-off-by: Mathieu Tortuyaux <[email protected]>
1 parent 61f109a commit 7558c8e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

internal/resource/url.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io"
2626
"net"
2727
"net/http"
28+
"net/netip"
2829
"net/url"
2930
"os"
3031
"strings"
@@ -339,10 +340,17 @@ func (f *Fetcher) fetchFromHTTP(u url.URL, dest io.Writer, opts FetchOptions) er
339340
p int
340341
)
341342

343+
host := u.Hostname()
344+
addr, _ := netip.ParseAddr(host)
345+
network := "tcp6"
346+
if addr.Is4() {
347+
network = "tcp4"
348+
}
349+
342350
// Assert that the port is not already used.
343351
for {
344352
p = opts.LocalPort()
345-
l, err := net.Listen("tcp4", fmt.Sprintf(":%d", p))
353+
l, err := net.Listen(network, fmt.Sprintf(":%d", p))
346354
if err != nil && errors.Is(err, syscall.EADDRINUSE) {
347355
continue
348356
} else if err == nil {

0 commit comments

Comments
 (0)