mps

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: BSD-3-Clause Imports: 30 Imported by: 0

README ΒΆ


MPS

English | πŸ‡¨πŸ‡³δΈ­ζ–‡

πŸ“– Introduction

MPS stars GitHub release (latest SemVer) GitHub go.mod Go version license

MPS (middle-proxy-server) is an high-performance middle proxy library. support HTTP, HTTPS, Websocket, ForwardProxy, ReverseProxy, TunnelProxy, MitmProxy.

πŸš€ Features

  • Http Proxy
  • Https Proxy
  • Forward Proxy
  • Reverse Proxy
  • Tunnel Proxy
  • Mitm Proxy (Man-in-the-middle)
  • WekSocket Proxy

🧰 Install

go get -u github.com/telanflow/mps

πŸ›  How to use

A simple proxy service

package main

import (
    "github.com/telanflow/mps"
    "log"
    "net/http"
)

func main() {
    proxy := mps.NewHttpProxy()
    log.Fatal(http.ListenAndServe(":8080", proxy))
}

More examples

🧬 Middleware

Middleware can intercept requests and responses. we have several middleware implementations built in, including BasicAuth

func main() {
    proxy := mps.NewHttpProxy()
    
    proxy.Use(mps.MiddlewareFunc(func(req *http.Request, ctx *mps.Context) (*http.Response, error) {
        log.Printf("[INFO] middleware -- %s %s", req.Method, req.URL)
        return ctx.Next(req)
    }))
    
    proxy.UseFunc(func(req *http.Request, ctx *mps.Context) (*http.Response, error) {
        log.Printf("[INFO] middleware -- %s %s", req.Method, req.URL)
        resp, err := ctx.Next(req)
        if err != nil {
            return nil, err
        }
        log.Printf("[INFO] resp -- %d", resp.StatusCode)
        return resp, err
    })
    
    log.Fatal(http.ListenAndServe(":8080", proxy))
}

♻️ Filters

Filters can filter requests and responses for unified processing. It is based on middleware implementation.

func main() {
    proxy := mps.NewHttpProxy()
    
    // request Filter Group
    reqGroup := proxy.OnRequest(mps.FilterHostMatches(regexp.MustCompile("^.*$")))
    reqGroup.DoFunc(func(req *http.Request, ctx *mps.Context) (*http.Request, *http.Response) {
        log.Printf("[INFO] req -- %s %s", req.Method, req.URL)
        return req, nil
    })
    
    // response Filter Group
    respGroup := proxy.OnResponse()
    respGroup.DoFunc(func(resp *http.Response, err error, ctx *mps.Context) (*http.Response, error) {
        if err != nil {
            log.Printf("[ERRO] resp -- %s %v", ctx.Request.Method, err)
            return nil, err
        }
    
        log.Printf("[INFO] resp -- %d", resp.StatusCode)
        return resp, err
    })
    
    log.Fatal(http.ListenAndServe(":8080", proxy))
}

πŸ“„ License

Source code in MPS is available under the BSD 3 License.

Documentation ΒΆ

Overview ΒΆ

Taken from $GOROOT/src/pkg/net/http/chunked needed to write https responses to client.

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var (
	// http request is nil
	RequestNilErr = errors.New("request is nil")
	// http request method not support
	MethodNotSupportErr = errors.New("request method not support")
	// http request is websocket
	RequestWebsocketUpgradeErr = errors.New("websocket upgrade")
)
View Source
var (
	HttpTunnelOk   = []byte("HTTP/1.0 200 Connection Established\r\n\r\n")
	HttpTunnelFail = []byte("HTTP/1.1 502 Bad Gateway\r\n\r\n")
)
View Source
var DefaultTransport = &http.Transport{
	DialContext: (&net.Dialer{
		Timeout:   15 * time.Second,
		KeepAlive: 30 * time.Second,
		DualStack: true,
	}).DialContext,
	ForceAttemptHTTP2:     true,
	MaxIdleConns:          100,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   10 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
	TLSClientConfig:       &tls.Config{InsecureSkipVerify: true},
	Proxy:                 http.ProxyFromEnvironment,
}

Default http.Transport option

View Source
var (
	HttpMitmOk = []byte("HTTP/1.0 200 Connection Established\r\n\r\n")
)

Functions ΒΆ

func ConnError ΒΆ

func ConnError(w net.Conn)

func RemoveProxyHeaders ΒΆ

func RemoveProxyHeaders(r *http.Request)

Hop-by-hop headers. These are removed when sent to the backend. As of RFC 7230, hop-by-hop headers are required to appear in the Connection header field. These are the headers defined by the obsoleted RFC 2616 (section 13.5.1) and are used for backward compatibility.

func ResetClientHeaders ΒΆ

func ResetClientHeaders(r *http.Request)

ResetClientHeaders These Headers must be reset when a client Request is issued to reuse a Request

Types ΒΆ

type Context ΒΆ

type Context struct {
	// context.Context
	Context context.Context

	// Request context-dependent requests
	Request *http.Request

	// Response is associated with Request
	Response *http.Response

	// Transport is used for global HTTP requests, and it will be reused.
	Transport *http.Transport

	// In some cases it is not always necessary to remove the proxy headers.
	// For example, cascade proxy
	KeepProxyHeaders bool

	// In some cases it is not always necessary to reset the headers.
	KeepClientHeaders bool

	// KeepDestinationHeaders indicates the proxy should retain any headers
	// present in the http.Response before proxying
	KeepDestinationHeaders bool
	// contains filtered or unexported fields
}

Context for the request which contains Middleware, Transport, and other values

func NewContext ΒΆ

func NewContext() *Context

NewContext create http request Context

func (*Context) Next ΒΆ

func (ctx *Context) Next(req *http.Request) (*http.Response, error)

Next to exec middlewares Execute the next middleware as a linked list. "ctx.Next(req)" eg:

func Handle(req *http.Request, ctx *Context) (*http.Response, error) {
		// You can do anything to modify the http.Request ...
		resp, err := ctx.Next(req)
		// You can do anything to modify the http.Response ...
		return resp, err
}

Alternatively, you can simply return the response without executing `ctx.Next()`, which will interrupt subsequent middleware execution.

func (*Context) RoundTrip ΒΆ

func (ctx *Context) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

For higher-level HTTP client support (such as handling of cookies and redirects), see Get, Post, and the Client type.

Like the RoundTripper interface, the error types returned by RoundTrip are unspecified.

func (*Context) Use ΒΆ

func (ctx *Context) Use(middleware ...Middleware)

Use registers an Middleware to proxy

func (*Context) UseFunc ΒΆ

func (ctx *Context) UseFunc(fns ...MiddlewareFunc)

UseFunc registers an MiddlewareFunc to proxy

func (*Context) WithRequest ΒΆ

func (ctx *Context) WithRequest(req *http.Request) *Context

WithRequest get the Context of the request

type CounterEncryptorRand ΒΆ

type CounterEncryptorRand struct {
	// contains filtered or unexported fields
}

func NewCounterEncryptorRand ΒΆ

func NewCounterEncryptorRand(key interface{}, seed []byte) (r CounterEncryptorRand, err error)

func (*CounterEncryptorRand) Read ΒΆ

func (c *CounterEncryptorRand) Read(b []byte) (n int, err error)

func (*CounterEncryptorRand) Seed ΒΆ

func (c *CounterEncryptorRand) Seed(b []byte)

type Filter ΒΆ

type Filter interface {
	Match(req *http.Request) bool
}

Filter is an request interceptor

func FilterHostIs ΒΆ

func FilterHostIs(hosts ...string) Filter

FilterHostIs returns a Filter, testing whether the host to which the request is directed to equal to one of the given strings

func FilterHostMatches ΒΆ

func FilterHostMatches(regexps ...*regexp.Regexp) Filter

FilterHostMatches for request.Host

func FilterUrlHasPrefix ΒΆ

func FilterUrlHasPrefix(prefix string) Filter

FilterUrlHasPrefix returns a Filter checking wether the destination URL the proxy client has requested has the given prefix, with or without the host. For example FilterUrlHasPrefix("host/x") will match requests of the form 'GET host/x', and will match requests to url 'http://host/x'

func FilterUrlIs ΒΆ

func FilterUrlIs(urls ...string) Filter

FilterUrlIs returns a Filter, testing whether or not the request URL is one of the given strings with or without the host prefix. FilterUrlIs("google.com/","foo") will match requests 'GET /' to 'google.com', requests `'GET google.com/' to any host, and requests of the form 'GET foo'.

func FilterUrlMatches ΒΆ

func FilterUrlMatches(re *regexp.Regexp) Filter

FilterUrlMatches returns a Filter testing whether the destination URL of the request matches the given regexp, with or without prefix

type FilterFunc ΒΆ

type FilterFunc func(req *http.Request) bool

A wrapper that would convert a function to a Filter interface type

func (FilterFunc) Match ΒΆ

func (f FilterFunc) Match(req *http.Request) bool

Filter.Match(req) <=> FilterFunc(req)

type FilterGroup ΒΆ

type FilterGroup interface {
	Handle()
}

type ForwardHandler ΒΆ

type ForwardHandler struct {
	Ctx        *Context
	BufferPool httputil.BufferPool
}

ForwardHandler The forward proxy type. Implements http.Handler.

func NewForwardHandler ΒΆ

func NewForwardHandler() *ForwardHandler

NewForwardHandler Create a forward proxy

func NewForwardHandlerWithContext ΒΆ

func NewForwardHandlerWithContext(ctx *Context) *ForwardHandler

NewForwardHandlerWithContext Create a ForwardHandler with Context

func (*ForwardHandler) OnRequest ΒΆ

func (forward *ForwardHandler) OnRequest(filters ...Filter) *ReqFilterGroup

OnRequest filter requests through Filters

func (*ForwardHandler) OnResponse ΒΆ

func (forward *ForwardHandler) OnResponse(filters ...Filter) *RespFilterGroup

OnResponse filter response through Filters

func (*ForwardHandler) ServeHTTP ΒΆ

func (forward *ForwardHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Standard net/http function. You can use it alone

func (*ForwardHandler) Transport ΒΆ

func (forward *ForwardHandler) Transport() *http.Transport

Transport

func (*ForwardHandler) Use ΒΆ

func (forward *ForwardHandler) Use(middleware ...Middleware)

Use registers an Middleware to proxy

func (*ForwardHandler) UseFunc ΒΆ

func (forward *ForwardHandler) UseFunc(fus ...MiddlewareFunc)

UseFunc registers an MiddlewareFunc to proxy

type Handle ΒΆ

type Handle interface {
	RequestHandle
	ResponseHandle
}

type HttpProxy ΒΆ

type HttpProxy struct {
	// Handles Connect requests use the TunnelHandler by default
	HandleConnect http.Handler

	// HTTP requests use the ForwardHandler by default
	HttpHandler http.Handler

	// HTTP requests use the ReverseHandler by default
	ReverseHandler http.Handler

	// Client request Context
	Ctx *Context
}

The basic proxy type. Implements http.Handler.

func NewHttpProxy ΒΆ

func NewHttpProxy() *HttpProxy

func (*HttpProxy) OnRequest ΒΆ

func (proxy *HttpProxy) OnRequest(filters ...Filter) *ReqFilterGroup

OnRequest filter requests through Filters

func (*HttpProxy) OnResponse ΒΆ

func (proxy *HttpProxy) OnResponse(filters ...Filter) *RespFilterGroup

OnResponse filter response through Filters

func (*HttpProxy) ServeHTTP ΒΆ

func (proxy *HttpProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Standard net/http function.

func (*HttpProxy) Transport ΒΆ

func (proxy *HttpProxy) Transport() *http.Transport

Transport get http.Transport instance

func (*HttpProxy) Use ΒΆ

func (proxy *HttpProxy) Use(middleware ...Middleware)

Use registers an Middleware to proxy

func (*HttpProxy) UseFunc ΒΆ

func (proxy *HttpProxy) UseFunc(fus ...MiddlewareFunc)

UseFunc registers an MiddlewareFunc to proxy

type Middleware ΒΆ

type Middleware interface {
	// Handle execute the next middleware as a linked list. "ctx.Next(req)"
	// eg:
	// 		func Handle(req *http.Request, ctx *Context) (*http.Response, error) {
	//				// You can do anything to modify the http.Request ...
	// 				resp, err := ctx.Next(req)
	// 				// You can do anything to modify the http.Response ...
	//				return resp, err
	// 		}
	//
	// Alternatively, you can simply return the response without executing `ctx.Next()`,
	// which will interrupt subsequent middleware execution.
	Handle(req *http.Request, ctx *Context) (*http.Response, error)
}

Middleware will "tamper" with the request coming to the proxy server

type MiddlewareFunc ΒΆ

type MiddlewareFunc func(req *http.Request, ctx *Context) (*http.Response, error)

MiddlewareFunc A wrapper that would convert a function to a Middleware interface type

func (MiddlewareFunc) Handle ΒΆ

func (f MiddlewareFunc) Handle(req *http.Request, ctx *Context) (*http.Response, error)

Handle Middleware.Handle(req, ctx) <=> MiddlewareFunc(req, ctx)

type MitmHandler ΒΆ

type MitmHandler struct {
	Ctx         *Context
	BufferPool  httputil.BufferPool
	Certificate tls.Certificate
	// CertContainer is certificate storage container
	CertContainer cert.Container
}

MitmHandler The Man-in-the-middle proxy type. Implements http.Handler.

func NewMitmHandler ΒΆ

func NewMitmHandler() *MitmHandler

NewMitmHandler Create a mitmHandler, use default cert.

func NewMitmHandlerWithCert ΒΆ

func NewMitmHandlerWithCert(ctx *Context, certPEMBlock, keyPEMBlock []byte) (*MitmHandler, error)

NewMitmHandlerWithCert Create a MitmHandler with cert pem block

func NewMitmHandlerWithCertFile ΒΆ

func NewMitmHandlerWithCertFile(ctx *Context, certFile, keyFile string) (*MitmHandler, error)

NewMitmHandlerWithCertFile Create a MitmHandler with cert file

func NewMitmHandlerWithContext ΒΆ

func NewMitmHandlerWithContext(ctx *Context) *MitmHandler

NewMitmHandlerWithContext Create a MitmHandler, use default cert.

func (*MitmHandler) OnRequest ΒΆ

func (mitm *MitmHandler) OnRequest(filters ...Filter) *ReqFilterGroup

OnRequest filter requests through Filters

func (*MitmHandler) OnResponse ΒΆ

func (mitm *MitmHandler) OnResponse(filters ...Filter) *RespFilterGroup

OnResponse filter response through Filters

func (*MitmHandler) ServeHTTP ΒΆ

func (mitm *MitmHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Standard net/http function. You can use it alone

func (*MitmHandler) TLSConfigFromCA ΒΆ

func (mitm *MitmHandler) TLSConfigFromCA(host string) (*tls.Config, error)

func (*MitmHandler) Transport ΒΆ

func (mitm *MitmHandler) Transport() *http.Transport

Transport

func (*MitmHandler) Use ΒΆ

func (mitm *MitmHandler) Use(middleware ...Middleware)

Use registers a Middleware to proxy

func (*MitmHandler) UseFunc ΒΆ

func (mitm *MitmHandler) UseFunc(fus ...MiddlewareFunc)

UseFunc registers an MiddlewareFunc to proxy

type ReqFilterGroup ΒΆ

type ReqFilterGroup struct {
	// contains filtered or unexported fields
}

ReqFilterGroup ReqCondition is a request filter group

func (*ReqFilterGroup) Do ΒΆ

func (cond *ReqFilterGroup) Do(h RequestHandle)

func (*ReqFilterGroup) DoFunc ΒΆ

func (cond *ReqFilterGroup) DoFunc(fn func(req *http.Request, ctx *Context) (*http.Request, *http.Response))

type RequestHandle ΒΆ

type RequestHandle interface {
	HandleRequest(req *http.Request, ctx *Context) (*http.Request, *http.Response)
}

type RequestHandleFunc ΒΆ

type RequestHandleFunc func(req *http.Request, ctx *Context) (*http.Request, *http.Response)

A wrapper that would convert a function to a RequestHandle interface type

func (RequestHandleFunc) HandleRequest ΒΆ

func (f RequestHandleFunc) HandleRequest(req *http.Request, ctx *Context) (*http.Request, *http.Response)

RequestHandle.Handle(req, ctx) <=> RequestHandleFunc(req, ctx)

type RespFilterGroup ΒΆ

type RespFilterGroup struct {
	// contains filtered or unexported fields
}

RespFilterGroup ReqCondition is a response filter group

func (*RespFilterGroup) Do ΒΆ

func (cond *RespFilterGroup) Do(h ResponseHandle)

func (*RespFilterGroup) DoFunc ΒΆ

func (cond *RespFilterGroup) DoFunc(fn func(resp *http.Response, err error, ctx *Context) (*http.Response, error))

type ResponseHandle ΒΆ

type ResponseHandle interface {
	HandleResponse(resp *http.Response, err error, ctx *Context) (*http.Response, error)
}

type ResponseHandleFunc ΒΆ

type ResponseHandleFunc func(resp *http.Response, err error, ctx *Context) (*http.Response, error)

A wrapper that would convert a function to a ResponseHandle interface type

func (ResponseHandleFunc) HandleResponse ΒΆ

func (f ResponseHandleFunc) HandleResponse(resp *http.Response, err error, ctx *Context) (*http.Response, error)

ResponseHandle.Handle(resp, ctx) <=> ResponseHandleFunc(resp, ctx)

type ReverseHandler ΒΆ

type ReverseHandler struct {
	Ctx        *Context
	BufferPool httputil.BufferPool
}

ReverseHandler is a reverse proxy server implementation

func NewReverseHandler ΒΆ

func NewReverseHandler() *ReverseHandler

NewReverseHandler Create a reverse proxy

func (*ReverseHandler) OnRequest ΒΆ

func (reverse *ReverseHandler) OnRequest(filters ...Filter) *ReqFilterGroup

OnRequest filter requests through Filters

func (*ReverseHandler) OnResponse ΒΆ

func (reverse *ReverseHandler) OnResponse(filters ...Filter) *RespFilterGroup

OnResponse filter response through Filters

func (*ReverseHandler) ServeHTTP ΒΆ

func (reverse *ReverseHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Standard net/http function. You can use it alone

func (*ReverseHandler) Transport ΒΆ

func (reverse *ReverseHandler) Transport() *http.Transport

Transport

func (*ReverseHandler) Use ΒΆ

func (reverse *ReverseHandler) Use(middleware ...Middleware)

Use registers an Middleware to proxy

func (*ReverseHandler) UseFunc ΒΆ

func (reverse *ReverseHandler) UseFunc(fus ...MiddlewareFunc)

UseFunc registers an MiddlewareFunc to proxy

type TunnelHandler ΒΆ

type TunnelHandler struct {
	Ctx           *Context
	BufferPool    httputil.BufferPool
	ConnContainer pool.ConnContainer
}

TunnelHandler The tunnel proxy type. Implements http.Handler.

func NewTunnelHandler ΒΆ

func NewTunnelHandler() *TunnelHandler

NewTunnelHandler Create a tunnel handler

func NewTunnelHandlerWithContext ΒΆ

func NewTunnelHandlerWithContext(ctx *Context) *TunnelHandler

NewTunnelHandlerWithContext Create a tunnel handler with Context

func (*TunnelHandler) ConnectDial ΒΆ

func (tunnel *TunnelHandler) ConnectDial(network, addr string) (net.Conn, error)

func (*TunnelHandler) OnRequest ΒΆ

func (tunnel *TunnelHandler) OnRequest(filters ...Filter) *ReqFilterGroup

OnRequest filter requests through Filters

func (*TunnelHandler) OnResponse ΒΆ

func (tunnel *TunnelHandler) OnResponse(filters ...Filter) *RespFilterGroup

OnResponse filter response through Filters

func (*TunnelHandler) ServeHTTP ΒΆ

func (tunnel *TunnelHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Standard net/http function. You can use it alone

func (*TunnelHandler) Transport ΒΆ

func (tunnel *TunnelHandler) Transport() *http.Transport

Transport get http.Transport instance

func (*TunnelHandler) Use ΒΆ

func (tunnel *TunnelHandler) Use(middleware ...Middleware)

Use registers an Middleware to proxy

func (*TunnelHandler) UseFunc ΒΆ

func (tunnel *TunnelHandler) UseFunc(fus ...MiddlewareFunc)

UseFunc registers an MiddlewareFunc to proxy

type WebsocketHandler ΒΆ

type WebsocketHandler struct {
	Ctx        *Context
	BufferPool httputil.BufferPool
}

WebsocketHandler The websocket proxy type. Implements http.Handler.

func NewWebsocketHandler ΒΆ

func NewWebsocketHandler() *WebsocketHandler

NewWebsocketHandler Create a websocket handler

func NewWebsocketHandlerWithContext ΒΆ

func NewWebsocketHandlerWithContext(ctx *Context) *WebsocketHandler

NewWebsocketHandlerWithContext Create a tunnel handler with Context

func (*WebsocketHandler) ConnectDial ΒΆ

func (ws *WebsocketHandler) ConnectDial(network, addr string) (net.Conn, error)

func (*WebsocketHandler) ServeHTTP ΒΆ

func (ws *WebsocketHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Standard net/http function. You can use it alone

func (*WebsocketHandler) Transport ΒΆ added in v0.1.2

func (ws *WebsocketHandler) Transport() *http.Transport

Transport get http.Transport instance

Directories ΒΆ

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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