import asyncio, aiohttp, os, re, time

class Zenctry:
    def __init__(self):
        self.sources = [
            "https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all",
            "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 = []

    async def check(self, session, proxy):
        start = time.time()
        try:
            async with session.get("http://google.com", proxy=f"http://{proxy}", timeout=5) as r:
                if r.status == 200:
                    ms = int((time.time() - start) * 1000)
                    self.valid.append(proxy)
                    print(f"[ALIVE] {proxy} | {ms}ms")
        except: pass

    async def start_logic(self):
        print("\n--- Zenctry Proxy Tool ---")
        mode = input("1- Scrape+Check\n2- Local proxies.txt\nSeçim: ")
        proxies = []
        if mode == "1":
            async with aiohttp.ClientSession() as s:
                for url in self.sources:
                    try:
                        async with s.get(url) as r:
                            proxies.extend(re.findall(r'\d+\.\d+\.\d+\.\d+:\d+', await r.text()))
                    except: continue
        else:
            if os.path.exists("proxies.txt"):
                with open("proxies.txt","r") as f: proxies = f.read().splitlines()
        
        proxies = list(set(proxies))
        print(f"[+] {len(proxies)} proxy işleniyor...")
        conn = aiohttp.TCPConnector(limit=1000)
        async with aiohttp.ClientSession(connector=conn) as s:
            await asyncio.gather(*[self.check(s, p) for p in proxies])
        with open("checked.txt","w") as f: f.write("\n".join(self.valid))
        print(f"\n[!] Bitti. Aktif: {len(self.valid)}")

def run():
    asyncio.run(Zenctry().start_logic())
