elazarl / goproxy

An HTTP proxy library for Go
BSD 3-Clause "New" or "Revised" License
6.06k stars 1.1k forks source link

Handle connect hijack & MITM at the same time not working #530

Open k0ngoz opened 8 months ago

k0ngoz commented 8 months ago

I have been unable to use .handleConnect, to hijack the CONNECT requests, and also .DoFunc to be able to access and modify request data.

When using .handleConnect, my .DoFunc function is just not triggered at all.

` p := goproxy.NewProxyHttpServer()

homeDir, err := os.UserHomeDir()
if err != nil {
    log.Fatalf("Error getting home directory: %v", err)
}

caCertPath := filepath.Join(homeDir, ".mitmproxy", "mitmproxy-ca-cert.pem")
caKeyPath := filepath.Join(homeDir, ".mitmproxy", "mitmproxy-ca.pem")

caCert, err := ioutil.ReadFile(caCertPath)
if err != nil {
    log.Fatalf("Error reading CA certificate: %v", err)
}

caKey, err := ioutil.ReadFile(caKeyPath)
if err != nil {
    log.Fatalf("Error reading CA private key: %v", err)
}

goproxyCa, err := tls.X509KeyPair(caCert, caKey)
if err != nil {
    log.Fatalf("Error loading key pair: %v", err)
}

ca := x509.NewCertPool()
ca.AppendCertsFromPEM(caCert)
goproxy.GoproxyCa = goproxyCa

p.OnRequest().HandleConnect(goproxy.FuncHttpsHandler(
    func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
        log.Println("Handling HTTPS CONNECT request for:", host)
        return &goproxy.ConnectAction{
            Action:    goproxy.ConnectHijack,
            TLSConfig: goproxy.TLSConfigFromCA(&goproxyCa),
            Hijack:    getTLSHijackFunc(hj),
        }, host
    }))

// Handle regular HTTP requests
p.OnRequest().DoFunc(
    func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {

        log.Println("Received regular HTTP request:", r.Method, r.URL.String())
        return r, nil
    },
)

log.Fatal(http.ListenAndServe(opts.ListenAddress, p))

`

prafgup commented 3 months ago

Hey @k0ngoz, I have a similar use-case, where one is able to Hijack CONNECT and also able to modify the request data, which currently does not work due to SSL certificate failures.

elazarl commented 2 months ago

@k0ngoz hi, any news?