Skip to content

Commit aa4ffe1

Browse files
authored
Merge pull request #1 from chrxmvtik/main
feat: added transport options to use proxy settings if available
2 parents 01d275d + f0ccb42 commit aa4ffe1

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

main.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"io/ioutil"
1313
"log"
1414
"net"
15+
"net/http"
16+
"net/url"
1517
"os"
1618
"path/filepath"
1719
"strings"
@@ -319,8 +321,11 @@ func main() {
319321
instDir := filepath.Join(tmpDir, "installer")
320322
os.MkdirAll(instDir, 0o755)
321323

324+
transport := setupTransportWithProxy()
325+
opts := crane.WithTransport(transport)
326+
322327
log.Printf("pulling image %s", imageFlag)
323-
img, err := crane.Pull(imageFlag)
328+
img, err := crane.Pull(imageFlag, opts)
324329
must("pull image", err)
325330

326331
log.Print("extracting image layers")
@@ -460,3 +465,23 @@ func setupLoop(path string) (string, *os.File) {
460465
}
461466
return loop, lf
462467
}
468+
469+
/* ---------------- setup transport that respects proxy settings -------------------------------------- */
470+
func setupTransportWithProxy() *http.Transport {
471+
transport := http.DefaultTransport.(*http.Transport).Clone()
472+
transport.Proxy = func(req *http.Request) (*url.URL, error) {
473+
proxyURL, err := http.ProxyFromEnvironment(req)
474+
if err != nil {
475+
log.Printf("Warning: error reading proxy settings: %v", err)
476+
return nil, nil // Fallback to direct connection on error.
477+
}
478+
479+
if proxyURL != nil {
480+
log.Printf("Using proxy: %s", proxyURL.String())
481+
} else {
482+
log.Printf("No proxy configured, using direct connection")
483+
}
484+
return proxyURL, nil
485+
}
486+
return transport
487+
}

0 commit comments

Comments
 (0)