CCCPaste

8c96531950d03

export PROXY_LOGIN="DoctorZlo1998"
export PROXY_PASSWORD="OMGb0x13"

echo $PROXY_LOGIN
echo $PROXY_PASSWORD
cat > /usr/local/proxy/secure_proxy.py << 'EOF'
import socket
import threading
import select
import socketserver
import time
from collections import defaultdict

PORT = 2784   # новый порт
MAX_REQ_PER_MIN = 8
BLOCK_TIME = 600  # 10 минут бана

requests = defaultdict(list)
blocked = {}

def log(msg):
    print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {msg}")

def is_blocked(ip):
    if ip in blocked and time.time() < blocked[ip]:
        return True
    elif ip in blocked:
        del blocked[ip]
    return False

def register_request(ip):
    now = time.time()
    requests[ip] = [t for t in requests[ip] if now - t < 60]
    requests[ip].append(now)

    if len(requests[ip]) > MAX_REQ_PER_MIN:
        blocked[ip] = now + BLOCK_TIME
        log(f"[BAN] {ip} заблокирован на 10 минут")
        return False
    return True


class Socks5Handler(socketserver.StreamRequestHandler):

    def handle(self):
        client_ip = self.client_address[0]
        log(f"Подключение от {client_ip}")

        if is_blocked(client_ip):
            log(f"[ОТКАЗ] {client_ip} в бане")
            self.connection.close()
            return

        if not register_request(client_ip):
            self.connection.close()
            return

        try:
            self.connection.settimeout(5)

            self.connection.recv(262)
            self.connection.sendall(b"\x05\x00")

            data = self.connection.recv(4)
            if len(data) < 4 or data[0] != 0x05:
                return

            addrtype = data[3]

            if addrtype == 1:  # IPv4
                addr = self.connection.recv(4)
                address = socket.inet_ntoa(addr)
            elif addrtype == 3:  # Domain
                domain_len = self.connection.recv(1)[0]
                domain = self.connection.recv(domain_len).decode()
                address = domain
            else:
                return

            port = int.from_bytes(self.connection.recv(2), 'big')
            log(f"Запрос: {address}:{port}")

            remote = socket.create_connection((address, port), timeout=5)

            self.connection.sendall(
                b"\x05\x00\x00\x01" +
                socket.inet_aton("0.0.0.0") +
                (0).to_bytes(2, 'big')
            )

            while True:
                r, _, _ = select.select([self.connection, remote], [], [], 10)
                if self.connection in r:
                    data = self.connection.recv(4096)
                    if not data:
                        break
                    remote.sendall(data)
                if remote in r:
                    data = remote.recv(4096)
                    if not data:
                        break
                    self.connection.sendall(data)

            remote.close()
            self.connection.close()

        except Exception as e:
            log(f"Ошибка: {e}")
            self.connection.close()


class ThreadedSocksServer(socketserver.ThreadingTCPServer):
    allow_reuse_address = True


if __name__ == "__main__":
    log(f"🟢 SOCKS5 сервер запущен на порту {PORT}")
    server = ThreadedSocksServer(('0.0.0.0', PORT), Socks5Handler)
    server.serve_forever()
EOF