Documentation
¶
Overview ¶
Package protoroute 实现协议分流模块:多个服务共用一个 TCP 端口,按客户端最先发送的数据识别协议 (TLS 再看 SNI 与 ALPN,HTTP 再看 Host),把整条连接透传给对应的后端。
文件划分:sniff.go 识别协议,tls.go 解析 TLS ClientHello,router.go 按识别结果选择目标, runner.go 管理监听与连接,migrate.go 从旧版端口转发迁移 SNI 分流规则。 监听、连接数限制、目标故障转移与 PROXY 协议等四层中转的公共部分在 internal/l4proxy,与端口转发模块共用。
Index ¶
- Constants
- type Module
- func (m *Module) ID() string
- func (m *Module) Init(env module.Env) error
- func (m *Module) Metrics() []module.Sample
- func (m *Module) Migrate(store *config.Store) (string, error)
- 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 Route
- type Rule
- type RuleView
- type Settings
- type StatsSnapshot
Constants ¶
const ( // ProtoTLS 按 ClientHello 中的 SNI 与 ALPN 分流,旧配置中协议为空的路由也按 TLS 处理 ProtoTLS = "tls" // ProtoHTTP 为明文 HTTP/1.x 与 HTTP/2 明文直连(h2c prior knowledge),按 Host 头分流 ProtoHTTP = "http" // ProtoSSH 客户端连接后先发送 "SSH-" 开头的版本标识 ProtoSSH = "ssh" // ProtoRDP 为远程桌面,首包是 TPKT 头加 X.224 连接请求 ProtoRDP = "rdp" // ProtoSOCKS 包括 SOCKS4/4a 与 SOCKS5 ProtoSOCKS = "socks" // ProtoOpenVPN 为 TCP 模式的 OpenVPN,首包是客户端硬重置请求 ProtoOpenVPN = "openvpn" // ProtoXMPP 客户端先发送 XML 声明或 stream 开始标签 ProtoXMPP = "xmpp" // ProtoMQTT 首包是 CONNECT 报文 ProtoMQTT = "mqtt" // ProtoPostgres 首包是启动消息、SSLRequest 或 GSSENCRequest ProtoPostgres = "postgresql" // ProtoRTSP 为摄像头、流媒体常用的 RTSP,请求行版本为 RTSP/x ProtoRTSP = "rtsp" // ProtoSIP 为 TCP 上的 SIP 信令,请求行版本为 SIP/2.0 ProtoSIP = "sip" // ProtoMinecraft 为 Minecraft Java 版,按握手包中的服务器地址分流 ProtoMinecraft = "minecraft" // ProtoRedis 为以 RESP 数组发送命令的 Redis 客户端 ProtoRedis = "redis" // ProtoAMQP 为 AMQP 0-9-1(RabbitMQ)与 AMQP 1.0 ProtoAMQP = "amqp" // ProtoSMB 为直接走 TCP 的 SMB1/2/3 ProtoSMB = "smb" // ProtoGit 为 git:// 协议(git daemon) ProtoGit = "git" // ProtoBitTorrent 为未加密的 BitTorrent 对等连接 ProtoBitTorrent = "bittorrent" // ProtoADB 为 adb connect 使用的 Android 调试桥 ProtoADB = "adb" // ProtoIRC 为明文 IRC 客户端 ProtoIRC = "irc" // ProtoSPA 为经 TCP 发来的端口敲门报文(fwknop 客户端 -P tcp),交给端口敲门模块认证,不转发 ProtoSPA = "spa" // ProtoTimeout 表示客户端在识别超时内没有发送任何数据,用于 VNC、MySQL、FTP、SMTP 等由服务端先发送数据的协议 ProtoTimeout = "timeout" )
分流路由可识别的协议。识别只看客户端最先发送的数据,不解密、不终止任何协议。
const ID = "protoroute"
ID 是模块标识。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是协议分流模块。
func (*Module) Summary ¶
func (m *Module) Summary() []module.SummaryItem
Summary 实现 module.Summarizer。
type Route ¶
type Route struct {
Name string `json:"name"`
// Disabled 为 true 时路由临时停用:配置保留,但不参与匹配,命中它的连接按其他路由或默认目标处理。
// 缺省为启用,旧配置与不带该字段的 API 请求都视为启用
Disabled bool `json:"disabled,omitempty"`
// Protocol 为路由匹配的协议(tls、http、ssh、rdp、socks、openvpn、xmpp、mqtt、postgresql、timeout),空按 tls 处理
Protocol string `json:"protocol"`
// Domains 仅 TLS(匹配 SNI)与 HTTP(匹配 Host)路由使用:精确域名、*.example.com 通配(匹配任意层级子域名)
// 或 *(匹配所有携带域名的连接),不区分大小写;留空匹配该协议的全部连接
Domains []string `json:"domains"`
// ALPN 仅 TLS 路由使用:客户端声明的 ALPN 含其中任一项时才匹配,例如 h2、http/1.1、acme-tls/1、postgresql;留空不限
ALPN []string `json:"alpn"`
TargetAddresses []string `json:"targetAddresses"`
// TargetPort 为目标端口,0 表示与监听端口相同
TargetPort int `json:"targetPort"`
// ProxyProtocol 为发给该目标的 PROXY 协议版本(v1/v2),空为不发送
ProxyProtocol string `json:"proxyProtocol"`
// Knock 为端口敲门规则 key:非空时只有持有该规则有效敲门授权的来源地址才能使用这条路由,其余连接直接断开。
// 敲门路由(协议 spa)不能设置
Knock string `json:"knock,omitempty"`
}
Route 是分流规则中的一条路由,作用相当于 haproxy 的一个 backend。
type Rule ¶
type Rule struct {
Key string `json:"key"`
Name string `json:"name"`
Enable bool `json:"enable"`
// Group 为列表中的分组名称,只影响管理界面的展示,留空为未分组
Group string `json:"group"`
Protocols []string `json:"protocols"`
ListenAddress string `json:"listenAddress"`
ListenPorts string `json:"listenPorts"`
// AutoFirewall 为 true 时在本机防火墙(nftables / iptables / ip6tables 的 input 链)放行监听成功的端口,
// 连续端口合并为一条规则,绑定具体地址时只放行发往该地址的连接,只监听回环地址时不放行;规则停止时撤销
AutoFirewall bool `json:"autoFirewall"`
// Routes 是按顺序展示的路由表,匹配优先级见 router.go
Routes []Route `json:"routes"`
// TargetAddresses、TargetPorts、ProxyProtocol 是默认目标:未命中路由或无法识别协议时使用,地址留空表示直接断开
TargetAddresses []string `json:"targetAddresses"`
TargetPorts string `json:"targetPorts"`
ProxyProtocol string `json:"proxyProtocol"`
// SniffTimeout 是等待客户端发来可识别数据的秒数,可带小数(最短 0.1 秒),0 表示默认 5 秒。
// 超时仍未收到任何数据的连接交给「等待超时」路由,因此它也是 VNC 等服务端先发送数据的协议的连接延迟;
// 客户端先发送数据的协议通常在握手后几毫秒内就发出首包,有等待超时路由时可调到 0.3~1 秒
SniffTimeout float64 `json:"sniffTimeout"`
// MaxConnections 是该规则每个端口的最大并发连接数,0 表示使用全局默认值
MaxConnections int `json:"maxConnections"`
// MaxConnsPerSource 是每个端口上单个来源(IPv4 地址、IPv6 /64)的最大并发连接数,0 表示使用全局默认值
MaxConnsPerSource int `json:"maxConnsPerSource"`
// Access 为来源 IP 访问控制(白名单 / 黑名单),在识别协议之前判断;封禁表中的地址无论何种模式都会被拒绝
iplist.Access
Remark string `json:"remark"`
}
Rule 是一条协议分流规则:在监听端口上预读客户端最先发送的数据,识别协议(TLS 再看 SNI 与 ALPN,HTTP 再看 Host)后, 按路由把整条连接透传到对应后端,不解密、不终止任何协议。作用相当于 haproxy 的 req_ssl_sni 规则加上 sslh。
type RuleView ¶
type RuleView struct {
Rule
Running bool `json:"running"`
Error string `json:"error,omitempty"`
Details []string `json:"details,omitempty"`
Stats StatsSnapshot `json:"stats"`
// Firewall 为自动放行防火墙的结果说明,未开启或规则未运行时为空
Firewall string `json:"firewall,omitempty"`
// FirewallFailed 表示自动放行有端口放行失败(例如没有权限),界面据此提示
FirewallFailed bool `json:"firewallFailed,omitempty"`
}
RuleView 是带运行状态的规则,用于 API 输出。
type Settings ¶
type Settings struct {
Enable bool `json:"enable"`
DefaultMaxConnections int `json:"defaultMaxConnections"`
// DefaultMaxConnsPerSource 是单个来源(IPv4 地址、IPv6 /64)的默认最大并发连接数,0 表示取所在端口最大连接数的一半;
// 回环与内网来源不受此限制。只连接不发送数据的连接由识别超时处理,不另设超时
DefaultMaxConnsPerSource int `json:"defaultMaxConnsPerSource"`
// PerSourceExempt 为不受单来源上限约束的来源(IP 或 CIDR),例如把所有访客汇聚成同一个来源的公网中转或前置代理;
// 回环与内网来源本来就不受限制,不必填写
PerSourceExempt []string `json:"perSourceExempt"`
// DialTimeout 是连接目标的超时(秒)
DialTimeout int `json:"dialTimeout"`
}
Settings 是模块级配置。
type StatsSnapshot ¶
type StatsSnapshot struct {
ActiveConns int64 `json:"activeConns"`
TotalConns int64 `json:"totalConns"`
BytesIn int64 `json:"bytesIn"`
BytesOut int64 `json:"bytesOut"`
// RouteConns 与规则的 routes 一一对应,为各路由累计连接数
RouteConns []int64 `json:"routeConns"`
DefaultConns int64 `json:"defaultConns"`
// Rejected 是未命中路由且没有默认目标而被断开的连接数
Rejected int64 `json:"rejected"`
// Denied 是不满足来源 IP 访问控制而被断开的连接数
Denied int64 `json:"denied"`
// KnockDenied 是没有有效敲门授权(规则或路由要求敲门)而被断开的连接数
KnockDenied int64 `json:"knockDenied"`
}
StatsSnapshot 是规则统计的快照,用于 API 输出。