Guest User

Untitled

a guest
Jun 26th, 2026
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. import asyncio
  2. import re
  3. import argparse
  4. from pathlib import Path
  5.  
  6. import aiohttp
  7. from aiohttp_socks import ProxyConnector
  8.  
  9.  
  10. # Add/remove sources here.
  11. # Use sources that are allowed to be scraped and respect their terms.
  12. SOURCES = {
  13. "socks5": [
  14. "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/socks5.txt",
  15. "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks5.txt",
  16. ],
  17. "https": [
  18. "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/http.txt",
  19. "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt",
  20. ],
  21. }
  22.  
  23. PROXY_RE = re.compile(
  24. r"(?:(socks5|https?|socks4)://)?"
  25. r"((?:\d{1,3}\.){3}\d{1,3}|[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})"
  26. r":(\d{2,5})",
  27. re.IGNORECASE,
  28. )
  29.  
  30.  
  31. def extract_proxies(text: str, default_type: str):
  32. found = set()
  33.  
  34. for match in PROXY_RE.finditer(text):
  35. scheme, host, port = match.groups()
  36. port = int(port)
  37.  
  38. if not 1 <= port <= 65535:
  39. continue
  40.  
  41. if scheme:
  42. scheme = scheme.lower()
  43. if scheme == "socks5":
  44. proxy_type = "socks5"
  45. elif scheme in {"http", "https"}:
  46. proxy_type = "https"
  47. else:
  48. continue
  49. else:
  50. proxy_type = default_type
  51.  
  52. found.add((proxy_type, f"{host}:{port}"))
  53.  
  54. return found
  55.  
  56.  
  57. async def fetch_source(session, url: str, proxy_type: str):
  58. try:
  59. async with session.get(url, timeout=15) as response:
  60. text = await response.text(errors="ignore")
  61. return extract_proxies(text, proxy_type)
  62. except Exception:
  63. return set()
  64.  
  65.  
  66. async def scrape_all():
  67. results = {
  68. "socks5": set(),
  69. "https": set(),
  70. }
  71.  
  72. async with aiohttp.ClientSession() as session:
  73. tasks = []
  74.  
  75. for proxy_type, urls in SOURCES.items():
  76. for url in urls:
  77. tasks.append(fetch_source(session, url, proxy_type))
  78.  
  79. pages = await asyncio.gather(*tasks)
  80.  
  81. for page_results in pages:
  82. for proxy_type, proxy in page_results:
  83. if proxy_type in results:
  84. results[proxy_type].add(proxy)
  85.  
  86. return results
  87.  
  88.  
  89. async def check_https(proxy: str, timeout: int):
  90. proxy_url = f"http://{proxy}"
  91.  
  92. try:
  93. async with aiohttp.ClientSession() as session:
  94. async with session.get(
  95. "https://httpbin.org/ip",
  96. proxy=proxy_url,
  97. timeout=timeout,
  98. ) as response:
  99. return response.status == 200
  100. except Exception:
  101. return False
  102.  
  103.  
  104. async def check_socks5(proxy: str, timeout: int):
  105. proxy_url = f"socks5://{proxy}"
  106.  
  107. try:
  108. connector = ProxyConnector.from_url(proxy_url)
  109. async with aiohttp.ClientSession(connector=connector) as session:
  110. async with session.get(
  111. "https://httpbin.org/ip",
  112. timeout=timeout,
  113. ) as response:
  114. return response.status == 200
  115. except Exception:
  116. return False
  117.  
  118.  
  119. async def check_many(proxy_type: str, proxies: set[str], concurrency: int, timeout: int):
  120. semaphore = asyncio.Semaphore(concurrency)
  121. alive = set()
  122.  
  123. async def worker(proxy):
  124. async with semaphore:
  125. if proxy_type == "socks5":
  126. ok = await check_socks5(proxy, timeout)
  127. else:
  128. ok = await check_https(proxy, timeout)
  129.  
  130. if ok:
  131. alive.add(proxy)
  132.  
  133. await asyncio.gather(*(worker(proxy) for proxy in proxies))
  134. return alive
  135.  
  136.  
  137. def save_list(filename: str, proxies: set[str]):
  138. Path(filename).write_text(
  139. "\n".join(sorted(proxies)),
  140. encoding="utf-8",
  141. )
  142.  
  143.  
  144. async def main():
  145. parser = argparse.ArgumentParser()
  146. parser.add_argument("--check", action="store_true", help="Check proxies before saving")
  147. parser.add_argument("--concurrency", type=int, default=250)
  148. parser.add_argument("--timeout", type=int, default=7)
  149. args = parser.parse_args()
  150.  
  151. print("[+] Scraping proxies...")
  152. results = await scrape_all()
  153.  
  154. socks5 = results["socks5"]
  155. https = results["https"]
  156.  
  157. print(f"[+] Found SOCKS5: {len(socks5)}")
  158. print(f"[+] Found HTTPS/HTTP-CONNECT: {len(https)}")
  159.  
  160. if args.check:
  161. print("[+] Checking SOCKS5 proxies...")
  162. socks5 = await check_many("socks5", socks5, args.concurrency, args.timeout)
  163.  
  164. print("[+] Checking HTTPS proxies...")
  165. https = await check_many("https", https, args.concurrency, args.timeout)
  166.  
  167. print(f"[+] Alive SOCKS5: {len(socks5)}")
  168. print(f"[+] Alive HTTPS: {len(https)}")
  169.  
  170. save_list("socks5.txt", socks5)
  171. save_list("https.txt", https)
  172.  
  173. print("[+] Saved:")
  174. print(" socks5.txt")
  175. print(" https.txt")
  176.  
  177.  
  178. if __name__ == "__main__":
  179. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment