A few code styling things

This commit is contained in:
games647
2016-09-20 13:32:06 +02:00
parent da266c7e91
commit 5075a71843
16 changed files with 44 additions and 59 deletions

View File

@ -65,11 +65,6 @@ public class BukkitCore extends FastLoginCore<Player> {
}); });
} }
@Override
public void loadConfig() {
plugin.saveDefaultConfig();
}
@Override @Override
public MojangApiConnector makeApiConnector(Logger logger, List<String> addresses, int requests) { public MojangApiConnector makeApiConnector(Logger logger, List<String> addresses, int requests) {
return new MojangApiBukkit(logger, addresses, requests); return new MojangApiBukkit(logger, addresses, requests);

View File

@ -1,6 +1,7 @@
package com.github.games647.fastlogin.bukkit; package com.github.games647.fastlogin.bukkit;
import com.github.games647.fastlogin.core.shared.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.shared.LoginSession;
import com.github.games647.fastlogin.core.shared.MojangApiConnector; import com.github.games647.fastlogin.core.shared.MojangApiConnector;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -24,11 +25,7 @@ public class MojangApiBukkit extends MojangApiConnector {
} }
@Override @Override
public boolean hasJoinedServer(Object session, String serverId) { public boolean hasJoinedServer(LoginSession session, String serverId) {
if (!(session instanceof BukkitLoginSession)) {
return false;
}
BukkitLoginSession playerSession = (BukkitLoginSession) session; BukkitLoginSession playerSession = (BukkitLoginSession) session;
try { try {
String url = HAS_JOINED_URL + "username=" + playerSession.getUsername() + "&serverId=" + serverId; String url = HAS_JOINED_URL + "username=" + playerSession.getUsername() + "&serverId=" + serverId;

View File

@ -24,6 +24,7 @@ public class AuthMeHook implements AuthPlugin<Player> {
} }
@Override @Override
@SuppressWarnings("deprecation")
public boolean forceLogin(Player player) { public boolean forceLogin(Player player) {
//skips registration and login //skips registration and login
if (isNewAPIAvailable) { if (isNewAPIAvailable) {
@ -36,6 +37,7 @@ public class AuthMeHook implements AuthPlugin<Player> {
} }
@Override @Override
@SuppressWarnings("deprecation")
public boolean isRegistered(String playerName) throws Exception { public boolean isRegistered(String playerName) throws Exception {
if (isNewAPIAvailable) { if (isNewAPIAvailable) {
return NewAPI.getInstance().isRegistered(playerName); return NewAPI.getInstance().isRegistered(playerName);
@ -45,6 +47,7 @@ public class AuthMeHook implements AuthPlugin<Player> {
} }
@Override @Override
@SuppressWarnings("deprecation")
public boolean forceRegister(Player player, String password) { public boolean forceRegister(Player player, String password) {
if (isNewAPIAvailable) { if (isNewAPIAvailable) {
NewAPI.getInstance().forceRegister(player, password); NewAPI.getInstance().forceRegister(player, password);

View File

@ -102,8 +102,6 @@ public class BungeeCordListener implements PluginMessageListener {
public Set<UUID> loadBungeeCordIds() { public Set<UUID> loadBungeeCordIds() {
File whitelistFile = new File(plugin.getDataFolder(), FILE_NAME); File whitelistFile = new File(plugin.getDataFolder(), FILE_NAME);
//create a new folder if it doesn't exist. Fail silently otherwise
whitelistFile.getParentFile().mkdir();
try { try {
if (!whitelistFile.exists()) { if (!whitelistFile.exists()) {
whitelistFile.createNewFile(); whitelistFile.createNewFile();

View File

@ -23,7 +23,8 @@ import net.md_5.bungee.config.YamlConfiguration;
public class BungeeCore extends FastLoginCore<ProxiedPlayer> { public class BungeeCore extends FastLoginCore<ProxiedPlayer> {
private static Map<String, Object> generateConfigMap(Configuration config) { private static Map<String, Object> generateConfigMap(Configuration config) {
return config.getKeys().stream().filter(key -> config.get(key) != null) return config.getKeys().stream()
.filter(key -> config.get(key) != null)
.collect(Collectors.toMap(key -> key, config::get)); .collect(Collectors.toMap(key -> key, config::get));
} }
@ -46,13 +47,15 @@ public class BungeeCore extends FastLoginCore<ProxiedPlayer> {
} }
@Override @Override
@SuppressWarnings("deprecation")
public ThreadFactory getThreadFactory() { public ThreadFactory getThreadFactory() {
String pluginName = plugin.getDescription().getName(); String pluginName = plugin.getDescription().getName();
return new ThreadFactoryBuilder() return new ThreadFactoryBuilder()
.setNameFormat(pluginName + " Database Pool Thread #%1$d") .setNameFormat(pluginName + " Database Pool Thread #%1$d")
//Hikari create daemons by default //Hikari create daemons by default
.setDaemon(true) .setDaemon(true)
.setThreadFactory(new GroupedThreadFactory(plugin, pluginName)).build(); .setThreadFactory(new GroupedThreadFactory(plugin, pluginName))
.build();
} }
@Override @Override
@ -77,15 +80,6 @@ public class BungeeCore extends FastLoginCore<ProxiedPlayer> {
} }
} }
@Override
public void loadConfig() {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
plugin.saveDefaultFile("config.yml");
}
@Override @Override
public MojangApiConnector makeApiConnector(Logger logger, List<String> addresses, int requests) { public MojangApiConnector makeApiConnector(Logger logger, List<String> addresses, int requests) {
return new MojangApiBungee(logger, addresses, requests); return new MojangApiBungee(logger, addresses, requests);

View File

@ -48,7 +48,6 @@ public class FastLoginBungee extends Plugin {
return; return;
} }
core.loadConfig();
core.setApiConnector(); core.setApiConnector();
core.loadMessages(); core.loadMessages();
@ -73,6 +72,10 @@ public class FastLoginBungee extends Plugin {
} }
public void saveDefaultFile(String fileName) { public void saveDefaultFile(String fileName) {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
File configFile = new File(getDataFolder(), fileName); File configFile = new File(getDataFolder(), fileName);
if (!configFile.exists()) { if (!configFile.exists()) {
InputStream in = getResourceAsStream(fileName); InputStream in = getResourceAsStream(fileName);

View File

@ -1,5 +1,6 @@
package com.github.games647.fastlogin.bungee; package com.github.games647.fastlogin.bungee;
import com.github.games647.fastlogin.core.shared.LoginSession;
import com.github.games647.fastlogin.core.shared.MojangApiConnector; import com.github.games647.fastlogin.core.shared.MojangApiConnector;
import java.util.List; import java.util.List;
@ -33,7 +34,7 @@ public class MojangApiBungee extends MojangApiConnector {
} }
@Override @Override
public boolean hasJoinedServer(Object session, String serverId) { public boolean hasJoinedServer(LoginSession session, String serverId) {
//this is not needed in Bungee //this is not needed in Bungee
throw new UnsupportedOperationException("Not supported"); throw new UnsupportedOperationException("Not supported");
} }

View File

@ -34,7 +34,7 @@ public class ForceLoginTask implements Runnable {
BungeeLoginSession session = plugin.getSession().get(pendingConnection); BungeeLoginSession session = plugin.getSession().get(pendingConnection);
if (session == null || !player.isConnected()) { if (session == null || !player.isConnected()) {
plugin.getLogger().log(Level.FINE, "Invalid session player {0} proparly left the server", player); plugin.getLogger().log(Level.FINE, "Invalid session player {0} propaly left the server", player);
return; return;
} }

View File

@ -23,12 +23,7 @@ public class PlayerProfile {
} }
public PlayerProfile(UUID uuid, String playerName, boolean premium, String lastIp) { public PlayerProfile(UUID uuid, String playerName, boolean premium, String lastIp) {
this.userId = -1; this(-1, uuid, playerName, premium, lastIp, System.currentTimeMillis());
this.uuid = uuid;
this.playerName = playerName;
this.premium = premium;
this.lastIp = lastIp;
} }
public synchronized String getPlayerName() { public synchronized String getPlayerName() {

View File

@ -19,4 +19,8 @@ public class SharedConfig {
public <T> T get(String path) { public <T> T get(String path) {
return get(path, null); return get(path, null);
} }
public Map<String, Object> getConfigValues() {
return configValues;
}
} }

View File

@ -3,9 +3,9 @@ package com.github.games647.fastlogin.core.hooks;
/** /**
* Represents a supporting authentication plugin in BungeeCord and Bukkit/Spigot/... servers * Represents a supporting authentication plugin in BungeeCord and Bukkit/Spigot/... servers
* *
* @param <T> either org.bukkit.entity.Player for Bukkit or net.md_5.bungee.api.connection.ProxiedPlayer for BungeeCord * @param <P> either org.bukkit.entity.Player for Bukkit or net.md_5.bungee.api.connection.ProxiedPlayer for BungeeCord
*/ */
public interface AuthPlugin<T> { public interface AuthPlugin<P> {
/** /**
* Login the premium (paid account) player after the player joined successfully the server. * Login the premium (paid account) player after the player joined successfully the server.
@ -16,7 +16,7 @@ public interface AuthPlugin<T> {
* @param player the player that needs to be logged in * @param player the player that needs to be logged in
* @return if the operation was successful * @return if the operation was successful
*/ */
boolean forceLogin(T player); boolean forceLogin(P player);
/** /**
* Forces a register in order to protect the paid account. * Forces a register in order to protect the paid account.
@ -39,7 +39,7 @@ public interface AuthPlugin<T> {
* @param password a strong random generated password * @param password a strong random generated password
* @return if the operation was successful * @return if the operation was successful
*/ */
boolean forceRegister(T player, String password); boolean forceRegister(P player, String password);
/** /**
* Checks whether an account exists for this player name. * Checks whether an account exists for this player name.

View File

@ -2,14 +2,14 @@ package com.github.games647.fastlogin.core.hooks;
import java.util.Random; import java.util.Random;
public class DefaultPasswordGenerator<T> implements PasswordGenerator<T> { public class DefaultPasswordGenerator<P> implements PasswordGenerator<P> {
private static final char[] PASSWORD_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" private static final char[] PASSWORD_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
.toCharArray(); .toCharArray();
private final Random random = new Random(); private final Random random = new Random();
@Override @Override
public String getRandomPassword(T player) { public String getRandomPassword(P player) {
StringBuilder generatedPassword = new StringBuilder(8); StringBuilder generatedPassword = new StringBuilder(8);
for (int i = 1; i <= 8; i++) { for (int i = 1; i <= 8; i++) {
generatedPassword.append(PASSWORD_CHARACTERS[random.nextInt(PASSWORD_CHARACTERS.length - 1)]); generatedPassword.append(PASSWORD_CHARACTERS[random.nextInt(PASSWORD_CHARACTERS.length - 1)]);

View File

@ -1,6 +1,6 @@
package com.github.games647.fastlogin.core.hooks; package com.github.games647.fastlogin.core.hooks;
public interface PasswordGenerator<T> { public interface PasswordGenerator<P> {
String getRandomPassword(T player); String getRandomPassword(P player);
} }

View File

@ -62,7 +62,7 @@ public abstract class FastLoginCore<P> {
protected final Map<String, String> localeMessages = new ConcurrentHashMap<>(); protected final Map<String, String> localeMessages = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Object> pendingLogins; private final ConcurrentMap<String, Object> pendingLogins = FastLoginCore.buildCache(5, 0);
private final Set<UUID> pendingConfirms = Sets.newHashSet(); private final Set<UUID> pendingConfirms = Sets.newHashSet();
private final SharedConfig sharedConfig; private final SharedConfig sharedConfig;
@ -72,7 +72,6 @@ public abstract class FastLoginCore<P> {
private AuthPlugin<P> authPlugin; private AuthPlugin<P> authPlugin;
public FastLoginCore(Map<String, Object> config) { public FastLoginCore(Map<String, Object> config) {
this.pendingLogins = FastLoginCore.buildCache(5, 0);
this.sharedConfig = new SharedConfig(config); this.sharedConfig = new SharedConfig(config);
} }
@ -102,8 +101,6 @@ public abstract class FastLoginCore<P> {
public abstract void loadMessages(); public abstract void loadMessages();
public abstract void loadConfig();
public abstract MojangApiConnector makeApiConnector(Logger logger, List<String> addresses, int requests); public abstract MojangApiConnector makeApiConnector(Logger logger, List<String> addresses, int requests);
public boolean setupDatabase() { public boolean setupDatabase() {
@ -165,7 +162,7 @@ public abstract class FastLoginCore<P> {
return false; return false;
} }
public SharedConfig getSharedConfig() { public SharedConfig getConfig() {
return sharedConfig; return sharedConfig;
} }

View File

@ -23,13 +23,12 @@ public abstract class JoinManagement<T, S extends LoginSource> {
return; return;
} }
SharedConfig sharedConfig = core.getSharedConfig(); SharedConfig config = core.getConfig();
String ip = source.getAddress().getAddress().getHostAddress(); String ip = source.getAddress().getAddress().getHostAddress();
try { try {
if (profile.getUserId() == -1) { if (profile.getUserId() == -1) {
if (core.getPendingLogins().containsKey(ip + username) if (core.getPendingLogins().containsKey(ip + username) && config.get("secondAttemptCracked", false)) {
&& sharedConfig.get("secondAttemptCracked", false)) {
core.getLogger().log(Level.INFO, "Second attempt login -> cracked {0}", username); core.getLogger().log(Level.INFO, "Second attempt login -> cracked {0}", username);
//first login request failed so make a cracked session //first login request failed so make a cracked session
@ -38,20 +37,21 @@ public abstract class JoinManagement<T, S extends LoginSource> {
} }
UUID premiumUUID = null; UUID premiumUUID = null;
if (sharedConfig.get("nameChangeCheck", false) || sharedConfig.get("autoRegister", false)) { if (config.get("nameChangeCheck", false) || config.get("autoRegister", false)) {
core.getLogger().log(Level.FINER, "Player {0} uses a premium username", username);
premiumUUID = core.getApiConnector().getPremiumUUID(username); premiumUUID = core.getApiConnector().getPremiumUUID(username);
} }
if (premiumUUID == null if (premiumUUID == null
|| (!checkNameChange(premiumUUID, source, username) || (!checkNameChange(source, username, premiumUUID)
&& !checkPremiumName(username, source, profile))) { && !checkPremiumName(source, username, profile))) {
//nothing detected the player as premium -> start a cracked session //nothing detected the player as premium -> start a cracked session
startCrackedSession(source, profile, username); startCrackedSession(source, profile, username);
} }
} else if (profile.isPremium()) { } else if (profile.isPremium()) {
requestPremiumLogin(source, profile, username, true); requestPremiumLogin(source, profile, username, true);
} else { } else {
if (core.getSharedConfig().get("switchMode", false)) { if (core.getConfig().get("switchMode", false)) {
source.kick(core.getMessage("switch-kick-message")); source.kick(core.getMessage("switch-kick-message"));
return; return;
} }
@ -63,10 +63,8 @@ public abstract class JoinManagement<T, S extends LoginSource> {
} }
} }
private boolean checkPremiumName(String username, S source, PlayerProfile profile) throws Exception { private boolean checkPremiumName(S source, String username, PlayerProfile profile) throws Exception {
if (core.getSharedConfig().get("autoRegister", false) if (core.getConfig().get("autoRegister", false) && (authHook == null || !authHook.isRegistered(username))) {
&& (authHook == null || !authHook.isRegistered(username))) {
core.getLogger().log(Level.FINER, "Player {0} uses a premium username", username);
requestPremiumLogin(source, profile, username, false); requestPremiumLogin(source, profile, username, false);
return true; return true;
} }
@ -74,9 +72,9 @@ public abstract class JoinManagement<T, S extends LoginSource> {
return false; return false;
} }
private boolean checkNameChange(UUID premiumUUID, S source, String username) { private boolean checkNameChange(S source, String username, UUID premiumUUID) {
//user not exists in the db //user not exists in the db
if (core.getSharedConfig().get("nameChangeCheck", false)) { if (core.getConfig().get("nameChangeCheck", false)) {
PlayerProfile profile = core.getStorage().loadProfile(premiumUUID); PlayerProfile profile = core.getStorage().loadProfile(premiumUUID);
if (profile != null) { if (profile != null) {
//uuid exists in the database //uuid exists in the database

View File

@ -137,7 +137,7 @@ public abstract class MojangApiConnector {
return null; return null;
} }
public abstract boolean hasJoinedServer(Object session, String serverId); public abstract boolean hasJoinedServer(LoginSession session, String serverId);
protected abstract String getUUIDFromJson(String json); protected abstract String getUUIDFromJson(String json);