]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/shell-proxy/ssh-proxy.py
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / shell-proxy / ssh-proxy.py
1 #!/usr/bin/env python3
2 import os
3 import subprocess
4 import sys
5 from urllib.parse import urlparse
6
7 proxy = next(os.environ[_] for _ in ("HTTP_PROXY", "HTTPS_PROXY") if _ in os.environ)
8
9 parsed = urlparse(proxy)
10
11 proxy_protocols = {
12     "http": "connect",
13     "https": "connect",
14     "socks": "5",
15     "socks5": "5",
16     "socks4": "4",
17     "socks4a": "4",
18 }
19
20 if parsed.scheme not in proxy_protocols:
21     raise TypeError('unsupported proxy protocol: "{}"'.format(parsed.scheme))
22
23 def make_argv():
24     yield "nc"
25     if sys.platform == 'linux':
26         # caveats: macOS built-in netcat command not supported proxy-type
27         yield "-X" # --proxy-type
28         # Supported protocols are 4 (SOCKS v4), 5 (SOCKS v5) and connect (HTTP proxy).
29         # Default SOCKS v5 is used.
30         yield proxy_protocols[parsed.scheme]
31     yield "-x" # --proxy
32     yield parsed.netloc # proxy-host:proxy-port
33     yield sys.argv[1] # host
34     yield sys.argv[2] # port
35
36 subprocess.call(make_argv())