Documentation
¶
Overview ¶
Package portforward 实现 TCP/UDP 端口转发模块。
Index ¶
- Constants
- func ParsePorts(spec string) ([]int, error)
- type Module
- func (m *Module) ID() string
- func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)
- func (m *Module) ImportRules(rules []Rule) (int, error)
- func (m *Module) Init(env module.Env) error
- func (m *Module) Metrics() []module.Sample
- func (m *Module) Routes(r module.Router)
- func (m *Module) Start(ctx context.Context) error
- func (m *Module) Stop() error
- func (m *Module) Summary() []module.SummaryItem
- func (m *Module) Title() string
- type PortPair
- type Rule
- type RuleView
- type SNIRoute
- type Settings
- type Stats
- type StatsSnapshot
Constants ¶
const ID = "portforward"
ID 是模块标识。
const ModeSNI = "sni"
ModeSNI 是按 SNI 分流的规则模式。
Variables ¶
This section is empty.
Functions ¶
func ParsePorts ¶
ParsePorts 解析端口表达式,支持 "80"、"80,443"、"8000-8010" 及其组合,保持书写顺序。
Types ¶
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是端口转发模块。
func (*Module) ImportLucky ¶
func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)
ImportLucky 实现 luckyimport.Importer。
func (*Module) ImportRules ¶
ImportRules 追加导入规则(由 Lucky 导入器调用),返回导入数量。
func (*Module) Summary ¶
func (m *Module) Summary() []module.SummaryItem
Summary 实现 module.Summarizer。
type Rule ¶
type Rule struct {
Key string `json:"key"`
Name string `json:"name"`
Enable bool `json:"enable"`
Protocols []string `json:"protocols"`
ListenAddress string `json:"listenAddress"`
ListenPorts string `json:"listenPorts"`
TargetAddresses []string `json:"targetAddresses"`
TargetPorts string `json:"targetPorts"`
// MaxConnections 是该规则每个 TCP 端口的最大并发连接数,0 表示使用全局默认值
MaxConnections int `json:"maxConnections"`
// UDPSessionTimeout 是 UDP 会话空闲超时(秒),0 表示使用全局默认值
UDPSessionTimeout int `json:"udpSessionTimeout"`
// ProxyProtocol 非空时,TCP 连接目标后先发送 PROXY 协议头(v1 或 v2),把真实客户端地址告诉下游
// (haproxy accept-proxy、nginx proxy_protocol、Gately Web 服务"接收 PROXY 协议")。目标必须支持,否则连接会被拒绝。
ProxyProtocol string `json:"proxyProtocol"`
// Mode 为空时按端口直接转发;为 "sni" 时读取 TLS ClientHello 中的 SNI,按 SNIRoutes 分流(仅 TCP,TLS 不终止)。
// SNI 模式下 TargetAddresses/TargetPorts/ProxyProtocol 作为默认目标,未命中路由或客户端未携带 SNI 时使用,可留空表示直接断开。
Mode string `json:"mode"`
SNIRoutes []SNIRoute `json:"sniRoutes"`
Remark string `json:"remark"`
}
Rule 是一条端口转发规则。
type RuleView ¶
type RuleView struct {
Rule
Running bool `json:"running"`
Error string `json:"error,omitempty"`
Details []string `json:"details,omitempty"`
Stats StatsSnapshot `json:"stats"`
}
RuleView 是带运行状态的规则,用于 API 输出。
type SNIRoute ¶ added in v0.5.2
type SNIRoute struct {
Name string `json:"name"`
// Domains 为匹配的域名:精确域名、*.example.com 通配(匹配任意层级子域名)或 *(匹配所有携带 SNI 的连接),不区分大小写
Domains []string `json:"domains"`
TargetAddresses []string `json:"targetAddresses"`
// TargetPort 为目标端口,0 表示与监听端口相同
TargetPort int `json:"targetPort"`
// ProxyProtocol 为发给该目标的 PROXY 协议版本(v1/v2),空为不发送
ProxyProtocol string `json:"proxyProtocol"`
}
SNIRoute 是 SNI 分流规则中的一条路由,作用相当于 haproxy 的一个 backend。
type Settings ¶
type Settings struct {
Enable bool `json:"enable"`
DefaultMaxConnections int `json:"defaultMaxConnections"`
DefaultUDPSessionTimeout int `json:"defaultUDPSessionTimeout"`
MaxUDPSessions int `json:"maxUDPSessions"`
// DialTimeout 是连接目标的超时(秒)
DialTimeout int `json:"dialTimeout"`
}
Settings 是模块级配置。
type Stats ¶
type Stats struct {
ActiveConns atomic.Int64
TotalConns atomic.Int64
BytesIn atomic.Int64 // 客户端 -> 目标
BytesOut atomic.Int64 // 目标 -> 客户端
// SNIRejected 是 SNI 未命中且没有默认目标而被断开的连接数
SNIRejected atomic.Int64
}
Stats 是规则的运行统计,所有字段使用原子操作。
type StatsSnapshot ¶
type StatsSnapshot struct {
ActiveConns int64 `json:"activeConns"`
TotalConns int64 `json:"totalConns"`
BytesIn int64 `json:"bytesIn"`
BytesOut int64 `json:"bytesOut"`
// 以下仅 SNI 分流规则输出:RouteConns 与规则的 sniRoutes 一一对应,为各路由累计连接数
RouteConns []int64 `json:"routeConns,omitempty"`
DefaultConns int64 `json:"defaultConns,omitempty"`
SNIRejected int64 `json:"sniRejected,omitempty"`
}
StatsSnapshot 是统计的快照,用于 API 输出。