Move managers to the cache package

- Not entirely accurate name but not really worth to have a separate package for managers
This commit is contained in:
ljacqu
2016-03-05 16:03:00 +01:00
parent fd8db2cd51
commit 19adcdcceb
9 changed files with 9 additions and 7 deletions
+81
View File
@@ -0,0 +1,81 @@
package fr.xephi.authme.cache;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.SecuritySettings;
import java.util.concurrent.ConcurrentHashMap;
/**
* Manager for the handling of captchas.
*/
public class CaptchaManager {
private final int threshold;
private final int captchaLength;
private final ConcurrentHashMap<String, Integer> playerCounts;
private final ConcurrentHashMap<String, String> captchaCodes;
public CaptchaManager(NewSetting settings) {
this.playerCounts = new ConcurrentHashMap<>();
this.captchaCodes = new ConcurrentHashMap<>();
this.threshold = settings.getProperty(SecuritySettings.MAX_LOGIN_TRIES_BEFORE_CAPTCHA);
this.captchaLength = settings.getProperty(SecuritySettings.CAPTCHA_LENGTH);
}
public void increaseCount(String player) {
String playerLower = player.toLowerCase();
Integer currentCount = playerCounts.get(playerLower);
if (currentCount == null) {
playerCounts.put(playerLower, 1);
} else {
playerCounts.put(playerLower, currentCount + 1);
}
}
/**
* Return whether the given player is required to solve a captcha.
*
* @param player The player to verify
* @return True if the player has to solve a captcha, false otherwise
*/
public boolean isCaptchaRequired(String player) {
Integer count = playerCounts.get(player.toLowerCase());
return count != null && count >= threshold;
}
/**
* Return the captcha code for the player. Creates one if none present, so call only after
* checking with {@link #isCaptchaRequired}.
*
* @param player The player
* @return The code required for the player
*/
public String getCaptchaCode(String player) {
String code = captchaCodes.get(player.toLowerCase());
if (code == null) {
code = RandomString.generate(captchaLength);
captchaCodes.put(player.toLowerCase(), code);
}
return code;
}
/**
* Return whether the supplied code is correct for the given player.
*
* @param player The player to check
* @param code The supplied code
* @return True if the code matches or if no captcha is required for the player, false otherwise
*/
public boolean checkCode(String player, String code) {
String savedCode = captchaCodes.get(player.toLowerCase());
if (savedCode == null) {
return true;
} else if (savedCode.equalsIgnoreCase(code)) {
captchaCodes.remove(player.toLowerCase());
return true;
}
return false;
}
}
@@ -0,0 +1,79 @@
package fr.xephi.authme.cache;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
/**
* Stateful manager for looking up IP address appropriately, including caching.
*/
public class IpAddressManager {
/** Cache for IP lookups per player. */
private final ConcurrentHashMap<String, String> ipCache;
/** Whether or not to use the VeryGames API for IP lookups. */
private final boolean useVeryGamesIpCheck;
/**
* Constructor.
*
* @param settings The settings instance
*/
public IpAddressManager(NewSetting settings) {
this.useVeryGamesIpCheck = settings.getProperty(HooksSettings.ENABLE_VERYGAMES_IP_CHECK);
this.ipCache = new ConcurrentHashMap<>();
}
public String getPlayerIp(Player player) {
final String playerName = player.getName().toLowerCase();
final String cachedValue = ipCache.get(playerName);
if (cachedValue != null) {
return cachedValue;
}
final String plainIp = player.getAddress().getAddress().getHostAddress();
if (useVeryGamesIpCheck) {
String veryGamesResult = getVeryGamesIp(plainIp, player.getAddress().getPort());
if (veryGamesResult != null) {
ipCache.put(playerName, veryGamesResult);
return veryGamesResult;
}
} else {
ipCache.put(playerName, plainIp);
}
return plainIp;
}
public void addCache(String player, String ip) {
ipCache.put(player.toLowerCase(), ip);
}
public void removeCache(String player) {
ipCache.remove(player.toLowerCase());
}
// returns null if IP could not be looked up
private String getVeryGamesIp(final String plainIp, final int port) {
final String sUrl = String.format("http://monitor-1.verygames.net/api/?action=ipclean-real-ip"
+ "&out=raw&ip=%s&port=%d", plainIp, port);
try {
String result = Resources.toString(new URL(sUrl), Charsets.UTF_8);
if (!StringUtils.isEmpty(result) && !result.contains("error")) {
return result;
}
} catch (IOException e) {
ConsoleLogger.logException("Could not fetch Very Games API with URL '" + sUrl + "':", e);
}
return null;
}
}