Switch to mcapi.ca and add configurable number of requests

This commit is contained in:
games647
2016-07-07 12:20:39 +02:00
parent 757ddb905a
commit c50249edea
9 changed files with 77 additions and 79 deletions
@@ -11,7 +11,9 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
@@ -22,7 +24,7 @@ public abstract class MojangApiConnector {
private static final int TIMEOUT = 1 * 1_000;
private static final String USER_AGENT = "Premium-Checker";
private static final String MCAPI_UUID_URL = "https://us.mc-api.net/v3/uuid/";
private static final String MCAPI_UUID_URL = "https://mcapi.ca/uuid/player/";
//only premium (paid account) users have a uuid from here
private static final String UUID_LINK = "https://api.mojang.com/users/profiles/minecraft/";
@@ -34,15 +36,23 @@ public abstract class MojangApiConnector {
//compile the pattern only on plugin enable -> and this have to be threadsafe
private final Pattern playernameMatcher = Pattern.compile(VALID_PLAYERNAME);
private final ConcurrentMap<Object, Object> requests;
private final BalancedSSLFactory sslFactory;
private final boolean apiLookup;
private final int rateLimit;
private long lastRateLimit;
protected final FastLoginCore plugin;
protected final Logger logger;
public MojangApiConnector(FastLoginCore plugin, List<String> localAddresses, boolean apiLookup) {
this.plugin = plugin;
this.apiLookup = apiLookup;
public MojangApiConnector(ConcurrentMap<Object, Object> requests, Logger logger, List<String> localAddresses
, int rateLimit) {
this.logger = logger;
this.requests = requests;
if (rateLimit > 600) {
this.rateLimit = 600;
} else {
this.rateLimit = rateLimit;
}
if (localAddresses.isEmpty()) {
this.sslFactory = null;
@@ -52,13 +62,13 @@ public abstract class MojangApiConnector {
try {
InetAddress address = InetAddress.getByName(localAddress);
if (!address.isAnyLocalAddress()) {
plugin.getLogger().log(Level.WARNING, "Submitted IP-Address is not local", address);
logger.log(Level.WARNING, "Submitted IP-Address is not local", address);
continue;
}
addresses.add(address);
} catch (UnknownHostException ex) {
plugin.getLogger().log(Level.SEVERE, "IP-Address is unknown to us", ex);
logger.log(Level.SEVERE, "IP-Address is unknown to us", ex);
}
}
@@ -76,11 +86,13 @@ public abstract class MojangApiConnector {
if (playernameMatcher.matcher(playerName).matches()) {
//only make a API call if the name is valid existing mojang account
if (System.currentTimeMillis() - lastRateLimit < 1_000 * 60 * 10) {
plugin.getLogger().fine("STILL WAITING FOR RATE_LIMIT - TRYING Third-party API");
if (requests.size() >= rateLimit || System.currentTimeMillis() - lastRateLimit < 1_000 * 60 * 10) {
// plugin.getLogger().fine("STILL WAITING FOR RATE_LIMIT - TRYING Third-party API");
return getUUIDFromAPI(playerName);
}
requests.put(new Object(), new Object());
try {
HttpsURLConnection connection = getConnection(UUID_LINK + playerName);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
@@ -90,13 +102,13 @@ public abstract class MojangApiConnector {
return getUUIDFromJson(line);
}
} else if (connection.getResponseCode() == RATE_LIMIT_CODE) {
plugin.getLogger().info("RATE_LIMIT REACHED - TRYING SECOND API");
logger.info("RATE_LIMIT REACHED - TRYING THIRD-PARTY API");
lastRateLimit = System.currentTimeMillis();
return getUUIDFromAPI(playerName);
}
//204 - no content for not found
} catch (Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to check if player has a paid account", ex);
logger.log(Level.SEVERE, "Failed to check if player has a paid account", ex);
}
//this connection doesn't need to be closed. So can make use of keep alive in java
}
@@ -105,10 +117,6 @@ public abstract class MojangApiConnector {
}
public UUID getUUIDFromAPI(String playerName) {
if (!playernameMatcher.matcher(playerName).matches()) {
return null;
}
try {
HttpURLConnection httpConnection = (HttpURLConnection) new URL(MCAPI_UUID_URL + playerName).openConnection();
httpConnection.addRequestProperty("Content-Type", "application/json");
@@ -122,10 +130,10 @@ public abstract class MojangApiConnector {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
String line = reader.readLine();
if (line != null && !line.equals("null")) {
return getUUIDFromJsonAPI(line);
return getUUIDFromJson(line);
}
} catch (IOException iOException) {
plugin.getLogger().log(Level.SEVERE, "Tried converting name->uuid from third-party api", iOException);
logger.log(Level.SEVERE, "Tried converting name->uuid from third-party api", iOException);
}
return null;
@@ -135,8 +143,6 @@ public abstract class MojangApiConnector {
protected abstract UUID getUUIDFromJson(String json);
protected abstract UUID getUUIDFromJsonAPI(String json);
protected HttpsURLConnection getConnection(String url) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(TIMEOUT);
+4 -6
View File
@@ -122,14 +122,12 @@ premium-warning: true
# - 192-168-0-2
ip-addresses: []
# This option exists because of the reason as the previous one. Once FastLogin hits the rate-limit it will make requests
# to a third-party API. In this case: https://us.mc-api.net - Please support them if you can
#
# For the next ten minutes FastLogin will make requests to this website. Once the ten minutes are expired FastLogin
# will make requests to Mojang again, because the rate-limit expired. (in order to reduce the load to the third-party-api)
# How many requests should be established until the plugin uses the third-party API https://mcapi.ca/
# Once this number is reached in a range of ten minutes it will start connecting to https://mcapi.ca/ for the next ten minutes
# This option exists in order to workaround the rate-limiting. Name -> UUID are fetched in the same way like heads
#
# If you want to join the discussion visit this: https://github.com/games647/FastLogin/issues/27#issuecomment-226954350
lookup-third-party: true
mojang-request-limit: 600
# Database configuration
# Recommened is the use of MariaDB (a better version of MySQL)