SocketHookManager

This commit is contained in:
Gravit 2018-12-29 17:00:50 +07:00
parent 92a253eb82
commit 4d7234512c
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
7 changed files with 108 additions and 14 deletions

View file

@ -62,12 +62,7 @@
import ru.gravit.launchserver.config.HWIDHandlerAdapter; import ru.gravit.launchserver.config.HWIDHandlerAdapter;
import ru.gravit.launchserver.config.PermissionsHandlerAdapter; import ru.gravit.launchserver.config.PermissionsHandlerAdapter;
import ru.gravit.launchserver.config.TextureProviderAdapter; import ru.gravit.launchserver.config.TextureProviderAdapter;
import ru.gravit.launchserver.manangers.BuildHookManager; import ru.gravit.launchserver.manangers.*;
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.response.Response; import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.ServerSocketHandler; import ru.gravit.launchserver.socket.ServerSocketHandler;
import ru.gravit.launchserver.texture.RequestTextureProvider; import ru.gravit.launchserver.texture.RequestTextureProvider;
@ -280,6 +275,8 @@ public static void main(String... args) throws Throwable {
public final AuthLimiter limiter; public final AuthLimiter limiter;
public final SessionManager sessionManager; public final SessionManager sessionManager;
public final SocketHookManager socketHookManager;
// Server // Server
public final ModulesManager modulesManager; public final ModulesManager modulesManager;
@ -395,6 +392,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
mirrorManager = new MirrorManager(); mirrorManager = new MirrorManager();
reloadManager = new ReloadManager(); reloadManager = new ReloadManager();
reconfigurableManager = new ReconfigurableManager(); reconfigurableManager = new ReconfigurableManager();
socketHookManager = new SocketHookManager();
GarbageManager.registerNeedGC(sessionManager); GarbageManager.registerNeedGC(sessionManager);
GarbageManager.registerNeedGC(limiter); GarbageManager.registerNeedGC(limiter);

View file

@ -11,7 +11,7 @@
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
public final class JsonAuthProvider extends AuthProvider { public final class JsonAuthProvider extends AuthProvider {
private Gson gson = new Gson(); private static Gson gson = new Gson();
private URL url; private URL url;
public class authResult { public class authResult {

View file

@ -14,7 +14,7 @@
public final class MojangAuthProvider extends AuthProvider { 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 Pattern UUID_REGEX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
private static final URL URL; private static final URL URL;
private final Gson gson = new Gson(); private static final Gson gson = new Gson();
static { static {
try { try {

View file

@ -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<SocketPostHook> POST_HOOKS;
private Set<SocketPreHook> PRE_HOOKS;
private Set<SocketErrorHook> 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;
}
}

View file

@ -11,6 +11,7 @@
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.manangers.SessionManager; import ru.gravit.launchserver.manangers.SessionManager;
import ru.gravit.launchserver.manangers.SocketHookManager;
import ru.gravit.launchserver.response.Response; import ru.gravit.launchserver.response.Response;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -31,11 +32,13 @@ public Handshake(int type, long session) {
private final Socket socket; private final Socket socket;
private final SessionManager sessions; 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.server = server;
this.socket = socket; this.socket = socket;
sessions = sessionManager; sessions = sessionManager;
this.socketHookManager = socketHookManager;
// Fix socket flags // Fix socket flags
IOHelper.setSocketFlags(socket); IOHelper.setSocketFlags(socket);
} }
@ -106,14 +109,26 @@ public void run() {
cancelled = true; cancelled = true;
return; 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 // Start response
socketHookManager.preHook(context);
try { 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) { } catch (RequestException e) {
LogHelper.subDebug(String.format("#%d Request error: %s", handshake.session, e.getMessage())); if(server.socketHookManager.errorHook(context,e))
if(e.getMessage() == null) LogHelper.error(e); {
output.writeString(e.getMessage(), 0); 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) { } catch (Exception e) {
savedError = e; savedError = e;

View file

@ -104,7 +104,7 @@ public void run() {
continue; // Listener didn't accepted this connection continue; // Listener didn't accepted this connection
// Reply in separate thread // 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) { } catch (IOException e) {
// Ignore error after close/rebind // Ignore error after close/rebind

View file

@ -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;
}