Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import re
- import argparse
- from pathlib import Path
- import aiohttp
- from aiohttp_socks import ProxyConnector
- # Add/remove sources here.
- # Use sources that are allowed to be scraped and respect their terms.
- SOURCES = {
- "socks5": [
- "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/socks5.txt",
- "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks5.txt",
- ],
- "https": [
- "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/http.txt",
- "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt",
- ],
- }
- PROXY_RE = re.compile(
- r"(?:(socks5|https?|socks4)://)?"
- r"((?:\d{1,3}\.){3}\d{1,3}|[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})"
- r":(\d{2,5})",
- re.IGNORECASE,
- )
- def extract_proxies(text: str, default_type: str):
- found = set()
- for match in PROXY_RE.finditer(text):
- scheme, host, port = match.groups()
- port = int(port)
- if not 1 <= port <= 65535:
- continue
- if scheme:
- scheme = scheme.lower()
- if scheme == "socks5":
- proxy_type = "socks5"
- elif scheme in {"http", "https"}:
- proxy_type = "https"
- else:
- continue
- else:
- proxy_type = default_type
- found.add((proxy_type, f"{host}:{port}"))
- return found
- async def fetch_source(session, url: str, proxy_type: str):
- try:
- async with session.get(url, timeout=15) as response:
- text = await response.text(errors="ignore")
- return extract_proxies(text, proxy_type)
- except Exception:
- return set()
- async def scrape_all():
- results = {
- "socks5": set(),
- "https": set(),
- }
- async with aiohttp.ClientSession() as session:
- tasks = []
- for proxy_type, urls in SOURCES.items():
- for url in urls:
- tasks.append(fetch_source(session, url, proxy_type))
- pages = await asyncio.gather(*tasks)
- for page_results in pages:
- for proxy_type, proxy in page_results:
- if proxy_type in results:
- results[proxy_type].add(proxy)
- return results
- async def check_https(proxy: str, timeout: int):
- proxy_url = f"http://{proxy}"
- try:
- async with aiohttp.ClientSession() as session:
- async with session.get(
- "https://httpbin.org/ip",
- proxy=proxy_url,
- timeout=timeout,
- ) as response:
- return response.status == 200
- except Exception:
- return False
- async def check_socks5(proxy: str, timeout: int):
- proxy_url = f"socks5://{proxy}"
- try:
- connector = ProxyConnector.from_url(proxy_url)
- async with aiohttp.ClientSession(connector=connector) as session:
- async with session.get(
- "https://httpbin.org/ip",
- timeout=timeout,
- ) as response:
- return response.status == 200
- except Exception:
- return False
- async def check_many(proxy_type: str, proxies: set[str], concurrency: int, timeout: int):
- semaphore = asyncio.Semaphore(concurrency)
- alive = set()
- async def worker(proxy):
- async with semaphore:
- if proxy_type == "socks5":
- ok = await check_socks5(proxy, timeout)
- else:
- ok = await check_https(proxy, timeout)
- if ok:
- alive.add(proxy)
- await asyncio.gather(*(worker(proxy) for proxy in proxies))
- return alive
- def save_list(filename: str, proxies: set[str]):
- Path(filename).write_text(
- "\n".join(sorted(proxies)),
- encoding="utf-8",
- )
- async def main():
- parser = argparse.ArgumentParser()
- parser.add_argument("--check", action="store_true", help="Check proxies before saving")
- parser.add_argument("--concurrency", type=int, default=250)
- parser.add_argument("--timeout", type=int, default=7)
- args = parser.parse_args()
- print("[+] Scraping proxies...")
- results = await scrape_all()
- socks5 = results["socks5"]
- https = results["https"]
- print(f"[+] Found SOCKS5: {len(socks5)}")
- print(f"[+] Found HTTPS/HTTP-CONNECT: {len(https)}")
- if args.check:
- print("[+] Checking SOCKS5 proxies...")
- socks5 = await check_many("socks5", socks5, args.concurrency, args.timeout)
- print("[+] Checking HTTPS proxies...")
- https = await check_many("https", https, args.concurrency, args.timeout)
- print(f"[+] Alive SOCKS5: {len(socks5)}")
- print(f"[+] Alive HTTPS: {len(https)}")
- save_list("socks5.txt", socks5)
- save_list("https.txt", https)
- print("[+] Saved:")
- print(" socks5.txt")
- print(" https.txt")
- if __name__ == "__main__":
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment