socksio library

1.0.0 · active · verified Thu Apr 09

socksio is a sans-I/O implementation of the SOCKS4, SOCKS4A, and SOCKS5 proxy protocols. It provides the state machines necessary for handling SOCKS proxy negotiation without performing any network I/O itself, making it ideal for integration into asynchronous network frameworks. Currently at version 1.0.0, it follows a stable release cadence with releases primarily for bug fixes or minor enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionality of `socksio` by simulating a SOCKS5 proxy handshake. It shows how to initialize a connection, send client data, receive simulated server responses, and observe the state transitions. Remember that `socksio` handles only the protocol state machine, not actual network I/O.

from socksio import Socks5Connection, Socks5AuthMethods, SOCKS5_NO_AUTH, ConnectionState

# This example demonstrates the SOCKS5 state machine, it does not perform actual network I/O.
# In a real application, 'client_greeting_data' would be written to a socket,
# and 'server_greeting_response' would be read from that socket.

# 1. Initialize a SOCKS5 connection to a target host and port.
target_host = "example.com"
target_port = 80
conn = Socks5Connection(target_host, target_port)
print(f"Initial state: {conn.state}") # Expected: ConnectionState.INIT

# 2. Client sends greeting with supported authentication methods.
# For SOCKS5, common is NO_AUTH.
client_greeting_data = conn.send(Socks5AuthMethods([SOCKS5_NO_AUTH]))
print(f"\nClient sends greeting ({len(client_greeting_data)} bytes): {client_greeting_data.hex()}")
print(f"State after sending greeting: {conn.state}") # Expected: ConnectionState.WAITING_FOR_AUTH_METHOD_SELECTION

# 3. Simulate server response to greeting.
# A SOCKS5 server typically responds with version (0x05) and selected method (0x00 for NO_AUTH).
server_greeting_response = b'\x05\x00'
conn.receive(server_greeting_response)
print(f"\nServer responds to greeting ({len(server_greeting_response)} bytes): {server_greeting_response.hex()}")
print(f"State after receiving greeting response: {conn.state}") # Expected: ConnectionState.AUTH_SUCCESS

# 4. Client sends connection request.
# The `send()` method generates the connect request based on target_host/port.
client_connect_data = conn.send()
print(f"\nClient sends connect request ({len(client_connect_data)} bytes): {client_connect_data.hex()}")
print(f"State after sending connect request: {conn.state}") # Expected: ConnectionState.WAITING_FOR_CONNECT_REPLY

# 5. Simulate server response to connection request.
# A successful SOCKS5 connect reply for IPv4 would be:
# 0x05 (version) 0x00 (success) 0x00 (reserved) 0x01 (IPv4 address type)
# followed by bound address (4 bytes, e.g., 127.0.0.1) and bound port (2 bytes, e.g., 80)
server_connect_response = b'\x05\x00\x00\x01\x7f\x00\x00\x01\x00\x50' # 127.0.0.1:80
conn.receive(server_connect_response)
print(f"\nServer responds to connect request ({len(server_connect_response)} bytes): {server_connect_response.hex()}")
print(f"Final state: {conn.state}") # Expected: ConnectionState.OPEN

if conn.is_connected:
    print("\nSOCKS5 handshake successfully completed (simulated).")
    print(f"Bound address: {conn.bound_address}, Bound port: {conn.bound_port}")
else:
    print("\nSOCKS5 handshake failed (simulated).")

view raw JSON →