Created
January 23, 2025 20:21
-
-
Save Ragnoroct/82ea020085e78ca3eb833a61cea79e42 to your computer and use it in GitHub Desktop.
Easy SSH SOCK4/SOCK5 Tunnel Proxy
This file contains 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
from socket import create_connection, timeout | |
from requests import get | |
from subprocess import Popen | |
from contextlib import suppress | |
from os import environ | |
from sys import stdout as sys_stdout, stderr as sys_stderr | |
ssh_sock5_proxy = Popen( | |
["ssh", "-o", "StrictHostKeyChecking=no", "-D", "8888", "-C", "-N", f"ubuntu@{environ['SSH_IP']}"], | |
stdout=sys_stdout, | |
stderr=sys_stderr, | |
) | |
while True: | |
with suppress(ConnectionRefusedError, timeout): | |
create_connection(("localhost", 8888), timeout=1) | |
break # wait for tunnel to be up | |
ip_json = get("https://api.ipify.org?format=json").json() | |
ip_json_proxy = get( | |
"https://api.ipify.org?format=json", | |
proxies={ | |
"http": "socks5h://localhost:8888", | |
"https": "socks5h://localhost:8888", | |
}, | |
).json() | |
print(" my ip:", ip_json["ip"]) | |
print("sock5 ip:", ip_json_proxy["ip"]) | |
ssh_sock5_proxy.kill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment