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 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
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
DefaultCaCert provides default CA certificate.
var DefaultCaKey []byte
DefaultCaKey provides default CA key.
Functions ¶
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 // 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 // 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.
func NewProxyCert ¶
NewProxyCert returns a new Proxy given CA certificate and key.