Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor reconnection logic #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/main/java/net/elytrium/limboauth/LimboAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.scheduler.ScheduledTask;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.util.ratelimit.Ratelimiter;
import com.velocitypowered.proxy.util.ratelimit.Ratelimiters;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand Down Expand Up @@ -153,7 +154,6 @@ public class LimboAuth {
private final Map<InetAddress, CachedBruteforceUser> bruteforceCache = new ConcurrentHashMap<>();
private final Map<UUID, Runnable> postLoginTasks = new ConcurrentHashMap<>();
private final Set<String> unsafePasswords = new HashSet<>();
private final Set<String> forcedPreviously = Collections.synchronizedSet(new HashSet<>());
private final Set<String> pendingLogins = ConcurrentHashMap.newKeySet();

private final HttpClient client = HttpClient.newHttpClient();
Expand Down Expand Up @@ -558,7 +558,7 @@ public boolean needAuth(Player player) {

public void authPlayer(Player player) {
boolean isFloodgate = !Settings.IMP.MAIN.FLOODGATE_NEED_AUTH && this.floodgateApi.isFloodgatePlayer(player.getUniqueId());
if (!isFloodgate && this.isForcedPreviously(player.getUsername()) && this.isPremium(player.getUsername())) {
if (!isFloodgate && this.isForceOfflineMode(player) && this.isPremium(player.getUsername())) {
player.disconnect(this.reconnectKick);
return;
}
Expand Down Expand Up @@ -923,16 +923,9 @@ public void clearBruteforceAttempts(InetAddress address) {
this.bruteforceCache.remove(address);
}

public void saveForceOfflineMode(String nickname) {
this.forcedPreviously.add(nickname);
}

public void unsetForcedPreviously(String nickname) {
this.forcedPreviously.remove(nickname);
}

public boolean isForcedPreviously(String nickname) {
return this.forcedPreviously.contains(nickname);
public boolean isForceOfflineMode(Player player) {
return player instanceof ConnectedPlayer connectedPlayer
&& connectedPlayer.getConnection().getChannel().hasAttr(AuthListener.FORCE_OFFLINE_MODE);
}

public Set<String> getPendingLogins() {
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/net/elytrium/limboauth/listener/AuthListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.client.InitialInboundConnection;
import com.velocitypowered.proxy.connection.client.LoginInboundConnection;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.sql.SQLException;
Expand All @@ -54,6 +56,8 @@ public class AuthListener {
private static final MethodHandle DELEGATE_FIELD;
//private static final MethodHandle LOGIN_FIELD;

public static final AttributeKey<Boolean> FORCE_OFFLINE_MODE = AttributeKey.valueOf("limboauth-force-offline-mode");

private final LimboAuth plugin;
private final Dao<RegisteredPlayer, String> playerDao;
private final FloodgateApiHolder floodgateApi;
Expand Down Expand Up @@ -106,13 +110,11 @@ public void onPreLoginEvent(PreLoginEvent event) {
} else {
try {
MinecraftConnection connection = this.getConnection(event.getConnection());
if (!connection.isClosed()) {
this.plugin.saveForceOfflineMode(username);

// As Velocity doesnt have any events for our usecase, just inject into netty
connection.getChannel().closeFuture().addListener(future -> {
this.plugin.unsetForcedPreviously(username);
});
if (connection != null) {
Channel channel = connection.getChannel();
if (channel != null) {
connection.getChannel().attr(FORCE_OFFLINE_MODE).set(true);
}
}
} catch (Throwable throwable) {
throw new IllegalStateException("failed to track client disconnection", throwable);
Expand Down
Loading