From c8733351614bed77bd72708fdb12b36268cb2a28 Mon Sep 17 00:00:00 2001 From: Benjamin Frank Date: Tue, 5 Jul 2016 21:40:33 +0200 Subject: Support proxy settings in CBT. Proxy settings can be supplied either via Java system-properties or via environment variables (http_proxy/https_proxy/no_proxy). Java system-properties take precedence over env vars. Evaluation of proxy settings happens only during nailgun startup for now. --- cbt | 2 +- nailgun_launcher/NailgunLauncher.java | 1 + nailgun_launcher/Stage0Lib.java | 48 ++++++++++++++++++++++++++++++++--- stage1/Stage1Lib.scala | 2 +- stage2/Lib.scala | 2 +- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/cbt b/cbt index 3e2d0a9..63b272e 100755 --- a/cbt +++ b/cbt @@ -195,7 +195,7 @@ stage1 () { then log "Running JVM directly" $* # -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=localhost:5005 - java -Xmx6072m -Xss10M -cp $NAILGUN$TARGET cbt.NailgunLauncher $(time_taken) "$CWD" $* + java $JAVA_OPTS -Xmx6072m -Xss10M -cp $NAILGUN$TARGET cbt.NailgunLauncher $(time_taken) "$CWD" $* else log "Running via nailgun." $* for i in 0 1 2 3 4 5 6 7 8 9; do diff --git a/nailgun_launcher/NailgunLauncher.java b/nailgun_launcher/NailgunLauncher.java index c909c12..159041f 100644 --- a/nailgun_launcher/NailgunLauncher.java +++ b/nailgun_launcher/NailgunLauncher.java @@ -54,6 +54,7 @@ public class NailgunLauncher{ return; } + installProxySettings(); String[] diff = args[0].split("\\."); long start = _start - (Long.parseLong(diff[0]) * 1000L) - Long.parseLong(diff[1]); diff --git a/nailgun_launcher/Stage0Lib.java b/nailgun_launcher/Stage0Lib.java index 9c13680..5f8c5c7 100644 --- a/nailgun_launcher/Stage0Lib.java +++ b/nailgun_launcher/Stage0Lib.java @@ -67,7 +67,7 @@ public class Stage0Lib{ if(changed){ List zincArgs = new ArrayList( Arrays.asList( - new String[]{ + new String[]{ "-scala-compiler", earlyDeps.scalaCompiler_2_11_8_File, "-scala-library", earlyDeps.scalaLibrary_2_11_8_File, "-scala-extra", earlyDeps.scalaReflect_2_11_8_File, @@ -113,13 +113,55 @@ public class Stage0Lib{ new URL[]{ new URL("file:"+file) }, parent ); } + + private static String getVarFromEnv(String envKey) { + String value = System.getenv(envKey); + if(value==null || value.isEmpty()) { + value = System.getenv(envKey.toUpperCase()); + } + return value; + } + + private static void setProxyfromPropOrEnv(String envKey, String propKeyH, String propKeyP) { + String proxyHost = System.getProperty(propKeyH); + String proxyPort = System.getProperty(propKeyP); + if((proxyHost==null || proxyHost.isEmpty()) && (proxyPort==null || proxyPort.isEmpty())) { + String envVar = getVarFromEnv(envKey); + if(envVar != null && !envVar.isEmpty()) { + String[] proxy = envVar.replaceFirst("^https?://", "").split(":", 2); + System.setProperty(propKeyH, proxy[0]); + System.setProperty(propKeyP, proxy[1]); + } + } + } + + public static void installProxySettings() throws URISyntaxException { + setProxyfromPropOrEnv("http_proxy", "http.proxyHost", "http.proxyPort"); + setProxyfromPropOrEnv("https_proxy", "https.proxyHost", "https.proxyPort"); + String nonHosts = System.getProperty("http.nonProxyHosts"); + if(nonHosts==null || nonHosts.isEmpty()) { + String envVar = getVarFromEnv("no_proxy"); + if(envVar != null && !envVar.isEmpty()) { + System.setProperty("http.nonProxyHosts", envVar.replaceAll(",","|")); + } + } + } + + private static final ProxySelector ps = ProxySelector.getDefault(); + + public static HttpURLConnection openConnectionConsideringProxy(URL urlString) + throws IOException, URISyntaxException { + java.net.Proxy proxy = ps.select(urlString.toURI()).get(0); + return (HttpURLConnection) urlString.openConnection(proxy); + } + public static void download(URL urlString, Path target, String sha1) throws Exception { final Path unverified = Paths.get(target+".unverified"); if(!Files.exists(target)) { new File(target.toString()).getParentFile().mkdirs(); System.err.println("downloading " + urlString); System.err.println("to " + target); - final InputStream stream = urlString.openStream(); + final InputStream stream = openConnectionConsideringProxy(urlString).getInputStream(); Files.copy(stream, unverified, StandardCopyOption.REPLACE_EXISTING); stream.close(); final String checksum = sha1(Files.readAllBytes(unverified)); @@ -151,4 +193,4 @@ public class Stage0Lib{ copy[array.length] = item; return copy; } -} \ No newline at end of file +} diff --git a/stage1/Stage1Lib.scala b/stage1/Stage1Lib.scala index 7b4db59..9e500a3 100644 --- a/stage1/Stage1Lib.scala +++ b/stage1/Stage1Lib.scala @@ -60,7 +60,7 @@ class Stage1Lib( val logger: Logger ) extends BaseLib{ true } else { val incomplete = ( target ++ ".incomplete" ).toPath; - val connection = url.openConnection.asInstanceOf[HttpURLConnection] + val connection = Stage0Lib.openConnectionConsideringProxy(url) if(connection.getResponseCode != HttpURLConnection.HTTP_OK){ logger.resolver(blue("not found: ") ++ url.string) false diff --git a/stage2/Lib.scala b/stage2/Lib.scala index a060ac6..b65af2d 100644 --- a/stage2/Lib.scala +++ b/stage2/Lib.scala @@ -394,7 +394,7 @@ final class Lib(logger: Logger) extends Stage1Lib(logger) with Scaffold{ import java.io._ val url = baseUrl ++ fileName System.err.println(blue("uploading ") ++ url.toString) - val httpCon = url.openConnection.asInstanceOf[HttpURLConnection] + val httpCon = Stage0Lib.openConnectionConsideringProxy(url) httpCon.setDoOutput(true) httpCon.setRequestMethod("PUT") credentials.foreach( -- cgit v1.2.3