Documentation
¶
Overview ¶
Package gproxy implements a gnet-based event-driven MTProxy server.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Secrets is the list of allowed proxy secrets.
Secrets []Secret
Host string // Default SNI hostname (from first secret)
// Network
BindAddr string
// TLS Fronting
MaskHost string // Domain to mimic (SNI validation, proxy links)
MaskPort int // Default port
FetchRealCert bool
SpliceUnrecognized bool
CertRefreshHours int
// Certificate fetching (where to connect to get real cert)
// Defaults to MaskHost:MaskPort if not set
CertHost string
CertPort int
// Splice target (where to forward unrecognized clients)
// Defaults to MaskHost:MaskPort if not set
SpliceHost string
SplicePort int
SpliceProxyProtocol int // 0 = off, 1 = v1 (text), 2 = v2 (binary)
// Performance
IPPreference dc.IPPreference
IdleTimeout time.Duration
TimeSkewTolerance time.Duration
// Upstream (DC connection)
Socks5Addr string // SOCKS5 proxy for DC connections (e.g., "127.0.0.1:1080")
// gnet-specific
Multicore bool // Use multiple event loops
ReusePort bool // Enable SO_REUSEPORT
LockOSThread bool // Lock goroutines to OS threads
NumEventLoop int // Number of event loops (0 = auto)
}
Config configures the gnet proxy server.
type ConnContext ¶
type ConnContext struct {
// contains filtered or unexported fields
}
ConnContext holds per-connection state for the gnet event handler.
func NewConnContext ¶
func NewConnContext() *ConnContext
NewConnContext creates a new connection context.
func (*ConnContext) Relay ¶
func (c *ConnContext) Relay() *RelayContext
Relay returns the relay context (lock-free, may be nil).
func (*ConnContext) SetRelay ¶
func (c *ConnContext) SetRelay(r *RelayContext)
SetRelay sets the relay context and transitions to relay state.
func (*ConnContext) SetSpliceConn ¶ added in v0.1.2
func (c *ConnContext) SetSpliceConn(conn net.Conn)
SetSpliceConn sets the splice connection.
func (*ConnContext) SetState ¶
func (c *ConnContext) SetState(state ConnState)
SetState sets the connection state (lock-free).
func (*ConnContext) SpliceConn ¶ added in v0.1.2
func (c *ConnContext) SpliceConn() net.Conn
SpliceConn returns the splice connection (lock-free, may be nil).
func (*ConnContext) State ¶
func (c *ConnContext) State() ConnState
State returns the current connection state (lock-free).
type ConnState ¶
type ConnState int32
ConnState represents the current state of a client connection.
const ( StateReadTLSHeader ConnState = iota // Need 5 bytes for TLS record header StateReadTLSPayload // Need header.length bytes for payload StateReadO2Frame // Need 64 bytes for obfuscated2 frame StateDialingDC // Async dial in progress StateRelaying // Bidirectional relay active StateSplicing // Forward to mask host (invalid client) StateClosed // Connection is closing )
type DCConnContext ¶ added in v0.1.2
type DCConnContext struct {
// Link back to client connection
ClientConn gnet.Conn
// Client context for state access
ClientCtx *ConnContext
// DC ciphers (proxy <-> DC)
DCEncrypt cipher.Stream
DCDecrypt cipher.Stream
// Client ciphers (cached from relay context)
ClientEncrypt cipher.Stream // encrypt TO client
}
DCConnContext holds per-DC-connection state.
type Logger ¶
type Logger interface {
Debug(format string, args ...any)
Info(format string, args ...any)
Warn(format string, args ...any)
Error(format string, args ...any)
}
Logger interface for proxy logging.
type ProxyHandler ¶
type ProxyHandler struct {
gnet.BuiltinEventEngine
// contains filtered or unexported fields
}
ProxyHandler implements gnet.EventHandler for the MTProxy server.
func NewProxyHandler ¶
func NewProxyHandler(cfg *Config, logger Logger) *ProxyHandler
NewProxyHandler creates a new gnet proxy handler.
func (*ProxyHandler) OnBoot ¶
func (h *ProxyHandler) OnBoot(eng gnet.Engine) gnet.Action
OnBoot is called when the gnet engine starts.
func (*ProxyHandler) OnShutdown ¶
func (h *ProxyHandler) OnShutdown(eng gnet.Engine)
OnShutdown is called when the gnet engine shuts down.
type RelayContext ¶
type RelayContext struct {
// Client ciphers (client <-> proxy)
Encryptor cipher.Stream // encrypt data TO client
Decryptor cipher.Stream // decrypt data FROM client
// DC connection and ciphers (proxy <-> DC)
DCConn gnet.Conn // gnet connection to Telegram DC (enrolled in dcClient)
DCEncrypt cipher.Stream // encrypt data TO DC
DCDecrypt cipher.Stream // decrypt data FROM DC
}
RelayContext holds immutable relay state set once after handshake. Read without locking via atomic pointer.
type ReplayCache ¶
type ReplayCache struct {
// contains filtered or unexported fields
}
ReplayCache detects replay attacks by tracking seen session IDs. Uses striped locking (32 shards) to reduce contention under high load.
func NewReplayCache ¶
func NewReplayCache(maxSize int, ttl time.Duration) *ReplayCache
NewReplayCache creates a new replay cache.
func (*ReplayCache) Seen ¶
func (c *ReplayCache) Seen(sessionID []byte) bool
Seen checks if the session ID was seen before. Returns true if this is a replay, false if new.