httpproxy

package module
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2022 License: BSD-3-Clause Imports: 26 Imported by: 0

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

View Source
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

View Source
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.

View Source
var DefaultCaCert []byte

DefaultCaCert provides default CA certificate.

View Source
var DefaultCaKey []byte

DefaultCaKey provides default CA key.

Functions

func InMemoryResponse

func InMemoryResponse(code int, header http.Header, body []byte) *http.Response

InMemoryResponse creates new HTTP response given arguments.

func ServeInMemory

func ServeInMemory(w http.ResponseWriter, code int, header http.Header, body []byte) error

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 NewCaSigner

func NewCaSigner() *CaSigner

NewCaSigner returns a new CaSigner without caching.

func NewCaSignerCache

func NewCaSignerCache(max int) *CaSigner

NewCaSignerCache returns a new CaSigner with caching given max.

func (*CaSigner) SignHost

func (c *CaSigner) SignHost(host string) (cert *tls.Certificate)

SignHost generates TLS certificate given single host, signed by CA certificate.

type ConnResponseWriter

type ConnResponseWriter struct {
	Conn net.Conn
	// contains filtered or unexported fields
}

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 ConnectAction

type ConnectAction int

ConnectAction specifies action of after the CONNECT.

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.

func NewError

func NewError(errString string) *Error

NewError returns a new Error.

func (*Error) Error

func (e *Error) Error() string

Error implements error interface.

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 NewProxy

func NewProxy() (*Proxy, error)

NewProxy returns a new Proxy has default CA certificate and key.

func NewProxyCert

func NewProxyCert(caCert, caKey []byte) (*Proxy, error)

NewProxyCert returns a new Proxy given CA certificate and key.

func (*Proxy) ServeHTTP

func (prx *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL