Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

providers: add scaleway #1794

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/resource/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"fmt"
"hash"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"syscall"
"time"

"cloud.google.com/go/compute/metadata"
Expand Down Expand Up @@ -125,6 +127,10 @@ type FetchOptions struct {
// HTTPVerb is an HTTP request method to indicate the desired action to
// be performed for a given resource.
HTTPVerb string

// LocalPort is a function returning a local port used to establish the TCP connection.
// Most of the time, letting the Kernel choose a random port is enough.
LocalPort func() int
}

// FetchToBuffer will fetch the given url into a temporary file, and then read
Expand Down Expand Up @@ -287,6 +293,28 @@ func (f *Fetcher) fetchFromHTTP(u url.URL, dest io.Writer, opts FetchOptions) er
}
}

if opts.LocalPort != nil {
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) {
continue
} else if err == nil {
l.Close()
break
}
}
d.LocalAddr = &net.TCPAddr{Port: p}

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

// We do not want to redirect HTTP headers
f.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
req.Header = make(http.Header)
Expand Down