use of com.github.games647.craftapi.resolver.RateLimitException in project CraftAPI by games647.
the class MojangResolver method downloadSkin.
@Override
public Optional<SkinProperty> downloadSkin(UUID uuid) throws IOException, RateLimitException {
Optional<SkinProperty> optSkin = cache.getSkin(uuid);
if (optSkin.isPresent()) {
return optSkin;
}
String url = String.format(SKIN_URL, UUIDAdapter.toMojangId(uuid));
HttpURLConnection conn = getConnection(url);
int responseCode = conn.getResponseCode();
if (responseCode == RateLimitException.RATE_LIMIT_RESPONSE_CODE) {
discard(conn);
throw new RateLimitException();
}
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
return Optional.empty();
}
Textures texturesModel = parseRequest(conn, in -> readJson(in, Textures.class));
SkinProperty property = texturesModel.getProperties()[0];
cache.addSkin(uuid, property);
return Optional.of(property);
}
use of com.github.games647.craftapi.resolver.RateLimitException in project ChangeSkin by games647.
the class MojangSkinApi method getUUID.
public Optional<UUID> getUUID(String playerName) throws NotPremiumException, RateLimitException {
logger.debug("Making UUID->Name request for {}", playerName);
if (!validNamePattern.matcher(playerName).matches()) {
throw new NotPremiumException(playerName);
}
Proxy proxy = null;
try {
HttpURLConnection connection;
if (requests.size() >= rateLimit || Duration.between(lastRateLimit, Instant.now()).getSeconds() < 60 * 10) {
synchronized (proxies) {
if (proxies.hasNext()) {
proxy = proxies.next();
connection = getConnection(UUID_URL + playerName, proxy);
} else {
return Optional.empty();
}
}
} else {
requests.put(new Object(), new Object());
connection = getConnection(UUID_URL + playerName);
}
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
throw new NotPremiumException(playerName);
} else if (responseCode == RATE_LIMIT_ID) {
logger.info("Mojang's rate-limit reached. The public IPv4 address of this server issued more than 600" + " Name -> UUID requests within 10 minutes. Once those 10 minutes ended we could make requests" + " again. In the meanwhile new skins can only be downloaded using the UUID directly." + " If you are using BungeeCord, consider adding a caching server in order to prevent multiple" + " spigot servers creating the same requests against Mojang's servers.");
lastRateLimit = Instant.now();
if (!connection.usingProxy()) {
return getUUID(playerName);
} else {
throw new RateLimitException("Rate-Limit hit on request name->uuid of " + playerName);
}
}
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
GameProfile playerProfile = gson.fromJson(reader, GameProfile.class);
return Optional.of(playerProfile.getId());
}
} else {
logger.error("Received response code: {} for {} using proxy: {}", responseCode, playerName, proxy);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
logger.error("Error stream: {}", CharStreams.toString(reader));
}
}
} catch (IOException ioEx) {
logger.error("Tried converting player name: {} to uuid", playerName, ioEx);
}
return Optional.empty();
}
use of com.github.games647.craftapi.resolver.RateLimitException in project FastLogin by games647.
the class JoinManagement method onLogin.
public void onLogin(String username, S source) {
core.getPlugin().getLog().info("Handling player {}", username);
StoredProfile profile = core.getStorage().loadProfile(username);
if (profile == null) {
return;
}
// check if the player is connecting through Bedrock Edition
if (bedrockService != null && bedrockService.isBedrockConnection(username)) {
// perform Bedrock specific checks and skip Java checks, if they are not needed
if (bedrockService.performChecks(username, source)) {
return;
}
}
callFastLoginPreLoginEvent(username, source, profile);
Configuration config = core.getConfig();
String ip = source.getAddress().getAddress().getHostAddress();
profile.setLastIp(ip);
try {
if (profile.isSaved()) {
if (profile.isPremium()) {
core.getPlugin().getLog().info("Requesting premium login for registered player: {}", username);
requestPremiumLogin(source, profile, username, true);
} else {
if (isValidUsername(source, profile)) {
startCrackedSession(source, profile, username);
}
}
} else {
if (core.getPendingLogin().remove(ip + username) != null && config.get("secondAttemptCracked", false)) {
core.getPlugin().getLog().info("Second attempt login -> cracked {}", username);
// first login request failed so make a cracked session
startCrackedSession(source, profile, username);
return;
}
Optional<Profile> premiumUUID = Optional.empty();
if (config.get("nameChangeCheck", false) || config.get("autoRegister", false)) {
premiumUUID = core.getResolver().findProfile(username);
}
if (!premiumUUID.isPresent() || (!checkNameChange(source, username, premiumUUID.get()) && !checkPremiumName(source, username, profile))) {
// nothing detected the player as premium -> start a cracked session
if (core.getConfig().get("switchMode", false)) {
source.kick(core.getMessage("switch-kick-message"));
return;
}
startCrackedSession(source, profile, username);
}
}
} catch (RateLimitException rateLimitEx) {
core.getPlugin().getLog().error("Mojang's rate limit reached for {}. The public IPv4 address of this" + " server issued more than 600 Name -> UUID requests within 10 minutes. After those 10" + " minutes we can make requests again.", username);
} catch (Exception ex) {
core.getPlugin().getLog().error("Failed to check premium state for {}", username, ex);
core.getPlugin().getLog().error("Failed to check premium state of {}", username, ex);
}
}
use of com.github.games647.craftapi.resolver.RateLimitException in project ChangeSkin by games647.
the class SharedNameResolver method run.
@Override
public void run() {
UUID uuid = core.getUuidCache().get(targetName);
if (uuid == null) {
if (core.getCrackedNames().containsKey(targetName)) {
sendMessageInvoker("not-premium");
return;
}
try {
Optional<UUID> optUUID = core.getSkinApi().getUUID(targetName);
if (optUUID.isPresent()) {
uuid = optUUID.get();
core.getUuidCache().put(targetName, uuid);
} else {
sendMessageInvoker("no-resolve");
}
} catch (NotPremiumException notPremiumEx) {
core.getCrackedNames().put(targetName, new Object());
sendMessageInvoker("not-premium");
} catch (RateLimitException rateLimitEx) {
sendMessageInvoker("rate-limit");
}
}
if (uuid != null) {
sendMessageInvoker("uuid-resolved");
if (!hasSkinPermission(uuid)) {
return;
}
scheduleDownloader(uuid);
}
}
use of com.github.games647.craftapi.resolver.RateLimitException in project ChangeSkin by games647.
the class SharedListener method refetchSkin.
protected boolean refetchSkin(String playerName, UserPreference preferences) {
UUID ownerUUID = core.getUuidCache().get(playerName);
if (ownerUUID == null && !core.getCrackedNames().containsKey(playerName)) {
try {
Optional<UUID> optUUID = core.getSkinApi().getUUID(playerName);
if (optUUID.isPresent()) {
ownerUUID = optUUID.get();
}
} catch (NotPremiumException ex) {
core.getCrackedNames().put(playerName, new Object());
} catch (RateLimitException ex) {
// ignore
}
}
if (ownerUUID != null) {
core.getUuidCache().put(playerName, ownerUUID);
SkinModel storedSkin = core.checkAutoUpdate(core.getStorage().getSkin(ownerUUID));
if (storedSkin == null) {
storedSkin = core.getSkinApi().downloadSkin(ownerUUID).orElse(null);
}
preferences.setTargetSkin(storedSkin);
save(preferences);
return true;
}
return false;
}
Aggregations