import asyncio
import aiohttp
import os
import re
import sys

class Zenctry:
    def __init__(self):
        self.sources = [
            "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all",
            "https://www.proxy-list.download/api/v1/get?type=http",
            "https://www.proxyscan.io/download?type=http",
            "https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt",
            "https://raw.githubusercontent.com/ShiftyTR/Proxy-List/master/http.txt",
            "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt",
            "https://raw.githubusercontent.com/hookzof/socks5_list/master/proxy.txt",
            "https://raw.githubusercontent.com/clarketm/proxy-list/master/proxy-list-raw.txt",
            "https://raw.githubusercontent.com/sunny9577/proxy-scraper/master/proxies.txt",
            "https://raw.githubusercontent.com/roosterkid/openproxylist/main/HTTPS_RAW.txt"
        ]
        self.valid_proxies = []

    async def scrape(self):
        print("[+] Kaynaklardan proxy toplanıyor...")
        proxies = []
        async with aiohttp.ClientSession() as session:
            for url in self.sources:
                try:
                    async with session.get(url, timeout=10) as resp:
                        text = await resp.text()
                        found = re.findall(r'\d+\.\d+\.\d+\.\d+:\d+', text)
                        proxies.extend(found)
                except:
                    continue
        return list(set(proxies))

    async def check_proxy(self, session, proxy):
        url = "http://httpbin.org/ip"
        try:
            async with session.get(url, proxy=f"http://{proxy}", timeout=5) as resp:
                if resp.status == 200:
                    self.valid_proxies.append(proxy)
                    print(f"[ALIVE] {proxy}")
        except:
            pass

    async def run_checker(self, proxy_list):
        print(f"[+] {len(proxy_list)} proxy kontrol ediliyor (Hız: Max)...")
        connector = aiohttp.TCPConnector(limit=500) # Aynı anda 500 bağlantı
        async with aiohttp.ClientSession(connector=connector) as session:
            tasks = [self.check_proxy(session, p) for p in proxy_list]
            await asyncio.gather(*tasks)

def main():
    tool = Zenctry()
    print("--- Zenctry Proxy Tool ---")
    print("1- Scrape + Fast Check")
    print("2- Local (proxies.txt) Check")
    
    choice = input("\nSeçim yapın: ")
    
    if choice == "1":
        proxies = asyncio.run(tool.scrape())
    elif choice == "2":
        if os.path.exists("proxies.txt"):
            with open("proxies.txt", "r") as f:
                proxies = f.read().splitlines()
        else:
            print("[-] proxies.txt bulunamadı!")
            return
    else:
        return

    asyncio.run(tool.run_checker(proxies))
    
    with open("checked.txt", "w") as f:
        f.write("\n".join(tool.valid_proxies))
    
    print(f"\n[!] İşlem bitti. {len(tool.valid_proxies)} aktif proxy 'checked.txt' dosyasına kaydedildi.")

if __name__ == "__main__":
    main()
