4 from subprocess import check_output, list2cmdline
6 cwd = os.path.dirname(__file__)
7 ssh_agent = os.path.join(cwd, "ssh-agent.py")
8 proxy_env = "SHELLPROXY_URL"
9 proxy_config = os.environ.get("SHELLPROXY_CONFIG") or os.path.expandvars("$HOME/.config/proxy")
11 usage="""shell-proxy: no proxy configuration found.
13 Set `{env}` or create a config file at `{config}`
14 See the plugin README for more information.""".format(env=proxy_env, config=proxy_config)
17 default_proxy = os.environ.get(proxy_env)
20 if os.path.isfile(proxy_config):
21 return check_output(proxy_config).decode("utf-8").strip()
22 print(usage, file=sys.stderr)
26 def make_proxies(url: str):
27 proxies = {"%s_PROXY" % _: url for _ in ("HTTP", "HTTPS", "FTP", "RSYNC", "ALL")}
28 proxies.update({name.lower(): value for (name, value) in proxies.items()})
29 proxies["GIT_SSH"] = ssh_agent
33 def merge(mapping: dict):
34 return ("%s=%s" % _ for _ in mapping.items())
38 proxies = make_proxies(get_http_proxy())
40 _: "env __SSH_PROGRAM_NAME__=%s %s" % (_, ssh_agent)
41 for _ in ("ssh", "sftp", "scp", "slogin", "ssh-copy-id")
45 cmdline("export", *merge(self.proxies))
46 cmdline("alias", *merge(self.aliases))
49 cmdline("unset", *self.proxies.keys())
50 cmdline("unalias", *self.aliases.keys())
54 "%11s = %s" % (name, os.environ[name])
55 for name in self.proxies.keys()
62 print("usage: proxy {enable,disable,status}", file=sys.stderr)
66 print(list2cmdline(items))
70 command = CommandSet()
71 if len(sys.argv) == 1:
74 getattr(command, sys.argv[1], command.usage)()
77 if __name__ == "__main__":