added register_enable, login_timeout to config, replaced thread in AuthHandler with a central timer

This commit is contained in:
LAX1DUDE 2022-07-24 14:13:40 -07:00
parent 7ffa94ab17
commit e277662c8e
4 changed files with 77 additions and 30 deletions

View file

@ -88,6 +88,7 @@ import net.md_5.bungee.command.CommandServer;
import net.md_5.bungee.command.ConsoleCommandSender;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.YamlConfig;
import net.md_5.bungee.eaglercraft.AuthHandler;
import net.md_5.bungee.eaglercraft.AuthSystem;
import net.md_5.bungee.eaglercraft.BanList;
import net.md_5.bungee.eaglercraft.DomainBlacklist;
@ -115,6 +116,7 @@ public class BungeeCord extends ProxyServer {
private final Timer saveThread;
private final Timer reloadBanThread;
private final Timer closeInactiveSockets;
private final Timer authTimeoutTimer;
private Collection<Channel> listeners;
private Collection<WebSocketListener> wsListeners;
private final Map<String, UserConnection> connections;
@ -141,7 +143,8 @@ public class BungeeCord extends ProxyServer {
this.eventLoops = (MultithreadEventLoopGroup) new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setNameFormat("Netty IO Thread #%1$d").build());
this.saveThread = new Timer("Reconnect Saver");
this.reloadBanThread = new Timer("Ban List Reload");
this.closeInactiveSockets = new Timer("close Inactive WebSockets");
this.closeInactiveSockets = new Timer("Close Inactive WebSockets");
this.authTimeoutTimer = new Timer("Auth Timeout");
this.listeners = new HashSet<Channel>();
this.wsListeners = new HashSet<WebSocketListener>();
this.connections = (Map<String, UserConnection>) new CaseInsensitiveMap();
@ -274,6 +277,13 @@ public class BungeeCord extends ProxyServer {
}
}
}, 0L, TimeUnit.SECONDS.toMillis(10L));
final int authTimeout = this.config.getAuthInfo().getLoginTimeout();
this.authTimeoutTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
AuthHandler.closeInactive(authTimeout);
}
}, 0L, TimeUnit.SECONDS.toMillis(2L));
}
public void startListeners() {
@ -362,6 +372,9 @@ public class BungeeCord extends ProxyServer {
BungeeCord.this.reconnectHandler.save();
BungeeCord.this.reconnectHandler.close();
BungeeCord.this.saveThread.cancel();
BungeeCord.this.reloadBanThread.cancel();
BungeeCord.this.closeInactiveSockets.cancel();
BungeeCord.this.authTimeoutTimer.cancel();
BungeeCord.this.getLogger().info("Disabling plugins");
for (final Plugin plugin : BungeeCord.this.pluginManager.getPlugins()) {
plugin.onDisable();

View file

@ -5,21 +5,30 @@ import java.util.List;
public class AuthServiceInfo {
private final boolean enabled;
private final boolean registerEnabled;
private final String authfile;
private final int ipLimit;
private final List<String> joinMessages;
private final int loginTimeout;
public AuthServiceInfo(boolean enabled, String authfile, int timeout, List<String> joinMessages) {
public AuthServiceInfo(boolean enabled, boolean registerEnabled, String authfile,
int timeout, List<String> joinMessages, int loginTimeout) {
this.enabled = enabled;
this.registerEnabled = registerEnabled;
this.authfile = authfile;
this.ipLimit = timeout;
this.joinMessages = joinMessages;
this.loginTimeout = loginTimeout;
}
public boolean isEnabled() {
return enabled;
}
public boolean isRegisterEnabled() {
return registerEnabled;
}
public String getAuthfile() {
return authfile;
}
@ -32,4 +41,8 @@ public class AuthServiceInfo {
return joinMessages;
}
public int getLoginTimeout() {
return loginTimeout;
}
}

View file

@ -281,7 +281,8 @@ public class YamlConfig implements ConfigurationAdapter {
final Map<String, Object> auth = this.get("authservice", new HashMap<String, Object>());
final List<String> defaultJoinMessages = new ArrayList<String>();
defaultJoinMessages.add("&3Welcome to my &aEaglercraftBungee &3server!");
return new AuthServiceInfo(this.get("enabled", false, auth), this.get("authfile", "auths.db", auth), this.get("ip_limit", 0, auth), this.get("join_messages", defaultJoinMessages, auth));
return new AuthServiceInfo(this.get("enabled", false, auth), this.get("register_enabled", true, auth), this.get("authfile", "auths.db", auth),
this.get("ip_limit", 0, auth), this.get("join_messages", defaultJoinMessages, auth), this.get("login_timeout", 30, auth));
}
@Override

View file

@ -1,5 +1,9 @@
package net.md_5.bungee.eaglercraft;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ServerConnection;
import net.md_5.bungee.UserConnection;
@ -26,28 +30,20 @@ public class AuthHandler extends PacketHandler {
private final HandlerBoss handlerBoss;
private final String username;
private static final Collection<AuthHandler> openHandlers = new LinkedList();
private boolean loggedIn = false;
private long startTime;
public AuthHandler(final ProxyServer bungee, final UserConnection con, final HandlerBoss handlerBoss) {
this.bungee = bungee;
this.con = con;
this.handlerBoss = handlerBoss;
this.username = this.con.getName();
this.startTime = System.currentTimeMillis();
new Thread(() -> {
for (int i = 0; i < 120; i++) {
if (this.loggedIn)
break;
try {
Thread.sleep(250);
} catch (InterruptedException ignored) {
synchronized(openHandlers) {
openHandlers.add(this);
}
}
if (this.loggedIn)
return;
this.con.disconnect("You did not login in time!");
}).start();
this.con.unsafe().sendPacket(new Packet1Login(0, "END", (byte) 2, 1, (byte) 0, (byte) 0,
(byte) this.con.getPendingConnection().getListener().getTabListSize()));
@ -102,6 +98,7 @@ public class AuthHandler extends PacketHandler {
break;
case "register":
case "reg":
if(BungeeCord.getInstance().config.getAuthInfo().isRegisterEnabled()) {
if (args.length == 1 || args.length == 2) {
this.con.sendMessage("\u00A7cUsage: /" + args[0].toLowerCase() + " <password> <confirmPassword>");
} else if (!args[1].equals(args[2])) {
@ -115,6 +112,9 @@ public class AuthHandler extends PacketHandler {
} else {
this.con.sendMessage("\u00A7cUnable to register...");
}
}else {
this.con.disconnect("Registration is not enabled!");
}
break;
case "changepassword":
case "changepasswd":
@ -155,4 +155,24 @@ public class AuthHandler extends PacketHandler {
public String toString() {
return "[" + this.con.getName() + "] -> AuthHandler";
}
public static void closeInactive(int timeout) {
synchronized(openHandlers) {
long millis = System.currentTimeMillis();
timeout *= 1000;
Iterator<AuthHandler> handlers = openHandlers.iterator();
while(handlers.hasNext()) {
AuthHandler h = handlers.next();
if(!h.loggedIn) {
if(millis - h.startTime > timeout) {
h.con.disconnect("You did not login in time you eagler!");
handlers.remove();
}
}else {
handlers.remove();
}
}
}
}
}