l4proxy

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package l4proxy 是四层(TCP/UDP)中转的公共部分:端口表达式、目标选择与故障转移、PROXY 协议头、 带连接数限制的 TCP 监听与流量统计,供端口转发与协议分流模块共用。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanTargets

func CleanTargets(in []string) []string

CleanTargets 去掉目标地址两侧的空白与 IPv6 方括号,并丢弃空项。

func ParsePorts

func ParsePorts(spec string) ([]int, error)

ParsePorts 解析端口表达式,支持 "80"、"80,443"、"8000-8010" 及其组合,保持书写顺序。

func ValidateProxyProtocol

func ValidateProxyProtocol(v string) error

ValidateProxyProtocol 校验发给目标的 PROXY 协议版本:空为不发送,v1 或 v2。

func WritePrefix

func WritePrefix(target, client net.Conn, version string, peeked []byte) error

WritePrefix 在双向转发开始前写入 PROXY 协议头(version 非空时)与已预读的客户端数据,合并为一次写入以减少小包。

Types

type Picker

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

Picker 在多个目标地址间轮询,连接失败时依次尝试下一个。

func NewPicker

func NewPicker(hosts []string) *Picker

NewPicker 创建目标选择器,hosts 为目标地址列表(不含端口)。

func (*Picker) Dial

func (p *Picker) Dial(network string, port int, timeout time.Duration) (net.Conn, error)

Dial 按轮询顺序依次连接各目标地址的 port 端口,返回第一个连接成功的连接。

func (*Picker) DialContext

func (p *Picker) DialContext(ctx context.Context, network string, port int, timeout time.Duration) (net.Conn, error)

DialContext 与 Dial 相同,ctx 取消(如规则停止)时立即放弃,不再尝试后面的目标。

func (*Picker) Hosts

func (p *Picker) Hosts() []string

Hosts 返回目标地址列表。

func (*Picker) Order

func (p *Picker) Order() []string

Order 返回本次尝试的目标顺序:从轮询位置开始的完整一圈。

type PortPair

type PortPair struct {
	Listen int `json:"listen"`
	Target int `json:"target"`
}

PortPair 是一组监听端口到目标端口的映射。

func MapPorts

func MapPorts(listenSpec, targetSpec string) ([]PortPair, error)

MapPorts 计算监听端口与目标端口的映射。 目标端口数量必须与监听端口相同(一一对应),或只有一个(全部转发到同一端口)。 目标端口为空时与监听端口相同。

type Server

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

Server 管理一条规则的全部监听器与连接协程,Stop 时一并关闭并等待退出。

func (*Server) Context

func (s *Server) Context(parent context.Context) context.Context

Context 返回随 Stop 取消的子上下文,规则的监听与连接都应使用它。只能调用一次。

func (*Server) Go

func (s *Server) Go(f func())

Go 启动一个 Stop 时需要等待其退出的协程。

func (*Server) ServeTCP

func (s *Server) ServeTCP(ctx context.Context, network, addr string, maxConns int, warnf func(format string, args ...any), handle func(net.Conn)) error

ServeTCP 在 addr 上监听 TCP,每个连接在独立协程中交给 handle 处理,handle 返回后连接计数释放。 被封禁地址的连接在 Accept 时直接关闭;并发连接超过 maxConns 时新连接被拒绝,并通过 warnf 记录。 handle 负责关闭连接。

func (*Server) Stop

func (s *Server) Stop()

Stop 取消上下文、关闭全部登记的对象,并等待协程退出。

func (*Server) Track

func (s *Server) Track(c io.Closer)

Track 登记一个需要在 Stop 时关闭的对象(监听器、UDP 套接字等)。

type Stats

type Stats struct {
	ActiveConns atomic.Int64
	TotalConns  atomic.Int64
	BytesIn     atomic.Int64 // 客户端 -> 目标
	BytesOut    atomic.Int64 // 目标 -> 客户端
}

Stats 是一条规则的运行统计,所有字段使用原子操作。

Jump to

Keyboard shortcuts

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