Fix SOCKS5 proxy authentication

SOCKS5 proxy authentication didn't work with the system properties
java.net.socks.username and java.net.socks.password. Instead we now
use the Authenticator class to provide a proxy authenticator.
This commit is contained in:
0xboobface 2018-10-23 21:15:37 +02:00
parent c0e1490530
commit ba4f7b6e1a
1 changed files with 18 additions and 6 deletions

View File

@ -1,6 +1,8 @@
package ctbrec.io;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.concurrent.TimeUnit;
import ctbrec.Config;
@ -38,18 +40,15 @@ public abstract class HttpClient {
System.setProperty("socksProxyVersion", "4");
System.setProperty("socksProxyHost", Config.getInstance().getSettings().proxyHost);
System.setProperty("socksProxyPort", Config.getInstance().getSettings().proxyPort);
if(Config.getInstance().getSettings().proxyUser != null && !Config.getInstance().getSettings().proxyUser.isEmpty()) {
System.setProperty("java.net.socks.username", Config.getInstance().getSettings().proxyUser);
System.setProperty("java.net.socks.password", Config.getInstance().getSettings().proxyPassword);
}
break;
case SOCKS5:
System.setProperty("socksProxyVersion", "5");
System.setProperty("socksProxyHost", Config.getInstance().getSettings().proxyHost);
System.setProperty("socksProxyPort", Config.getInstance().getSettings().proxyPort);
if(Config.getInstance().getSettings().proxyUser != null && !Config.getInstance().getSettings().proxyUser.isEmpty()) {
System.setProperty("java.net.socks.username", Config.getInstance().getSettings().proxyUser);
System.setProperty("java.net.socks.password", Config.getInstance().getSettings().proxyPassword);
String username = Config.getInstance().getSettings().proxyUser;
String password = Config.getInstance().getSettings().proxyPassword;
Authenticator.setDefault(new ProxyAuth(username, password));
}
break;
case DIRECT:
@ -102,4 +101,17 @@ public abstract class HttpClient {
client.connectionPool().evictAll();
client.dispatcher().executorService().shutdown();
}
public static class ProxyAuth extends Authenticator {
private PasswordAuthentication auth;
private ProxyAuth(String user, String password) {
auth = new PasswordAuthentication(user, password == null ? new char[]{} : password.toCharArray());
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return auth;
}
}
}