An open API service indexing awesome lists of open source software.

https://github.com/maximilianfeldthusen/proxycheck

straightforward python script to check proxy
https://github.com/maximilianfeldthusen/proxycheck

Last synced: 6 months ago
JSON representation

straightforward python script to check proxy

Awesome Lists containing this project

README

          

## Documentation

### ProxyCheck

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:

### Imports and Setup
```python
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.

### Socket Reading Function
```python
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)`: Reads `n` bytes from a socket. It keeps reading until it has received the full amount or the socket is closed.

### Address Conversion
```python
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. Uses `getaddrinfo` to resolve the hostname and handles both IPv4 and IPv6.

### Conversion Helper
```python
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.

### SOCKS4 and SOCKS5 Error Handling
```python
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)` and `SOCKS5_ex(ans)`: Functions to handle specific errors returned by the SOCKS server for SOCKS4 and SOCKS5, respectively. They raise exceptions with descriptive error messages.

### SOCKS Proxy Connection
```python
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.

### Main Execution Block
```python
if __name__ == "__main__":
pass
```
- The script checks if it's being run as the main module.

### Reading from Input Files
```python
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.

### Summary
- 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.

![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)