import socket
import threading
import socks
def handle_connect(client_socket, target_host, target_port, proxy_config):
try:
remote_socket = socks.socksocket()
remote_socket.set_proxy(
socks.SOCKS5,
proxy_config['host'],
proxy_config['port'],
True,
proxy_config['user'],
proxy_config['pass']
)
remote_socket.connect((target_host, target_port))
client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
threading.Thread(target=pipe, args=(client_socket, remote_socket)).start()
threading.Thread(target=pipe, args=(remote_socket, client_socket)).start()
except Exception as e:
print(f"[!] Ошибка подключения к SOCKS5: {e}")
client_socket.close()
def pipe(source, dest):
try:
while True:
data = source.recv(4096)
if not data:
break
dest.sendall(data)
except:
pass
finally:
source.close()
dest.close()
def parse_host_port(request_line):
try:
parts = request_line.split()
if len(parts) >= 2:
host_port = parts[1]
host, port = host_port.split(':')
return host, int(port)
except Exception as e:
print(f"[!] Ошибка разбора хоста: {e}")
return None, None
def handle_client(client_socket, proxy_config):
try:
request = client_socket.recv(4096)
if not request:
client_socket.close()
return
request_line = request.decode(errors='ignore').split('\r\n')[0]
method = request_line.split(' ')[0]
if method == 'CONNECT':
target_host, target_port = parse_host_port(request_line)
if target_host and target_port:
print(f"[>] CONNECT {target_host}:{target_port}")
handle_connect(client_socket, target_host, target_port, proxy_config)
else:
print("[!] Не удалось разобрать хост и порт.")
client_socket.close()
elif method in ['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS']:
headers = request.decode(errors='ignore').split('\r\n')
host_line = next((h for h in headers if h.lower().startswith('host:')), None)
if host_line:
host = host_line.split(':', 1)[1].strip()
if ':' in host:
target_host, target_port = host.split(':')
target_port = int(target_port)
else:
target_host = host
target_port = 80
print(f"[>] {method} {target_host}:{target_port}")
remote_socket = socks.socksocket()
remote_socket.set_proxy(
socks.SOCKS5,
proxy_config['host'],
proxy_config['port'],
True,
proxy_config['user'],
proxy_config['pass']
)
remote_socket.connect((target_host, target_port))
remote_socket.sendall(request)
while True:
data = remote_socket.recv(4096)
if not data:
break
client_socket.sendall(data)
remote_socket.close()
client_socket.close()
else:
print("[!] Host-заголовок не найден.")
client_socket.close()
else:
print(f"[!] Метод {method} не поддерживается.")
client_socket.sendall(b"HTTP/1.1 405 Method Not Allowed\r\n\r\n")
client_socket.close()
except Exception as e:
print(f"[!] Ошибка клиента: {e}")
client_socket.close()
def start_proxy_with_config(SOCKS5_HOST, SOCKS5_PORT, SOCKS5_USER, SOCKS5_PASS, LISTEN_PORT):
LISTEN_HOST = '127.0.0.1' # фиксировано
proxy_config = {
'host': SOCKS5_HOST,
'port': SOCKS5_PORT,
'user': SOCKS5_USER,
'pass': SOCKS5_PASS,
}
print(f"[*] Прокси-сервер запущен на http://{LISTEN_HOST}:{LISTEN_PORT}")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((LISTEN_HOST, LISTEN_PORT))
server.listen(100)
try:
while True:
client_sock, addr = server.accept()
threading.Thread(target=handle_client, args=(client_sock, proxy_config)).start()
except KeyboardInterrupt:
print("[*] Завершение работы...")
finally:
server.close()
start_proxy_with_config(SOCKS5_HOST='ИП',
SOCKS5_PORT=ПОРТ,
SOCKS5_USER='ПОЛЬЗОВАТЕЛЬ ПРОКСИ',
SOCKS5_PASS= 'ПАРОЛЬ ПРОКСИ',
LISTEN_PORT= 6577 (Порт на котором развернуты прокси)
)
Всем спасибо решение нашел с помощью питона