diff --git a/https.go b/https.go index a0350d4e..c7b12749 100644 --- a/https.go +++ b/https.go @@ -247,7 +247,13 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request clientTlsReader := bufio.NewReader(rawClientTls) for !isEOF(clientTlsReader) { req, err := http.ReadRequest(clientTlsReader) - ctx := &ProxyCtx{Req: req, Session: atomic.AddInt64(&proxy.sess, 1), Proxy: proxy, UserData: ctx.UserData, RoundTripper: ctx.RoundTripper} + ctx := &ProxyCtx{ + Req: req, + Session: atomic.AddInt64(&proxy.sess, 1), + Proxy: proxy, + UserData: ctx.UserData, + RoundTripper: ctx.RoundTripper, + } if err != nil && !errors.Is(err, io.EOF) { return } @@ -255,7 +261,10 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request ctx.Warnf("Cannot read TLS request from mitm'd client %v %v", r.Host, err) return } - req.RemoteAddr = r.RemoteAddr // since we're converting the request, need to carry over the original connecting IP as well + + // since we're converting the request, need to carry over the + // original connecting IP as well + req.RemoteAddr = r.RemoteAddr ctx.Logf("req %v", r.Host) if !strings.HasPrefix(req.URL.String(), "https://") { @@ -410,7 +419,12 @@ func httpError(w io.WriteCloser, ctx *ProxyCtx, err error) { if ctx.Proxy.ConnectionErrHandler != nil { ctx.Proxy.ConnectionErrHandler(w, ctx, err) } else { - errStr := fmt.Sprintf("HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s", len(err.Error()), err.Error()) + errorMessage := err.Error() + errStr := fmt.Sprintf( + "HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s", + len(errorMessage), + errorMessage, + ) if _, err := io.WriteString(w, errStr); err != nil { ctx.Warnf("Error responding to client: %s", err) } @@ -456,7 +470,10 @@ func (proxy *ProxyHttpServer) NewConnectDialToProxy(httpsProxy string) func(netw return proxy.NewConnectDialToProxyWithHandler(httpsProxy, nil) } -func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(httpsProxy string, connectReqHandler func(req *http.Request)) func(network, addr string) (net.Conn, error) { +func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler( + httpsProxy string, + connectReqHandler func(req *http.Request), +) func(network, addr string) (net.Conn, error) { u, err := url.Parse(httpsProxy) if err != nil { return nil diff --git a/proxy_test.go b/proxy_test.go index 632ed9b0..a52d8092 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -119,10 +119,12 @@ func oneShotProxy(proxy *goproxy.ProxyHttpServer) (client *http.Client, s *httpt func TestSimpleHook(t *testing.T) { proxy := goproxy.NewProxyHttpServer() - proxy.OnRequest(goproxy.SrcIpIs("127.0.0.1")).DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { - req.URL.Path = "/bobo" - return req, nil - }) + proxy.OnRequest(goproxy.SrcIpIs("127.0.0.1")).DoFunc( + func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { + req.URL.Path = "/bobo" + return req, nil + }, + ) client, l := oneShotProxy(proxy) defer l.Close() @@ -213,10 +215,12 @@ func TestOneShotFileServer(t *testing.T) { func TestContentType(t *testing.T) { proxy := goproxy.NewProxyHttpServer() - proxy.OnResponse(goproxy.ContentTypeIs("image/png")).DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { - resp.Header.Set("X-Shmoopi", "1") - return resp - }) + proxy.OnResponse(goproxy.ContentTypeIs("image/png")).DoFunc( + func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { + resp.Header.Set("X-Shmoopi", "1") + return resp + }, + ) client, l := oneShotProxy(proxy) defer l.Close() diff --git a/websocket.go b/websocket.go index 753a1e8d..07c73a3b 100644 --- a/websocket.go +++ b/websocket.go @@ -25,7 +25,13 @@ func isWebSocketRequest(r *http.Request) bool { headerContains(r.Header, "Upgrade", "websocket") } -func (proxy *ProxyHttpServer) serveWebsocketTLS(ctx *ProxyCtx, w http.ResponseWriter, req *http.Request, tlsConfig *tls.Config, clientConn *tls.Conn) { +func (proxy *ProxyHttpServer) serveWebsocketTLS( + ctx *ProxyCtx, + w http.ResponseWriter, + req *http.Request, + tlsConfig *tls.Config, + clientConn *tls.Conn, +) { targetURL := url.URL{Scheme: "wss", Host: req.URL.Host, Path: req.URL.Path} // Connect to upstream @@ -46,7 +52,12 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS(ctx *ProxyCtx, w http.ResponseWr proxy.proxyWebsocket(ctx, targetConn, clientConn) } -func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS(ctx *ProxyCtx, w http.ResponseWriter, req *http.Request, clientConn *tls.Conn) { +func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS( + ctx *ProxyCtx, + w http.ResponseWriter, + req *http.Request, + clientConn *tls.Conn, +) { targetURL := url.URL{Scheme: "ws", Host: req.URL.Host, Path: req.URL.Path} // Connect to upstream @@ -98,7 +109,12 @@ func (proxy *ProxyHttpServer) serveWebsocket(ctx *ProxyCtx, w http.ResponseWrite proxy.proxyWebsocket(ctx, targetConn, clientConn) } -func (proxy *ProxyHttpServer) websocketHandshake(ctx *ProxyCtx, req *http.Request, targetSiteConn io.ReadWriter, clientConn io.ReadWriter) error { +func (proxy *ProxyHttpServer) websocketHandshake( + ctx *ProxyCtx, + req *http.Request, + targetSiteConn io.ReadWriter, + clientConn io.ReadWriter, +) error { // write handshake request to target err := req.Write(targetSiteConn) if err != nil {