Go HTTP proxy server library
Package httpproxy provides a customizable HTTP proxy; supports HTTP, HTTPS through
CONNECT. And also provides HTTPS connection using "Man in the Middle" style
attack.
It's easy to use. httpproxy.Proxy
implements Handler
interface of net/http
package to offer http.ListenAndServe
function.
Installing
go get -u github.com/go-httpproxy/httpproxy
go get -u gopkg.in/httpproxy.v1
Usage
Library has two significant structs: Proxy and Context.
Proxy struct
type Proxy struct {
SessionNo int64
Rt http.RoundTripper
Ca tls.Certificate
UserData interface{}
OnError func(ctx *Context, where string, err *Error, opErr error)
OnAccept func(ctx *Context, w http.ResponseWriter, r *http.Request) bool
OnAuth func(ctx *Context, authType string, user string, pass string) bool
OnConnect func(ctx *Context, host string) (ConnectAction ConnectAction,
newHost string)
OnRequest func(ctx *Context, req *http.Request) (resp *http.Response)
OnResponse func(ctx *Context, req *http.Request, resp *http.Response)
MitmChunked bool
AuthType string
}
Context struct
type Context struct {
Prx *Proxy
SessionNo int64
SubSessionNo int64
Req *http.Request
ConnectReq *http.Request
ConnectAction ConnectAction
ConnectHost string
UserData interface{}
}
Examples
For more examples, examples/
examples/go-httpproxy-simple
package main
import (
"log"
"net/http"
"github.com/go-httpproxy/httpproxy"
)
func OnError(ctx *httpproxy.Context, where string,
err *httpproxy.Error, opErr error) {
log.Printf("ERR: %s: %s [%s]", where, err, opErr)
}
func OnAccept(ctx *httpproxy.Context, w http.ResponseWriter,
r *http.Request) bool {
if r.Method == "GET" && !r.URL.IsAbs() && r.URL.Path == "/info" {
w.Write([]byte("This is go-httpproxy."))
return true
}
return false
}
func OnAuth(ctx *httpproxy.Context, authType string, user string, pass string) bool {
if user == "test" && pass == "1234" {
return true
}
return false
}
func OnConnect(ctx *httpproxy.Context, host string) (
ConnectAction httpproxy.ConnectAction, newHost string) {
return httpproxy.ConnectMitm, host
}
func OnRequest(ctx *httpproxy.Context, req *http.Request) (
resp *http.Response) {
log.Printf("INFO: Proxy: %s %s", req.Method, req.URL.String())
return
}
func OnResponse(ctx *httpproxy.Context, req *http.Request,
resp *http.Response) {
resp.Header.Add("Via", "go-httpproxy")
}
func main() {
prx, _ := httpproxy.NewProxy()
prx.OnError = OnError
prx.OnAccept = OnAccept
prx.OnAuth = OnAuth
prx.OnConnect = OnConnect
prx.OnRequest = OnRequest
prx.OnResponse = OnResponse
http.ListenAndServe(":8080", prx)
}