ssh-proxy.py 775 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import sys
  5. from urllib.parse import urlparse
  6. proxy = next(os.environ[_] for _ in ("HTTP_PROXY", "HTTPS_PROXY") if _ in os.environ)
  7. parsed = urlparse(proxy)
  8. proxy_protocols = {
  9. "http": "connect",
  10. "https": "connect",
  11. "socks": "5",
  12. "socks5": "5",
  13. "socks4": "4",
  14. "socks4a": "4",
  15. }
  16. if parsed.scheme not in proxy_protocols:
  17. raise TypeError('unsupported proxy protocol: "{}"'.format(parsed.scheme))
  18. argv = [
  19. "nc",
  20. "-X",
  21. proxy_protocols[parsed.scheme], # Supported protocols are 4 (SOCKS v4), 5 (SOCKS v5) and connect (HTTP proxy). Default SOCKS v5 is used.
  22. "-x",
  23. parsed.netloc, # proxy-host:proxy-port
  24. sys.argv[1], # host
  25. sys.argv[2], # port
  26. ]
  27. subprocess.call(argv)