Documentation
¶
Overview ¶
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.
Index ¶
- Constants
- Variables
- func ClassifyDialError(err error) string
- func ExtractDialAddr(err error) string
- func InMemoryResponse(code int, header http.Header, body []byte) *http.Response
- func ServeInMemory(w http.ResponseWriter, code int, header http.Header, body []byte) error
- func ServeResponse(w http.ResponseWriter, resp *http.Response) error
- func SignHosts(ca tls.Certificate, hosts []string) (*tls.Certificate, error)
- type CaSigner
- type ConnResponseWriter
- type ConnectAction
- type Context
- type Error
- type Proxy
- type TunnelInfo
Constants ¶
const ( // ConnectNone specifies that proxy request is not CONNECT. // If it returned in OnConnect, proxy connection closes immediately. ConnectNone = ConnectAction(iota) // ConnectProxy specifies directly socket proxy after the CONNECT. ConnectProxy // ConnectMitm specifies proxy "Man in the Middle" style attack // after the CONNECT. ConnectMitm )
Constants of ConnectAction type.
Variables ¶
var ( ErrPanic = NewError("panic") ErrResponseWrite = NewError("response write") ErrRequestRead = NewError("request read") ErrRemoteConnect = NewError("remote connect") ErrNotSupportHijacking = NewError("hijacking not supported") ErrTLSSignHost = NewError("TLS sign host") ErrTLSHandshake = NewError("TLS handshake") ErrAbsURLAfterCONNECT = NewError("absolute URL after CONNECT") ErrRoundTrip = NewError("round trip") ErrUnsupportedTransferEncoding = NewError("unsupported transfer encoding") ErrNotSupportHTTPVer = NewError("http version not supported") )
Library specific errors.
var DefaultCaCert = []byte(`-----BEGIN CERTIFICATE-----
MIIFkzCCA3ugAwIBAgIJAKEbW2ujNjX9MA0GCSqGSIb3DQEBCwUAMGAxCzAJBgNV
BAYTAlRSMREwDwYDVQQIDAhJc3RhbmJ1bDEVMBMGA1UECgwMZ28taHR0cHByb3h5
MRIwEAYDVQQLDAlodHRwcHJveHkxEzARBgNVBAMMCmdpdGh1Yi5jb20wHhcNMTgw
Mj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014F6Y0TgBnU54S4QfElKAzO4XS2IwseAboBDx4H0naA5E
2jtdDSl516EcLaAEPamS7A4aTH7RRMZSGO9KTfNY4lp66BZvWD42V1yEaiHhyBuk
wv0OY1kM4tmBdPipuGSpOYEpNOrBtaq7WFjhXLsZvBrCAGQF7qkDSeKoA5PHgWjm
kqA+a0Nb0N1LBArV+ccZwmb//jnJ08eygsQEXRresIsjrF5HCOu0VChcTScaNung
ec3EALNpyEW6mEafO8mY8H7jIvPiNMsQZ9+et4oM2LJie/jNOr5VC4d/czEEPGxR
/Vwo5vz7iX4bV6eZHDxbR274EwKMx2xFAoIBAQDlHsidPhfVElXBV1uAfUdQ92LA
b5gmAorx104YYauJ8A8cB3hJ7+pItgxsiUF+SAtlpt/rL3H9MD5m5eLZudFv7NsF
E+4WIvzSesF/LS+zVQ7UuFkiXPnUvdlXmnZRR8RdtM6n/xnBU2r3ta7Yq6EV/6nE
GK7KSSnouV5LAtvyDVTu+b6IAguOiIW6d+9T4H3QwnnQeyKE+5NWc3fB4dPqc5AS
s39uFDUnxsMb2Nl3JcNJHYBTm9ubjAZSo/3NuB0z/Gm+ssOcExTD//vW7BxxSAcs
/xlPPTPbY5qoMAT7kK71kd4Ypnqbcs3UPpAHtcPkjWpuWOlebK0J7UYToj4f
-----END RSA PRIVATE KEY-----`)
DefaultCaKey provides default CA key.
Functions ¶
func ClassifyDialError ¶
func ExtractDialAddr ¶
func InMemoryResponse ¶
InMemoryResponse creates new HTTP response given arguments.
func ServeInMemory ¶
ServeInMemory serves HTTP response given arguments to http.ResponseWriter.
func ServeResponse ¶
func ServeResponse(w http.ResponseWriter, resp *http.Response) error
ServeResponse serves HTTP response to http.ResponseWriter.
func SignHosts ¶
func SignHosts(ca tls.Certificate, hosts []string) (*tls.Certificate, error)
SignHosts generates TLS certificate given hosts, signed by CA certificate.
Types ¶
type CaSigner ¶
type CaSigner struct {
// Ca specifies CA certificate. You must set before using.
Ca *tls.Certificate
// contains filtered or unexported fields
}
CaSigner is a certificate signer by CA certificate. It supports caching.
func NewCaSignerCache ¶
NewCaSignerCache returns a new CaSigner with caching given max.
type ConnResponseWriter ¶
ConnResponseWriter implements http.ResponseWriter interface to use hijacked HTTP connection.
func NewConnResponseWriter ¶
func NewConnResponseWriter(conn net.Conn) *ConnResponseWriter
NewConnResponseWriter returns a new ConnResponseWriter.
func (*ConnResponseWriter) Close ¶
func (c *ConnResponseWriter) Close() error
Close closes network connection.
func (*ConnResponseWriter) Header ¶
func (c *ConnResponseWriter) Header() http.Header
Header returns the header map that will be sent by WriteHeader.
func (*ConnResponseWriter) Write ¶
func (c *ConnResponseWriter) Write(body []byte) (int, error)
Write writes the data to the connection as part of an HTTP reply.
func (*ConnResponseWriter) WriteHeader ¶
func (c *ConnResponseWriter) WriteHeader(statusCode int)
WriteHeader sends an HTTP response header with status code.
type Context ¶
type Context struct {
// Pointer of Proxy struct handled this context.
// It's using internally. Don't change in Context struct!
Prx *Proxy
// Session number of this context obtained from Proxy struct.
SessionNo int64
// Sub session number of processing remote connection.
SubSessionNo int64
// Original Proxy request.
// It's using internally. Don't change in Context struct!
Req *http.Request
// Original Proxy request, if proxy request method is CONNECT.
// It's using internally. Don't change in Context struct!
ConnectReq *http.Request
// Action of after the CONNECT, if proxy request method is CONNECT.
// It's using internally. Don't change in Context struct!
ConnectAction ConnectAction
// Remote host, if proxy request method is CONNECT.
// It's using internally. Don't change in Context struct!
ConnectHost string
ConnectDialDuration time.Duration
ConnectDialError string
ConnectDialAddr string
// User data to use free.
UserData interface{}
// contains filtered or unexported fields
}
Context keeps context of each proxy request.
type Error ¶
type Error struct {
ErrString string
}
Error struct is base of library specific errors.
type Proxy ¶
type Proxy struct {
// Session number of last proxy request.
SessionNo int64
// RoundTripper interface to obtain remote response.
// By default, it uses &http.Transport{}.
Rt http.RoundTripper
// Certificate key pair.
Ca tls.Certificate
// User data to use free.
UserData interface{}
// Error callback.
OnError func(ctx *Context, where string, err *Error, opErr error)
// Accept callback. It greets proxy request like ServeHTTP function of
// http.Handler.
// If it returns true, stops processing proxy request.
OnAccept func(ctx *Context, w http.ResponseWriter, r *http.Request) bool
// Auth callback. If you need authentication, set this callback.
// If it returns true, authentication succeeded.
OnAuth func(ctx *Context, authType string, user string, pass string) bool
// Connect callback. It sets connect action and new host.
// If len(newhost) > 0, host changes.
OnConnect func(ctx *Context, host string) (ConnectAction ConnectAction,
newHost string)
// Request callback. It greets remote request.
// If it returns non-nil response, stops processing remote request.
OnRequest func(ctx *Context, req *http.Request) (resp *http.Response)
// Response callback. It greets remote response.
// Remote response sends after this callback.
OnResponse func(ctx *Context, req *http.Request, resp *http.Response)
// If ConnectAction is ConnectMitm, it sets chunked to Transfer-Encoding.
// By default, true.
MitmChunked bool
// HTTP Authentication type. If it's not specified (""), uses "Basic".
// By default, "".
AuthType string
// Timeout for TCP dial attempts during CONNECT tunneling.
// By default, 30 seconds.
DialTimeout time.Duration
// contains filtered or unexported fields
}
Proxy defines parameters for running an HTTP Proxy. It implements http.Handler interface for ListenAndServe function. If you need, you must set Proxy struct before handling requests. A Proxy must not be copied after first use: a copy would not share the open tunnels.
func NewProxyCert ¶
NewProxyCert returns a new Proxy given CA certificate and key.
func (*Proxy) ActiveTunnels ¶
ActiveTunnels returns the number of open tunnels, including the ones still being set up (OnConnect, dial, TLS handshake). A tunnel closed by CloseAllTunnels no longer counts, even while its handler is still blocked in a callback.
func (*Proxy) CloseAllTunnels ¶
func (prx *Proxy) CloseAllTunnels() []TunnelInfo
CloseAllTunnels closes the tunnels open at the time of the call, whatever they are doing, and returns them. It cuts the requests in progress, and aborts the dials and the upstream calls of MITM requests. Use it at a deadline, once the server no longer accepts connections. Requests still in OnAccept or OnAuth are not tunnels yet: http.Server.Close closes their connections.
func (*Proxy) ServeHTTP ¶
func (prx *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler.
func (*Proxy) SetDraining ¶
SetDraining switches draining mode on or off. While draining, MITM sessions answer with "Connection: close" and end after the response, so that clients open their next connection elsewhere. This is decided right before each response's headers are sent: a response whose headers were already sent keeps its session, and the next response closes it.
Draining closes nothing by itself. A MITM session waiting for its next request stays open until that request is answered, its client closes it or CloseAllTunnels cuts it: closing it earlier could race with a request the client is sending. Pass-through tunnels are not affected either: wait for them with ActiveTunnels and cut the remaining ones with CloseAllTunnels.
type TunnelInfo ¶
type TunnelInfo struct {
Host string
Action ConnectAction
Age time.Duration
}
TunnelInfo describes a tunnel closed by the proxy.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
go-httpproxy-demo
command
go-httpproxy-demo is an example for HTTP and HTTPS web proxy.
|
go-httpproxy-demo is an example for HTTP and HTTPS web proxy. |
|
go-httpproxy-simple
command
|