diff --git a/eaglercraftbungee/src/main/java/net/md_5/bungee/BungeeCord.java b/eaglercraftbungee/src/main/java/net/md_5/bungee/BungeeCord.java index d4fa3e3..ff822ad 100644 --- a/eaglercraftbungee/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/eaglercraftbungee/src/main/java/net/md_5/bungee/BungeeCord.java @@ -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 listeners; private Collection wsListeners; private final Map 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(); this.wsListeners = new HashSet(); this.connections = (Map) 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(); diff --git a/eaglercraftbungee/src/main/java/net/md_5/bungee/api/config/AuthServiceInfo.java b/eaglercraftbungee/src/main/java/net/md_5/bungee/api/config/AuthServiceInfo.java index 454026b..000d4dd 100644 --- a/eaglercraftbungee/src/main/java/net/md_5/bungee/api/config/AuthServiceInfo.java +++ b/eaglercraftbungee/src/main/java/net/md_5/bungee/api/config/AuthServiceInfo.java @@ -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 joinMessages; + private final int loginTimeout; - public AuthServiceInfo(boolean enabled, String authfile, int timeout, List joinMessages) { + public AuthServiceInfo(boolean enabled, boolean registerEnabled, String authfile, + int timeout, List 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; + } + } diff --git a/eaglercraftbungee/src/main/java/net/md_5/bungee/config/YamlConfig.java b/eaglercraftbungee/src/main/java/net/md_5/bungee/config/YamlConfig.java index 34b0354..9c31fa1 100644 --- a/eaglercraftbungee/src/main/java/net/md_5/bungee/config/YamlConfig.java +++ b/eaglercraftbungee/src/main/java/net/md_5/bungee/config/YamlConfig.java @@ -281,7 +281,8 @@ public class YamlConfig implements ConfigurationAdapter { final Map auth = this.get("authservice", new HashMap()); final List defaultJoinMessages = new ArrayList(); 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 diff --git a/eaglercraftbungee/src/main/java/net/md_5/bungee/eaglercraft/AuthHandler.java b/eaglercraftbungee/src/main/java/net/md_5/bungee/eaglercraft/AuthHandler.java index 62e5e8a..dda8ea4 100644 --- a/eaglercraftbungee/src/main/java/net/md_5/bungee/eaglercraft/AuthHandler.java +++ b/eaglercraftbungee/src/main/java/net/md_5/bungee/eaglercraft/AuthHandler.java @@ -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,29 +30,21 @@ public class AuthHandler extends PacketHandler { private final HandlerBoss handlerBoss; private final String username; + private static final Collection 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) { - } - } - if (this.loggedIn) - return; - this.con.disconnect("You did not login in time!"); - }).start(); - + synchronized(openHandlers) { + openHandlers.add(this); + } + this.con.unsafe().sendPacket(new Packet1Login(0, "END", (byte) 2, 1, (byte) 0, (byte) 0, (byte) this.con.getPendingConnection().getListener().getTabListSize())); this.con.unsafe().sendPacket(new Packet9Respawn(1, (byte) 0, (byte) 2, (short) 255, "END")); @@ -102,18 +98,22 @@ public class AuthHandler extends PacketHandler { break; case "register": case "reg": - if (args.length == 1 || args.length == 2) { - this.con.sendMessage("\u00A7cUsage: /" + args[0].toLowerCase() + " "); - } else if (!args[1].equals(args[2])) { - this.con.sendMessage("\u00A7cThose passwords do not match!"); - } else if (authSystem.isRegistered(this.username)) { - this.con.sendMessage("\u00A7cThis username is already registered!"); - } else if (authSystem.register(this.username, args[1], - this.con.getAddress().getAddress().getHostAddress())) { - this.con.sendMessage("\u00A7cSuccessfully registered and logging in..."); - this.onLogin(); - } else { - this.con.sendMessage("\u00A7cUnable to register..."); + if(BungeeCord.getInstance().config.getAuthInfo().isRegisterEnabled()) { + if (args.length == 1 || args.length == 2) { + this.con.sendMessage("\u00A7cUsage: /" + args[0].toLowerCase() + " "); + } else if (!args[1].equals(args[2])) { + this.con.sendMessage("\u00A7cThose passwords do not match!"); + } else if (authSystem.isRegistered(this.username)) { + this.con.sendMessage("\u00A7cThis username is already registered!"); + } else if (authSystem.register(this.username, args[1], + this.con.getAddress().getAddress().getHostAddress())) { + this.con.sendMessage("\u00A7cSuccessfully registered and logging in..."); + this.onLogin(); + } else { + this.con.sendMessage("\u00A7cUnable to register..."); + } + }else { + this.con.disconnect("Registration is not enabled!"); } break; case "changepassword": @@ -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 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(); + } + } + } + } + }