From 8fa03efbda11be6aa303c33a87775abbd981ba18 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sun, 18 Sep 2022 18:49:57 +0200 Subject: [PATCH] [HttpUtils] only fetch proxyHost/Port/UserName/UserPassword once at startup, hopefully fix auth --- .../io/gitlab/jfronny/commons/HttpUtils.java | 65 ++++++++----------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/src/main/java/io/gitlab/jfronny/commons/HttpUtils.java b/src/main/java/io/gitlab/jfronny/commons/HttpUtils.java index 54a6f45..4599f44 100644 --- a/src/main/java/io/gitlab/jfronny/commons/HttpUtils.java +++ b/src/main/java/io/gitlab/jfronny/commons/HttpUtils.java @@ -11,9 +11,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -21,46 +19,36 @@ import java.util.stream.Stream; public class HttpUtils { private static String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"; + private static final String PROXY_AUTH; + private static final HttpClient CLIENT; + static { // Enables HTTPS proxying System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); + + HttpClient.Builder clientBuilder = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS); + + String host = System.getProperty("http.proxyHost"); + String port = System.getProperty("http.proxyPort"); + if (host != null && port != null) { + if (port.matches("\\d+")) { + clientBuilder.proxy(ProxySelector.of(new InetSocketAddress(host, Integer.parseInt(port)))); + } else { + System.err.println("Could not parse proxy settings: Port is not a number"); + } + } + + String name = System.getProperty("http.proxyUserName"); + String pass = System.getProperty("http.proxyUserPassword"); + if (name != null && pass != null) { + PROXY_AUTH = "Basic " + new String(Base64.getEncoder().encode((name + ":" + pass).getBytes())); + } else { + PROXY_AUTH = null; + } + + CLIENT = clientBuilder.build(); } - // Attempt to respect proxy settings - private static final HttpClient CLIENT = HttpClient.newBuilder().proxy(new ProxySelector() { - @Override - public List select(URI uri) { - String host = System.getProperty("http.proxyHost"); - String port = System.getProperty("http.proxyPort"); - if (host == null || port == null) - return List.of(); - if (!port.matches("\\d+")) { - System.err.println("Could not parse proxy settings: Port is not a number"); - return List.of(); - } - return List.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, Integer.parseInt(port)))); - } - - @Override - public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) { - - } - }) - .followRedirects(HttpClient.Redirect.ALWAYS) - .authenticator(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - String name = System.getProperty("http.proxyUserName"); - String pass = System.getProperty("http.proxyUserPassword"); - if (name == null - || pass == null - || getRequestorType() != RequestorType.PROXY) - return super.getPasswordAuthentication(); - return new PasswordAuthentication(name, pass.toCharArray()); - } - }) - .build(); - public static void setUserAgent(String hostname) { if (hostname == null || hostname.isEmpty()) throw new IllegalArgumentException("Hostname cannot be empty"); HttpUtils.userAgent = hostname; @@ -142,6 +130,7 @@ public class HttpUtils { if (sent > 3) throw new IOException("Attempted third redirect, stopping"); builder.header("Accept", accept); if (method != null) builder.method(method.name(), HttpRequest.BodyPublishers.noBody()); + if (PROXY_AUTH != null) builder.header("Proxy-Authorization", PROXY_AUTH); HttpResponse res; try {