f36c6db0eac17b022eee87411e6996a5f5fc8457

Author
Abhinav <abhinavms@outlook.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(shell-proxy): allow excluding endpoints with `NO_PROXY` (#11924)

Diff

 1diff --git a/plugins/shell-proxy/README.md b/plugins/shell-proxy/README.md
 2index b19888c565289690b96e8f4b3e30c8775f921e71..102e46b6b807751b7221de02619f5fc3b3a69ed9 100644
 3--- a/plugins/shell-proxy/README.md
 4+++ b/plugins/shell-proxy/README.md
 5@@ -23,6 +23,7 @@ Set `SHELLPROXY_URL` environment variable to the URL of the proxy server:
 6 
 7 ```sh
 8 SHELLPROXY_URL="http://127.0.0.1:8123"
 9+SHELLPROXY_NO_PROXY="localhost,127.0.0.1"
10 proxy enable
11 ```
12 
13@@ -36,11 +37,15 @@ Example:
14 ```sh
15 #!/bin/bash
16 
17+# HTTP Proxy
18 if [[ "$(uname)" = Darwin ]]; then
19   echo "http://127.0.0.1:6152" # Surge Mac
20 else
21   echo "http://127.0.0.1:8123" # polipo
22 fi
23+
24+# No Proxy
25+echo "localhost,127.0.0.1"
26 ```
27 
28 ### Method 3
29diff --git a/plugins/shell-proxy/proxy.py b/plugins/shell-proxy/proxy.py
30index 14f2944cc9707fc01dc44ee54e85d7e6bc71893e..8c2aaf9f3ccefedcd63643b4a2efbe835b406d3c 100755
31--- a/plugins/shell-proxy/proxy.py
32+++ b/plugins/shell-proxy/proxy.py
33@@ -6,6 +6,7 @@ from subprocess import check_output, list2cmdline
34 cwd = os.path.dirname(__file__)
35 ssh_agent = os.path.join(cwd, "ssh-agent.py")
36 proxy_env = "SHELLPROXY_URL"
37+no_proxy_env = "SHELLPROXY_NO_PROXY"
38 proxy_config = os.environ.get("SHELLPROXY_CONFIG") or os.path.expandvars("$HOME/.config/proxy")
39 
40 usage="""shell-proxy: no proxy configuration found.
41@@ -15,18 +16,30 @@ See the plugin README for more information.""".format(env=proxy_env, config=prox
42 
43 def get_http_proxy():
44     default_proxy = os.environ.get(proxy_env)
45-    if default_proxy:
46-        return default_proxy
47+    no_proxy = os.environ.get(no_proxy_env)
48+    if default_proxy and no_proxy:
49+        return default_proxy, no_proxy
50+
51     if os.path.isfile(proxy_config):
52-        return check_output(proxy_config).decode("utf-8").strip()
53+        proxy_configdata = [line.strip() for line in check_output(proxy_config).decode("utf-8").splitlines()]
54+        if len(proxy_configdata) >= 1:
55+            if not default_proxy:
56+                default_proxy = proxy_configdata[0]
57+            if len(proxy_configdata) == 2 and not no_proxy:
58+                no_proxy = proxy_configdata[1]
59+    
60+    if default_proxy:
61+        return default_proxy, no_proxy
62     print(usage, file=sys.stderr)
63     sys.exit(1)
64 
65 
66-def make_proxies(url: str):
67+def make_proxies(url: str, no_proxy: str):
68     proxies = {"%s_PROXY" % _: url for _ in ("HTTP", "HTTPS", "FTP", "RSYNC", "ALL")}
69     proxies.update({name.lower(): value for (name, value) in proxies.items()})
70     proxies["GIT_SSH"] = ssh_agent
71+    if no_proxy:
72+        proxies.update({"NO_PROXY": no_proxy, "no_proxy": no_proxy})
73     return proxies
74 
75 
76@@ -35,7 +48,7 @@ def merge(mapping: dict):
77 
78 
79 class CommandSet:
80-    proxies = make_proxies(get_http_proxy())
81+    proxies = make_proxies(*get_http_proxy())
82     aliases = {
83         _: "env __SSH_PROGRAM_NAME__=%s %s" % (_, ssh_agent)
84         for _ in ("ssh", "sftp", "scp", "slogin", "ssh-copy-id")
85diff --git a/plugins/shell-proxy/shell-proxy.plugin.zsh b/plugins/shell-proxy/shell-proxy.plugin.zsh
86index 4fdbe9322025d36fdadc8c8ba94aa521d2002ad2..f6c31da450e141c108ed4c3e9071a6f8fe11c468 100644
87--- a/plugins/shell-proxy/shell-proxy.plugin.zsh
88+++ b/plugins/shell-proxy/shell-proxy.plugin.zsh
89@@ -27,7 +27,7 @@ eval '
90 
91     # capture the output of the proxy script and bail out if it fails
92     local output
93-    output="$(SHELLPROXY_URL="$SHELLPROXY_URL" SHELLPROXY_CONFIG="$SHELLPROXY_CONFIG" "$proxy" "$1")" ||
94+    output="$(SHELLPROXY_URL="$SHELLPROXY_URL" SHELLPROXY_NO_PROXY="$SHELLPROXY_NO_PROXY" SHELLPROXY_CONFIG="$SHELLPROXY_CONFIG" "$proxy" "$1")" ||
95       return $?
96 
97     # evaluate the output generated by the proxy script