Last active
April 16, 2025 23:57
-
-
Save essingen123/51305caad1ee1ac46bc4be1376a4c0ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess, sys, shutil, argparse | |
""" | |
📡 host_access_diag_with_ip_tor_etc.py | |
A Python script for diagnosing host/IP access issues (DNS, TCP, HTTP/S), with optional Tor check via Docker. | |
🧰 Features: | |
- Tests DNS resolution (local & Google) | |
- TCP connectivity check to any port | |
- HTTP/HTTPS HEAD request | |
- Optional Tor SOCKS5 check via Docker | |
- Automatically pulls & starts Tor in a Docker container if not running | |
💡 Usage: | |
./host_access_diag_with_ip_tor_etc.py example.com:443 | |
./host_access_diag_with_ip_tor_etc.py example.com:2083 --tor 127.0.0.1:9050 | |
By ki | |
""" | |
def check_tool(name): | |
if not shutil.which(name): | |
print(f"[!] Missing tool: {name}. Please install it and retry.") | |
sys.exit(1) | |
def ensure_docker_tor(): | |
"""Ensure a Tor Docker container is running on port 9050.""" | |
try: | |
subprocess.run("docker start tor-proxy", shell=True, check=True, | |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
print("[✓] Tor container started.") | |
except subprocess.CalledProcessError: | |
print("[~] Tor container not found. Pulling and running it now...") | |
try: | |
subprocess.run( | |
"docker run -d --name tor-proxy -p 9050:9050 --restart always dperson/torproxy", | |
shell=True, check=True) | |
print("[✓] Tor container downloaded and started.") | |
except subprocess.CalledProcessError: | |
print("[!] Failed to start Tor Docker container.") | |
sys.exit(1) | |
def run(cmd, results, label): | |
"""Run shell command and record True/False in results[label].""" | |
print(f"$ {cmd}") | |
try: | |
out = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT, | |
text=True, timeout=15) | |
print(out.strip()) | |
results[label] = True | |
except subprocess.CalledProcessError as e: | |
print(f"Error (exit {e.returncode}): {e.output.strip()}") | |
results[label] = False | |
except subprocess.TimeoutExpired: | |
print("Error: command timed out") | |
results[label] = False | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Diagnose DNS and port connectivity with optional Tor proxy.") | |
parser.add_argument("hostport", | |
help="Host or host:port to test, e.g. example.com or example.com:2083") | |
parser.add_argument("--tor", default="127.0.0.1:9050", | |
help="SOCKS5 proxy address for Tor (host:port)") | |
args = parser.parse_args() | |
# Split host and port | |
if ":" in args.hostport: | |
host, port_str = args.hostport.rsplit(':', 1) | |
try: | |
port = int(port_str) | |
except ValueError: | |
print(f"Invalid port number: {port_str}") | |
sys.exit(1) | |
else: | |
host = args.hostport | |
port = 443 | |
# Ensure required tools | |
for tool in ("dig", "nc", "curl", "docker"): | |
check_tool(tool) | |
# Ensure Tor proxy | |
ensure_docker_tor() | |
print(f"### Testing {host} on port {port} ###") | |
results = {} | |
# 1) DNS default resolver | |
print("\n1) DNS (default resolver):") | |
run(f"dig +short {host}", results, 'dns_default') | |
# 2) DNS Google | |
print("2) DNS (Google DNS 8.8.8.8):") | |
run(f"dig @8.8.8.8 +short {host}", results, 'dns_google') | |
# 3) TCP connectivity | |
print(f"\n3) TCP connect to port {port}:") | |
run(f"nc -v -z {host} {port}", results, f'tcp_{port}') | |
# 4) HTTP HEAD direct | |
scheme = 'https' if port in (443, 2083) else 'http' | |
url = f"{scheme}://{host}:{port}" if port not in (80, 443) else f"{scheme}://{host}" | |
print(f"\n4) HTTP HEAD direct at {url}:") | |
run(f"curl -I --connect-timeout 5 {url}", results, 'http_direct') | |
# 5) HTTP HEAD via Tor | |
print(f"\n5) HTTP HEAD via Tor SOCKS5 ({args.tor}):") | |
run(f"curl --socks5-hostname {args.tor} -I --connect-timeout 5 {url}", results, 'http_tor') | |
# Conclusion summary | |
print("\n=== Conclusion ===") | |
summary = [ | |
("DNS (default)", 'dns_default'), | |
("DNS (Google)", 'dns_google'), | |
(f"TCP port {port}", f'tcp_{port}'), | |
("HTTP direct", 'http_direct'), | |
("HTTP via Tor", 'http_tor'), | |
] | |
for label, key in summary: | |
status = 'OK' if results.get(key) else 'FAIL' | |
print(f"- {label}: {status}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment