tunnel

package
v0.0.0-...-4a21aa2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = errors.New("tunnel not found")
	ErrInvalidState  = errors.New("tunnel not in stoppable state")
	ErrAlreadyExists = errors.New("tunnel ID already exists")
)

Functions

This section is empty.

Types

type AdapterConfig

type AdapterConfig struct {
	TLS *tls.Config
}

type AggPolicy

type AggPolicy string
const (
	AggPolicyRoundRobin  AggPolicy = "RoundRobin"
	AggPolicyWeightedBPS AggPolicy = "WeightedBPS"
	AggPolicyMinRTT      AggPolicy = "MinRTT"
	AggPolicyHybrid      AggPolicy = "Hybrid"
)

type AggSnapshot

type AggSnapshot struct {
	TotalBps int64
	Paths    []PathSnapshot
}

type BandwidthAggregator

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

func NewBandwidthAggregator

func NewBandwidthAggregator(policy AggPolicy) *BandwidthAggregator

func (*BandwidthAggregator) AddPath

func (a *BandwidthAggregator) AddPath(exitID string, conn net.Conn)

func (*BandwidthAggregator) Close

func (a *BandwidthAggregator) Close() error

func (*BandwidthAggregator) Write

func (a *BandwidthAggregator) Write(b []byte) (int, error)

type ClientAdapter

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

func NewClientAdapter

func NewClientAdapter(parsedURL *url.URL, logger *logs.Logger) (*ClientAdapter, error)

func (*ClientAdapter) AddEntry

func (a *ClientAdapter) AddEntry(cfg EntryConfig) error

func (*ClientAdapter) AddExit

func (a *ClientAdapter) AddExit(cfg ExitConfig) error

func (*ClientAdapter) AggMetrics

func (a *ClientAdapter) AggMetrics(tunnelID string) AggSnapshot

func (*ClientAdapter) DisableAggregation

func (a *ClientAdapter) DisableAggregation(tunnelID string) error

func (*ClientAdapter) EnableAggregation

func (a *ClientAdapter) EnableAggregation(tunnelID string, policy AggPolicy) error

func (*ClientAdapter) GetHopChain

func (a *ClientAdapter) GetHopChain(tunnelID string) (HopChain, error)

func (*ClientAdapter) ListNodes

func (a *ClientAdapter) ListNodes() NodeSnapshot

func (*ClientAdapter) Metrics

func (a *ClientAdapter) Metrics() CoreMetrics

func (*ClientAdapter) RemoveEntry

func (a *ClientAdapter) RemoveEntry(id string) error

func (*ClientAdapter) RemoveExit

func (a *ClientAdapter) RemoveExit(id string) error

func (*ClientAdapter) SetHopChain

func (a *ClientAdapter) SetHopChain(tunnelID string, chain HopChain) error

func (*ClientAdapter) Start

func (a *ClientAdapter) Start(ctx context.Context) error

type CoreMetrics

type CoreMetrics struct {
	TCPS     int64
	UDPS     int64
	TCPRxBps float64 // 字节/秒,由 Adapter 用单调时间戳计算
	TCPTxBps float64
	PingMs   float64
	PoolSize int
}

type EntryConfig

type EntryConfig struct {
	ID       string
	Protocol Protocol
	Listen   string
	TLS      *tls.Config
}

type EntryHealth

type EntryHealth struct {
	ID    string
	State string
}

type EntryNode

type EntryNode struct {
	Config   EntryConfig
	Listener net.Listener
	Cancel   context.CancelFunc
}

type EntryPool

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

func NewEntryPool

func NewEntryPool(registry *ProtocolRegistry, health *HealthChecker) *EntryPool

func (*EntryPool) Add

func (p *EntryPool) Add(cfg EntryConfig) error

func (*EntryPool) Close

func (p *EntryPool) Close()

func (*EntryPool) List

func (p *EntryPool) List() []EntryConfig

func (*EntryPool) Remove

func (p *EntryPool) Remove(id string) error

type ExitConfig

type ExitConfig struct {
	ID       string
	Addr     string
	Protocol Protocol
	Weight   int
}

type ExitHealth

type ExitHealth struct {
	ID    string
	State string
	RTT   time.Duration
	Load  int64
}

type ExitNode

type ExitNode struct {
	Config ExitConfig
	Load   int64 // atomic
}

func (*ExitNode) DecLoad

func (n *ExitNode) DecLoad()

func (*ExitNode) IncLoad

func (n *ExitNode) IncLoad()

type ExitRegistry

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

func NewExitRegistry

func NewExitRegistry(health *HealthChecker) *ExitRegistry

func (*ExitRegistry) Add

func (r *ExitRegistry) Add(cfg ExitConfig)

func (*ExitRegistry) Remove

func (r *ExitRegistry) Remove(id string)

func (*ExitRegistry) Select

func (r *ExitRegistry) Select() (*ExitNode, error)

func (*ExitRegistry) SetPolicy

func (r *ExitRegistry) SetPolicy(p LBPolicy)

type HealthChecker

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

func NewHealthChecker

func NewHealthChecker() *HealthChecker

func (*HealthChecker) GetHealth

func (hc *HealthChecker) GetHealth(id string) (NodeHealth, bool)

func (*HealthChecker) ReportFailure

func (hc *HealthChecker) ReportFailure(id string)

func (*HealthChecker) ReportSuccess

func (hc *HealthChecker) ReportSuccess(id string, rtt time.Duration)

func (*HealthChecker) StartProbe

func (hc *HealthChecker) StartProbe(ctx context.Context, interval time.Duration, probeFunc func(id string) (time.Duration, error))

type HealthState

type HealthState string
const (
	HealthActive   HealthState = "active"
	HealthDegraded HealthState = "degraded"
	HealthOffline  HealthState = "offline"
)

type Hop

type Hop struct {
	NodeID   string
	Addr     string
	Protocol Protocol
	TLS      *tls.Config
}

type HopChain

type HopChain struct {
	TunnelID string
	Hops     []Hop // 按序执行,index 0 = 第一跳
}

type HybridScheduler

type HybridScheduler struct{}

func (*HybridScheduler) Select

func (s *HybridScheduler) Select(paths []*PathConn) *PathConn

type LBPolicy

type LBPolicy string
const (
	LBWeightedRR   LBPolicy = "WeightedRR"
	LBLeastConn    LBPolicy = "LeastConn"
	LBLatencyAware LBPolicy = "LatencyAware"
)

type MinRTTScheduler

type MinRTTScheduler struct{}

func (*MinRTTScheduler) Select

func (s *MinRTTScheduler) Select(paths []*PathConn) *PathConn

type NodeHealth

type NodeHealth struct {
	ID           string
	RTT          time.Duration
	LossRate     float64
	FailCount    int
	SuccessCount int
	State        HealthState
	LastSeen     time.Time
}

type NodeSnapshot

type NodeSnapshot struct {
	Entries []EntryHealth
	Exits   []ExitHealth
}

type Packet

type Packet struct {
	SeqID uint64
	Data  []byte
}

type PacketReorder

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

func NewPacketReorder

func NewPacketReorder(windowSize int) *PacketReorder

func (*PacketReorder) Insert

func (r *PacketReorder) Insert(p Packet) []Packet

type PathConn

type PathConn struct {
	ExitID string
	Weight float64
	Bps    int64 // atomic
	RTT    int64 // atomic, nanoseconds
}

type PathScheduler

type PathScheduler interface {
	Select(paths []*PathConn) *PathConn
}

type PathSnapshot

type PathSnapshot struct {
	ExitID string
	Bps    int64
	RTT    time.Duration
	Weight float64
}

type Protocol

type Protocol string
const (
	ProtocolTCP  Protocol = "tcp"
	ProtocolUDP  Protocol = "udp"
	ProtocolWS   Protocol = "ws"
	ProtocolTLS  Protocol = "tls"
	ProtocolQUIC Protocol = "quic"
)

type ProtocolAdapter

type ProtocolAdapter interface {
	Name() Protocol
	Listen(ctx context.Context, addr string, cfg AdapterConfig) (net.Listener, error)
	Dial(ctx context.Context, addr string, cfg AdapterConfig) (net.Conn, error)
	Handshake(conn net.Conn) (net.Conn, error)
}

type ProtocolRegistry

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

func NewProtocolRegistry

func NewProtocolRegistry() *ProtocolRegistry

func (*ProtocolRegistry) Get

func (*ProtocolRegistry) Register

func (r *ProtocolRegistry) Register(adapter ProtocolAdapter)

type QUICAdapter

type QUICAdapter struct{}

QUICAdapter implementation

func (QUICAdapter) Dial

func (a QUICAdapter) Dial(ctx context.Context, addr string, cfg AdapterConfig) (net.Conn, error)

func (QUICAdapter) Handshake

func (a QUICAdapter) Handshake(conn net.Conn) (net.Conn, error)

func (QUICAdapter) Listen

func (a QUICAdapter) Listen(ctx context.Context, addr string, cfg AdapterConfig) (net.Listener, error)

func (QUICAdapter) Name

func (a QUICAdapter) Name() Protocol

type RelayEngine

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

func NewRelayEngine

func NewRelayEngine(registry *ProtocolRegistry) *RelayEngine

func (*RelayEngine) Pipe

func (e *RelayEngine) Pipe(conn1, conn2 net.Conn) error

辅助方法:双向 Pipe

func (*RelayEngine) Relay

func (e *RelayEngine) Relay(ctx context.Context, chain HopChain, src net.Conn) error

type RoundRobinScheduler

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

func (*RoundRobinScheduler) Select

func (s *RoundRobinScheduler) Select(paths []*PathConn) *PathConn

type ServerAdapter

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

func NewServerAdapter

func NewServerAdapter(parsedURL *url.URL, tlsCode string, tlsConfig *tls.Config, logger *logs.Logger) (*ServerAdapter, error)

func (*ServerAdapter) AddEntry

func (a *ServerAdapter) AddEntry(cfg EntryConfig) error

func (*ServerAdapter) AddExit

func (a *ServerAdapter) AddExit(cfg ExitConfig) error

func (*ServerAdapter) AggMetrics

func (a *ServerAdapter) AggMetrics(tunnelID string) AggSnapshot

func (*ServerAdapter) DisableAggregation

func (a *ServerAdapter) DisableAggregation(tunnelID string) error

func (*ServerAdapter) EnableAggregation

func (a *ServerAdapter) EnableAggregation(tunnelID string, policy AggPolicy) error

func (*ServerAdapter) GetHopChain

func (a *ServerAdapter) GetHopChain(tunnelID string) (HopChain, error)

func (*ServerAdapter) ListNodes

func (a *ServerAdapter) ListNodes() NodeSnapshot

func (*ServerAdapter) Metrics

func (a *ServerAdapter) Metrics() CoreMetrics

func (*ServerAdapter) RemoveEntry

func (a *ServerAdapter) RemoveEntry(id string) error

func (*ServerAdapter) RemoveExit

func (a *ServerAdapter) RemoveExit(id string) error

func (*ServerAdapter) SetHopChain

func (a *ServerAdapter) SetHopChain(tunnelID string, chain HopChain) error

func (*ServerAdapter) Start

func (a *ServerAdapter) Start(ctx context.Context) error

type TCPAdapter

type TCPAdapter struct{}

TCPAdapter implementation

func (TCPAdapter) Dial

func (a TCPAdapter) Dial(ctx context.Context, addr string, cfg AdapterConfig) (net.Conn, error)

func (TCPAdapter) Handshake

func (a TCPAdapter) Handshake(conn net.Conn) (net.Conn, error)

func (TCPAdapter) Listen

func (a TCPAdapter) Listen(ctx context.Context, addr string, cfg AdapterConfig) (net.Listener, error)

func (TCPAdapter) Name

func (a TCPAdapter) Name() Protocol

type TLSAdapter

type TLSAdapter struct{}

TLSAdapter implementation

func (TLSAdapter) Dial

func (a TLSAdapter) Dial(ctx context.Context, addr string, cfg AdapterConfig) (net.Conn, error)

func (TLSAdapter) Handshake

func (a TLSAdapter) Handshake(conn net.Conn) (net.Conn, error)

func (TLSAdapter) Listen

func (a TLSAdapter) Listen(ctx context.Context, addr string, cfg AdapterConfig) (net.Listener, error)

func (TLSAdapter) Name

func (a TLSAdapter) Name() Protocol

type TunnelCore

type TunnelCore interface {
	Start(ctx context.Context) error
	Metrics() CoreMetrics

	// v5 新增 — 多节点
	AddEntry(cfg EntryConfig) error
	RemoveEntry(id string) error
	AddExit(cfg ExitConfig) error
	RemoveExit(id string) error
	ListNodes() NodeSnapshot

	// v5 新增 — 多跳
	SetHopChain(tunnelID string, chain HopChain) error
	GetHopChain(tunnelID string) (HopChain, error)

	// v5 新增 — 带宽聚合
	EnableAggregation(tunnelID string, policy AggPolicy) error
	DisableAggregation(tunnelID string) error
	AggMetrics(tunnelID string) AggSnapshot
}

TunnelCore 是 Agent 与隧道实例之间的唯一契约

type TunnelEvent

type TunnelEvent struct {
	ID    string
	State TunnelState
	Err   error
}

type TunnelInstance

type TunnelInstance struct {
	Spec TunnelSpec
	// contains filtered or unexported fields
}

func (*TunnelInstance) Core

func (i *TunnelInstance) Core() TunnelCore

func (*TunnelInstance) State

func (i *TunnelInstance) State() TunnelState

type TunnelManager

type TunnelManager struct {

	// v5 全局组件引用
	Protocols *ProtocolRegistry
	Health    *HealthChecker
	Entries   *EntryPool
	Exits     *ExitRegistry
	// contains filtered or unexported fields
}

func NewTunnelManager

func NewTunnelManager(rootCtx context.Context, onChange chan<- TunnelEvent, reg *ProtocolRegistry, hc *HealthChecker, ep *EntryPool, ex *ExitRegistry) *TunnelManager

func (*TunnelManager) All

func (m *TunnelManager) All() []*TunnelInstance

func (*TunnelManager) Get

func (m *TunnelManager) Get(id string) (*TunnelInstance, bool)

func (*TunnelManager) Start

func (m *TunnelManager) Start(spec TunnelSpec) error

func (*TunnelManager) Stop

func (m *TunnelManager) Stop(id string) error

type TunnelSpec

type TunnelSpec struct {
	ID   string
	Mode string // "server" | "client"
	URL  string // NodePass 原生 URL
}

type TunnelState

type TunnelState int32
const (
	StateStarting TunnelState = iota // goroutine 已启动,等待 Start() 返回
	StateRunning                     // 隧道正常工作
	StateStopping                    // 已收到 Stop 指令,context 已 cancel
	StateStopped                     // goroutine 正常退出
	StateError                       // goroutine 退出但有 error
)

func (TunnelState) String

func (s TunnelState) String() string

type UDPAdapter

type UDPAdapter struct{}

UDPAdapter implementation

func (UDPAdapter) Dial

func (a UDPAdapter) Dial(ctx context.Context, addr string, cfg AdapterConfig) (net.Conn, error)

func (UDPAdapter) Handshake

func (a UDPAdapter) Handshake(conn net.Conn) (net.Conn, error)

func (UDPAdapter) Listen

func (a UDPAdapter) Listen(ctx context.Context, addr string, cfg AdapterConfig) (net.Listener, error)

func (UDPAdapter) Name

func (a UDPAdapter) Name() Protocol

type V5Tunnel

type V5Tunnel struct {
	ID   string
	Spec TunnelSpec
	// contains filtered or unexported fields
}

func NewV5Tunnel

func NewV5Tunnel(spec TunnelSpec, reg *ProtocolRegistry, hc *HealthChecker, logger *logs.Logger) *V5Tunnel

func (*V5Tunnel) AddEntry

func (t *V5Tunnel) AddEntry(cfg EntryConfig) error

func (*V5Tunnel) AddExit

func (t *V5Tunnel) AddExit(cfg ExitConfig) error

func (*V5Tunnel) AggMetrics

func (t *V5Tunnel) AggMetrics(tunnelID string) AggSnapshot

func (*V5Tunnel) DisableAggregation

func (t *V5Tunnel) DisableAggregation(tunnelID string) error

func (*V5Tunnel) EnableAggregation

func (t *V5Tunnel) EnableAggregation(tunnelID string, policy AggPolicy) error

func (*V5Tunnel) GetHopChain

func (t *V5Tunnel) GetHopChain(tunnelID string) (HopChain, error)

func (*V5Tunnel) HandleConn

func (t *V5Tunnel) HandleConn(ctx context.Context, src net.Conn)

func (*V5Tunnel) ListNodes

func (t *V5Tunnel) ListNodes() NodeSnapshot

func (*V5Tunnel) Metrics

func (t *V5Tunnel) Metrics() CoreMetrics

func (*V5Tunnel) RemoveEntry

func (t *V5Tunnel) RemoveEntry(id string) error

func (*V5Tunnel) RemoveExit

func (t *V5Tunnel) RemoveExit(id string) error

func (*V5Tunnel) SetHopChain

func (t *V5Tunnel) SetHopChain(tunnelID string, chain HopChain) error

func (*V5Tunnel) Start

func (t *V5Tunnel) Start(ctx context.Context) error

type WSAdapter

type WSAdapter struct{}

WSAdapter implementation

func (WSAdapter) Dial

func (a WSAdapter) Dial(ctx context.Context, addr string, cfg AdapterConfig) (net.Conn, error)

func (WSAdapter) Handshake

func (a WSAdapter) Handshake(conn net.Conn) (net.Conn, error)

func (WSAdapter) Listen

func (a WSAdapter) Listen(ctx context.Context, addr string, cfg AdapterConfig) (net.Listener, error)

func (WSAdapter) Name

func (a WSAdapter) Name() Protocol

type WeightedBPSScheduler

type WeightedBPSScheduler struct{}

func (*WeightedBPSScheduler) Select

func (s *WeightedBPSScheduler) Select(paths []*PathConn) *PathConn

Jump to

Keyboard shortcuts

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