Created
March 13, 2026 10:06
-
-
Save mrtnvgr/9890f72943672174375f0831bfbac6f3 to your computer and use it in GitHub Desktop.
Connect to the first available local proxy through torsocks
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 | |
| """ | |
| Usage: $(gimmetor.py) | |
| """ | |
| from ipaddress import ip_network | |
| import concurrent.futures | |
| import subprocess | |
| import socket | |
| import sys | |
| import re | |
| def get_local_network() -> str: | |
| result = subprocess.run(["ip", "route"], capture_output=True, text=True) | |
| match = re.search(r"(\d+\.\d+\.\d+)\.\d+/\d+", result.stdout) | |
| return f"{match.group(1)}.0/24" | |
| def check_port(ip: str, port: int) -> bool: | |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | |
| sock.settimeout(0.05) | |
| return sock.connect_ex((ip, port)) == 0 | |
| def get_proxy() -> str: | |
| net = get_local_network() | |
| net = ip_network(net, strict=False) | |
| with concurrent.futures.ThreadPoolExecutor(max_workers=255) as executor: | |
| future_to_ip = {executor.submit(check_port, str(ip), 9050): ip for ip in net.hosts()} | |
| for future in concurrent.futures.as_completed(future_to_ip): | |
| if not future.result(): continue | |
| ip = future_to_ip[future] | |
| return str(ip) | |
| sys.exit(1) | |
| proxy = get_proxy() | |
| print(f". torsocks -a {proxy} on") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment