elazarl / goproxy

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

golang TSL/SSL proxy or intercept incoming and outgoing request information #400

Open 1683728221 opened 4 years ago

1683728221 commented 4 years ago

Hello there! Can you give a relatively simple TSL/SSL proxy or interception case? Although HTTP proxy can output request information and server return information, there are still differences between http and https protocols, and https is much safer than http. As a novice learning golang language programming, it is too difficult to think about TSL/SSL proxy. I originally wanted to write an agent program and set the local browser to 127.0.0.1:8080, and let the agent program print all the information, similar to the Burp Suite packet capture function. But I have been researching for several days, but I still haven’t been able to implement it. I found that the https protocol is too difficult. I have just started with the golang language, and I can see the code structure if I go deeper. Can you help guide the novice? Thank you, experts. ! ! !

hamza72x commented 3 years ago

Probably not best practise, just a demo...

package main

import (
    "flag"
    "log"
    "net/http"
    "time"

    "github.com/elazarl/goproxy"
    hel "github.com/hamza02x/go-helper"
)

var (
    interceptCheck = make(chan int)
    interceptFile  = "intercept.txt"
)

func init() {
    hel.StrToFile(interceptFile, "")
}

func checkIntercepting() {
    go func() {
        if hel.FileExists(interceptFile) {
            hel.Pl("Still intercepting")
            time.Sleep(300 * time.Millisecond)
            checkIntercepting()
        } else {
            interceptCheck <- 1
        }
    }()
}

func main() {

    http_addr := flag.String("httpaddr", ":3129", "proxy http listen address")
    flag.Parse()

    proxy := goproxy.NewProxyHttpServer()

    proxy.OnRequest().HandleConnectFunc(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
        hel.Pl("Intercepted on ", ctx.Req)
        checkIntercepting()
        <-interceptCheck
        return goproxy.MitmConnect2, host
    })

    log.Fatalln(http.ListenAndServe(*http_addr, proxy))

}

func orPanic(err error) {
    if err != nil {
        panic(err)
    }
}