diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 57c7f92a..76bb4ee4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -62,12 +62,7 @@ import ru.gravit.launchserver.config.HWIDHandlerAdapter; import ru.gravit.launchserver.config.PermissionsHandlerAdapter; import ru.gravit.launchserver.config.TextureProviderAdapter; -import ru.gravit.launchserver.manangers.BuildHookManager; -import ru.gravit.launchserver.manangers.MirrorManager; -import ru.gravit.launchserver.manangers.ModulesManager; -import ru.gravit.launchserver.manangers.ReconfigurableManager; -import ru.gravit.launchserver.manangers.ReloadManager; -import ru.gravit.launchserver.manangers.SessionManager; +import ru.gravit.launchserver.manangers.*; import ru.gravit.launchserver.response.Response; import ru.gravit.launchserver.socket.ServerSocketHandler; import ru.gravit.launchserver.texture.RequestTextureProvider; @@ -280,6 +275,8 @@ public static void main(String... args) throws Throwable { public final AuthLimiter limiter; public final SessionManager sessionManager; + + public final SocketHookManager socketHookManager; // Server public final ModulesManager modulesManager; @@ -395,6 +392,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { mirrorManager = new MirrorManager(); reloadManager = new ReloadManager(); reconfigurableManager = new ReconfigurableManager(); + socketHookManager = new SocketHookManager(); GarbageManager.registerNeedGC(sessionManager); GarbageManager.registerNeedGC(limiter); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java index f43a4bb2..bca1e034 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -11,7 +11,7 @@ import ru.gravit.utils.helper.SecurityHelper; public final class JsonAuthProvider extends AuthProvider { - private Gson gson = new Gson(); + private static Gson gson = new Gson(); private URL url; public class authResult { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java index cdb737d1..7e14c836 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java @@ -14,7 +14,7 @@ public final class MojangAuthProvider extends AuthProvider { private static final Pattern UUID_REGEX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); private static final URL URL; - private final Gson gson = new Gson(); + private static final Gson gson = new Gson(); static { try { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/SocketHookManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/SocketHookManager.java new file mode 100644 index 00000000..d032fa08 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/SocketHookManager.java @@ -0,0 +1,69 @@ +package ru.gravit.launchserver.manangers; + +import ru.gravit.launcher.request.RequestException; +import ru.gravit.launchserver.socket.SocketContext; + +import java.util.HashSet; +import java.util.Set; + +public class SocketHookManager { + @FunctionalInterface + interface SocketPreHook + { + boolean preHook(SocketContext context); //Вернуть true если необходимо продолжть обработку, false если остановить обработку + } + @FunctionalInterface + interface SocketPostHook + { + void postHook(SocketContext context); + } + @FunctionalInterface + interface SocketErrorHook + { + boolean errorHook(SocketContext context, RequestException e); //Вернуть true если необходимо продолжть обработку, false если остановить обработку + } + private Set POST_HOOKS; + private Set PRE_HOOKS; + private Set ERROR_HOOKS; + public void registerPostHook(SocketPostHook hook) + { + if(POST_HOOKS == null) POST_HOOKS = new HashSet<>(); + POST_HOOKS.add(hook); + } + public void registerPreHook(SocketPreHook hook) + { + if(PRE_HOOKS == null) PRE_HOOKS = new HashSet<>(); + PRE_HOOKS.add(hook); + } + public void registerErrorHook(SocketErrorHook hook) + { + if(ERROR_HOOKS == null) ERROR_HOOKS = new HashSet<>(); + ERROR_HOOKS.add(hook); + } + public boolean preHook(SocketContext context) + { + if(PRE_HOOKS == null) return true; + for(SocketPreHook preHook : PRE_HOOKS) + { + if(!preHook.preHook(context)) return false; + } + return true; + } + public void postHook(SocketContext context) + { + if(POST_HOOKS == null) return; + for(SocketPostHook postHook : POST_HOOKS) + { + postHook.postHook(context); + } + } + public boolean errorHook(SocketContext context, RequestException e) + { + if(ERROR_HOOKS == null) return true; + for(SocketErrorHook errorHook : ERROR_HOOKS) + { + if(!errorHook.errorHook(context,e)) return false; + } + return true; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ResponseThread.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ResponseThread.java index e545ab8c..f86df51a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ResponseThread.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ResponseThread.java @@ -11,6 +11,7 @@ import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.manangers.SessionManager; +import ru.gravit.launchserver.manangers.SocketHookManager; import ru.gravit.launchserver.response.Response; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; @@ -31,11 +32,13 @@ public Handshake(int type, long session) { private final Socket socket; private final SessionManager sessions; + private final SocketHookManager socketHookManager; - public ResponseThread(LaunchServer server, long id, Socket socket, SessionManager sessionManager) throws SocketException { + public ResponseThread(LaunchServer server, long id, Socket socket, SessionManager sessionManager, SocketHookManager socketHookManager) throws SocketException { this.server = server; this.socket = socket; sessions = sessionManager; + this.socketHookManager = socketHookManager; // Fix socket flags IOHelper.setSocketFlags(socket); } @@ -106,14 +109,26 @@ public void run() { cancelled = true; return; } + SocketContext context = new SocketContext(); + context.input = input; + context.output = output; + context.ip = IOHelper.getIP(socket.getRemoteSocketAddress()); + context.session = handshake.session; + context.type = handshake.type; // Start response + socketHookManager.preHook(context); try { - respond(handshake.type, input, output, handshake.session, IOHelper.getIP(socket.getRemoteSocketAddress())); + respond(handshake.type, input, output, handshake.session, context.ip); + socketHookManager.postHook(context); } catch (RequestException e) { - LogHelper.subDebug(String.format("#%d Request error: %s", handshake.session, e.getMessage())); - if(e.getMessage() == null) LogHelper.error(e); - output.writeString(e.getMessage(), 0); + if(server.socketHookManager.errorHook(context,e)) + { + LogHelper.subDebug(String.format("#%d Request error: %s", handshake.session, e.getMessage())); + if(e.getMessage() == null) LogHelper.error(e); + output.writeString(e.getMessage(), 0); + } + } } catch (Exception e) { savedError = e; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java index acfdd3da..5509390f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java @@ -104,7 +104,7 @@ public void run() { continue; // Listener didn't accepted this connection // Reply in separate thread - threadPool.execute(new ResponseThread(server, id, socket, sessionManager)); + threadPool.execute(new ResponseThread(server, id, socket, sessionManager, server.socketHookManager)); } } catch (IOException e) { // Ignore error after close/rebind diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/SocketContext.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/SocketContext.java new file mode 100644 index 00000000..f4012818 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/SocketContext.java @@ -0,0 +1,12 @@ +package ru.gravit.launchserver.socket; + +import ru.gravit.launcher.serialize.HInput; +import ru.gravit.launcher.serialize.HOutput; + +public class SocketContext { + HInput input; + HOutput output; + long session; + String ip; + Integer type; +}