Skip to content

Commit

Permalink
feat: should be working now [not tested yet]
Browse files Browse the repository at this point in the history
  • Loading branch information
SnakeAmazing committed Nov 18, 2023
1 parent 8d43d13 commit 5367dbc
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,20 @@ interface AltAccountExpiration {
@ConfHeader({"Controls if all servers should register the IP address of the player connecting."})
interface AltsRegistry {

@ConfComments("If true, all servers will register alts.")
@ConfKey("enable-all")
@ConfDefault.DefaultBoolean(true)
boolean enableAll();

@ConfComments({"If enableAll is false, this list will be used ",
"to determine which servers will register alts.",
@ConfComments({"The server names in this list will be excluded from associating the IP address of the player connecting.",
"Please note that the server's name of the list should be the same as the ones in your proxy config",
"This is intended to be used by LibertyBans proxy installations.",
"If this is a backend server, please skip this option."})
@ConfKey("servers")
"This is intended to be used by LibertyBans proxy installations."
})
@ConfKey("servers-without-ip-registration")
@DefaultStrings("")
List<String> servers();

@ConfComments({"If true, this backend server will register alts"})
@ConfKey("should-register")
@ConfComments({"If you want to register the IP address of the player connecting, set this to true.",
"If you are running a Proxy and don't want to register the IP when players connect, ",
"set this to false and add the auth server names in the list above.",
"If this is a backend server, then set it to false if it's an auth server, and true otherwise."})
@ConfKey("should-register-on-connection)")
@ConfDefault.DefaultBoolean(true)
boolean shouldRegister();
boolean shouldRegisterOnConnection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jooq.DSLContext;
import space.arim.libertybans.api.NetworkAddress;
import space.arim.libertybans.api.PunishmentType;
Expand Down Expand Up @@ -75,20 +76,10 @@ CentralisedFuture<Component> executeAndCheckConnection(UUID uuid, String name, N
Set<ServerScope> scopes, SelectorImpl selector) {
return queryExecutor.get().queryWithRetry((context, transaction) -> {
Instant currentTime = time.currentTimestamp();

EnforcementConfig config = configs.getMainConfig().enforcement();
if (config.altsRegistry().enableAll()) {
doAssociation(uuid, name, address, currentTime, context);

} else if (config.altsRegistry().shouldRegister()) {
if (config.altsRegistry().shouldRegisterOnConnection()) {
doAssociation(uuid, name, address, currentTime, context);

} else {
List<String> servers = config.altsRegistry().servers();
String serverName = configs.getScopeConfig().serverName().overrideValue();
if (!servers.isEmpty() && servers.contains(serverName)) {
doAssociation(uuid, name, address, currentTime, context);
}
}

Punishment ban = selector.selectionByApplicabilityBuilder(uuid, address)
Expand Down Expand Up @@ -126,6 +117,36 @@ CentralisedFuture<Component> executeAndCheckConnection(UUID uuid, String name, N
});
}

public CentralisedFuture<@Nullable Component> checkServerSwitch(UUID uuid, String name, NetworkAddress address,
String destinationServer, ServerScope scope, SelectorImpl selector) {

return queryExecutor.get().queryWithRetry((context, transaction) -> {
boolean shouldRegister = configs.getMainConfig().enforcement().altsRegistry().shouldRegisterOnConnection();
List<String> servers = configs.getMainConfig().enforcement().altsRegistry().servers();

if (shouldRegister || !servers.contains(destinationServer)) {
doAssociation(uuid, name, address, time.currentTimestamp(), context);
}

if (!configs.getMainConfig().platforms().proxies().enforceServerSwitch()) {
return null;
}

return selector.selectionByApplicabilityBuilder(uuid, address)
.type(PunishmentType.BAN)
.scope(scope)
.build()
.getFirstSpecificPunishment(SortPunishments.LATEST_END_DATE_FIRST);

}).thenCompose((punishment) -> {
if (punishment instanceof Punishment) {
return formatter.getPunishmentMessage((Punishment) punishment);
} else {
return futuresFactory.completedFuture(null);
}
});
}

private void doAssociation(UUID uuid, String name, NetworkAddress address,
Instant currentTime, DSLContext context) {
Association association = new Association(uuid, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public interface Guardian {
* Adds the uuid and name to the local fast cache, queries for an applicable ban, and formats the
* ban reason as the punishment message.
*
* @param uuid the player's uuid
* @param name the player's name
* @param address the player's network address
* @param uuid the player's uuid
* @param name the player's name
* @param address the player's network address
* @return a future which yields the punishment message if denied, else null if allowed
*/
CentralisedFuture<@Nullable Component> executeAndCheckConnection(UUID uuid, String name, NetworkAddress address);
Expand All @@ -63,11 +63,27 @@ public interface Guardian {
* Queries for an applicable ban, and formats the ban reason as the punishment message.
*
* @param uuid the player's uuid
* @param name the player's name
* @param address the player's network address
* @param destinationServer the player's destination server
* @return a future which yields the punishment message if denied, else null if allowed
*/
CentralisedFuture<@Nullable Component> checkServerSwitch(UUID uuid, InetAddress address, String destinationServer);
CentralisedFuture<@Nullable Component> checkServerSwitch(UUID uuid, String name, NetworkAddress address, String destinationServer);

/**
* Enforces a server switch, returning a punishment message if denied, null if allowed. <br>
* <br>
* Queries for an applicable ban, and formats the ban reason as the punishment message.
*
* @param uuid the player's uuid
* @param name the player's name
* @param address the player's network address
* @param destinationServer the player's destination server
* @return a future which yields the punishment message if denied, else null if allowed
*/
default CentralisedFuture<@Nullable Component> checkServerSwitch(UUID uuid, String name, InetAddress address, String destinationServer) {
return checkServerSwitch(uuid, name, NetworkAddress.of(address), destinationServer);
}

/**
* Enforces a chat message or executed command, returning a punishment message if denied, null if allowed. <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import space.arim.libertybans.api.NetworkAddress;
import space.arim.libertybans.api.PunishmentType;
import space.arim.libertybans.api.scope.ScopeManager;
import space.arim.libertybans.api.select.SortPunishments;
import space.arim.libertybans.core.config.Configs;
import space.arim.libertybans.core.config.InternalFormatter;
import space.arim.libertybans.core.selector.cache.MuteCache;
Expand Down Expand Up @@ -98,23 +96,13 @@ private static <R> Function<Throwable, R> timeoutHandler(String where) {
}

@Override
public CentralisedFuture<@Nullable Component> checkServerSwitch(UUID uuid, InetAddress address,
public CentralisedFuture<@Nullable Component> checkServerSwitch(UUID uuid, String name, NetworkAddress address,
String destinationServer) {
if (!configs.getMainConfig().platforms().proxies().enforceServerSwitch()) {
return futuresFactory.completedFuture(null);
}
return selector
.selectionByApplicabilityBuilder(uuid, address)
.type(PunishmentType.BAN)
.scope(scopeManager.specificScope(destinationServer))
.build()
.getFirstSpecificPunishment(SortPunishments.LATEST_END_DATE_FIRST)
.thenCompose((punishment) -> {
if (punishment.isEmpty()) {
return futuresFactory.completedFuture(null);
}
return formatter.getPunishmentMessage(punishment.get());
})
.executeAndCheckServerSwitch(
uuid, name, address,
scopeManager.specificScope(destinationServer), destinationServer
)
.toCompletableFuture()
.orTimeout(12, TimeUnit.SECONDS)
.exceptionally(timeoutHandler("server switch"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,27 @@ public interface InternalSelector extends PunishmentSelector {
/**
* Checks a player connection's in a single connection query, enforcing any applicable bans,
* connection limits, and dealing out alt checks
*
* @param uuid the player uuid
* @param name the player name
* @param address the player address
* @param scopes the server scopes to include in the selection query
*
* @param uuid the player uuid
* @param name the player name
* @param address the player address
* @param scopes the server scopes to include in the selection query
* @return a future which yields the denial message, or null if there is none
*/
CentralisedFuture<Component> executeAndCheckConnection(UUID uuid, String name, NetworkAddress address,
Set<ServerScope> scopes);

/**
* Checks a player connection's in a single connection query, enforcing any applicable bans if
* enforce server switch is enabled.
*
* @param uuid the player uuid
* @param name the player name
* @param address the player address
* @param scope the server scope to include in the selection query
* @param destinationServer the server the player is switching to
* @return a future which yields the denial message, or null if there is none
*/
CentralisedFuture<Component> executeAndCheckServerSwitch(UUID uuid, String name, NetworkAddress address,
ServerScope scope, String destinationServer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public CentralisedFuture<Component> executeAndCheckConnection(UUID uuid, String
return gatekeeper.executeAndCheckConnection(uuid, name, address, scopes, this);
}

@Override
public CentralisedFuture<Component> executeAndCheckServerSwitch(UUID uuid, String name, NetworkAddress address,
ServerScope scope, String destinationServer) {
return gatekeeper.checkServerSwitch(uuid, name, address, destinationServer, scope, this);
}

@Override
public ReactionStage<Optional<Punishment>> getCachedMute(UUID uuid, NetworkAddress address) {
Objects.requireNonNull(uuid, "uuid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void onServerSwitch(ServerConnectEvent event) {
InetAddress address = addressReporter.getAddress(player);

Component message = guardian.checkServerSwitch(
player.getUniqueId(), address, event.getTarget().getName()
player.getUniqueId(), player.getName(), address, event.getTarget().getName()
).join();
if (message != null) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public EventTask onConnect(LoginEvent event) {
return null;
}
Player player = event.getPlayer();

return EventTask.resumeWhenComplete(guardian.executeAndCheckConnection(
player.getUniqueId(), player.getUsername(), player.getRemoteAddress().getAddress()
).thenAccept((message) -> {
Expand All @@ -91,7 +92,8 @@ public EventTask onServerSwitch(ServerPreConnectEvent event) {
}
Player player = event.getPlayer();
return EventTask.resumeWhenComplete(guardian.checkServerSwitch(
player.getUniqueId(), player.getRemoteAddress().getAddress(), destination.getServerInfo().getName()
player.getUniqueId(), player.getUsername(), player.getRemoteAddress().getAddress(),
destination.getServerInfo().getName()
).thenAccept((message) -> {
if (message != null) {
event.setResult(ServerPreConnectEvent.ServerResult.denied());
Expand Down

0 comments on commit 5367dbc

Please sign in to comment.