Skip to content

Commit

Permalink
fixup! resource/url: customize local address (port)
Browse files Browse the repository at this point in the history
  • Loading branch information
tormath1 committed Jan 22, 2024
1 parent a2bdf34 commit 0d427b8
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions internal/resource/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"net/url"
"os"
"strings"
"syscall"
"time"

"cloud.google.com/go/compute/metadata"
Expand Down Expand Up @@ -293,9 +294,23 @@ func (f *Fetcher) fetchFromHTTP(u url.URL, dest io.Writer, opts FetchOptions) er
}

if opts.LocalPort != nil {
var d net.Dialer

d.LocalAddr = &net.TCPAddr{Port: opts.LocalPort()}
var (
d net.Dialer
p int
)

// Assert that the port is not already used.
for {
p = opts.LocalPort()
l, err := net.Listen("tcp4", fmt.Sprintf(":%d", p))
if err != nil && errors.Is(err, syscall.EADDRINUSE) {
p = opts.LocalPort()

Check failure on line 307 in internal/resource/url.go

View workflow job for this annotation

GitHub Actions / Test (1.21.x)

SA4006: this value of `p` is never used (staticcheck)
} else if err == nil {
l.Close()
break
}
}
d.LocalAddr = &net.TCPAddr{Port: p}

f.client.transport.DialContext = d.DialContext
}
Expand Down

0 comments on commit 0d427b8

Please sign in to comment.