Use BedrockService in JoinManagement

Since the code only needs to interact with Geyser, if Floodgate is not
installed, and perform similar things with both, it's reasonable, to
merge their code.
This commit breaks premium checking with `auth-type=online` in Geyser
This commit is contained in:
Smart123s
2021-11-28 13:44:57 +01:00
committed by games647
parent f76c7bd62f
commit fcd2aa95f0
15 changed files with 94 additions and 73 deletions
@@ -27,13 +27,18 @@ package com.github.games647.fastlogin.core.hooks.bedrock;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import com.github.games647.craftapi.model.Profile;
import com.github.games647.craftapi.resolver.RateLimitException;
import com.github.games647.fastlogin.core.StoredProfile;
import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.shared.LoginSource;
public abstract class BedrockService {
/**
* @param B is an instance of either FloodgatePlayer or GeyserSession
*/
public abstract class BedrockService<B> {
protected final FastLoginCore<?, ?, ?> core;
protected final String allowConflict;
@@ -79,4 +84,39 @@ public abstract class BedrockService {
}
/**
* The Floodgate / Geyser API does not support querying players by name, so this function
* iterates over every online Bedrock Player and checks if the requested
* username can be found
* <br>
* <i>Falls back to non-prefixed name checks, if ProtocolLib is installed</i>
*
* @param prefixedUsername the name of the player with the prefix appended
* @return Bedrock Player if found, null otherwise
*/
public B getBedrockPlayer(String prefixedUsername) {
return null;
}
public B getBedrockPlayer(UUID uuid) {
return null;
}
public boolean isBedrockPlayer(UUID uuid) {
return getBedrockPlayer(uuid) != null;
}
public boolean isBedrockConnection(String username) {
return getBedrockPlayer(username) != null;
}
/**
* Checks if a profile's name starts with the Floodgate prefix, if it's available
* @param profile profile of the conecting player
* @return true if the username is forbidden
*/
public boolean isUsernameForbidden(StoredProfile profile) {
return false;
}
}
@@ -35,7 +35,7 @@ import java.util.UUID;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
public class FloodgateService extends BedrockService {
public class FloodgateService extends BedrockService<FloodgatePlayer> {
private final FloodgateApi floodgate;
@@ -69,6 +69,7 @@ public class FloodgateService extends BedrockService {
return true;
}
@Override
public boolean isUsernameForbidden(StoredProfile profile) {
String playerPrefix = FloodgateApi.getInstance().getPlayerPrefix();
return profile.getName().startsWith(playerPrefix) && !playerPrefix.isEmpty();
@@ -77,7 +78,7 @@ public class FloodgateService extends BedrockService {
@Override
public void checkNameConflict(String username, LoginSource source) {
// check if the Bedrock player is linked to a Java account
FloodgatePlayer floodgatePlayer = getFloodgatePlayer(username);
FloodgatePlayer floodgatePlayer = getBedrockPlayer(username);
boolean isLinked = floodgatePlayer.getLinkedPlayer() != null;
if ("false".equals(allowConflict)
@@ -98,7 +99,7 @@ public class FloodgateService extends BedrockService {
* @param prefixedUsername the name of the player with the prefix appended
* @return FloodgatePlayer if found, null otherwise
*/
public FloodgatePlayer getFloodgatePlayer(String prefixedUsername) {
public FloodgatePlayer getBedrockPlayer(String prefixedUsername) {
//prefixes are broken with ProtocolLib, so fall back to name checks without prefixes
//this should be removed if #493 gets fixed
if (core.getPlugin().isPluginInstalled("ProtocolLib")) {
@@ -118,15 +119,15 @@ public class FloodgateService extends BedrockService {
return null;
}
public FloodgatePlayer getFloodgatePlayer(UUID uuid) {
public FloodgatePlayer getBedrockPlayer(UUID uuid) {
return FloodgateApi.getInstance().getPlayer(uuid);
}
public boolean isFloodgatePlayer(UUID uuid) {
return getFloodgatePlayer(uuid) != null;
public boolean isBedrockPlayer(UUID uuid) {
return getBedrockPlayer(uuid) != null;
}
public boolean isFloodgateConnection(String username) {
return getFloodgatePlayer(username) != null;
public boolean isBedrockConnection(String username) {
return getBedrockPlayer(username) != null;
}
}
@@ -33,7 +33,7 @@ import com.github.games647.fastlogin.core.shared.LoginSource;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession;
public class GeyserService extends BedrockService {
public class GeyserService extends BedrockService<GeyserSession> {
private final GeyserConnector geyser;
private final FastLoginCore<?, ?, ?> core;
@@ -54,15 +54,8 @@ public class GeyserService extends BedrockService {
}
}
/**
* The Geyser API does not support querying players by name, so this function
* iterates over every online Geyser Player and checks if the requested
* username can be found
*
* @param username the name of the player
* @return GeyserSession if found, null otherwise
*/
public GeyserSession getGeyserPlayer(String username) {
@Override
public GeyserSession getBedrockPlayer(String username) {
for (GeyserSession gSess : geyser.getSessionManager().getSessions().values()) {
if (gSess.getName().equals(username)) {
return gSess;
@@ -72,15 +65,8 @@ public class GeyserService extends BedrockService {
return null;
}
public GeyserSession getGeyserPlayer(UUID uuid) {
@Override
public GeyserSession getBedrockPlayer(UUID uuid) {
return geyser.getPlayerByUuid(uuid);
}
public boolean isGeyserPlayer(UUID uuid) {
return getGeyserPlayer(uuid) != null;
}
public boolean isGeyserConnection(String username) {
return getGeyserPlayer(username) != null;
}
}
@@ -29,8 +29,7 @@ import com.github.games647.craftapi.model.Profile;
import com.github.games647.craftapi.resolver.RateLimitException;
import com.github.games647.fastlogin.core.StoredProfile;
import com.github.games647.fastlogin.core.hooks.AuthPlugin;
import com.github.games647.fastlogin.core.hooks.bedrock.FloodgateService;
import com.github.games647.fastlogin.core.hooks.bedrock.GeyserService;
import com.github.games647.fastlogin.core.hooks.bedrock.BedrockService;
import com.github.games647.fastlogin.core.shared.event.FastLoginPreLoginEvent;
import java.util.Optional;
@@ -41,15 +40,12 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
protected final FastLoginCore<P, C, ?> core;
protected final AuthPlugin<P> authHook;
private final FloodgateService floodgateService;
private final GeyserService geyserService;
private final BedrockService<?> bedrockService;
public JoinManagement(FastLoginCore<P, C, ?> core, AuthPlugin<P> authHook, FloodgateService floodService,
GeyserService geyserService) {
public JoinManagement(FastLoginCore<P, C, ?> core, AuthPlugin<P> authHook, BedrockService<?> bedrockService) {
this.core = core;
this.authHook = authHook;
this.floodgateService = floodService;
this.geyserService = geyserService;
this.bedrockService = bedrockService;
}
public void onLogin(String username, S source) {
@@ -59,18 +55,14 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
return;
}
//check if the player is connecting through Floodgate
if (floodgateService != null) {
if (floodgateService.isFloodgateConnection(username)) {
floodgateService.checkNameConflict(username, source);
// skip flow for any floodgate player
//check if the player is connecting through Bedrock Edition
if (bedrockService != null) {
if (bedrockService.isBedrockConnection(username)) {
bedrockService.checkNameConflict(username, source);
// skip flow for any Bedrock player
return;
}
}
//check if the player is connecting through Geyser (without Floodgate)
else if (geyserService != null && geyserService.isGeyserConnection(username)) {
}
callFastLoginPreLoginEvent(username, source, profile);
@@ -125,7 +117,7 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
}
protected boolean isValidUsername(LoginSource source, StoredProfile profile) throws Exception {
if (floodgateService != null && floodgateService.isUsernameForbidden(profile)) {
if (bedrockService != null && bedrockService.isUsernameForbidden(profile)) {
core.getPlugin().getLog().info("Floodgate Prefix detected on cracked player");
source.kick("Your username contains illegal characters");
return false;
@@ -26,8 +26,7 @@
package com.github.games647.fastlogin.core.shared;
import com.github.games647.fastlogin.core.AsyncScheduler;
import com.github.games647.fastlogin.core.hooks.bedrock.FloodgateService;
import com.github.games647.fastlogin.core.hooks.bedrock.GeyserService;
import com.github.games647.fastlogin.core.hooks.bedrock.BedrockService;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.nio.file.Path;
@@ -55,9 +54,7 @@ public interface PlatformPlugin<C> {
}
}
FloodgateService getFloodgateService();
GeyserService getGeyserService();
BedrockService<?> getBedrockService();
default ThreadFactory getThreadFactory() {
return new ThreadFactoryBuilder()