Files
2026-02-16 15:47:28 +01:00

87 lines
2.8 KiB
Go

// Package protocol defines the communication protocol between proxy and agent.
package protocol
import (
"errors"
"net"
)
// Protocol error codes for agent-server communication.
// Uses byte values to minimize binary size and network traffic.
const (
// General errors (0-9)
ErrNone byte = 0 // Operation completed successfully
ErrInvalidCommand byte = 1 // Command type is not recognized
ErrContextCanceled byte = 2 // Context canceled
// Connection errors (10-19)
ErrConnectionClosed byte = 10 // Connection was terminated
ErrConnectionNotFound byte = 11 // Connection ID does not exist
ErrConnectionExists byte = 12 // Connection ID already in use
ErrInvalidState byte = 13 // Connection in wrong state for operation
ErrPacketSendFailed byte = 14 // Packet transmission failed
ErrHandlerStopped byte = 15 // Protocol handler is not running
ErrUnexpectedPacket byte = 16 // Received unexpected packet type
ErrBufferFull byte = 17 // Per-connection delivery buffer is full
// Transport errors (20-29)
ErrTransportClosed byte = 20 // Transport layer terminated
ErrTransportTimeout byte = 21 // Transport operation timed out
ErrTransportError byte = 22 // Transport operation failed
// SOCKS errors (30-39)
ErrInvalidSocksVersion byte = 30 // Unsupported SOCKS protocol version
ErrUnsupportedCommand byte = 31 // SOCKS command not implemented
ErrHostUnreachable byte = 32 // Target host not accessible
ErrConnectionRefused byte = 33 // Target refused connection
ErrNetworkUnreachable byte = 34 // Network path not accessible
ErrAddressNotSupported byte = 35 // Address format not supported
ErrTTLExpired byte = 36 // Time-to-live exceeded
ErrGeneralSocksFailure byte = 37 // Unspecified SOCKS failure
ErrAuthFailed byte = 38 // Authentication rejected
// Packet errors (40-49)
ErrInvalidPacket byte = 40 // Malformed packet structure
)
// MapNetError converts a network error to an appropriate protocol error code.
// This consolidates error mapping logic used throughout the codebase.
func MapNetError(err error) byte {
if err == nil {
return ErrNone
}
// Check for timeout
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return ErrTTLExpired
}
// Check for net.OpError (most common network error type)
if opErr, ok := err.(*net.OpError); ok {
if errors.Is(opErr, net.ErrClosed) {
return ErrTransportClosed
}
switch opErr.Op {
case "dial":
return ErrNetworkUnreachable
case "read", "write":
return ErrHostUnreachable
case "listen":
return ErrNetworkUnreachable
}
}
// Check for DNS errors
if _, ok := err.(*net.DNSError); ok {
return ErrHostUnreachable
}
// Check for closed connection
if errors.Is(err, net.ErrClosed) {
return ErrTransportClosed
}
// Default to connection refused
return ErrConnectionRefused
}