Skip to content

Commit

Permalink
Change the Path of Cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewise-tryan committed Nov 13, 2019
1 parent 6bdaa46 commit 02269e1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -68,6 +69,7 @@ var (
},
}
proxyCounter int
transport http.RoundTripper
)

func main() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{},
}
}

Expand Down
19 changes: 19 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 02269e1

Please sign in to comment.