Skip to content

Commit

Permalink
Add no_proxy environment variable support (#128)
Browse files Browse the repository at this point in the history
Signed-off-by: Loukas Agorgianitis <loukas@agorgianitis.com>
  • Loading branch information
agorgl authored Dec 3, 2024
1 parent f225b96 commit 1824d12
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 20 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,10 @@ The `-Sdeps-file` option may be used to load a different project file than `dep

deps.clj supports setting a proxy server via the "standard" environment variables (the lowercase versions are tried first):
- `http_proxy` or `HTTP_PROXY` for http traffic
- `https_proxy` or `HTTPs_PROXY` for https traffic
- `https_proxy` or `HTTPS_PROXY` for https traffic
- `no_proxy` or `NO_PROXY` a list of hosts that should be reached directly, bypassing the proxy

This will set the JVM properties `-Dhttp[s].proxyHost` and `-Dhttp[s].proxyPort`.
This will set the JVM properties `-Dhttp[s].proxyHost`, `-Dhttp[s].proxyPort` and `-Dhttp[s].nonProxyHosts`.

The format of the proxy string supported is `http[s]://[username:password@]host:port`. Any username and password info is ignored as not supported by the underlying JVM properties.

Expand Down
25 changes: 19 additions & 6 deletions deps.bat
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ For more info, see:
(do (warn "WARNING: Can't parse proxy info - found:" s "- proceeding without using proxy!")
nil)))))

(defn- parse-noproxy-list
[s]
(when s
(str/split s #",")))

(defn get-proxy-info
"Returns a map with proxy information parsed from env vars. The map
will contain :http-proxy and :https-proxy entries if the relevant
Expand All @@ -330,21 +335,27 @@ For more info, see:
(let [http-proxy (parse-proxy-info (or (*getenv-fn* "http_proxy")
(*getenv-fn* "HTTP_PROXY")))
https-proxy (parse-proxy-info (or (*getenv-fn* "https_proxy")
(*getenv-fn* "HTTPS_PROXY")))]
(*getenv-fn* "HTTPS_PROXY")))
no-proxy (parse-noproxy-list (or (*getenv-fn* "no_proxy")
(*getenv-fn* "NO_PROXY")))]
(cond-> {}
http-proxy (assoc :http-proxy http-proxy)
https-proxy (assoc :https-proxy https-proxy))))
https-proxy (assoc :https-proxy https-proxy)
no-proxy (assoc :no-proxy no-proxy))))

(defn set-proxy-system-props!
"Sets the proxy system properties in the current JVM.
proxy-info parameter is as returned from `get-proxy-info.`"
[{:keys [http-proxy https-proxy]}]
[{:keys [http-proxy https-proxy no-proxy]}]
(when http-proxy
(System/setProperty "http.proxyHost" (:host http-proxy))
(System/setProperty "http.proxyPort" (:port http-proxy)))
(when https-proxy
(System/setProperty "https.proxyHost" (:host https-proxy))
(System/setProperty "https.proxyPort" (:port https-proxy))))
(System/setProperty "https.proxyPort" (:port https-proxy)))
(when no-proxy
(System/setProperty "http.nonProxyHosts" (str/join "|" no-proxy))
(System/setProperty "https.nonProxyHosts" (str/join "|" no-proxy))))

(defn clojure-tools-download-direct!
"Downloads from `:url` to `:dest` file returning true on success."
Expand Down Expand Up @@ -495,12 +506,14 @@ public class ClojureToolsDownloader {
"Returns a vector containing the JVM system property arguments to be passed to a new process
to set its proxy system properties.
proxy-info parameter is as returned from `get-proxy-info.`"
[{:keys [http-proxy https-proxy]}]
[{:keys [http-proxy https-proxy no-proxy]}]
(cond-> []
http-proxy (concat [(str "-Dhttp.proxyHost=" (:host http-proxy))
(str "-Dhttp.proxyPort=" (:port http-proxy))])
https-proxy (concat [(str "-Dhttps.proxyHost=" (:host https-proxy))
(str "-Dhttps.proxyPort=" (:port https-proxy))])))
(str "-Dhttps.proxyPort=" (:port https-proxy))])
no-proxy (concat [(str "-Dhttp.nonProxyHosts=" (str/join "|" no-proxy))
(str "-Dhttps.nonProxyHosts=" (str/join "|" no-proxy))])))

(defn clojure-tools-download-java!
"Downloads `:url` zip file to `:dest` by invoking `java` with
Expand Down
25 changes: 19 additions & 6 deletions deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ For more info, see:
(do (warn "WARNING: Can't parse proxy info - found:" s "- proceeding without using proxy!")
nil)))))

(defn- parse-noproxy-list
[s]
(when s
(str/split s #",")))

(defn get-proxy-info
"Returns a map with proxy information parsed from env vars. The map
will contain :http-proxy and :https-proxy entries if the relevant
Expand All @@ -325,21 +330,27 @@ For more info, see:
(let [http-proxy (parse-proxy-info (or (*getenv-fn* "http_proxy")
(*getenv-fn* "HTTP_PROXY")))
https-proxy (parse-proxy-info (or (*getenv-fn* "https_proxy")
(*getenv-fn* "HTTPS_PROXY")))]
(*getenv-fn* "HTTPS_PROXY")))
no-proxy (parse-noproxy-list (or (*getenv-fn* "no_proxy")
(*getenv-fn* "NO_PROXY")))]
(cond-> {}
http-proxy (assoc :http-proxy http-proxy)
https-proxy (assoc :https-proxy https-proxy))))
https-proxy (assoc :https-proxy https-proxy)
no-proxy (assoc :no-proxy no-proxy))))

(defn set-proxy-system-props!
"Sets the proxy system properties in the current JVM.
proxy-info parameter is as returned from `get-proxy-info.`"
[{:keys [http-proxy https-proxy]}]
[{:keys [http-proxy https-proxy no-proxy]}]
(when http-proxy
(System/setProperty "http.proxyHost" (:host http-proxy))
(System/setProperty "http.proxyPort" (:port http-proxy)))
(when https-proxy
(System/setProperty "https.proxyHost" (:host https-proxy))
(System/setProperty "https.proxyPort" (:port https-proxy))))
(System/setProperty "https.proxyPort" (:port https-proxy)))
(when no-proxy
(System/setProperty "http.nonProxyHosts" (str/join "|" no-proxy))
(System/setProperty "https.nonProxyHosts" (str/join "|" no-proxy))))

(defn clojure-tools-download-direct!
"Downloads from `:url` to `:dest` file returning true on success."
Expand Down Expand Up @@ -490,12 +501,14 @@ public class ClojureToolsDownloader {
"Returns a vector containing the JVM system property arguments to be passed to a new process
to set its proxy system properties.
proxy-info parameter is as returned from `get-proxy-info.`"
[{:keys [http-proxy https-proxy]}]
[{:keys [http-proxy https-proxy no-proxy]}]
(cond-> []
http-proxy (concat [(str "-Dhttp.proxyHost=" (:host http-proxy))
(str "-Dhttp.proxyPort=" (:port http-proxy))])
https-proxy (concat [(str "-Dhttps.proxyHost=" (:host https-proxy))
(str "-Dhttps.proxyPort=" (:port https-proxy))])))
(str "-Dhttps.proxyPort=" (:port https-proxy))])
no-proxy (concat [(str "-Dhttp.nonProxyHosts=" (str/join "|" no-proxy))
(str "-Dhttps.nonProxyHosts=" (str/join "|" no-proxy))])))

(defn clojure-tools-download-java!
"Downloads `:url` zip file to `:dest` by invoking `java` with
Expand Down
25 changes: 19 additions & 6 deletions src/borkdude/deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ For more info, see:
(do (warn "WARNING: Can't parse proxy info - found:" s "- proceeding without using proxy!")
nil)))))

(defn- parse-noproxy-list
[s]
(when s
(str/split s #",")))

(defn get-proxy-info
"Returns a map with proxy information parsed from env vars. The map
will contain :http-proxy and :https-proxy entries if the relevant
Expand All @@ -322,21 +327,27 @@ For more info, see:
(let [http-proxy (parse-proxy-info (or (*getenv-fn* "http_proxy")
(*getenv-fn* "HTTP_PROXY")))
https-proxy (parse-proxy-info (or (*getenv-fn* "https_proxy")
(*getenv-fn* "HTTPS_PROXY")))]
(*getenv-fn* "HTTPS_PROXY")))
no-proxy (parse-noproxy-list (or (*getenv-fn* "no_proxy")
(*getenv-fn* "NO_PROXY")))]
(cond-> {}
http-proxy (assoc :http-proxy http-proxy)
https-proxy (assoc :https-proxy https-proxy))))
https-proxy (assoc :https-proxy https-proxy)
no-proxy (assoc :no-proxy no-proxy))))

(defn set-proxy-system-props!
"Sets the proxy system properties in the current JVM.
proxy-info parameter is as returned from `get-proxy-info.`"
[{:keys [http-proxy https-proxy]}]
[{:keys [http-proxy https-proxy no-proxy]}]
(when http-proxy
(System/setProperty "http.proxyHost" (:host http-proxy))
(System/setProperty "http.proxyPort" (:port http-proxy)))
(when https-proxy
(System/setProperty "https.proxyHost" (:host https-proxy))
(System/setProperty "https.proxyPort" (:port https-proxy))))
(System/setProperty "https.proxyPort" (:port https-proxy)))
(when no-proxy
(System/setProperty "http.nonProxyHosts" (str/join "|" no-proxy))
(System/setProperty "https.nonProxyHosts" (str/join "|" no-proxy))))

(defn clojure-tools-download-direct!
"Downloads from `:url` to `:dest` file returning true on success."
Expand Down Expand Up @@ -487,12 +498,14 @@ public class ClojureToolsDownloader {
"Returns a vector containing the JVM system property arguments to be passed to a new process
to set its proxy system properties.
proxy-info parameter is as returned from `get-proxy-info.`"
[{:keys [http-proxy https-proxy]}]
[{:keys [http-proxy https-proxy no-proxy]}]
(cond-> []
http-proxy (concat [(str "-Dhttp.proxyHost=" (:host http-proxy))
(str "-Dhttp.proxyPort=" (:port http-proxy))])
https-proxy (concat [(str "-Dhttps.proxyHost=" (:host https-proxy))
(str "-Dhttps.proxyPort=" (:port https-proxy))])))
(str "-Dhttps.proxyPort=" (:port https-proxy))])
no-proxy (concat [(str "-Dhttp.nonProxyHosts=" (str/join "|" no-proxy))
(str "-Dhttps.nonProxyHosts=" (str/join "|" no-proxy))])))

(defn clojure-tools-download-java!
"Downloads `:url` zip file to `:dest` by invoking `java` with
Expand Down
7 changes: 7 additions & 0 deletions test/borkdude/deps_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
(is (= {:host "aHost" :port "1234"} (#'deps/parse-proxy-info "https://aHost:1234")))
(is (= {:host "aHost" :port "1234"} (#'deps/parse-proxy-info "https://user:pw@aHost:1234")))
(is (nil? (#'deps/parse-proxy-info "http://aHost:abc")))
(is (= ["host1" "host2"] (#'deps/parse-noproxy-list "host1,host2")))
(is (= {:http-proxy {:host "aHost" :port "1234"}}
(binding [deps/*getenv-fn* {"http_proxy" "http://aHost:1234"}]
(deps/get-proxy-info))))
Expand All @@ -139,6 +140,12 @@
(deps/get-proxy-info))))
(is (= {}
(binding [deps/*getenv-fn* {"http_proxy" "http://aHost:abc"}]
(deps/get-proxy-info))))
(is (= {:no-proxy ["host1" "host2"]}
(binding [deps/*getenv-fn* {"no_proxy" "host1,host2"}]
(deps/get-proxy-info))))
(is (= {:no-proxy ["host1" "host2"]}
(binding [deps/*getenv-fn* {"NO_PROXY" "host1,host2"}]
(deps/get-proxy-info)))))

(deftest jvm-opts-test
Expand Down

0 comments on commit 1824d12

Please sign in to comment.