More shared project code for less errors and less duplication

This commit is contained in:
games647
2016-09-11 18:59:42 +02:00
parent 8e5da01be0
commit 3e9c8e3a7e
16 changed files with 402 additions and 280 deletions
@@ -0,0 +1,10 @@
package com.github.games647.fastlogin.core;
public interface AuthPlugin<T> {
boolean forceLogin(T player);
boolean forceRegister(T player, String password);
boolean isRegistered(String playerName) throws Exception;
}
@@ -33,12 +33,14 @@ public abstract class FastLoginCore {
protected final Map<String, String> localeMessages = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Object> pendingLogins;
private final SharedConfig sharedConfig;
private MojangApiConnector mojangApiConnector;
private AuthStorage storage;
public FastLoginCore(ConcurrentMap<String, Object> pendingLogins) {
public FastLoginCore(ConcurrentMap<String, Object> pendingLogins, Map<String, Object> config) {
this.pendingLogins = pendingLogins;
this.sharedConfig = new SharedConfig(config);
}
public void setMojangApiConnector(MojangApiConnector mojangApiConnector) {
@@ -118,6 +120,10 @@ public abstract class FastLoginCore {
return false;
}
public SharedConfig getSharedConfig() {
return sharedConfig;
}
public ConcurrentMap<String, Object> getPendingLogins() {
return pendingLogins;
}
@@ -0,0 +1,96 @@
package com.github.games647.fastlogin.core;
import java.util.UUID;
import java.util.logging.Level;
public abstract class JoinManagement<T, S extends LoginSource> {
protected final FastLoginCore core;
protected final AuthPlugin<T> authHook;
public JoinManagement(FastLoginCore core, AuthPlugin<T> authHook) {
this.core = core;
this.authHook = authHook;
}
public void onLogin(String username, S source) {
PlayerProfile profile = core.getStorage().loadProfile(username);
if (profile == null) {
return;
}
SharedConfig sharedConfig = core.getSharedConfig();
String ip = source.getAddress().getAddress().getHostAddress();
try {
if (profile.getUserId() == -1) {
if (core.getPendingLogins().containsKey(ip + username)
&& sharedConfig.get("secondAttemptCracked", false)) {
core.getLogger().log(Level.INFO, "Second attempt login -> cracked {0}", username);
//first login request failed so make a cracked session
startCrackedSession(source, profile, username);
return;
}
UUID premiumUUID = null;
if (sharedConfig.get("nameChangeCheck", false) || sharedConfig.get("autoRegister", false)) {
premiumUUID = core.getMojangApiConnector().getPremiumUUID(username);
}
if (premiumUUID == null
|| (!checkNameChange(premiumUUID, source, username)
&& !checkPremiumName(username, source, profile))) {
//nothing detected the player as premium -> start a cracked session
startCrackedSession(source, profile, username);
}
} else if (profile.isPremium()) {
requestPremiumLogin(source, profile, username, true);
} else {
if (core.getSharedConfig().get("switchMode", false)) {
source.kick(core.getMessage("switch-kick-message"));
return;
}
startCrackedSession(source, profile, username);
}
} catch (Exception ex) {
core.getLogger().log(Level.SEVERE, "Failed to check premium state", ex);
}
}
private boolean checkPremiumName(String username, S source, PlayerProfile profile)
throws Exception {
if (core.getSharedConfig().get("autoRegister", false)
&& (authHook == null || !authHook.isRegistered(username))) {
core.getLogger().log(Level.FINER, "Player {0} uses a premium username", username);
requestPremiumLogin(source, profile, username, false);
return true;
}
return false;
}
private boolean checkNameChange(UUID premiumUUID, S source, String username) {
//user not exists in the db
if (core.getSharedConfig().get("nameChangeCheck", false)) {
PlayerProfile profile = core.getStorage().loadProfile(premiumUUID);
if (profile != null) {
//uuid exists in the database
core.getLogger().log(Level.FINER, "Player {0} changed it's username", premiumUUID);
//update the username to the new one in the database
profile.setPlayerName(username);
requestPremiumLogin(source, profile, username, false);
return true;
}
}
return false;
}
public abstract void requestPremiumLogin(S source, PlayerProfile profile, String username, boolean registered);
public abstract void startCrackedSession(S source, PlayerProfile profile, String username);
}
@@ -0,0 +1,12 @@
package com.github.games647.fastlogin.core;
import java.net.InetSocketAddress;
public interface LoginSource {
void setOnlineMode() throws Exception;
void kick(String message) throws Exception;
InetSocketAddress getAddress();
}
@@ -0,0 +1,22 @@
package com.github.games647.fastlogin.core;
import java.util.Map;
public class SharedConfig {
private final Map<String, Object> configValues;
public SharedConfig(Map<String, Object> configValues) {
this.configValues = configValues;
}
@SuppressWarnings("unchecked")
public <T> T get(String path, T def) {
Object val = configValues.get(path);
return ( val != null ) ? (T) val : def;
}
public <T> T get(String path) {
return get(path, null);
}
}