diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/components/AuthLimiterComponent.java b/LaunchServer/src/main/java/ru/gravit/launchserver/components/AuthLimiterComponent.java index d6c44235..be01905d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/components/AuthLimiterComponent.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/components/AuthLimiterComponent.java @@ -6,6 +6,7 @@ import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.auth.AuthResponse; +import ru.gravit.utils.HookException; import java.util.ArrayList; import java.util.HashMap; @@ -18,7 +19,7 @@ public void preInit(LaunchServer launchServer) { @Override public void init(LaunchServer launchServer) { - launchServer.authHookManager.registerPreHook(this::preAuthHook); + launchServer.authHookManager.preHook.registerHook(this::preAuthHook); } @Override @@ -26,10 +27,11 @@ public void postInit(LaunchServer launchServer) { } - public void preAuthHook(AuthResponse.AuthContext context, Client client) throws AuthException { + public boolean preAuthHook(AuthResponse.AuthContext context, Client client) { if (isLimit(context.ip)) { - AuthProvider.authError(message); + throw new HookException(message); } + return false; } static class AuthEntry { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/AuthHookManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/AuthHookManager.java index a0c0ba1c..d50fc91f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/AuthHookManager.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/AuthHookManager.java @@ -1,75 +1,16 @@ package ru.gravit.launchserver.manangers.hook; -import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.auth.AuthResponse; - -import java.util.HashSet; -import java.util.Set; +import ru.gravit.launchserver.websocket.json.auth.CheckServerResponse; +import ru.gravit.launchserver.websocket.json.auth.JoinServerResponse; +import ru.gravit.launchserver.websocket.json.auth.SetProfileResponse; +import ru.gravit.utils.BiHookSet; public class AuthHookManager { - private Set PRE_HOOKS = new HashSet<>(); - private Set POST_HOOKS = new HashSet<>(); - private Set CHECKSERVER_HOOKS = new HashSet<>(); - private Set JOINSERVER_HOOKS = new HashSet<>(); - - @FunctionalInterface - public interface AuthPreHook { - void preAuthHook(AuthResponse.AuthContext context, Client client) throws AuthException; - } - - @FunctionalInterface - public interface AuthPostHook { - void postAuthHook(AuthResponse.AuthContext context, Client client) throws AuthException; - } - - @FunctionalInterface - public interface CheckServerHook { - void checkServerHook(String username, String serverID) throws AuthException; - } - - @FunctionalInterface - public interface JoinServerHook { - void joinServerHook(String username, String accessToken, String serverID) throws AuthException; - } - - public void registerPostHook(AuthPostHook hook) { - POST_HOOKS.add(hook); - } - - public void registerJoinServerHook(JoinServerHook hook) { - JOINSERVER_HOOKS.add(hook); - } - - public void registerCheckServerHook(CheckServerHook hook) { - CHECKSERVER_HOOKS.add(hook); - } - - public void registerPreHook(AuthPreHook hook) { - PRE_HOOKS.add(hook); - } - - public void preHook(AuthResponse.AuthContext context, Client client) throws AuthException { - for (AuthPreHook preHook : PRE_HOOKS) { - preHook.preAuthHook(context, client); - } - } - - public void checkServerHook(String username, String serverID) throws AuthException { - for (CheckServerHook hook : CHECKSERVER_HOOKS) { - hook.checkServerHook(username, serverID); - } - } - - public void joinServerHook(String username, String accessToken, String serverID) throws AuthException { - for (JoinServerHook hook : JOINSERVER_HOOKS) { - hook.joinServerHook(username, accessToken, serverID); - } - } - - public void postHook(AuthResponse.AuthContext context, Client client) throws AuthException { - for (AuthPostHook postHook : POST_HOOKS) { - postHook.postAuthHook(context, client); - } - } + public BiHookSet preHook = new BiHookSet<>(); + public BiHookSet postHook = new BiHookSet<>(); + public BiHookSet checkServerHook = new BiHookSet<>(); + public BiHookSet joinServerHook = new BiHookSet<>(); + public BiHookSet setProfileHook = new BiHookSet<>(); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java index 8059d02e..2a1c67d4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/AuthResponse.java @@ -4,6 +4,7 @@ import ru.gravit.launcher.OshiHWID; import ru.gravit.launcher.events.request.AuthRequestEvent; import ru.gravit.launcher.profiles.ClientProfile; +import ru.gravit.launcher.request.RequestException; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthProviderPair; @@ -13,6 +14,7 @@ import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.SimpleResponse; import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse; +import ru.gravit.utils.HookException; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.SecurityHelper; @@ -84,7 +86,7 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti else pair = LaunchServer.server.config.getAuthProviderPair(auth_id); AuthContext context = new AuthContext(0, login, password.length(), customText, client, ip, null, false); AuthProvider provider = pair.provider; - LaunchServer.server.authHookManager.preHook(context, clientData); + LaunchServer.server.authHookManager.preHook.hook(context, clientData); provider.preAuth(login, password, customText, ip); AuthProviderResult aresult = provider.auth(login, password, ip); if (!VerifyHelper.isValidUsername(aresult.username)) { @@ -105,7 +107,7 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti //} if (authType == ConnectTypes.CLIENT) LaunchServer.server.config.hwidHandler.check(hwid, aresult.username); - LaunchServer.server.authHookManager.postHook(context, clientData); + LaunchServer.server.authHookManager.postHook.hook(context, clientData); clientData.isAuth = true; clientData.permissions = aresult.permissions; clientData.auth_id = auth_id; @@ -125,7 +127,7 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti LogHelper.debug("Auth: %s accessToken %s uuid: %s", login, result.accessToken, uuid.toString()); } sendResult(result); - } catch (AuthException | HWIDException e) { + } catch (AuthException | HWIDException | HookException e) { sendError(e.getMessage()); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/CheckServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/CheckServerResponse.java index f21d8a38..0b745ec4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/CheckServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/CheckServerResponse.java @@ -7,6 +7,7 @@ import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.SimpleResponse; import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse; +import ru.gravit.utils.HookException; import ru.gravit.utils.helper.LogHelper; public class CheckServerResponse extends SimpleResponse { @@ -23,11 +24,12 @@ public String getType() { public void execute(ChannelHandlerContext ctx, Client pClient) { CheckServerRequestEvent result = new CheckServerRequestEvent(); try { + server.authHookManager.checkServerHook.hook(this, pClient); result.uuid = pClient.auth.handler.checkServer(username, serverID); if (result.uuid != null) result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, result.uuid, username, client, pClient.auth.textureProvider); LogHelper.debug("checkServer: %s uuid: %s serverID: %s", result.playerProfile.username, result.uuid.toString(), serverID); - } catch (AuthException e) { + } catch (AuthException | HookException e) { sendError(e.getMessage()); return; } catch (Exception e) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/JoinServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/JoinServerResponse.java index e0abb1be..989b0f12 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/JoinServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/JoinServerResponse.java @@ -6,6 +6,7 @@ import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.SimpleResponse; +import ru.gravit.utils.HookException; import ru.gravit.utils.helper.LogHelper; public class JoinServerResponse extends SimpleResponse { @@ -17,11 +18,11 @@ public class JoinServerResponse extends SimpleResponse { public String getType() { return "joinServer"; } - @Override public void execute(ChannelHandlerContext ctx, Client client) { boolean success; try { + server.authHookManager.joinServerHook.hook(this, client); if(client.auth == null) { LogHelper.warning("Client auth is null. Using default."); @@ -29,7 +30,7 @@ public void execute(ChannelHandlerContext ctx, Client client) { } else success = client.auth.handler.joinServer(username, accessToken, serverID); LogHelper.debug("joinServer: %s accessToken: %s serverID: %s", username, accessToken, serverID); - } catch (AuthException e) { + } catch (AuthException | HookException e) { sendError(e.getMessage()); return; } catch (Exception e) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/SetProfileResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/SetProfileResponse.java index b57986d4..c1de900f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/SetProfileResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/auth/SetProfileResponse.java @@ -6,6 +6,7 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.SimpleResponse; +import ru.gravit.utils.HookException; import java.util.Collection; @@ -23,6 +24,12 @@ public void execute(ChannelHandlerContext ctx, Client client) { sendError("Access denied"); return; } + try { + server.authHookManager.setProfileHook.hook(this, client); + } catch (HookException e) + { + sendError(e.getMessage()); + } Collection profiles = LaunchServer.server.getProfiles(); for (ClientProfile p : profiles) { if (p.getTitle().equals(this.client)) { diff --git a/libLauncher/src/main/java/ru/gravit/utils/BiHookSet.java b/libLauncher/src/main/java/ru/gravit/utils/BiHookSet.java index 9e20c4a2..012483ba 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/BiHookSet.java +++ b/libLauncher/src/main/java/ru/gravit/utils/BiHookSet.java @@ -8,7 +8,7 @@ public class BiHookSet { @FunctionalInterface public interface Hook { - boolean hook(V object, R context); + boolean hook(V object, R context) throws HookException; } public void registerHook(Hook hook) { @@ -18,11 +18,11 @@ public boolean unregisterHook(Hook hook) { return list.remove(hook); } - public boolean hook(V object, R context) + public boolean hook(V context, R object) throws HookException { for(Hook hook : list) { - if(hook.hook(object, context)) return true; + if(hook.hook(context, object)) return true; } return false; } diff --git a/libLauncher/src/main/java/ru/gravit/utils/HookException.java b/libLauncher/src/main/java/ru/gravit/utils/HookException.java new file mode 100644 index 00000000..3d61748c --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/HookException.java @@ -0,0 +1,22 @@ +package ru.gravit.utils; + +public class HookException extends RuntimeException { + public HookException() { + } + + public HookException(String message) { + super(message); + } + + public HookException(String message, Throwable cause) { + super(message, cause); + } + + public HookException(Throwable cause) { + super(cause); + } + + public HookException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/HookSet.java b/libLauncher/src/main/java/ru/gravit/utils/HookSet.java index f1e1394c..38053667 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/HookSet.java +++ b/libLauncher/src/main/java/ru/gravit/utils/HookSet.java @@ -8,7 +8,7 @@ public class HookSet { @FunctionalInterface public interface Hook { - boolean hook(R context); + boolean hook(R context) throws HookException; } public void registerHook(Hook hook) { @@ -18,7 +18,7 @@ public boolean unregisterHook(Hook hook) { return list.remove(hook); } - public boolean hook(R context) + public boolean hook(R context) throws HookException { for(Hook hook : list) {