Documentation
¶
Overview ¶
Package sockloop provides packet processing loops for DNS tunneling. It manages the sending and receiving of DNS packets with support for batched operations and multipath routing.
Index ¶
- Variables
- func ProcessQueryFunc(process func(context.Context, []byte) ([]byte, error)) func(*net.UDPAddr, []byte) ([]byte, error)
- type BaseLoop
- func (l *BaseLoop) Context() context.Context
- func (l *BaseLoop) GetPacket() *Packet
- func (l *BaseLoop) IsRunning() bool
- func (l *BaseLoop) PutPacket(pkt *Packet)
- func (l *BaseLoop) QueueSend(pkt *Packet) bool
- func (l *BaseLoop) QueueSendData(data []byte, peerAddr *net.UDPAddr, pathID int) bool
- func (l *BaseLoop) RecordDecodeError()
- func (l *BaseLoop) RecordEncodeError()
- func (l *BaseLoop) RecordHandlerError()
- func (l *BaseLoop) RecordReceive(bytes int)
- func (l *BaseLoop) RecordReceiveError()
- func (l *BaseLoop) RecordSend(bytes int)
- func (l *BaseLoop) RecordSendError()
- func (l *BaseLoop) State() LoopState
- func (l *BaseLoop) Stats() LoopStats
- func (l *BaseLoop) Stop()
- type ClientLoop
- func (l *ClientLoop) AddResolver(id int, addr *net.UDPAddr) error
- func (l *ClientLoop) GetResolvers() []*ResolverInfo
- func (l *ClientLoop) RemoveResolver(id int) error
- func (l *ClientLoop) ResolverStats() []ResolverInfo
- func (l *ClientLoop) SendData(data []byte, pathID int) bool
- func (l *ClientLoop) SendDataToAll(data []byte) bool
- func (l *ClientLoop) SendPoll(pathID int) bool
- func (l *ClientLoop) SendPollToAll()
- func (l *ClientLoop) SetCallbacks(onResponse, onPollResponse func(int, []byte), onError func(error))
- func (l *ClientLoop) Start() error
- func (l *ClientLoop) Stats() ClientLoopStats
- func (l *ClientLoop) Stop()
- type ClientLoopConfig
- type ClientLoopStats
- type LoopConfig
- type LoopState
- type LoopStats
- type Packet
- type PacketDecoder
- type PacketEncoder
- type PacketHandler
- type PacketHandlerFunc
- type PacketPool
- type PacketQueue
- func (q *PacketQueue) Close()
- func (q *PacketQueue) Dequeue() *Packet
- func (q *PacketQueue) DequeueBatch(n int) []*Packet
- func (q *PacketQueue) DequeueBlocking(ctx context.Context) *Packet
- func (q *PacketQueue) Enqueue(pkt *Packet) bool
- func (q *PacketQueue) Items() <-chan *Packet
- func (q *PacketQueue) Len() int
- type ResolverInfo
- type ServerLoop
- func (l *ServerLoop) Addr() net.Addr
- func (l *ServerLoop) SendErrorResponse(peerAddr *net.UDPAddr, rawQuery []byte, rcode int)
- func (l *ServerLoop) SendResponse(peerAddr *net.UDPAddr, rawQuery []byte, data []byte)
- func (l *ServerLoop) SetCallbacks(onQuery func(*net.UDPAddr, []byte) ([]byte, error), onError func(error))
- func (l *ServerLoop) Start() error
- func (l *ServerLoop) Stats() ServerLoopStats
- func (l *ServerLoop) Stop()
- type ServerLoopConfig
- type ServerLoopStats
Constants ¶
This section is empty.
Variables ¶
var ( ErrLoopClosed = errors.New("packet loop is closed") ErrLoopRunning = errors.New("packet loop is already running") ErrLoopNotRunning = errors.New("packet loop is not running") ErrEncodeFailed = errors.New("packet encoding failed") ErrDecodeFailed = errors.New("packet decoding failed") ErrSendFailed = errors.New("packet send failed") ErrReceiveFailed = errors.New("packet receive failed") )
Common errors
Functions ¶
Types ¶
type BaseLoop ¶
type BaseLoop struct {
// contains filtered or unexported fields
}
BaseLoop provides common loop functionality
func NewBaseLoop ¶
func NewBaseLoop(config *LoopConfig, handler PacketHandler) *BaseLoop
NewBaseLoop creates a new base loop
func (*BaseLoop) QueueSendData ¶
QueueSendData creates a packet and queues it for sending
func (*BaseLoop) RecordDecodeError ¶
func (l *BaseLoop) RecordDecodeError()
RecordDecodeError records a decode error
func (*BaseLoop) RecordEncodeError ¶
func (l *BaseLoop) RecordEncodeError()
RecordEncodeError records an encode error
func (*BaseLoop) RecordHandlerError ¶
func (l *BaseLoop) RecordHandlerError()
RecordHandlerError records a handler error
func (*BaseLoop) RecordReceive ¶
RecordReceive records a received packet
func (*BaseLoop) RecordReceiveError ¶
func (l *BaseLoop) RecordReceiveError()
RecordReceiveError records a receive error
func (*BaseLoop) RecordSend ¶
RecordSend records a sent packet
func (*BaseLoop) RecordSendError ¶
func (l *BaseLoop) RecordSendError()
RecordSendError records a send error
type ClientLoop ¶
type ClientLoop struct {
*BaseLoop
// contains filtered or unexported fields
}
ClientLoop is the packet processing loop for the client
func NewClientLoop ¶
func NewClientLoop(config *ClientLoopConfig, codec *dns.ClientCodec) *ClientLoop
NewClientLoop creates a new client packet loop
func (*ClientLoop) AddResolver ¶
func (l *ClientLoop) AddResolver(id int, addr *net.UDPAddr) error
AddResolver adds a DNS resolver
func (*ClientLoop) GetResolvers ¶
func (l *ClientLoop) GetResolvers() []*ResolverInfo
GetResolvers returns all resolvers
func (*ClientLoop) RemoveResolver ¶
func (l *ClientLoop) RemoveResolver(id int) error
RemoveResolver removes a DNS resolver
func (*ClientLoop) ResolverStats ¶
func (l *ClientLoop) ResolverStats() []ResolverInfo
ResolverStats returns statistics for all resolvers
func (*ClientLoop) SendData ¶
func (l *ClientLoop) SendData(data []byte, pathID int) bool
SendData sends data to the server
func (*ClientLoop) SendDataToAll ¶
func (l *ClientLoop) SendDataToAll(data []byte) bool
SendDataToAll sends data to all resolvers
func (*ClientLoop) SendPoll ¶
func (l *ClientLoop) SendPoll(pathID int) bool
SendPoll sends a poll message
func (*ClientLoop) SendPollToAll ¶
func (l *ClientLoop) SendPollToAll()
SendPollToAll sends a poll message to all resolvers
func (*ClientLoop) SetCallbacks ¶
func (l *ClientLoop) SetCallbacks(onResponse, onPollResponse func(int, []byte), onError func(error))
SetCallbacks sets the callback functions
func (*ClientLoop) Stats ¶
func (l *ClientLoop) Stats() ClientLoopStats
Stats returns the client loop statistics
type ClientLoopConfig ¶
type ClientLoopConfig struct {
// BaseConfig is the base loop configuration
BaseConfig *LoopConfig
// DNSTimeout is the timeout for DNS queries
DNSTimeout time.Duration
// PollInterval is the interval between poll messages
PollInterval time.Duration
// MaxRetries is the maximum number of retries for failed queries
MaxRetries int
// EnableMultipath enables sending to multiple resolvers
EnableMultipath bool
}
ClientLoopConfig holds configuration for the client packet loop
func DefaultClientLoopConfig ¶
func DefaultClientLoopConfig() *ClientLoopConfig
DefaultClientLoopConfig returns default client loop configuration
type ClientLoopStats ¶
type ClientLoopStats struct {
// Base stats
LoopStats
// Query stats
QueriesSent uint64
ResponsesReceived uint64
Timeouts uint64
Retries uint64
// Resolver stats
ResolverCount int
}
ClientLoopStats holds statistics for the client loop
type LoopConfig ¶
type LoopConfig struct {
// BatchSize is the maximum number of packets to process in a batch
BatchSize int
// ReceiveTimeout is the timeout for receive operations
ReceiveTimeout time.Duration
// SendTimeout is the timeout for send operations
SendTimeout time.Duration
// BufferSize is the size of packet buffers
BufferSize int
// EnableGSO enables Generic Segmentation Offload
EnableGSO bool
// MaxGSOSegments is the maximum number of GSO segments
MaxGSOSegments int
}
LoopConfig holds configuration for the packet loop
func DefaultLoopConfig ¶
func DefaultLoopConfig() *LoopConfig
DefaultLoopConfig returns default loop configuration
type LoopState ¶
type LoopState int32
LoopState represents the state of the packet loop
const ( // LoopStateInitial indicates the loop is initialized but not started LoopStateInitial LoopState = iota // LoopStateRunning indicates the loop is running LoopStateRunning // LoopStateStopping indicates the loop is stopping LoopStateStopping // LoopStateStopped indicates the loop is stopped LoopStateStopped )
type LoopStats ¶
type LoopStats struct {
// Packets received
PacketsReceived uint64
// Packets sent
PacketsSent uint64
// Bytes received
BytesReceived uint64
// Bytes sent
BytesSent uint64
// Receive errors
ReceiveErrors uint64
// Send errors
SendErrors uint64
// Encode errors
EncodeErrors uint64
// Decode errors
DecodeErrors uint64
// Handler errors
HandlerErrors uint64
}
LoopStats holds statistics for the packet loop
type Packet ¶
type Packet struct {
// Data is the packet data
Data []byte
// PeerAddr is the remote address
PeerAddr *net.UDPAddr
// LocalAddr is the local address (for server)
LocalAddr *net.UDPAddr
// PathID identifies the path for multipath
PathID int
// Timestamp is when the packet was received/created
Timestamp time.Time
// IsPoll indicates if this is a poll packet
IsPoll bool
// MessageID is the DNS message ID
MessageID uint16
}
Packet represents a network packet with metadata
type PacketDecoder ¶
type PacketDecoder interface {
// Decode decodes a DNS packet into data
Decode(packet []byte) ([]byte, error)
}
PacketDecoder decodes DNS packets into data
type PacketEncoder ¶
type PacketEncoder interface {
// Encode encodes data into a DNS packet
Encode(data []byte) ([]byte, uint16, error)
}
PacketEncoder encodes data into DNS packets
type PacketHandler ¶
type PacketHandler interface {
// HandlePacket processes a received packet
// Returns response data to send, or nil if no response
HandlePacket(ctx context.Context, packet *Packet) ([]byte, error)
}
PacketHandler handles received packets
type PacketHandlerFunc ¶
PacketHandlerFunc is a function that implements PacketHandler
func (PacketHandlerFunc) HandlePacket ¶
HandlePacket implements PacketHandler
type PacketPool ¶
type PacketPool struct {
// contains filtered or unexported fields
}
PacketPool manages a pool of reusable packets
type PacketQueue ¶
type PacketQueue struct {
// contains filtered or unexported fields
}
PacketQueue is a bounded queue for packets
func NewPacketQueue ¶
func NewPacketQueue(size int, pool *PacketPool) *PacketQueue
NewPacketQueue creates a new packet queue
func (*PacketQueue) Dequeue ¶
func (q *PacketQueue) Dequeue() *Packet
Dequeue removes and returns the next packet
func (*PacketQueue) DequeueBatch ¶
func (q *PacketQueue) DequeueBatch(n int) []*Packet
DequeueBatch dequeues up to n packets
func (*PacketQueue) DequeueBlocking ¶
func (q *PacketQueue) DequeueBlocking(ctx context.Context) *Packet
DequeueBlocking blocks until a packet is available
func (*PacketQueue) Enqueue ¶
func (q *PacketQueue) Enqueue(pkt *Packet) bool
Enqueue adds a packet to the queue
func (*PacketQueue) Items ¶
func (q *PacketQueue) Items() <-chan *Packet
Items returns the channel for receiving packets
type ResolverInfo ¶
type ResolverInfo struct {
// ID is the resolver identifier
ID int
// Addr is the resolver address
Addr *net.UDPAddr
// Conn is the UDP connection to the resolver
Conn *net.UDPConn
// RTT is the estimated round-trip time
RTT time.Duration
// Available indicates if the resolver is available
Available bool
// LastResponse is the time of the last response
LastResponse time.Time
// Stats
QueriesSent uint64
ResponsesRecv uint64
Timeouts uint64
Errors uint64
}
ResolverInfo holds information about a DNS resolver
type ServerLoop ¶
type ServerLoop struct {
*BaseLoop
// contains filtered or unexported fields
}
ServerLoop is the packet processing loop for the server
func NewServerLoop ¶
func NewServerLoop(config *ServerLoopConfig, codec *dns.ServerCodec) *ServerLoop
NewServerLoop creates a new server packet loop
func (*ServerLoop) SendErrorResponse ¶
func (l *ServerLoop) SendErrorResponse(peerAddr *net.UDPAddr, rawQuery []byte, rcode int)
SendErrorResponse sends an error response to a client
func (*ServerLoop) SendResponse ¶
func (l *ServerLoop) SendResponse(peerAddr *net.UDPAddr, rawQuery []byte, data []byte)
SendResponse sends a response to a client
func (*ServerLoop) SetCallbacks ¶
func (l *ServerLoop) SetCallbacks(onQuery func(*net.UDPAddr, []byte) ([]byte, error), onError func(error))
SetCallbacks sets the callback functions
func (*ServerLoop) Stats ¶
func (l *ServerLoop) Stats() ServerLoopStats
Stats returns the server loop statistics
type ServerLoopConfig ¶
type ServerLoopConfig struct {
// BaseConfig is the base loop configuration
BaseConfig *LoopConfig
// ListenAddr is the address to listen on
ListenAddr string
// Domain is the domain for DNS queries
Domain string
// ResponseTimeout is the timeout for generating responses
ResponseTimeout time.Duration
}
ServerLoopConfig holds configuration for the server packet loop
func DefaultServerLoopConfig ¶
func DefaultServerLoopConfig() *ServerLoopConfig
DefaultServerLoopConfig returns default server loop configuration