diff --git a/main.go b/main.go index 8c9b403..3da4321 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ type runArgs struct { Email string `flag:"email,Contact email address presented to letsencrypt CA"` Install bool `flag:"install,Installs as a windows service"` Remove bool `flag:"remove,Removes the windows service"` + Debug bool `flag:"debug,Log the file path of requests"` } var ( @@ -68,6 +69,7 @@ var ( }, } proxyCounter int + transport http.RoundTripper ) func main() { @@ -199,6 +201,19 @@ func run() error { log.Fatal("Error loading .env file") } + transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: args.TLSSkipVerify}, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } + mapping, err := readMapping(args.MappingPath) if err != nil { return err @@ -392,17 +407,19 @@ func newSingleHostReverseProxy(target *url.URL, prefix string) *httputil.Reverse req.Header.Set("User-Agent", "") } req.Header.Set("X-Forwarded-Proto", "https") + if args.Debug { + log.Println(req.URL.String()) + } } if args.TLSSkipVerify { return &httputil.ReverseProxy{ - Director: director, - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, + Director: director, + Transport: &proxyTransport{}, } } return &httputil.ReverseProxy{ - Director: director, + Director: director, + Transport: &proxyTransport{}, } } diff --git a/proxy.go b/proxy.go index 18d543d..da511d8 100644 --- a/proxy.go +++ b/proxy.go @@ -60,3 +60,22 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } + +type proxyTransport struct { + CapturedTransport http.RoundTripper +} + +func (t *proxyTransport) RoundTrip(r *http.Request) (*http.Response, error) { + // Use the real transport to execute the request + response, err := transport.RoundTrip(r) + setCookie := response.Header.Get("SET-COOKIE") + if setCookie != "" { + parts := strings.Split(setCookie, ";") + newSetCookie := parts[0] + "; Path=/" + if len(parts) > 2 { + newSetCookie += ";" + parts[2] + } + response.Header.Set("SET-COOKIE", newSetCookie) + } + return response, err +}