The Python script implements a basic SOCKS (Socket Secure) proxy client, specifically SOCKS4 and SOCKS5. It connects to a specified SOCKS proxy server, measures the time taken for connections, and handles error responses from the SOCKS server. Below is a detailed explanation of the code sections:
from socket import socket, AF_INET, SOCK_STREAM, inet_aton, getaddrinfo, inet_pton, SOL_TCP
from string import ascii_letters
from random import choice
from time import perf_counter
import sys- The code imports necessary modules for socket programming, string manipulation, random selections, time measurements, and system-level operations.
def sock_read(sock, n):
b = ""
i = 0
while (i < n):
c = sock.recv(n - i)
if (len(c) < 1):
raise Exception("Closed socket")
b += c
i += len(c)
return b sock_read(sock, n): Readsnbytes from a socket. It keeps reading until it has received the full amount or the socket is closed.
def str2host(addr, ipv=4):
for i in ascii_letters:
if i in addr[0]:
d = getaddrinfo(addr[0], addr[1], 0, 0, SOL_TCP)
for j in d:
if (len(j[-1]) == 2) and (ipv == 4):
return inet_aton(j[-1][0])
elif (len(j[-1]) == 4) and (ipv == 6):
return inet_pton(AF_INET6, j[-1][0])
raise Exception("Host not found")
return inet_aton(addr[0])str2host(addr, ipv=4): Converts a hostname to an IP address. Usesgetaddrinfoto resolve the hostname and handles both IPv4 and IPv6.
def uint16str(n):
data = []
data.append(chr(n & 255))
n >>= 8
data.append(chr(n & 255))
data.reverse()
return ''.join(data)uint16str(n): Converts a 16-bit integer to a two-character string representation.
def SOCKS4_ex(ans):
s4_ex = { ... }
try:
raise Exception(s4_ex[ord(ans)])
except KeyError:
raise Exception("Unknown error code #" + str(ord(ans)))
def SOCKS5_ex(ans):
s5_ex = { ... }
try:
raise Exception(s5_ex[ord(ans)])
except KeyError:
raise Exception("Unknown error code #" + str(ord(ans)))SOCKS4_ex(ans)andSOCKS5_ex(ans): Functions to handle specific errors returned by the SOCKS server for SOCKS4 and SOCKS5, respectively. They raise exceptions with descriptive error messages.
def SOCKS_hop(sock, addr, proto=4, ipv=4):
if (proto == 4):
...
elif (proto == 5):
...
else:
raise Exception("Unknown SOCKS version")SOCKS_hop(sock, addr, proto=4, ipv=4): This function handles the connection to the SOCKS server based on the specified protocol (SOCKS4 or SOCKS5). It sends the appropriate request and checks for errors.
if __name__ == "__main__":
pass- The script checks if it's being run as the main module.
check_host = "74.125.230.84" # IP www.google.com
check_port = 80
timings = {}
plist = []
i = 1
while (i < len(sys.argv)):
f = open(sys.argv[i], "rt")
...
s.close()
f.close()
i += 1
timed = sorted(timings, key=lambda key: timings[key])
for i in timed:
print(i)- The script takes input files as command line arguments. Each file is expected to contain addresses (IP:port) for SOCKS servers. It connects to each server, measures the time taken to connect, and performs a SOCKS hop to a specified check host (Google's IP). It then sorts the timings and prints the results.
- This Python script connects to specified SOCKS proxies, measures how long it takes to establish a connection, and handles any errors that arise during the process. It is useful for testing the availability and performance of SOCKS proxies.