2020-05-24 18:48:43 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2021-12-29 09:42:35 +01:00
|
|
|
from urllib.parse import urlparse
|
2020-05-24 18:48:43 +02:00
|
|
|
|
|
|
|
proxy = next(os.environ[_] for _ in ("HTTP_PROXY", "HTTPS_PROXY") if _ in os.environ)
|
2021-12-29 09:42:35 +01:00
|
|
|
|
|
|
|
parsed = urlparse(proxy)
|
|
|
|
|
|
|
|
proxy_protocols = {
|
|
|
|
"http": "connect",
|
|
|
|
"https": "connect",
|
|
|
|
"socks": "5",
|
|
|
|
"socks5": "5",
|
|
|
|
"socks4": "4",
|
|
|
|
"socks4a": "4",
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsed.scheme not in proxy_protocols:
|
|
|
|
raise TypeError('unsupported proxy protocol: "{}"'.format(parsed.scheme))
|
|
|
|
|
2022-04-12 13:26:22 +02:00
|
|
|
def make_argv():
|
|
|
|
yield "nc"
|
2024-04-10 15:39:19 +02:00
|
|
|
if sys.platform in {'linux', 'cygwin'}:
|
|
|
|
# caveats: the built-in netcat of most linux distributions and cygwin support proxy type
|
2022-04-12 13:26:22 +02:00
|
|
|
# caveats: macOS built-in netcat command not supported proxy-type
|
|
|
|
yield "-X" # --proxy-type
|
|
|
|
# Supported protocols are 4 (SOCKS v4), 5 (SOCKS v5) and connect (HTTP proxy).
|
|
|
|
# Default SOCKS v5 is used.
|
|
|
|
yield proxy_protocols[parsed.scheme]
|
|
|
|
yield "-x" # --proxy
|
|
|
|
yield parsed.netloc # proxy-host:proxy-port
|
|
|
|
yield sys.argv[1] # host
|
|
|
|
yield sys.argv[2] # port
|
2020-05-24 18:48:43 +02:00
|
|
|
|
2022-04-12 13:26:22 +02:00
|
|
|
subprocess.call(make_argv())
|