Skip to content

Commit

Permalink
Fix line length linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikPelli committed Dec 23, 2024
1 parent f6daee9 commit a5bd389
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
25 changes: 21 additions & 4 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,24 @@ 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
}
if err != nil {
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://") {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
22 changes: 19 additions & 3 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a5bd389

Please # to comment.