|
from fastapi import FastAPI |
|
import requests |
|
import socks |
|
import socket |
|
import logging |
|
import time |
|
|
|
app = FastAPI(title="Tor IP Checker") |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
@app.get("/") |
|
async def check_tor_ip(): |
|
max_retries = 3 |
|
retry_delay = 5 |
|
for attempt in range(max_retries): |
|
try: |
|
with requests.Session() as session: |
|
session.proxies = { |
|
"http": "socks5h://localhost:9050", |
|
"https": "socks5h://localhost:9050" |
|
} |
|
|
|
logger.info("Requesting IP via Tor proxy...") |
|
response = session.get("https://check.torproject.org/api/ip", timeout=10) |
|
response.raise_for_status() |
|
|
|
data = response.json() |
|
ip_address = data.get("IP", "Unknown") |
|
is_tor = data.get("IsTor", False) |
|
|
|
return { |
|
"status": "success", |
|
"tor_enabled": is_tor, |
|
"ip_address": ip_address, |
|
"message": "You are using Tor!" if is_tor else "You are not using Tor." |
|
} |
|
|
|
except requests.RequestException as e: |
|
logger.error(f"Attempt {attempt + 1}/{max_retries} - Error: {str(e)}") |
|
if attempt < max_retries - 1: |
|
time.sleep(retry_delay) |
|
continue |
|
return { |
|
"status": "error", |
|
"tor_enabled": False, |
|
"ip_address": "Unknown", |
|
"message": f"Failed to check Tor IP after {max_retries} attempts: {str(e)}" |
|
} |