Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pipelines #595

Merged
merged 15 commits into from
Dec 23, 2024
Prev Previous commit
Next Next commit
Fix line length linting issues
ErikPelli committed Dec 23, 2024
commit a5bd389c09303948343b1f8f6bc62b616daab554
25 changes: 21 additions & 4 deletions https.go
Original file line number Diff line number Diff line change
@@ -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://") {
@@ -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
20 changes: 12 additions & 8 deletions proxy_test.go
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 19 additions & 3 deletions websocket.go
Original file line number Diff line number Diff line change
@@ -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 {