Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

245: Fix proxy protocol setting, add logging #382

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -21,5 +21,6 @@
* questions.
*/
module org.openjdk.skara.proxy {
requires java.logging;
exports org.openjdk.skara.proxy;
}
@@ -23,21 +23,27 @@
package org.openjdk.skara.proxy;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class HttpProxy {
private static void setProxyHostAndPortBasedOn(URI uri) {
var scheme = uri.getScheme();
private static final Logger log = Logger.getLogger("org.openjdk.skara.proxy");

private static boolean setProxyHostAndPortBasedOn(String protocol, URI uri) {
var port = String.valueOf(uri.getPort() == -1 ? 80 : uri.getPort());
if (System.getProperty(scheme + ".proxyHost") == null) {
System.setProperty(scheme + ".proxyHost", uri.getHost());
System.setProperty(scheme + ".proxyPort", port);
if (System.getProperty(protocol + ".proxyHost") == null) {
log.fine("Setting " + protocol + " proxy to " + uri.getHost() + ":" + port);
System.setProperty(protocol + ".proxyHost", uri.getHost());
System.setProperty(protocol + ".proxyPort", port);
return true;
}

log.fine("Not overriding " + protocol + " proxy setting. Current value: " +
System.getProperty(protocol + ".proxyHost") + ":" + System.getProperty(protocol + ".proxyPort"));
return false;
}

public static void setup() {
@@ -49,49 +55,52 @@ public static void setup() {

var output = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim();
var res = p.waitFor();
if (res == 0 && output != null && !output.isEmpty()) {
if (output.startsWith("https://") || output.startsWith("http://")) {
var uri = new URI(output);
setProxyHostAndPortBasedOn(uri);
} else {
for (var scheme : List.of("http", "https")) {
var uri = new URI(scheme + "://" + output);
setProxyHostAndPortBasedOn(uri);
}
if (res == 0 && !output.isEmpty()) {
if (!output.startsWith("http://") && !output.startsWith("https://")) {
// Try to parse it as a http url - we only care about the host and port
output = "http://" + output;
Copy link
Member

@edvbld edvbld Jan 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment that the protocol being prepended doesn't matter?

Copy link
Member Author

@rwestberg rwestberg Jan 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds reasonable.

}
var uri = new URI(output);
for (var protocol : List.of("http", "https")) {
setProxyHostAndPortBasedOn(protocol, uri);
}
return;
}
} catch (InterruptedException e) {
// pass
} catch (IOException e) {
// pass
} catch (URISyntaxException e) {
} catch (InterruptedException | IOException | URISyntaxException e) {
// pass
}

boolean hasSetProxy = false;
for (var key : List.of("http_proxy", "https_proxy")) {
var value = System.getenv(key);
value = value == null ? System.getenv(key.toUpperCase()) : value;
if (value != null) {
var protocol = key.split("_")[0].toLowerCase();
try {
if (!value.startsWith("http://") && !value.startsWith("https://")) {
var scheme = key.split("_")[0];
value = scheme + "://" + value;
// Try to parse it as a http url - we only care about the host and port
value = "http://" + value;
}
var uri = new URI(value);
setProxyHostAndPortBasedOn(uri);
hasSetProxy |= setProxyHostAndPortBasedOn(protocol, uri);
} catch (URISyntaxException e) {
// pass
}
}
}
var no_proxy = System.getenv("no_proxy");
no_proxy = no_proxy == null ? System.getenv("NO_PROXY") : no_proxy;
if (no_proxy != null && System.getProperty("http.nonProxyHosts") == null) {
var hosts = Arrays.stream(no_proxy.split(","))
.map(s -> s.startsWith(".") ? "*" + s : s)
.collect(Collectors.toList());
System.setProperty("http.nonProxyHosts", String.join("|", hosts));
if (no_proxy != null) {
if (System.getProperty("http.nonProxyHosts") == null || hasSetProxy) {
var hosts = Arrays.stream(no_proxy.split(","))
.map(s -> s.startsWith(".") ? "*" + s : s)
.collect(Collectors.toList());
System.setProperty("http.nonProxyHosts", String.join("|", hosts));
log.fine("Setting nonProxyHosts to " + String.join("|", hosts));
} else {
log.fine("Not overriding nonProxyHosts setting. Current value: " +
System.getProperty("http.nonProxyHosts"));
}
}
}
}