Skip to content

Commit f0ccb42

Browse files
committed
feat: added transport options to use proxy settings if available
Signed-off-by: Aleksander Brajer-Wiaderek <a.brajer-wiaderek@outlook.com>
1 parent 22f8604 commit f0ccb42

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

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"
@@ -313,8 +315,11 @@ func main() {
313315
instDir := filepath.Join(tmpDir, "installer")
314316
os.MkdirAll(instDir, 0o755)
315317

318+
transport := setupTransportWithProxy()
319+
opts := crane.WithTransport(transport)
320+
316321
log.Printf("pulling image %s", imageFlag)
317-
img, err := crane.Pull(imageFlag)
322+
img, err := crane.Pull(imageFlag, opts)
318323
must("pull image", err)
319324

320325
log.Print("extracting image layers")
@@ -454,3 +459,23 @@ func setupLoop(path string) (string, *os.File) {
454459
}
455460
return loop, lf
456461
}
462+
463+
/* ---------------- setup transport that respects proxy settings -------------------------------------- */
464+
func setupTransportWithProxy() *http.Transport {
465+
transport := http.DefaultTransport.(*http.Transport).Clone()
466+
transport.Proxy = func(req *http.Request) (*url.URL, error) {
467+
proxyURL, err := http.ProxyFromEnvironment(req)
468+
if err != nil {
469+
log.Printf("Warning: error reading proxy settings: %v", err)
470+
return nil, nil // Fallback to direct connection on error.
471+
}
472+
473+
if proxyURL != nil {
474+
log.Printf("Using proxy: %s", proxyURL.String())
475+
} else {
476+
log.Printf("No proxy configured, using direct connection")
477+
}
478+
return proxyURL, nil
479+
}
480+
return transport
481+
}

0 commit comments

Comments
 (0)