44 lines
No EOL
1.2 KiB
Python
44 lines
No EOL
1.2 KiB
Python
import socket
|
|
import subprocess
|
|
import time
|
|
|
|
|
|
def resolve(host: str) -> str | None:
|
|
try:
|
|
return socket.gethostbyname(host)
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def get_real_ip(timeout: int = 10) -> str:
|
|
try:
|
|
r = subprocess.run(
|
|
["curl", "-s", "--max-time", str(timeout), "https://ifconfig.me/ip"],
|
|
capture_output=True, timeout=timeout + 2,
|
|
)
|
|
return r.stdout.decode().strip() or "unknown (empty)"
|
|
except Exception as e:
|
|
return f"unknown ({e})"
|
|
|
|
|
|
def get_proxied_ip(socks5_port: int, timeout: int = 10) -> str:
|
|
try:
|
|
r = subprocess.run(
|
|
["curl", "-s", "--max-time", str(timeout),
|
|
"-x", f"socks5h://127.0.0.1:{socks5_port}",
|
|
"https://ifconfig.me/ip"],
|
|
capture_output=True, timeout=timeout + 2,
|
|
)
|
|
return r.stdout.decode().strip() or "unknown (empty)"
|
|
except Exception as e:
|
|
return f"unknown ({e})"
|
|
|
|
|
|
def verify_ip(socks5_port: int, retries: int = 3, delay: float = 2.0) -> str:
|
|
for attempt in range(1, retries + 1):
|
|
ip = get_proxied_ip(socks5_port)
|
|
if not ip.startswith("unknown"):
|
|
return ip
|
|
if attempt < retries:
|
|
time.sleep(delay)
|
|
return "unknown" |