Successfully implemented high-speed, socket-based proxy connections for both SOCKS5 and HTTP proxies with binary networking protocols.
- Handshake Phase: Binary greeting with authentication methods
- Authentication: Username/password (RFC 1929) with struct packing
- Connection Phase: Binary CONNECT request with address encoding
- Address Types: IPv4, IPv6, and domain name support
- Error Handling: Complete reply code parsing and error messages
_connect_socks5()- Main SOCKS5 connection handler_socks5_authenticate()- Binary authentication packet builder_socks5_send_connect_request()- Address encoding and connection request_get_socks5_error_message()- Human-readable error messages
# Greeting packet: VER + NMETHODS + METHODS
greeting = struct.pack('BB', SOCKS5_VERSION, len(auth_methods))
greeting += bytes(auth_methods)
# Auth packet: VER + ULEN + UNAME + PLEN + PASSWD
auth = struct.pack('B', 0x01)
auth += struct.pack('B', len(username)) + username_bytes
auth += struct.pack('B', len(password)) + password_bytes
# Connect request: VER + CMD + RSV + ATYP + DST.ADDR + DST.PORT
request = struct.pack('BBB', SOCKS5_VERSION, SOCKS5_CMD_CONNECT, 0x00)
request += struct.pack('B', address_type) + address_bytes
request += struct.pack('>H', port) # Big-endian port- Request Building: Proper HTTP/1.1 CONNECT method
- Authentication: Basic auth with Base64 encoding
- Response Parsing: Line-by-line HTTP response reading
- Status Handling: 200, 407, and other status codes
- Header Management: Proper header formatting and parsing
_connect_http()- HTTP CONNECT tunnel establishment- Binary response reading with
\r\n\r\ndetection - Status line parsing and error extraction
# HTTP CONNECT request
connect_request = "CONNECT host:port HTTP/1.1\r\n"
connect_request += "Host: host:port\r\n"
connect_request += "Proxy-Authorization: Basic {base64}\r\n"
connect_request += "\r\n"
# Response parsing - byte-by-byte reading until headers end
while not response_buffer.endswith(b"\r\n\r\n"):
chunk = await socket.recv(1)
response_buffer += chunk# TCP_NODELAY - Disable Nagle's algorithm for low latency
socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
# SO_KEEPALIVE - Enable TCP keepalive
socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# Configurable timeout and buffer size
socket.settimeout(timeout)_socket_send_all()- Ensures all bytes are sent (handles partial writes)_socket_recv_exact()- Receives exact number of bytes (blocks until complete)- Proper error handling for timeout, broken connections, and socket errors
send_data(data: bytes)- Send raw binary datareceive_data(buffer_size)- Receive binary data with bufferreceive_all(timeout)- Receive until connection closes
send_packet(packet: bytes)- Send with 4-byte length prefixreceive_packet()- Receive with length prefix- Format:
[4 bytes length][N bytes data]
# Send packet: length (4 bytes big-endian) + data
length_prefix = struct.pack('>I', len(packet))
full_packet = length_prefix + packet
# Receive packet: read length, then read exact data
length_bytes = await _socket_recv_exact(4)
packet_length = struct.unpack('>I', length_bytes)[0]
packet = await _socket_recv_exact(packet_length)send_http_request()- Complete HTTP request through tunnel- Builds proper HTTP/1.1 requests with headers
- Handles request body and Content-Length
- Returns complete response
- Context manager support (
async with) - Connection state tracking
- Graceful disconnect with
shutdown() - Connection validation with
SO_ERROR - Connection info dictionary
connection = Connection(
proxy_address="127.0.0.1",
proxy_port=1080,
proxy_type='socks5', # or 'http'
username="user", # optional
password="pass", # optional
timeout=30, # seconds
buffer_size=8192 # bytes
)
# Runtime adjustments
conn.set_timeout(60)
conn.set_buffer_size(16384)
# Connection info
info = conn.get_connection_info()ProxyConnectionError- Connection issuesProxyAuthenticationError- Auth failures
- SOCKS5 reply codes with descriptions
- HTTP status code parsing
- Socket timeout and broken connection detection
- Validation errors (packet size, credentials length)
import logging
logger = logging.getLogger(__name__)
# Debug logging throughout
logger.debug("Connecting to SOCKS5 proxy...")
logger.debug(f"Sent greeting: {greeting.hex()}")
logger.info(f"Successfully connected to {host}:{port}")- Direct Socket API - No HTTP library overhead
- TCP_NODELAY - Immediate packet sending
- Binary Operations - Struct packing instead of string manipulation
- Async I/O - Non-blocking operations
- Configurable Buffer - Tunable for workload
- Connection Reuse - Keepalive enabled
- Smaller Packets - Binary vs text encoding
- Faster Parsing - Struct unpacking vs string parsing
- Precise Control - Exact byte operations
- Protocol Compliance - RFC-compliant implementation
async with Connection("127.0.0.1", 1080, 'socks5') as conn:
await conn.connect("example.com", 80)
await conn.send_data(b"GET / HTTP/1.1\r\n\r\n")
response = await conn.receive_all()conn = Connection(
"proxy.com", 8080, 'http',
username="user", password="pass"
)
await conn.connect("api.example.com", 443)
await conn.send_data(tls_handshake_bytes)# Send with framing
binary_packet = b"\x00\x01\x02\x03"
await conn.send_packet(binary_packet)
# Receive with framing
received = await conn.receive_packet()- ✅ Type hints throughout
- ✅ Async/await patterns
- ✅ Context managers
- ✅ Comprehensive error handling
- ✅ Logging support
- ✅ Documentation strings
- ✅ Binary protocol constants
- ✅ Validation and sanitization
- Timeout protection
- Max packet size validation (10MB limit)
- Credential length validation
- Socket error checking
- Graceful shutdown
- Greeting → Server chooses auth method
- Authentication → Username/password validation (if required)
- Connection Request → Target address + port
- Server Reply → Success or error code
- Data Transfer → Bidirectional communication
- CONNECT Request → Target host:port
- Proxy Authentication → Basic auth header (if required)
- Response → 200 Connection Established
- Tunnel → Direct TCP forwarding
- All network data as
bytes - Struct module for packing/unpacking
- Big-endian for network byte order
- Proper encoding for text (UTF-8)
- Hex display for debugging
Potential additions:
- Connection pooling
- SOCKS4 support
- UDP support for SOCKS5
- SSL/TLS wrapping
- Proxy chaining
- Performance metrics
- Retry logic
- Rate limiting
- Connection health checks
- RFC 1928 - SOCKS Protocol Version 5
- RFC 1929 - Username/Password Authentication for SOCKS V5
- RFC 7231 - HTTP/1.1 Semantics (CONNECT method)
- Python socket documentation
- Python struct documentation
- Python asyncio documentation
A complete, high-performance proxy library with:
- Pure socket implementation
- Binary networking protocols
- Full SOCKS5 and HTTP proxy support
- Authentication for both protocols
- Optimized for speed and reliability
- Production-ready error handling
- Comprehensive examples and documentation