File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package model
2+
3+ import (
4+ "fmt"
5+ "net"
6+ "net/http"
7+ "strings"
8+ "time"
9+ )
10+
11+ var HttpClient = & http.Client {
12+ Transport : & localRedirectTransport {
13+ rt : http .DefaultTransport ,
14+ },
15+ Timeout : 5 * time .Second ,
16+ }
17+
18+ // localRedirectTransport rewrites requests to localhost -> host.docker.internal
19+ type localRedirectTransport struct {
20+ rt http.RoundTripper
21+ }
22+
23+ func (t * localRedirectTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
24+ // Rewrite localhost hostnames
25+ if strings .HasPrefix (req .URL .Host , "localhost" ) || strings .HasPrefix (req .URL .Host , "127.0.0.1" ) {
26+ // Keep the port if present
27+ host , port , _ := net .SplitHostPort (req .URL .Host )
28+ if host == "" {
29+ host = req .URL .Host
30+ }
31+ if port != "" {
32+ req .URL .Host = fmt .Sprintf ("host.docker.internal:%s" , port )
33+ } else {
34+ req .URL .Host = "host.docker.internal"
35+ }
36+ }
37+
38+ // Optional: update the Host header so server sees correct host
39+ if req .Host == "" || req .Host == "localhost" || req .Host == "127.0.0.1" {
40+ req .Host = strings .Split (req .URL .Host , ":" )[0 ]
41+ }
42+
43+ return t .rt .RoundTrip (req )
44+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package main
22
33import (
44 "bytes"
5+ "egress-uma/model"
56 "encoding/json"
67 "errors"
78 "io"
You can’t perform that action at this time.
0 commit comments