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

View File

@ -5,21 +5,30 @@ import java.util.List;
public class AuthServiceInfo { public class AuthServiceInfo {
private final boolean enabled; private final boolean enabled;
private final boolean registerEnabled;
private final String authfile; private final String authfile;
private final int ipLimit; private final int ipLimit;
private final List<String> joinMessages; 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.enabled = enabled;
this.registerEnabled = registerEnabled;
this.authfile = authfile; this.authfile = authfile;
this.ipLimit = timeout; this.ipLimit = timeout;
this.joinMessages = joinMessages; this.joinMessages = joinMessages;
this.loginTimeout = loginTimeout;
} }
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
public boolean isRegisterEnabled() {
return registerEnabled;
}
public String getAuthfile() { public String getAuthfile() {
return authfile; return authfile;
} }
@ -32,4 +41,8 @@ public class AuthServiceInfo {
return joinMessages; 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 Map<String, Object> auth = this.get("authservice", new HashMap<String, Object>());
final List<String> defaultJoinMessages = new ArrayList<String>(); final List<String> defaultJoinMessages = new ArrayList<String>();
defaultJoinMessages.add("&3Welcome to my &aEaglercraftBungee &3server!"); 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 @Override

View File

@ -1,5 +1,9 @@
package net.md_5.bungee.eaglercraft; 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.BungeeCord;
import net.md_5.bungee.ServerConnection; import net.md_5.bungee.ServerConnection;
import net.md_5.bungee.UserConnection; import net.md_5.bungee.UserConnection;
@ -26,28 +30,20 @@ public class AuthHandler extends PacketHandler {
private final HandlerBoss handlerBoss; private final HandlerBoss handlerBoss;
private final String username; private final String username;
private static final Collection<AuthHandler> openHandlers = new LinkedList();
private boolean loggedIn = false; private boolean loggedIn = false;
private long startTime;
public AuthHandler(final ProxyServer bungee, final UserConnection con, final HandlerBoss handlerBoss) { public AuthHandler(final ProxyServer bungee, final UserConnection con, final HandlerBoss handlerBoss) {
this.bungee = bungee; this.bungee = bungee;
this.con = con; this.con = con;
this.handlerBoss = handlerBoss; this.handlerBoss = handlerBoss;
this.username = this.con.getName(); this.username = this.con.getName();
this.startTime = System.currentTimeMillis();
new Thread(() -> { synchronized(openHandlers) {
for (int i = 0; i < 120; i++) { openHandlers.add(this);
if (this.loggedIn) }
break;
try {
Thread.sleep(250);
} catch (InterruptedException ignored) {
}
}
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, this.con.unsafe().sendPacket(new Packet1Login(0, "END", (byte) 2, 1, (byte) 0, (byte) 0,
(byte) this.con.getPendingConnection().getListener().getTabListSize())); (byte) this.con.getPendingConnection().getListener().getTabListSize()));
@ -102,18 +98,22 @@ public class AuthHandler extends PacketHandler {
break; break;
case "register": case "register":
case "reg": case "reg":
if (args.length == 1 || args.length == 2) { if(BungeeCord.getInstance().config.getAuthInfo().isRegisterEnabled()) {
this.con.sendMessage("\u00A7cUsage: /" + args[0].toLowerCase() + " <password> <confirmPassword>"); if (args.length == 1 || args.length == 2) {
} else if (!args[1].equals(args[2])) { this.con.sendMessage("\u00A7cUsage: /" + args[0].toLowerCase() + " <password> <confirmPassword>");
this.con.sendMessage("\u00A7cThose passwords do not match!"); } else if (!args[1].equals(args[2])) {
} else if (authSystem.isRegistered(this.username)) { this.con.sendMessage("\u00A7cThose passwords do not match!");
this.con.sendMessage("\u00A7cThis username is already registered!"); } else if (authSystem.isRegistered(this.username)) {
} else if (authSystem.register(this.username, args[1], this.con.sendMessage("\u00A7cThis username is already registered!");
this.con.getAddress().getAddress().getHostAddress())) { } else if (authSystem.register(this.username, args[1],
this.con.sendMessage("\u00A7cSuccessfully registered and logging in..."); this.con.getAddress().getAddress().getHostAddress())) {
this.onLogin(); this.con.sendMessage("\u00A7cSuccessfully registered and logging in...");
} else { this.onLogin();
this.con.sendMessage("\u00A7cUnable to register..."); } else {
this.con.sendMessage("\u00A7cUnable to register...");
}
}else {
this.con.disconnect("Registration is not enabled!");
} }
break; break;
case "changepassword": case "changepassword":
@ -155,4 +155,24 @@ public class AuthHandler extends PacketHandler {
public String toString() { public String toString() {
return "[" + this.con.getName() + "] -> AuthHandler"; 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();
}
}
}
}
} }