Skip to content

Commit

Permalink
#698 - Set jvm proxy settings on each call
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Couritas authored and Etienne Couritas committed Nov 13, 2018
1 parent 5713edc commit 93aeaef
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/src/main/scala/bloop/Bloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object Bloop extends CaseApp[CliOptions] {
def run(state: State, options: CliOptions): Unit = {
val origin = state.build.origin
val config = origin.underlying
bloop.cli.Proxy.setProxyJvmSettings(state)
def waitForState(a: Action, t: Task[State]): State = {
// Ignore the exit status here, all we want is the task to finish execution or fail.
Cli.waitUntilEndOfWorld(a, options, state.pool, config, state.logger, Array("from-shell")) {
Expand Down
90 changes: 90 additions & 0 deletions frontend/src/main/scala/bloop/cli/Proxy.scala
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")
}

()
}
}
1 change: 1 addition & 0 deletions frontend/src/main/scala/bloop/engine/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Interpreter {
def execute(action: Action, stateTask: Task[State]): Task[State] = {
def execute(action: Action, stateTask: Task[State], inRecursion: Boolean): Task[State] = {
stateTask.flatMap { state =>
bloop.cli.Proxy.setProxyJvmSettings(state)
action match {
// We keep it case because there is a 'match may not be exhaustive' false positive by scalac
// Looks related to existing bug report https://github.com/scala/bug/issues/10251
Expand Down

0 comments on commit 93aeaef

Please sign in to comment.