Search in sources :

Example 1 with RateLimitException

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SkinProperty(com.github.games647.craftapi.model.skin.SkinProperty) Textures(com.github.games647.craftapi.model.skin.Textures)

Example 2 with RateLimitException

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();
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GameProfile(com.github.games647.changeskin.core.model.GameProfile) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 3 with RateLimitException

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);
    }
}
Also used : StoredProfile(com.github.games647.fastlogin.core.StoredProfile) Configuration(net.md_5.bungee.config.Configuration) RateLimitException(com.github.games647.craftapi.resolver.RateLimitException) Profile(com.github.games647.craftapi.model.Profile) StoredProfile(com.github.games647.fastlogin.core.StoredProfile) RateLimitException(com.github.games647.craftapi.resolver.RateLimitException)

Example 4 with RateLimitException

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);
    }
}
Also used : RateLimitException(com.github.games647.changeskin.core.RateLimitException) NotPremiumException(com.github.games647.changeskin.core.NotPremiumException) UUID(java.util.UUID)

Example 5 with RateLimitException

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;
}
Also used : RateLimitException(com.github.games647.changeskin.core.RateLimitException) NotPremiumException(com.github.games647.changeskin.core.NotPremiumException) UUID(java.util.UUID) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Aggregations

Profile (com.github.games647.craftapi.model.Profile)3 HttpURLConnection (java.net.HttpURLConnection)3 NotPremiumException (com.github.games647.changeskin.core.NotPremiumException)2 RateLimitException (com.github.games647.changeskin.core.RateLimitException)2 RateLimitException (com.github.games647.craftapi.resolver.RateLimitException)2 StoredProfile (com.github.games647.fastlogin.core.StoredProfile)2 IOException (java.io.IOException)2 UUID (java.util.UUID)2 GameProfile (com.github.games647.changeskin.core.model.GameProfile)1 SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)1 SkinProperty (com.github.games647.craftapi.model.skin.SkinProperty)1 Textures (com.github.games647.craftapi.model.skin.Textures)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Proxy (java.net.Proxy)1 Configuration (net.md_5.bungee.config.Configuration)1