[HttpUtils] only fetch proxyHost/Port/UserName/UserPassword once at startup, hopefully fix auth
This commit is contained in:
parent
a68578f73f
commit
8fa03efbda
|
@ -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<Proxy> 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<T> res;
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue