Spaces:
Paused
Paused
Create scanner.py
Browse files- scanner.py +112 -0
scanner.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import re
|
| 3 |
+
import random
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
from curl_cffi.requests import AsyncSession
|
| 6 |
+
from curl_cffi.requests.errors import RequestsError
|
| 7 |
+
import config
|
| 8 |
+
|
| 9 |
+
async def check_fragment_status(
|
| 10 |
+
session: AsyncSession,
|
| 11 |
+
word: str,
|
| 12 |
+
semaphore: asyncio.Semaphore,
|
| 13 |
+
job_id: str,
|
| 14 |
+
) -> dict:
|
| 15 |
+
"""Worker engine. Checks a single username against Fragment."""
|
| 16 |
+
async with semaphore:
|
| 17 |
+
target_url = f"https://fragment.com/username/{word}"
|
| 18 |
+
retries = 0
|
| 19 |
+
|
| 20 |
+
while retries <= config.MAX_RETRIES:
|
| 21 |
+
try:
|
| 22 |
+
proxy_dict = None
|
| 23 |
+
if config.PROXY_POOL:
|
| 24 |
+
proxy_str = random.choice(config.PROXY_POOL)
|
| 25 |
+
proxy_dict = {"http": proxy_str, "https": proxy_str}
|
| 26 |
+
|
| 27 |
+
response = await session.get(target_url, timeout=10, proxies=proxy_dict)
|
| 28 |
+
http_status = response.status_code
|
| 29 |
+
text = response.text
|
| 30 |
+
final_url = str(response.url)
|
| 31 |
+
|
| 32 |
+
# CLOUDFLARE DETECTION
|
| 33 |
+
if ("Just a moment..." in text or
|
| 34 |
+
'id="challenge-running"' in text or
|
| 35 |
+
http_status in [429, 403, 503]):
|
| 36 |
+
retries += 1
|
| 37 |
+
await asyncio.sleep(2 ** retries)
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
# ROUTE-AGNOSTIC PARSING
|
| 41 |
+
if http_status in [200, 404]:
|
| 42 |
+
if "/?query=" in final_url:
|
| 43 |
+
row_pattern = rf'<div class="table-cell-value tm-value">@{word}</div>(.*?)</tr'
|
| 44 |
+
row_match = re.search(row_pattern, text, re.IGNORECASE | re.DOTALL)
|
| 45 |
+
if row_match:
|
| 46 |
+
status_match = re.search(r'<div class="table-cell-value tm-value tm-status-[^>]*>(.*?)</div>', row_match.group(1))
|
| 47 |
+
status = status_match.group(1).strip() if status_match else "Unknown State"
|
| 48 |
+
else:
|
| 49 |
+
status = "Not Found in Search Results"
|
| 50 |
+
else:
|
| 51 |
+
match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', text)
|
| 52 |
+
if match:
|
| 53 |
+
status = match.group(1).strip()
|
| 54 |
+
elif "Someone already claimed this username" in text:
|
| 55 |
+
status = "Taken"
|
| 56 |
+
elif "is currently not for sale" in text:
|
| 57 |
+
status = "Unavailable"
|
| 58 |
+
else:
|
| 59 |
+
status = f"Unknown Route: {final_url.replace('https://fragment.com', '')}"
|
| 60 |
+
|
| 61 |
+
await asyncio.sleep(config.DELAY_BETWEEN_REQUESTS)
|
| 62 |
+
return {
|
| 63 |
+
"Username": word,
|
| 64 |
+
"Status": status,
|
| 65 |
+
"Timestamp": datetime.now(timezone.utc).isoformat(),
|
| 66 |
+
"JobID": job_id
|
| 67 |
+
}
|
| 68 |
+
else:
|
| 69 |
+
return {
|
| 70 |
+
"Username": word,
|
| 71 |
+
"Status": f"Failed: HTTP {http_status}",
|
| 72 |
+
"Timestamp": datetime.now(timezone.utc).isoformat(),
|
| 73 |
+
"JobID": job_id
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
except (RequestsError, asyncio.TimeoutError, Exception):
|
| 77 |
+
retries += 1
|
| 78 |
+
await asyncio.sleep(1)
|
| 79 |
+
|
| 80 |
+
return {
|
| 81 |
+
"Username": word,
|
| 82 |
+
"Status": "BLOCKED: Max Retries Hit",
|
| 83 |
+
"Timestamp": datetime.now(timezone.utc).isoformat(),
|
| 84 |
+
"JobID": job_id
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
async def run_scan(
|
| 88 |
+
words: list[str],
|
| 89 |
+
job_id: str,
|
| 90 |
+
stop_event: asyncio.Event,
|
| 91 |
+
progress_callback
|
| 92 |
+
):
|
| 93 |
+
"""Main scan loop processing in batches via asyncio.as_completed."""
|
| 94 |
+
semaphore = asyncio.Semaphore(config.WORKERS)
|
| 95 |
+
|
| 96 |
+
async with AsyncSession(impersonate="chrome110") as session:
|
| 97 |
+
for i in range(0, len(words), config.BATCH_SIZE):
|
| 98 |
+
if stop_event.is_set():
|
| 99 |
+
break
|
| 100 |
+
|
| 101 |
+
batch = words[i : i + config.BATCH_SIZE]
|
| 102 |
+
tasks = [check_fragment_status(session, w, semaphore, job_id) for w in batch]
|
| 103 |
+
|
| 104 |
+
batch_results = []
|
| 105 |
+
for coro in asyncio.as_completed(tasks):
|
| 106 |
+
if stop_event.is_set():
|
| 107 |
+
break
|
| 108 |
+
result = await coro
|
| 109 |
+
batch_results.append(result)
|
| 110 |
+
|
| 111 |
+
completed = min(i + len(batch_results), len(words))
|
| 112 |
+
await progress_callback(completed, len(words), batch_results)
|