Documentation
¶
Index ¶
- Variables
- func AddrInCIDR(cidr string, n int) (string, error)
- func CloseConns(closers ...io.Closer)
- func DescribeRoute(src, dst net.Conn) string
- func DialFastest(ctx context.Context, dst *Destination, network string, timeout time.Duration, ...) (net.Conn, error)
- func DiscoverInterface() (*net.Interface, net.IP, error)
- func FindSafeCIDR() (string, error)
- func GetRxBytes() uint64
- func GetTxBytes() uint64
- func IsConnectionResetByPeer(err error) bool
- func SetTTL(conn net.Conn, isIPv4 bool, ttl uint8) error
- func TunnelConns(ctx context.Context, resCh chan<- TransferResult, src net.Conn, dst net.Conn, ...)
- func WaitForTunnelCompletion(ctx context.Context, logger zerolog.Logger, resCh <-chan TransferResult, ...) error
- type BindFunc
- type BufferedConn
- type ConnRegistry
- func (p *ConnRegistry[K]) CloseAll()
- func (p *ConnRegistry[K]) Evict(key K)
- func (p *ConnRegistry[K]) Fetch(key K) (*IdleTimeoutConn, bool)
- func (p *ConnRegistry[K]) Has(key K) bool
- func (p *ConnRegistry[K]) RunCleanupLoop(appctx context.Context)
- func (p *ConnRegistry[K]) Size() int
- func (p *ConnRegistry[K]) Store(key K, rawConn net.Conn) *IdleTimeoutConn
- type Destination
- type IPKey
- type IdleTimeoutConn
- type NATKey
- type Route
- type TrackingConn
- type TransferResult
- type TunnelDirType
Constants ¶
This section is empty.
Variables ¶
var ErrBlocked = errors.New("request blocked")
Functions ¶
func CloseConns ¶
CloseConns safely closes one or more io.Closer (like net.Conn). It is nil-safe and intentionally ignores errors from Close(), which is a common pattern in defer statements where handling the error is not feasible or desired.
func DescribeRoute ¶
func DialFastest ¶
func DialFastest( ctx context.Context, dst *Destination, network string, timeout time.Duration, bindFunc BindFunc, ) (net.Conn, error)
DialFastest attempts robust connections to the server and returns the first successful conn. All the other connections will be automatically canceled by calling `cancel()` timeout is applied per-dial as a deadline; pass 0 to disable. If bindFunc is not nil, it is called to configure the dialer before each dial attempt.
func DiscoverInterface ¶ added in v1.5.6
DiscoverInterface discovers the interface of the default route and its local IP from the OS. It does not require a gateway.
func FindSafeCIDR ¶
func GetRxBytes ¶
func GetRxBytes() uint64
func GetTxBytes ¶
func GetTxBytes() uint64
func IsConnectionResetByPeer ¶
func SetTTL ¶
SetTTL configures the TTL or Hop Limit depending on the IP version. The isIPv4 parameter is determined by examining the remote address of the connection.
func TunnelConns ¶
func TunnelConns( ctx context.Context, resCh chan<- TransferResult, src net.Conn, dst net.Conn, dir TunnelDirType, )
TunnelConns copies data from src to dst. It sends the result to resCh upon completion. It filters out benign errors like EOF, pipe closed, or read timeouts (for UDP).
func WaitForTunnelCompletion ¶
func WaitForTunnelCompletion( ctx context.Context, logger zerolog.Logger, resCh <-chan TransferResult, startedAt time.Time, route string, errHandler func(errs []error) error, ) error
WaitForTunnelCompletion aggregates results and logs the summary. errHandler processes the list of errors to determine the final error.
Types ¶
type BindFunc ¶
BindFunc is called to configure the dialer before dialing. If it returns an error, the dial attempt fails.
type BufferedConn ¶
BufferedConn wraps a net.Conn with a bufio.Reader to support peeking.
func NewBufferedConn ¶
func NewBufferedConn(c net.Conn) *BufferedConn
type ConnRegistry ¶
ConnRegistry manages UDP connections with LRU eviction policy and idle timeout.
func NewConnRegistry ¶
NewConnRegistry creates a new pool with the specified capacity and timeout.
func (*ConnRegistry[K]) CloseAll ¶
func (p *ConnRegistry[K]) CloseAll()
CloseAll closes all connections in the pool.
func (*ConnRegistry[K]) Evict ¶
func (p *ConnRegistry[K]) Evict(key K)
Evict closes and removes the connection from the pool.
func (*ConnRegistry[K]) Fetch ¶
func (p *ConnRegistry[K]) Fetch(key K) (*IdleTimeoutConn, bool)
Fetch retrieves a connection from the pool, refreshing its LRU status.
func (*ConnRegistry[K]) Has ¶
func (p *ConnRegistry[K]) Has(key K) bool
Has checks if the connection exists in the cache.
func (*ConnRegistry[K]) RunCleanupLoop ¶
func (p *ConnRegistry[K]) RunCleanupLoop(appctx context.Context)
RunCleanupLoop runs the background cleanup goroutine. It exits when appctx is cancelled, closing all remaining cached connections.
func (*ConnRegistry[K]) Size ¶
func (p *ConnRegistry[K]) Size() int
Size returns the number of connections in the pool.
func (*ConnRegistry[K]) Store ¶
func (p *ConnRegistry[K]) Store(key K, rawConn net.Conn) *IdleTimeoutConn
Store adds a connection to the cache and returns the wrapped connection. If capacity is full, evicts the least recently used connection.
type Destination ¶
Destination identifies *where* to dial — domain name, candidate IPs, and port. Per-dial concerns (timeout, bind hook, etc.) are passed to DialFastest separately so a Destination value can be reused across rule lookups without carrying stale settings.
func (*Destination) String ¶
func (d *Destination) String() string
type IdleTimeoutConn ¶
type IdleTimeoutConn struct {
net.Conn
Key any
Timeout time.Duration
// contains filtered or unexported fields
}
IdleTimeoutConn wraps a net.Conn to extend the deadline on every Read/Write call. This is useful for sessions which should stay alive as long as there is activity.
func NewIdleTimeoutConn ¶
func NewIdleTimeoutConn(conn net.Conn, timeout time.Duration) *IdleTimeoutConn
NewIdleTimeoutConn wraps a net.Conn and securely initializes its internal atomic deadlines.
func (*IdleTimeoutConn) Close ¶
func (c *IdleTimeoutConn) Close() error
func (*IdleTimeoutConn) ExtendDeadline ¶
func (c *IdleTimeoutConn) ExtendDeadline() bool
ExtendDeadline attempts to extend the connection's deadline. Returns false if the connection was already expired.
type NATKey ¶
NATKey represents a 4-tuple (SrcIP, SrcPort, DstIP, DstPort) for zero-allocation NAT session mapping
type Route ¶
Route represents the default network route.
func DiscoverRoute ¶ added in v1.5.6
DiscoverRoute discovers the default network route. If gateway is nil, the gateway IP and interface are resolved from the OS routing table. Otherwise the given gateway is used as is, and the interface is the one whose subnet contains it, so the two always match.
type TrackingConn ¶
type TransferResult ¶
type TransferResult struct {
Written int64
Dir TunnelDirType
Err error
}
TransferResult holds the result of a unidirectional tunnel transfer.