Search in sources :

Example 31 with Proxy

use of java.net.Proxy in project bazel by bazelbuild.

the class ProxyHelperTest method testTrailingSlash.

@Test
public void testTrailingSlash() throws Exception {
    Proxy proxy = ProxyHelper.createProxy("http://foo:bar@example.com:8000/");
    assertEquals(Proxy.Type.HTTP, proxy.type());
    assertThat(proxy.toString()).endsWith(":8000");
    assertEquals(System.getProperty("http.proxyHost"), "example.com");
    assertEquals(System.getProperty("http.proxyPort"), "8000");
    assertEquals(System.getProperty("http.proxyUser"), "foo");
    assertEquals(System.getProperty("http.proxyPassword"), "bar");
}
Also used : Proxy(java.net.Proxy) Test(org.junit.Test)

Example 32 with Proxy

use of java.net.Proxy in project bazel by bazelbuild.

the class ProxyHelperTest method testNoProxy.

@Test
public void testNoProxy() throws Exception {
    // Empty address.
    Proxy proxy = ProxyHelper.createProxy(null);
    assertEquals(Proxy.NO_PROXY, proxy);
    proxy = ProxyHelper.createProxy("");
    assertEquals(Proxy.NO_PROXY, proxy);
    Map<String, String> env = ImmutableMap.of();
    ProxyHelper helper = new ProxyHelper(env);
    proxy = helper.createProxyIfNeeded(new URL("https://www.something.com"));
    assertEquals(Proxy.NO_PROXY, proxy);
}
Also used : Proxy(java.net.Proxy) URL(java.net.URL) Test(org.junit.Test)

Example 33 with Proxy

use of java.net.Proxy in project bazel by bazelbuild.

the class ProxyHelperTest method testCreateIfNeededHttpsLowerCase.

@Test
public void testCreateIfNeededHttpsLowerCase() throws Exception {
    ProxyHelper helper = new ProxyHelper(ImmutableMap.of("https_proxy", "https://my.example.com"));
    Proxy proxy = helper.createProxyIfNeeded(new URL("https://www.something.com"));
    assertThat(proxy.toString()).endsWith("my.example.com:443");
}
Also used : Proxy(java.net.Proxy) URL(java.net.URL) Test(org.junit.Test)

Example 34 with Proxy

use of java.net.Proxy in project bazel by bazelbuild.

the class ProxyHelperTest method testProxyExplicitPort.

@Test
public void testProxyExplicitPort() throws Exception {
    Proxy proxy = ProxyHelper.createProxy("http://my.example.com:12345");
    assertThat(proxy.toString()).endsWith(":12345");
    assertEquals(System.getProperty("http.proxyHost"), "my.example.com");
    assertEquals(System.getProperty("http.proxyPort"), "12345");
    proxy = ProxyHelper.createProxy("https://my.example.com:12345");
    assertThat(proxy.toString()).endsWith(":12345");
    assertEquals(System.getProperty("https.proxyHost"), "my.example.com");
    assertEquals(System.getProperty("https.proxyPort"), "12345");
}
Also used : Proxy(java.net.Proxy) Test(org.junit.Test)

Example 35 with Proxy

use of java.net.Proxy in project bazel by bazelbuild.

the class ProxyHelper method createProxy.

/**
   * This method takes a proxyAddress as a String (ex.
   * http://userId:password@proxyhost.domain.com:8000) and sets JVM arguments for http and https
   * proxy as well as returns a java.net.Proxy object for optional use.
   *
   * @param proxyAddress The fully qualified address of the proxy server
   * @return Proxy
   * @throws IOException
   */
public static Proxy createProxy(@Nullable String proxyAddress) throws IOException {
    if (Strings.isNullOrEmpty(proxyAddress)) {
        return Proxy.NO_PROXY;
    }
    // Here there be dragons.
    Pattern urlPattern = Pattern.compile("^(https?)://(([^:@]+?)(?::([^@]+?))?@)?([^:]+)(?::(\\d+))?/?$");
    Matcher matcher = urlPattern.matcher(proxyAddress);
    if (!matcher.matches()) {
        throw new IOException("Proxy address " + proxyAddress + " is not a valid URL");
    }
    final String protocol = matcher.group(1);
    final String idAndPassword = matcher.group(2);
    final String username = matcher.group(3);
    final String password = matcher.group(4);
    final String hostname = matcher.group(5);
    final String portRaw = matcher.group(6);
    String cleanProxyAddress = proxyAddress;
    if (idAndPassword != null) {
        cleanProxyAddress = // Used to remove id+pwd from logging
        proxyAddress.replace(idAndPassword, "");
    }
    boolean https;
    switch(protocol) {
        case "https":
            https = true;
            break;
        case "http":
            https = false;
            break;
        default:
            throw new IOException("Invalid proxy protocol for " + cleanProxyAddress);
    }
    // Default port numbers
    int port = https ? 443 : 80;
    if (portRaw != null) {
        try {
            port = Integer.parseInt(portRaw);
        } catch (NumberFormatException e) {
            throw new IOException("Error parsing proxy port: " + cleanProxyAddress);
        }
    }
    // We need to set both of these because jgit uses whichever the resource dictates
    System.setProperty("https.proxyHost", hostname);
    System.setProperty("https.proxyPort", Integer.toString(port));
    System.setProperty("http.proxyHost", hostname);
    System.setProperty("http.proxyPort", Integer.toString(port));
    if (username != null) {
        if (password == null) {
            throw new IOException("No password given for proxy " + cleanProxyAddress);
        }
        // We need to make sure the proxy password is not url encoded; some special characters in
        // proxy passwords require url encoding for shells and other tools to properly consume.
        final String decodedPassword = URLDecoder.decode(password, "UTF-8");
        System.setProperty("http.proxyUser", username);
        System.setProperty("http.proxyPassword", decodedPassword);
        System.setProperty("https.proxyUser", username);
        System.setProperty("https.proxyPassword", decodedPassword);
        Authenticator.setDefault(new Authenticator() {

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, decodedPassword.toCharArray());
            }
        });
    }
    return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
}
Also used : Pattern(java.util.regex.Pattern) Proxy(java.net.Proxy) Matcher(java.util.regex.Matcher) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5