Documentation
¶
Index ¶
- Constants
- Variables
- func Debug(args ...any)
- func Debugf(format string, args ...any)
- func Error(args ...any)
- func Errorf(format string, args ...any)
- func Fatal(args ...any)
- func Fatalf(format string, args ...any)
- func Info(args ...any)
- func Infof(format string, args ...any)
- func IsConnAborted(err error) bool
- func IsConnReset(err error) bool
- func IsDomain(hostname string) bool
- func IsEOF(err error) bool
- func ListenAndServe(addr string, cfg *Config) error
- func Panic(args ...any)
- func Panicf(format string, args ...any)
- func Serve(ln net.Listener, cfg *Config) error
- func SetLogLevel(level Level)
- func Trace(args ...any)
- func Tracef(format string, args ...any)
- func Warn(args ...any)
- func Warnf(format string, args ...any)
- type Config
- type Conn
- type Context
- func (c *Context) Debug(args ...any)
- func (c *Context) Debugf(format string, args ...any)
- func (c *Context) Error(args ...any)
- func (c *Context) Errorf(format string, args ...any)
- func (c *Context) Fatal(args ...any)
- func (c *Context) Fatalf(format string, args ...any)
- func (c *Context) Info(args ...any)
- func (c *Context) Infof(format string, args ...any)
- func (c *Context) SetLogLevel(level Level)
- func (c *Context) SetLogger(logger Logger)
- func (c *Context) Warn(args ...any)
- func (c *Context) Warnf(format string, args ...any)
- type DispatchFn
- type Dispatcher
- type HandleHttpFn
- type HandleTcpFn
- type HandleWsFn
- type HandshakeFn
- type HttpHandler
- type Level
- type Limiter
- type Listener
- type Logger
- type Logrus
- type Negotiator
- type PeekReader
- type RawFilter
- type RawHandlerFn
- type RawMatchFn
- type RawMatcher
- type ReqFilter
- type ReqHandlerFn
- type ReqMatchFn
- type ReqMatcher
- type Resolver
- type RespFilter
- type RespHandlerFn
- type RespMatchFn
- type RespMatcher
- type StdLimiter
- type StdResolver
- type TLSConfig
- type TLSConfigFn
- type TcpHandler
- type WsFilter
- type WsHandler
- type WsHandlerFn
- type WsMatchFn
- type WsMatcher
Constants ¶
const CertificatePEM = `` /* 1536-byte string literal not displayed */
const PrivateKeyPEM = `` /* 1674-byte string literal not displayed */
Variables ¶
var Certificate *x509.Certificate
var ErrNilRequest = errors.New("nil request")
var HttpMethods = map[string]struct{}{ http.MethodGet[:3]: {}, http.MethodHead[:3]: {}, http.MethodPost[:3]: {}, http.MethodPut[:3]: {}, http.MethodPatch[:3]: {}, http.MethodConnect[:3]: {}, http.MethodDelete[:3]: {}, http.MethodOptions[:3]: {}, http.MethodTrace[:3]: {}, }
var PrivateKey *rsa.PrivateKey
Functions ¶
func IsConnAborted ¶ added in v1.4.4
func IsConnReset ¶ added in v1.4.4
func ListenAndServe ¶
func SetLogLevel ¶
func SetLogLevel(level Level)
Types ¶
type Config ¶
type Config struct { Limiter Limiter // 限速器(可选) Negotiator Negotiator // 代理协商(HTTP、SOCKS5) Resolver Resolver // 域名解析器 Dispatcher Dispatcher // 请求分发器 DefaultSNI string // 默认 SNI TLSConfig TLSConfig // TLS 配置回调函数 HttpHandler HttpHandler // HTTP 请求处理 WsHandler WsHandler // WebSocket 处理 TcpHandler TcpHandler // TCP 处理 Dialer proxy.Dialer // 连接拨号器(可叠加代理) ClientTLSConfig *tls.Config // 客户端 TLS 配置 // contains filtered or unexported fields }
func (*Config) WithRawMatcher ¶
func (c *Config) WithRawMatcher(matcher ...RawMatcher) *RawFilter
func (*Config) WithReqMatcher ¶
func (c *Config) WithReqMatcher(matcher ...ReqMatcher) *ReqFilter
func (*Config) WithRespMatcher ¶
func (c *Config) WithRespMatcher(matcher ...RespMatcher) *RespFilter
func (*Config) WithWsMatcher ¶
type Conn ¶
type Conn struct { net.Conn PeekRd *PeekReader }
type Context ¶
type Context struct { Id string Conn *Conn *Config DstHost string DstPort string DstConn net.Conn Req *http.Request Extra any // contains filtered or unexported fields }
func (*Context) SetLogLevel ¶
type DispatchFn ¶
func (DispatchFn) Dispatch ¶
func (f DispatchFn) Dispatch(ctx *Context) error
type Dispatcher ¶
type HandleHttpFn ¶
func (HandleHttpFn) HandleHttp ¶
func (f HandleHttpFn) HandleHttp(ctx *Context) error
type HandleTcpFn ¶
func (HandleTcpFn) HandleTcp ¶
func (f HandleTcpFn) HandleTcp(ctx *Context) error
type HandleWsFn ¶
HandleWsFn is a function type adapter that implements the WsHandler interface. HandleWsFn 是一个函数类型适配器,用于实现 WsHandler 接口。
func (HandleWsFn) HandleWs ¶
func (f HandleWsFn) HandleWs(ctx *Context) error
HandleWs calls the function itself to handle the WebSocket request. HandleWs 会直接调用函数本身来处理 WebSocket 请求。
type HandshakeFn ¶
HandshakeFn is a function adapter that implements the Negotiator interface. HandshakeFn 是一个实现 Negotiator 接口的函数适配器。
var HttpNegotiator HandshakeFn = func(ctx *Context) error { req, err := http.ReadRequest(bufio.NewReader(ctx.Conn.PeekRd)) if err != nil { ctx.Error(err) return err } if req.Method == http.MethodConnect { _, err = http.ReadRequest(bufio.NewReader(ctx.Conn)) if err != nil { ctx.Error(err) return err } status := "Connection Established" resp := fmt.Sprintf("%s %d %s\r\n\r\n", req.Proto, http.StatusOK, status) _, err = ctx.Conn.Write([]byte(resp)) if err != nil { ctx.Error(err) return err } } ctx.DstHost = req.URL.Hostname() ctx.DstPort = req.URL.Port() if ctx.DstPort == "" { switch req.Method { case http.MethodConnect: ctx.DstPort = "443" default: ctx.DstPort = "80" } } return nil }
var Socks5Negotiator HandshakeFn = func(ctx *Context) error { buf := make([]byte, 2) if _, err := ctx.Conn.Read(buf); err != nil { return err } if buf[0] != 0x05 { return errors.New("unsupported version") } methods := make([]byte, buf[1]) if _, err := ctx.Conn.Read(methods); err != nil { return err } if _, err := ctx.Conn.Write([]byte{0x05, 0x00}); err != nil { return err } buf = make([]byte, 4) if _, err := ctx.Conn.Read(buf); err != nil { return err } if buf[0] != 0x05 || buf[1] != 0x01 { return errors.New("unsupported request") } ctx.Debugf("Parsing SOCKS5 request: VER=0x%02X CMD=0x%02X", buf[0], buf[1]) switch buf[3] { case 0x01: ipv4 := make([]byte, 4) if _, err := ctx.Conn.Read(ipv4); err != nil { return err } ctx.DstHost = net.IP(ipv4).String() case 0x03: aLen := make([]byte, 1) if _, err := ctx.Conn.Read(aLen); err != nil { return err } domain := make([]byte, aLen[0]) if _, err := ctx.Conn.Read(domain); err != nil { return err } ctx.DstHost = string(domain) case 0x04: ipv6 := make([]byte, 16) if _, err := ctx.Conn.Read(ipv6); err != nil { return err } ctx.DstHost = net.IP(ipv6).String() default: return errors.New("unsupported address type") } port := make([]byte, 2) if _, err := ctx.Conn.Read(port); err != nil { return err } ctx.DstPort = strconv.Itoa(int(port[0])<<8 | int(port[1])) _, _ = ctx.Conn.Write([]byte{ 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }) return nil }
Socks5Negotiator handles SOCKS5 protocol negotiation as per RFC 1928. Socks5Negotiator 按照 RFC 1928 实现 SOCKS5 协议握手。
func (HandshakeFn) Handshake ¶
func (f HandshakeFn) Handshake(ctx *Context) error
Handshake calls the function itself. Handshake 方法直接调用函数本体。
type HttpHandler ¶
type Negotiator ¶
Negotiator defines the handshake behavior for various protocols. Negotiator 接口定义了各种协议的握手行为。
type PeekReader ¶ added in v1.4.4
type PeekReader struct {
// contains filtered or unexported fields
}
type RawFilter ¶
type RawFilter struct {
// contains filtered or unexported fields
}
func (*RawFilter) Handle ¶ added in v1.3.0
func (r *RawFilter) Handle(handle RawHandlerFn)
type RawHandlerFn ¶
type RawMatchFn ¶
func RawHostIs ¶
func RawHostIs(hosts ...string) RawMatchFn
type RawMatcher ¶
type ReqFilter ¶
type ReqFilter struct {
// contains filtered or unexported fields
}
func (*ReqFilter) Handle ¶
func (r *ReqFilter) Handle(handle ReqHandlerFn)
type ReqMatchFn ¶
func Not ¶
func Not(matcher ReqMatcher) ReqMatchFn
func ReqHostIs ¶
func ReqHostIs(hosts ...string) ReqMatchFn
type ReqMatcher ¶
type ReqMatcher interface { MatchReq(*http.Request, *Context) bool RespMatcher }
type Resolver ¶
func NewResolver ¶
func NewResolver() Resolver
type RespFilter ¶
type RespFilter struct {
// contains filtered or unexported fields
}
func (*RespFilter) Handle ¶
func (r *RespFilter) Handle(handle RespHandlerFn)
type RespMatchFn ¶
func StatusCodeIs ¶
func StatusCodeIs(codes ...int) RespMatchFn
type StdLimiter ¶ added in v1.3.2
func (*StdLimiter) Acquire ¶ added in v1.3.2
func (l *StdLimiter) Acquire()
func (*StdLimiter) Release ¶ added in v1.3.2
func (l *StdLimiter) Release()
type StdResolver ¶ added in v1.3.4
func (StdResolver) SetPTR ¶ added in v1.4.0
func (r StdResolver) SetPTR(ip string, domain string)
type TLSConfig ¶
TLSConfig defines an interface for dynamically generating a *tls.Config based on the provided Server Name (SAN). TLSConfig 定义了一个接口,用于根据传入的服务名称(SAN)动态生成 *tls.Config。
type TLSConfigFn ¶
TLSConfigFn is a function adapter that implements the TLSConfig interface. TLSConfigFn 是一个函数适配器,实现了 TLSConfig 接口。
func From ¶
func From(cert *x509.Certificate, privateKey crypto.PrivateKey) TLSConfigFn
From returns a static tls.Config using the given certificate and private key. From 使用传入的证书和私钥构造并返回一个固定的 tls.Config。
func FromCA ¶
func FromCA(cert *x509.Certificate, privateKey crypto.PrivateKey) TLSConfigFn
FromCA creates a TLSConfigFn using a CA certificate and private key, which generates leaf certificates signed by the CA for the specified SAN. FromCA 使用 CA 证书和私钥返回一个 TLSConfigFn, 会为指定的 SAN 签发由 CA 签名的服务端证书。
func FromSelfSigned ¶
func FromSelfSigned() TLSConfigFn
FromSelfSigned generates a self-signed certificate for the given SAN and returns a tls.Config containing that certificate. FromSelfSigned 会为指定 SAN 创建一个自签名证书,并返回包含该证书的 tls.Config。
type TcpHandler ¶
type WsFilter ¶
type WsFilter struct {
// contains filtered or unexported fields
}
func (*WsFilter) Handle ¶
func (w *WsFilter) Handle(handle WsHandlerFn)
type WsHandler ¶
WsHandler defines the interface for handling WebSocket requests. WsHandler 定义了处理 WebSocket 请求的接口。