-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#698 - Set jvm proxy settings on each call
- Loading branch information
Etienne Couritas
authored and
Etienne Couritas
committed
Nov 13, 2018
1 parent
5713edc
commit 93aeaef
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package bloop.cli | ||
|
||
import java.net.URL | ||
import java.net.MalformedURLException | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import bloop.engine.State | ||
|
||
/** | ||
* Proxy helper module | ||
* Information came from https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html | ||
*/ | ||
object Proxy { | ||
/** | ||
* Extract environments variables from the given state to set the proxy properties of the jvm. | ||
* If environement variable are not found in the state, the property corresponding is cleared, | ||
* to let user add and remove env var. | ||
* */ | ||
def setProxyJvmSettings(state : State) : Unit = { | ||
//no_proxy environment variable seems not be binded to a protocol. | ||
|
||
val maybeNoProxy = Option(state.commonOptions.env.getProperty("no_proxy")).map(_.replace(',','|')) | ||
|
||
val maybeHttpProxy = state.commonOptions.env.getProperty("http_proxy") | ||
if(maybeHttpProxy != null) { | ||
try { | ||
val url = new URL(maybeHttpProxy) | ||
System.setProperty("http.proxyHost",url.getHost()) | ||
System.setProperty("http.proxyPort",url.getPort().toString) | ||
maybeNoProxy.foreach(System.setProperty("http.nonProxyHosts", _)) | ||
} catch { | ||
case e : MalformedURLException => //Ignore, but should maybe log a warning if the URL is not parsable. | ||
} | ||
} else { | ||
System.clearProperty("http.proxyHost") | ||
System.clearProperty("http.proxyPort") | ||
if(maybeNoProxy.isEmpty) | ||
System.clearProperty("http.nonProxyHosts") | ||
} | ||
|
||
val maybeHttpsProxy = state.commonOptions.env.getProperty("https_proxy") | ||
if(maybeHttpsProxy != null) { | ||
try { | ||
val url = new URL(maybeHttpsProxy) | ||
System.setProperty("https.proxyHost",url.getHost()) | ||
System.setProperty("https.proxyPort",url.getPort().toString) | ||
//from java documentation https don't have no_proxy configuration. | ||
} catch { | ||
case e : MalformedURLException => //Ignore, but should maybe log a warning if the URL is not parsable. | ||
} | ||
} else { | ||
System.clearProperty("https.proxyHost") | ||
System.clearProperty("https.proxyPort") | ||
} | ||
|
||
val maybeFtpProxy = state.commonOptions.env.getProperty("ftp_proxy") | ||
if(maybeFtpProxy != null) { | ||
try { | ||
val url = new URL(maybeFtpProxy) | ||
System.setProperty("ftp.proxyHost",url.getHost()) | ||
System.setProperty("ftp.proxyPort",url.getPort().toString) | ||
maybeNoProxy.foreach(System.setProperty("ftp.nonProxyHosts", _)) | ||
} catch { | ||
case e : MalformedURLException => //Ignore, but should maybe log a warning if the URL is not parsable. | ||
} | ||
} else { | ||
System.clearProperty("ftp.proxyHost") | ||
System.clearProperty("ftp.proxyPort") | ||
if(maybeNoProxy.isEmpty) | ||
System.clearProperty("http.nonProxyHosts") | ||
} | ||
|
||
val maybeSocksProxy = state.commonOptions.env.getProperty("socks_proxy") | ||
if(maybeSocksProxy != null) { | ||
try { | ||
val url = new URL(maybeSocksProxy) | ||
System.setProperty("socksProxyHost",url.getHost()) | ||
System.setProperty("socksProxyPort",url.getPort().toString) | ||
} catch { | ||
case e : MalformedURLException => //Ignore, but should maybe log a warning if the URL is not parsable. | ||
} | ||
} else { | ||
System.clearProperty("socksProxyHost") | ||
System.clearProperty("socksProxyPort") | ||
} | ||
|
||
() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters