sniproxy

package
v0.0.0-...-46ed21b Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSNI           = errors.New("no SNI extension found")
	ErrInvalidTLS      = errors.New("invalid TLS record")
	ErrTruncatedData   = errors.New("truncated TLS data")
	ErrUnsupportedType = errors.New("unsupported TLS record type")
)

SNI 解析相关错误

Functions

func ParseSNI

func ParseSNI(data []byte) (string, error)

ParseSNI 从 TLS ClientHello 中解析 SNI

func PeekClientHello

func PeekClientHello(reader io.Reader) (sni string, data []byte, err error)

PeekClientHello 从连接中预读 TLS ClientHello 并解析 SNI 返回 SNI 和完整的读取数据(需要重放给后端)

Types

type HTTPReporter

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

HTTPReporter HTTP上报器(预留实现)

func NewHTTPReporter

func NewHTTPReporter(endpoint string) *HTTPReporter

NewHTTPReporter 创建HTTP上报器

func (*HTTPReporter) Flush

func (r *HTTPReporter) Flush() error

Flush 刷新缓冲区

func (*HTTPReporter) Report

func (r *HTTPReporter) Report(stats *Stats) error

Report 单条上报

func (*HTTPReporter) ReportBatch

func (r *HTTPReporter) ReportBatch(stats []*Stats) error

ReportBatch 批量上报

type NoopReporter

type NoopReporter struct{}

NoopReporter 空实现的上报器

func (*NoopReporter) Report

func (n *NoopReporter) Report(stats *Stats) error

func (*NoopReporter) ReportBatch

func (n *NoopReporter) ReportBatch(stats []*Stats) error

type Rule

type Rule struct {
	ID            string    `json:"id"`
	SourceIP      string    `json:"source_ip"`      // 源IP或CIDR,空表示匹配所有
	SNIPattern    string    `json:"sni_pattern"`    // SNI模式,支持通配符*
	TargetAddress string    `json:"target_address"` // 目标地址 (host:port)
	Transport     string    `json:"transport"`      // 传输类型: direct, socks5
	Priority      int       `json:"priority"`       // 优先级,数值越大优先级越高
	Enabled       bool      `json:"enabled"`        // 是否启用
	CreatedAt     time.Time `json:"created_at"`
	UpdatedAt     time.Time `json:"updated_at"`
}

Rule SNI代理规则

type RuleMatch

type RuleMatch struct {
	Rule          *Rule
	TargetAddress string
	Transport     string
}

RuleMatch 规则匹配结果

type RuleTable

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

RuleTable 规则表管理器

func NewRuleTable

func NewRuleTable(defaultTransport string) *RuleTable

NewRuleTable 创建规则表

func (*RuleTable) AddRule

func (rt *RuleTable) AddRule(rule *Rule)

AddRule 添加规则

func (*RuleTable) GetRules

func (rt *RuleTable) GetRules() []*Rule

GetRules 获取所有规则

func (*RuleTable) Match

func (rt *RuleTable) Match(sourceIP net.IP, sni string) *RuleMatch

Match 匹配规则

func (*RuleTable) RemoveRule

func (rt *RuleTable) RemoveRule(id string) bool

RemoveRule 移除规则

func (*RuleTable) SetRules

func (rt *RuleTable) SetRules(rules []*Rule)

SetRules 批量设置规则(替换所有现有规则)

func (*RuleTable) UpdateRule

func (rt *RuleTable) UpdateRule(id string, update func(*Rule)) bool

UpdateRule 更新规则

type Server

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

Server SNI代理服务器

func NewServer

func NewServer(cfg *config.Config) (*Server, error)

NewServer 创建SNI代理服务器

func (*Server) GetRuleTable

func (s *Server) GetRuleTable() *RuleTable

GetRuleTable 获取规则表

func (*Server) GetStats

func (s *Server) GetStats() *StatsCollector

GetStats 获取统计收集器

func (*Server) SetACMEHandler

func (s *Server) SetACMEHandler(handler http.Handler)

SetACMEHandler 设置ACME挑战处理器

func (*Server) SetDoHHandler

func (s *Server) SetDoHHandler(handler http.Handler)

SetDoHHandler 设置DoH处理器

func (*Server) SetStatsReporter

func (s *Server) SetStatsReporter(reporter StatsReporter)

SetStatsReporter 设置统计上报器

func (*Server) SetTLSConfig

func (s *Server) SetTLSConfig(tlsConfig *tls.Config)

SetTLSConfig 设置TLS配置(用于DoH)

func (*Server) Start

func (s *Server) Start() error

Start 启动服务器

func (*Server) Stop

func (s *Server) Stop() error

Stop 停止服务器

type Stats

type Stats struct {
	SourceIP      string    `json:"source_ip"`
	SNI           string    `json:"sni"`
	TargetAddress string    `json:"target_address"`
	Transport     string    `json:"transport"`
	BytesRead     int64     `json:"bytes_read"`
	BytesWritten  int64     `json:"bytes_written"`
	StartTime     time.Time `json:"start_time"`
	EndTime       time.Time `json:"end_time,omitempty"`
	Duration      int64     `json:"duration_ms"`
	Status        string    `json:"status"` // active, completed, error
	Error         string    `json:"error,omitempty"`
}

Stats 连接统计信息

type StatsCollector

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

StatsCollector 流量统计收集器

func NewStatsCollector

func NewStatsCollector(reporter StatsReporter) *StatsCollector

NewStatsCollector 创建统计收集器

func (*StatsCollector) EndConnection

func (sc *StatsCollector) EndConnection(connID string, err error)

EndConnection 记录连接结束

func (*StatsCollector) GetActiveConnections

func (sc *StatsCollector) GetActiveConnections() []*Stats

GetActiveConnections 获取活跃连接

func (*StatsCollector) GetSummary

func (sc *StatsCollector) GetSummary() map[string]int64

GetSummary 获取统计摘要

func (*StatsCollector) StartConnection

func (sc *StatsCollector) StartConnection(connID, sourceIP, sni, target, transport string) *Stats

StartConnection 记录连接开始

func (*StatsCollector) UpdateBytes

func (sc *StatsCollector) UpdateBytes(connID string, bytesRead, bytesWritten int64)

UpdateBytes 更新字节统计

type StatsReporter

type StatsReporter interface {
	// Report 上报统计信息
	Report(stats *Stats) error
	// ReportBatch 批量上报
	ReportBatch(stats []*Stats) error
}

StatsReporter 统计上报接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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