Use direct proxies instead of ssl factories for multiple IP-addresses

This commit is contained in:
games647
2017-10-14 18:25:12 +02:00
parent bb2cc1b42a
commit 4858049c2a
13 changed files with 59 additions and 129 deletions
@@ -138,7 +138,7 @@ public class AuthStorage {
public void save(PlayerProfile playerProfile) {
try (Connection con = dataSource.getConnection()) {
UUID uuid = playerProfile.getUuid();
UUID uuid = playerProfile.getUUID();
if (playerProfile.getUserId() == -1) {
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
@@ -1,66 +0,0 @@
package com.github.games647.fastlogin.core;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Iterator;
import javax.net.ssl.SSLSocketFactory;
public class BalancedSSLFactory extends SSLSocketFactory {
//in order to be thread-safe
private final Iterator<InetAddress> iterator;
private final SSLSocketFactory oldFactory;
public BalancedSSLFactory(SSLSocketFactory oldFactory, Iterable<InetAddress> localAddresses) {
this.oldFactory = oldFactory;
this.iterator = Iterables.cycle(localAddresses).iterator();
}
@Override
public String[] getDefaultCipherSuites() {
return oldFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return oldFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
return oldFactory.createSocket(host, port, getNextLocalAddress(), 0);
}
@Override
public Socket createSocket(String host, int port) throws IOException {
return oldFactory.createSocket(host, port, getNextLocalAddress(), 0);
}
@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
throws IOException {
//default
return oldFactory.createSocket(host, port, localAddress, localPort);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return oldFactory.createSocket(host, port, getNextLocalAddress(), 0);
}
@Override
public Socket createSocket(InetAddress host, int port, InetAddress local, int localPort) throws IOException {
//Default
return oldFactory.createSocket(host, port, local, localPort);
}
private InetAddress getNextLocalAddress() {
synchronized (iterator) {
return iterator.next();
}
}
}
@@ -43,11 +43,12 @@ public class PlayerProfile {
this.userId = generatedId;
}
public synchronized UUID getUuid() {
//todo: this should be optional
public synchronized UUID getUUID() {
return uuid;
}
public synchronized void setUuid(UUID uuid) {
public synchronized void setUUID(UUID uuid) {
this.uuid = uuid;
}
@@ -1,11 +1,9 @@
package com.github.games647.fastlogin.core.mojang;
import com.github.games647.fastlogin.core.BalancedSSLFactory;
import com.github.games647.fastlogin.core.CommonUtil;
import com.github.games647.fastlogin.core.shared.LoginSession;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.net.HostAndPort;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -14,12 +12,10 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import java.net.UnknownHostException;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
@@ -28,12 +24,10 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import org.slf4j.Logger;
@@ -53,7 +47,6 @@ public class MojangApiConnector {
private final Iterator<Proxy> proxies;
private final Map<Object, Object> requests = CommonUtil.buildCache(10, -1);
private final SSLSocketFactory sslFactory;
private final int rateLimit;
private Instant lastRateLimit = Instant.now().minus(10, ChronoUnit.MINUTES);
@@ -65,13 +58,16 @@ public class MojangApiConnector {
, Iterable<HostAndPort> proxies) {
this.logger = logger;
this.rateLimit = Math.max(rateLimit, 600);
this.sslFactory = buildAddresses(logger, localAddresses);
List<Proxy> proxyBuilder = Lists.newArrayList();
for (HostAndPort proxy : proxies) {
proxyBuilder.add(new Proxy(Type.HTTP, new InetSocketAddress(proxy.getHostText(), proxy.getPort())));
}
for (String address : localAddresses) {
proxyBuilder.add(new Proxy(Type.DIRECT, new InetSocketAddress(address.replace('-', '.'), 0)));
}
this.proxies = Iterables.cycle(proxyBuilder).iterator();
}
@@ -142,8 +138,6 @@ public class MojangApiConnector {
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setSSLSocketFactory(sslFactory);
//this connection doesn't need to be closed. So can make use of keep alive in java
return connection;
}
@@ -151,22 +145,4 @@ public class MojangApiConnector {
protected HttpsURLConnection getConnection(String url) throws IOException {
return getConnection(url, Proxy.NO_PROXY);
}
private SSLSocketFactory buildAddresses(Logger logger, Iterable<String> localAddresses) {
Set<InetAddress> addresses = Sets.newHashSet();
for (String localAddress : localAddresses) {
try {
InetAddress address = InetAddress.getByName(localAddress.replace('-', '.'));
addresses.add(address);
} catch (UnknownHostException ex) {
logger.error("IP-Address is unknown to us", ex);
}
}
if (addresses.isEmpty()) {
return HttpsURLConnection.getDefaultSSLSocketFactory();
}
return new BalancedSSLFactory(HttpsURLConnection.getDefaultSSLSocketFactory(), addresses);
}
}
@@ -49,7 +49,7 @@ public abstract class ForceLoginManagement<P extends C, C, L extends LoginSessio
if (success) {
//update only on success to prevent corrupt data
if (playerProfile != null) {
playerProfile.setUuid(session.getUuid());
playerProfile.setUUID(session.getUuid());
playerProfile.setPremium(true);
storage.save(playerProfile);
}
@@ -59,7 +59,7 @@ public abstract class ForceLoginManagement<P extends C, C, L extends LoginSessio
}
} else if (playerProfile != null) {
//cracked player
playerProfile.setUuid(null);
playerProfile.setUUID(null);
playerProfile.setPremium(false);
storage.save(playerProfile);
}