minimartian

package
v1.3.5-alpha-0814 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: AGPL-3.0, Apache-2.0 Imports: 35 Imported by: 0

Documentation

Overview

Package martian provides an HTTP/1.1 proxy with an API for configurable request and response modifiers.

Index

Constants

View Source
const (
	S5_CONNECT_HOST = "S5ConnectHost"
	S5_CONNECT_PORT = "S5ConnectPort"
	S5_CONNECT_ADDR = "S5ConnectAddr"
	AUTH_FINISH     = "authFinish"
)
View Source
const (
	UsernameAndPassVersion     = 0x01
	UsernameAndPassAuthSuccess = 0x00
	UsernameAndPassAuthFail    = 0x01
)

Variables

View Source
var (
	ErrS5Version  = errors.New("invalid s5 version")
	ErrBadRequest = errors.New("bad s5 request")
)
View Source
var IsDroppedError = utils.Error("dropped")

Functions

func IsSocks5HandleShake

func IsSocks5HandleShake(conn net.Conn) (fConn net.Conn, _ bool, _ byte, _ error)

func IsTlsHandleShake

func IsTlsHandleShake(conn net.Conn) (fConn net.Conn, _ bool, _ error)

func NewReply

func NewReply(host net.IP, port int) []byte

func ParseAddress

func ParseAddress(address string) (a byte, addr []byte, port []byte, err error)

Types

type Context

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

Context provides information and storage for a single request/response pair. Contexts are linked to shared session that is used for multiple requests on a single connection.

func NewContext

func NewContext(req *http.Request, proxy *Proxy) *Context

NewContext returns a context for the in-flight HTTP request.

func TestContext

func TestContext(req *http.Request, conn net.Conn, bw *bufio.ReadWriter, p *Proxy) (ctx *Context, remove func(), err error)

TestContext builds a new session and associated context and returns the context and a function to remove the associated context. If it fails to generate either a new session or a new context it will return an error. Intended for tests only.

func (*Context) APIRequest

func (ctx *Context) APIRequest()

APIRequest marks the requests as a request to the proxy API.

func (*Context) Get

func (ctx *Context) Get(key string) (interface{}, bool)

Get takes key and returns the associated value from the context.

func (*Context) GetSessionBoolValue

func (c *Context) GetSessionBoolValue(i string) bool

func (*Context) GetSessionIntValue

func (c *Context) GetSessionIntValue(i string) int

func (*Context) GetSessionStringValue

func (c *Context) GetSessionStringValue(i string) string

func (*Context) ID

func (ctx *Context) ID() string

ID returns the context ID.

func (*Context) IsAPIRequest

func (ctx *Context) IsAPIRequest() bool

IsAPIRequest returns true when the request patterns matches a pattern in the proxy mux. The mux is usually defined as a parameter to the api.Forwarder, which uses http.DefaultServeMux by default.

func (*Context) Session

func (ctx *Context) Session() *Session

Session returns the session for the context.

func (*Context) Set

func (ctx *Context) Set(key string, val interface{})

Set takes a key and associates it with val in the context. The value is persisted for the duration of the request and is removed on the following request.

func (*Context) SkipLogging

func (ctx *Context) SkipLogging()

SkipLogging skips logging by Martian loggers for the current request.

func (*Context) SkipRoundTrip

func (ctx *Context) SkipRoundTrip()

SkipRoundTrip skips the round trip for the current request.

func (*Context) SkippingLogging

func (ctx *Context) SkippingLogging() bool

SkippingLogging returns whether the current request / response pair will be logged.

func (*Context) SkippingRoundTrip

func (ctx *Context) SkippingRoundTrip() bool

SkippingRoundTrip returns whether the current round trip will be skipped.

type Datagram

type Datagram struct {
	Rsv     []byte // 0x00 0x00
	Frag    byte
	Atyp    byte
	DstHost []byte
	DstPort []byte // 2 bytes
	Data    []byte
}

func NewDatagram

func NewDatagram(atyp byte, dstaHost []byte, dstport []byte, data []byte) *Datagram

func NewDatagramFromBytes

func NewDatagramFromBytes(bb []byte) (*Datagram, error)

func (*Datagram) Bytes

func (d *Datagram) Bytes() []byte

type MultiError

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

MultiError is a collection of errors that implements the error interface.

func NewMultiError

func NewMultiError() *MultiError

NewMultiError returns a new MultiError.

func (*MultiError) Add

func (merr *MultiError) Add(err error)

Add appends an error to the error collection.

func (*MultiError) Empty

func (merr *MultiError) Empty() bool

Empty returns whether the *MultiError contains any errors.

func (*MultiError) Error

func (merr *MultiError) Error() string

Error returns the list of errors separated by newlines.

func (*MultiError) Errors

func (merr *MultiError) Errors() []error

Errors returns the error slice containing the error collection.

type Proxy

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

Proxy is an HTTP proxy with support for TLS MITM and customizable behavior.

func NewProxy

func NewProxy() *Proxy

NewProxy returns a new HTTP proxy.

func (*Proxy) Close

func (p *Proxy) Close()

Close sets the proxy to the closing state so it stops receiving new connections, finishes processing any inflight requests, and closes existing connections without reading anymore requests from them.

func (*Proxy) Closing

func (p *Proxy) Closing() bool

Closing returns whether the proxy is in the closing state.

func (*Proxy) GetMaxContentLength

func (p *Proxy) GetMaxContentLength() int

func (*Proxy) Serve

func (p *Proxy) Serve(l net.Listener, ctx context.Context) error

Serve accepts connections from the listener and handles the requests.

func (*Proxy) SetAuth

func (p *Proxy) SetAuth(user, pass string)

SetAuth sets the username and password for proxy authentication.

func (*Proxy) SetGMOnly

func (p *Proxy) SetGMOnly(enable bool)

SetGMOnly sets the switch to use ONLY GM TLS

func (*Proxy) SetGMPrefer

func (p *Proxy) SetGMPrefer(enable bool)

SetGMPrefer sets the switch to prefer using GM style TLS

func (*Proxy) SetGMTLS

func (p *Proxy) SetGMTLS(enable bool)

SetGMTLS sets the switch to turn on GM support

func (*Proxy) SetH2

func (p *Proxy) SetH2(enable bool)

SetH2 sets the switch to turn on HTTP2 support

func (*Proxy) SetHTTPForceClose added in v1.3.3

func (p *Proxy) SetHTTPForceClose(enable bool)

SetHTTPForceClose sets proxy no-keepalive

func (*Proxy) SetLowhttpConfig

func (p *Proxy) SetLowhttpConfig(config []lowhttp.LowhttpOpt)

SetLowhttpConfig sets the lowhttp config

func (*Proxy) SetMITM

func (p *Proxy) SetMITM(config *mitm.Config)

SetMITM sets the config to use for MITMing of CONNECT requests.

func (*Proxy) SetMaxContentLength

func (p *Proxy) SetMaxContentLength(i int)

func (*Proxy) SetRequestModifier

func (p *Proxy) SetRequestModifier(reqmod RequestModifier)

SetRequestModifier sets the request modifier.

func (*Proxy) SetResponseModifier

func (p *Proxy) SetResponseModifier(resmod ResponseModifier)

SetResponseModifier sets the response modifier.

func (*Proxy) SetTimeout

func (p *Proxy) SetTimeout(timeout time.Duration)

SetTimeout sets the request timeout of the proxy.

type RequestModifier

type RequestModifier interface {
	// ModifyRequest modifies the request.
	ModifyRequest(req *http.Request) error
}

RequestModifier is an interface that defines a request modifier that can be used by a proxy.

type RequestModifierFunc

type RequestModifierFunc func(req *http.Request) error

RequestModifierFunc is an adapter for using a function with the given signature as a RequestModifier.

func (RequestModifierFunc) ModifyRequest

func (f RequestModifierFunc) ModifyRequest(req *http.Request) error

ModifyRequest modifies the request using the given function.

type RequestResponseModifier

type RequestResponseModifier interface {
	RequestModifier
	ResponseModifier
}

RequestResponseModifier is an interface that is both a ResponseModifier and a RequestModifier.

func Noop

Noop returns a modifier that does not change the request or the response.

type ResponseModifier

type ResponseModifier interface {
	// ModifyResponse modifies the response.
	ModifyResponse(res *http.Response) error
}

ResponseModifier is an interface that defines a response modifier that can be used by a proxy.

type ResponseModifierFunc

type ResponseModifierFunc func(res *http.Response) error

ResponseModifierFunc is an adapter for using a function with the given signature as a ResponseModifier.

func (ResponseModifierFunc) ModifyResponse

func (f ResponseModifierFunc) ModifyResponse(res *http.Response) error

ModifyResponse modifies the response using the given function.

type S5Config

type S5Config struct {
	HandshakeTimeout    time.Duration
	S5RequestTimeout    time.Duration
	DialDstTimeout      time.Duration
	Debug               bool
	DownstreamHTTPProxy string
	ProxyUsername       string
	ProxyPassword       string
	Addr                string
	UdpChangeCache      *utils.Cache[*UDPExchange]
	UdpSrcCache         *utils.Cache[struct{}]
	UdpConn             *net.UDPConn
}

func NewSocks5Config

func NewSocks5Config() *S5Config

func (*S5Config) ConnectionFallback

func (c *S5Config) ConnectionFallback(src, proxiedConn net.Conn) error

func (*S5Config) HandleBind

func (c *S5Config) HandleBind(conn net.Conn, req *S5Request) error

func (*S5Config) HandleConnect

func (c *S5Config) HandleConnect(conn net.Conn, req *S5Request) error

func (*S5Config) HandleS5RequestHeader

func (c *S5Config) HandleS5RequestHeader(conn net.Conn) (*S5Request, error)

func (*S5Config) HandleUDPAssociate

func (c *S5Config) HandleUDPAssociate(conn net.Conn, req *S5Request) error

func (*S5Config) Handshake

func (c *S5Config) Handshake(conn net.Conn) error

func (*S5Config) HijackSource

func (c *S5Config) HijackSource(src, proxiedConn net.Conn) (net.Conn, net.Conn, bool, error)

func (*S5Config) Serve

func (c *S5Config) Serve(Addr string) error

func (*S5Config) ServeConn

func (c *S5Config) ServeConn(conn net.Conn) error

func (*S5Config) UDPHandle

func (c *S5Config) UDPHandle(addr *net.UDPAddr, d *Datagram) error

type S5Request

type S5Request struct {
	Ver     byte
	Cmd     byte
	Rsv     byte // 0x00
	Atyp    byte
	DstHost []byte
	DstPort []byte // 2 bytes
}

func (*S5Request) GetDstHost

func (r *S5Request) GetDstHost() string

func (*S5Request) GetDstPort

func (r *S5Request) GetDstPort() int

type Session

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

Session provides information and storage about a connection.

func (*Session) Get

func (s *Session) Get(key string) (interface{}, bool)

Get takes key and returns the associated value from the session.

func (*Session) Hijack

func (s *Session) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack takes control of the connection from the proxy. No further action will be taken by the proxy and the connection will be closed following the return of the hijacker.

func (*Session) Hijacked

func (s *Session) Hijacked() bool

Hijacked returns whether the connection has been hijacked.

func (*Session) ID

func (s *Session) ID() string

ID returns the session ID.

func (*Session) IsSecure

func (s *Session) IsSecure() bool

IsSecure returns whether the current session is from a secure connection, such as when receiving requests from a TLS connection that has been MITM'd.

func (*Session) MarkInsecure

func (s *Session) MarkInsecure()

MarkInsecure marks the session as insecure.

func (*Session) MarkSecure

func (s *Session) MarkSecure()

MarkSecure marks the session as secure.

func (*Session) Set

func (s *Session) Set(key string, val interface{})

Set takes a key and associates it with val in the session. The value is persisted for the entire session across multiple requests and responses.

type UDPExchange

type UDPExchange struct {
	ClientAddr *net.UDPAddr
	RemoteConn net.Conn
}

Directories

Path Synopsis
Package fifo provides Group, which is a list of modifiers that are executed consecutively.
Package fifo provides Group, which is a list of modifiers that are executed consecutively.
h2
Package h2 contains basic HTTP/2 handling for Martian.
Package h2 contains basic HTTP/2 handling for Martian.
grpc
Package grpc contains gRPC functionality for Martian proxy.
Package grpc contains gRPC functionality for Martian proxy.
Package mitm provides tooling for MITMing TLS connections.
Package mitm provides tooling for MITMing TLS connections.
Package proxyutil provides functionality for building proxies.
Package proxyutil provides functionality for building proxies.

Jump to

Keyboard shortcuts

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