import socket
import urllib.request
import ssl
from concurrent.futures import ThreadPoolExecutor

# Désactive vérification SSL pour éviter les erreurs sur certains proxies
ssl._create_default_https_context = ssl._create_unverified_context

ports_to_scan = [8080, 3128, 8000, 8888]  # ports classiques de proxy
timeout_socket = 0.5  # timeout pour tester si le port est ouvert
timeout_http = 1      # timeout pour tester YouTube via le proxy
test_url = "https://www.youtube.com/"

def test_proxy(ip, port):
    proxy = f"{ip}:{port}"
    proxy_handler = urllib.request.ProxyHandler({
        "http": f"http://{proxy}",
        "https": f"http://{proxy}"
    })
    opener = urllib.request.build_opener(proxy_handler)
    urllib.request.install_opener(opener)
    try:
        with urllib.request.urlopen(test_url, timeout=timeout_http) as response:
            if response.status == 200:
                print(f"[OK] Proxy fonctionnel : {proxy}")
                return proxy
    except:
        pass
    return None

def scan_ip(ip):
    print(f"[SCAN] Test de l'IP : {ip}")  # console log pour chaque IP
    for port in ports_to_scan:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(timeout_socket)
            result = sock.connect_ex((ip, port))
            sock.close()
            if result == 0:
                # Port ouvert, test YouTube
                proxy = test_proxy(ip, port)
                if proxy:
                    return proxy
        except:
            continue
    return None

if __name__ == "__main__":
    valid_proxies = []
    with ThreadPoolExecutor(max_workers=100) as executor:
        futures = [executor.submit(scan_ip, f"10.24.{i}.{j}") for i in range(0, 256) for j in range(1, 255)]
        for future in futures:
            proxy = future.result()
            if proxy:
                valid_proxies.append(proxy)

    print("\nProxies valides trouvés :")
    for p in valid_proxies:
        print(p)
