Fix address rotating for contacting the Mojang API

This commit is contained in:
games647
2017-10-07 19:48:29 +02:00
parent df945146b8
commit c7c0782071
4 changed files with 11 additions and 15 deletions

View File

@ -1,27 +1,23 @@
package com.github.games647.fastlogin.core; package com.github.games647.fastlogin.core;
import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.util.List; import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
public class BalancedSSLFactory extends SSLSocketFactory { public class BalancedSSLFactory extends SSLSocketFactory {
private final SSLSocketFactory oldFactory;
//in order to be thread-safe //in order to be thread-safe
private final List<InetAddress> localAddresses; private final Iterator<InetAddress> iterator;
private final SSLSocketFactory oldFactory;
private AtomicInteger id;
public BalancedSSLFactory(SSLSocketFactory oldFactory, Iterable<InetAddress> localAddresses) { public BalancedSSLFactory(SSLSocketFactory oldFactory, Iterable<InetAddress> localAddresses) {
this.oldFactory = oldFactory; this.oldFactory = oldFactory;
this.localAddresses = ImmutableList.copyOf(localAddresses); this.iterator = Iterables.cycle(localAddresses).iterator();
} }
@Override @Override
@ -63,7 +59,8 @@ public class BalancedSSLFactory extends SSLSocketFactory {
} }
private InetAddress getNextLocalAddress() { private InetAddress getNextLocalAddress() {
int index = id.incrementAndGet() % localAddresses.size(); synchronized (iterator) {
return localAddresses.get(index); return iterator.next();
}
} }
} }

View File

@ -68,8 +68,7 @@ public class CommonUtil {
Class<JDK14LoggerAdapter> adapterClass = JDK14LoggerAdapter.class; Class<JDK14LoggerAdapter> adapterClass = JDK14LoggerAdapter.class;
Constructor<JDK14LoggerAdapter> cons = adapterClass.getDeclaredConstructor(java.util.logging.Logger.class); Constructor<JDK14LoggerAdapter> cons = adapterClass.getDeclaredConstructor(java.util.logging.Logger.class);
cons.setAccessible(true); cons.setAccessible(true);
JDK14LoggerAdapter logger = cons.newInstance(parent); return cons.newInstance(parent);
return logger;
} catch (ReflectiveOperationException reflectEx) { } catch (ReflectiveOperationException reflectEx) {
parent.log(Level.WARNING, "Cannot create slf4j logging adapter", reflectEx); parent.log(Level.WARNING, "Cannot create slf4j logging adapter", reflectEx);
parent.log(Level.WARNING, "Creating logger instance manually..."); parent.log(Level.WARNING, "Creating logger instance manually...");

View File

@ -153,7 +153,7 @@ public class MojangApiConnector {
} }
private SSLSocketFactory buildAddresses(Logger logger, Collection<String> localAddresses) { private SSLSocketFactory buildAddresses(Logger logger, Collection<String> localAddresses) {
if (!localAddresses.isEmpty()) { if (localAddresses.isEmpty()) {
return HttpsURLConnection.getDefaultSSLSocketFactory(); return HttpsURLConnection.getDefaultSSLSocketFactory();
} }

View File

@ -65,7 +65,7 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
} }
private boolean checkPremiumName(S source, String username, PlayerProfile profile) throws Exception { private boolean checkPremiumName(S source, String username, PlayerProfile profile) throws Exception {
core.getPlugin().getLog().debug("GameProfile {} uses a premium username", username); core.getPlugin().getLog().info("GameProfile {} uses a premium username", username);
if (core.getConfig().get("autoRegister", false) && (authHook == null || !authHook.isRegistered(username))) { if (core.getConfig().get("autoRegister", false) && (authHook == null || !authHook.isRegistered(username))) {
requestPremiumLogin(source, profile, username, false); requestPremiumLogin(source, profile, username, false);
return true; return true;