Remove useless packages
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.maxmind.geoip.LookupService;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class GeoIpService {
|
||||
private static final String LICENSE =
|
||||
"[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com";
|
||||
private static final String GEOIP_URL =
|
||||
"http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz";
|
||||
private LookupService lookupService;
|
||||
private Thread downloadTask;
|
||||
|
||||
private final File dataFile;
|
||||
|
||||
@Inject
|
||||
GeoIpService(@DataFolder File dataFolder) {
|
||||
this.dataFile = new File(dataFolder, "GeoIP.dat");
|
||||
// Fires download of recent data or the initialization of the look up service
|
||||
isDataAvailable();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
GeoIpService(@DataFolder File dataFolder, LookupService lookupService) {
|
||||
this.dataFile = dataFolder;
|
||||
this.lookupService = lookupService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download (if absent or old) the GeoIpLite data file and then try to load it.
|
||||
*
|
||||
* @return True if the data is available, false otherwise.
|
||||
*/
|
||||
private synchronized boolean isDataAvailable() {
|
||||
if (downloadTask != null && downloadTask.isAlive()) {
|
||||
return false;
|
||||
}
|
||||
if (lookupService != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (dataFile.exists()) {
|
||||
boolean dataIsOld = (System.currentTimeMillis() - dataFile.lastModified()) > TimeUnit.DAYS.toMillis(30);
|
||||
if (!dataIsOld) {
|
||||
try {
|
||||
lookupService = new LookupService(dataFile);
|
||||
ConsoleLogger.info(LICENSE);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
ConsoleLogger.logException("Failed to load GeoLiteAPI database", e);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
FileUtils.delete(dataFile);
|
||||
}
|
||||
}
|
||||
// Ok, let's try to download the data file!
|
||||
downloadTask = createDownloadTask();
|
||||
downloadTask.start();
|
||||
return false;
|
||||
}
|
||||
|
||||
private Thread createDownloadTask() {
|
||||
return new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URL downloadUrl = new URL(GEOIP_URL);
|
||||
URLConnection conn = downloadUrl.openConnection();
|
||||
conn.setConnectTimeout(10000);
|
||||
conn.connect();
|
||||
InputStream input = conn.getInputStream();
|
||||
if (conn.getURL().toString().endsWith(".gz")) {
|
||||
input = new GZIPInputStream(input);
|
||||
}
|
||||
OutputStream output = new FileOutputStream(dataFile);
|
||||
byte[] buffer = new byte[2048];
|
||||
int length = input.read(buffer);
|
||||
while (length >= 0) {
|
||||
output.write(buffer, 0, length);
|
||||
length = input.read(buffer);
|
||||
}
|
||||
output.close();
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
ConsoleLogger.logException("Could not download GeoLiteAPI database", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the country code of the given IP address.
|
||||
*
|
||||
* @param ip textual IP address to lookup.
|
||||
*
|
||||
* @return two-character ISO 3166-1 alpha code for the country.
|
||||
*/
|
||||
public String getCountryCode(String ip) {
|
||||
if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
|
||||
return lookupService.getCountry(ip).getCode();
|
||||
}
|
||||
return "--";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the country name of the given IP address.
|
||||
*
|
||||
* @param ip textual IP address to lookup.
|
||||
*
|
||||
* @return The name of the country.
|
||||
*/
|
||||
public String getCountryName(String ip) {
|
||||
if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
|
||||
return lookupService.getCountry(ip).getName();
|
||||
}
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import ch.jalu.injector.annotations.NoFieldScan;
|
||||
import com.earth2me.essentials.Essentials;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import net.minelink.ctplus.CombatTagPlus;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Hooks into third-party plugins and allows to perform actions on them.
|
||||
*/
|
||||
@NoFieldScan
|
||||
public class PluginHookService {
|
||||
|
||||
private final PluginManager pluginManager;
|
||||
private Essentials essentials;
|
||||
private MultiverseCore multiverse;
|
||||
private CombatTagPlus combatTagPlus;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param pluginManager The server's plugin manager
|
||||
*/
|
||||
@Inject
|
||||
public PluginHookService(PluginManager pluginManager) {
|
||||
this.pluginManager = pluginManager;
|
||||
tryHookToCombatPlus();
|
||||
tryHookToEssentials();
|
||||
tryHookToMultiverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable the social spy status of the given user if Essentials is available.
|
||||
*
|
||||
* @param player The player to modify
|
||||
* @param socialSpyStatus The social spy status (enabled/disabled) to set
|
||||
*/
|
||||
public void setEssentialsSocialSpyStatus(Player player, boolean socialSpyStatus) {
|
||||
if (essentials != null) {
|
||||
essentials.getUser(player).setSocialSpyEnabled(socialSpyStatus);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If Essentials is hooked into, return Essentials' data folder.
|
||||
*
|
||||
* @return The Essentials data folder, or null if unavailable
|
||||
*/
|
||||
public File getEssentialsDataFolder() {
|
||||
if (essentials != null) {
|
||||
return essentials.getDataFolder();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the spawn of the given world as defined by Multiverse (if available).
|
||||
*
|
||||
* @param world The world to get the Multiverse spawn for
|
||||
* @return The spawn location from Multiverse, or null if unavailable
|
||||
*/
|
||||
public Location getMultiverseSpawn(World world) {
|
||||
if (multiverse != null) {
|
||||
MVWorldManager manager = multiverse.getMVWorldManager();
|
||||
if (manager.isMVWorld(world)) {
|
||||
return manager.getMVWorld(world).getSpawnLocation();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the player is an NPC.
|
||||
*
|
||||
* @param player The player to process
|
||||
* @return True if player is NPC, false otherwise
|
||||
*/
|
||||
public boolean isNpc(Player player) {
|
||||
return player.hasMetadata("NPC") || isNpcInCombatTagPlus(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the CombatTagPlus plugin whether the given player is an NPC.
|
||||
*
|
||||
* @param player The player to verify
|
||||
* @return True if the player is an NPC according to CombatTagPlus, false if not or if the plugin is unavailable
|
||||
*/
|
||||
private boolean isNpcInCombatTagPlus(Player player) {
|
||||
return combatTagPlus != null && combatTagPlus.getNpcPlayerHelper().isNpc(player);
|
||||
}
|
||||
|
||||
|
||||
// ------
|
||||
// "Is plugin available" methods
|
||||
// ------
|
||||
public boolean isEssentialsAvailable() {
|
||||
return essentials != null;
|
||||
}
|
||||
|
||||
public boolean isMultiverseAvailable() {
|
||||
return multiverse != null;
|
||||
}
|
||||
|
||||
public boolean isCombatTagPlusAvailable() {
|
||||
return combatTagPlus != null;
|
||||
}
|
||||
|
||||
// ------
|
||||
// Hook methods
|
||||
// ------
|
||||
public void tryHookToEssentials() {
|
||||
try {
|
||||
essentials = getPlugin(pluginManager, "Essentials", Essentials.class);
|
||||
} catch (Exception | NoClassDefFoundError ignored) {
|
||||
essentials = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void tryHookToCombatPlus() {
|
||||
try {
|
||||
combatTagPlus = getPlugin(pluginManager, "CombatTagPlus", CombatTagPlus.class);
|
||||
} catch (Exception | NoClassDefFoundError ignored) {
|
||||
combatTagPlus = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void tryHookToMultiverse() {
|
||||
try {
|
||||
multiverse = getPlugin(pluginManager, "Multiverse-Core", MultiverseCore.class);
|
||||
} catch (Exception | NoClassDefFoundError ignored) {
|
||||
multiverse = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ------
|
||||
// Unhook methods
|
||||
// ------
|
||||
public void unhookEssentials() {
|
||||
essentials = null;
|
||||
}
|
||||
public void unhookCombatPlus() {
|
||||
combatTagPlus = null;
|
||||
}
|
||||
public void unhookMultiverse() {
|
||||
multiverse = null;
|
||||
}
|
||||
|
||||
// ------
|
||||
// Helpers
|
||||
// ------
|
||||
private static <T extends Plugin> T getPlugin(PluginManager pluginManager, String name, Class<T> clazz)
|
||||
throws Exception, NoClassDefFoundError {
|
||||
if (pluginManager.isPluginEnabled(name)) {
|
||||
T plugin = clazz.cast(pluginManager.getPlugin(name));
|
||||
ConsoleLogger.info("Hooked successfully into " + name);
|
||||
return plugin;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package fr.xephi.authme.service;
|
||||
|
||||
import com.github.authme.configme.properties.Property;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.geoip.GeoIpManager;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
@@ -36,7 +35,7 @@ public class ValidationService implements Reloadable {
|
||||
@Inject
|
||||
private PermissionsManager permissionsManager;
|
||||
@Inject
|
||||
private GeoIpManager geoIpManager;
|
||||
private GeoIpService geoIpService;
|
||||
|
||||
private Pattern passwordRegex;
|
||||
private Set<String> unrestrictedNames;
|
||||
@@ -115,7 +114,7 @@ public class ValidationService implements Reloadable {
|
||||
return true;
|
||||
}
|
||||
|
||||
String countryCode = geoIpManager.getCountryCode(hostAddress);
|
||||
String countryCode = geoIpService.getCountryCode(hostAddress);
|
||||
return validateWhitelistAndBlacklist(countryCode,
|
||||
ProtectionSettings.COUNTRIES_WHITELIST,
|
||||
ProtectionSettings.COUNTRIES_BLACKLIST);
|
||||
|
||||
Reference in New Issue
Block a user