From b55e6f1f40443d1deaef58500b08a40c6b5bc0ed Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 20 Apr 2019 03:51:27 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=20?= =?UTF-8?q?=D1=87=D0=B5=D0=BB=D0=BE=D0=B2=D0=B5=D0=BA=D0=BE=D1=87=D0=B8?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D0=BC=D0=BE=D0=B9=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=B8=D1=81=D0=BA=D0=BB?= =?UTF-8?q?=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B2=20Response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 1 + .../websocket/WebSocketIndexPageHandler.java | 82 ------------------- .../websocket/WebSocketServerIndexPage.java | 61 -------------- .../websocket/WebSocketService.java | 34 ++++---- .../websockets/ClientWebSocketService.java | 2 + .../launcher/events/ExceptionEvent.java | 18 ++++ 6 files changed, 38 insertions(+), 160 deletions(-) delete mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketIndexPageHandler.java delete mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketServerIndexPage.java create mode 100644 libLauncher/src/main/java/ru/gravit/launcher/events/ExceptionEvent.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index f02a6627..7c271c3f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -245,6 +245,7 @@ public class LauncherConf public class NettyConfig { public boolean clientEnabled; + public boolean sendExcptionEnabled; public String launcherURL; public String downloadURL; public String launcherEXEURL; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketIndexPageHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketIndexPageHandler.java deleted file mode 100644 index 011e11c0..00000000 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketIndexPageHandler.java +++ /dev/null @@ -1,82 +0,0 @@ -package ru.gravit.launchserver.websocket; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.*; -import io.netty.handler.codec.http.*; -import io.netty.handler.ssl.SslHandler; -import io.netty.util.CharsetUtil; - -import static io.netty.handler.codec.http.HttpMethod.GET; -import static io.netty.handler.codec.http.HttpResponseStatus.*; -import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; -import static io.netty.handler.codec.rtsp.RtspResponseStatuses.NOT_FOUND; - -public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler { - - private final String websocketPath; - - public WebSocketIndexPageHandler(String websocketPath) { - this.websocketPath = websocketPath; - } - - @Override - protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) { - // Handle a bad request. - if (!req.decoderResult().isSuccess()) { - sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); - return; - } - - // Allow only GET methods. - if (req.method() != GET) { - sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); - return; - } - - // Send the index page - if ("/".equals(req.uri()) || "/index.html".equals(req.uri())) { - String webSocketLocation = getWebSocketLocation(ctx.pipeline(), req, websocketPath); - ByteBuf content = WebSocketServerIndexPage.getContent(webSocketLocation); - FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); - - res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); - HttpUtil.setContentLength(res, content.readableBytes()); - - sendHttpResponse(ctx, req, res); - } else { - sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND)); - } - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - cause.printStackTrace(); - ctx.close(); - } - - private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { - // Generate an error page if response getStatus code is not OK (200). - if (res.status().code() != 200) { - ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); - res.content().writeBytes(buf); - buf.release(); - HttpUtil.setContentLength(res, res.content().readableBytes()); - } - - // Send the response and close the connection if necessary. - ChannelFuture f = ctx.channel().writeAndFlush(res); - if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { - f.addListener(ChannelFutureListener.CLOSE); - } - } - - private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) { - String protocol = "ws"; - if (cp.get(SslHandler.class) != null) { - // SSL in use so use Secure WebSockets - protocol = "wss"; - } - return protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path; - } -} \ No newline at end of file diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketServerIndexPage.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketServerIndexPage.java deleted file mode 100644 index 8d2ba9e2..00000000 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketServerIndexPage.java +++ /dev/null @@ -1,61 +0,0 @@ -package ru.gravit.launchserver.websocket; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.util.CharsetUtil; - -public final class WebSocketServerIndexPage { - - private static final String NEWLINE = "\r\n"; - - public static ByteBuf getContent(String webSocketLocation) { - return Unpooled.copiedBuffer( - "Web Socket Test" + NEWLINE + - "" + NEWLINE + - "" + NEWLINE + - "
" + NEWLINE + - "" + - "" + NEWLINE + - "

Output

" + NEWLINE + - "" + NEWLINE + - "
" + NEWLINE + - "" + NEWLINE + - "" + NEWLINE, CharsetUtil.US_ASCII); - } - - private WebSocketServerIndexPage() { - // Unused - } -} \ No newline at end of file diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java index d4e384a6..1ec8ca55 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java @@ -7,6 +7,9 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.group.ChannelGroup; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import ru.gravit.launcher.events.ExceptionEvent; +import ru.gravit.launcher.events.RequestEvent; +import ru.gravit.launcher.events.request.ErrorRequestEvent; import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; import ru.gravit.launcher.request.JsonResultSerializeAdapter; @@ -66,7 +69,20 @@ void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) response.execute(ctx, client); } catch (Exception e) { LogHelper.error(e); - sendObject(ctx, new ExceptionResult(e)); + RequestEvent event; + if(server.config.netty.sendExcptionEnabled) + { + event = new ExceptionEvent(e); + } + else + { + event = new ErrorRequestEvent("Fatal server error. Contact administrator"); + } + if(response instanceof SimpleResponse) + { + event.requestUUID = ((SimpleResponse) response).requestUUID; + } + sendObject(ctx, event); } } @@ -134,20 +150,4 @@ public String getType() { } } - public static class ExceptionResult implements ResultInterface { - public ExceptionResult(Exception e) { - this.message = e.getMessage(); - this.clazz = e.getClass().getName(); - this.type = "exceptionError"; - } - - public final String message; - public final String clazz; - public final String type; - - @Override - public String getType() { - return "exception"; - } - } } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java index 143a7ae0..395aaf4a 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -3,6 +3,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.java_websocket.handshake.ServerHandshake; +import ru.gravit.launcher.events.ExceptionEvent; import ru.gravit.launcher.events.request.*; import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; @@ -127,6 +128,7 @@ public void registerResults() { registerResult("log", LogEvent.class); registerResult("execCmd", ExecCommandRequestEvent.class); registerResult("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class); + registerResult("exception", ExceptionEvent.class); } public void registerHandler(EventHandler eventHandler) { diff --git a/libLauncher/src/main/java/ru/gravit/launcher/events/ExceptionEvent.java b/libLauncher/src/main/java/ru/gravit/launcher/events/ExceptionEvent.java new file mode 100644 index 00000000..961335aa --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/launcher/events/ExceptionEvent.java @@ -0,0 +1,18 @@ +package ru.gravit.launcher.events; + +import ru.gravit.launcher.events.RequestEvent; + +public class ExceptionEvent extends RequestEvent { + public ExceptionEvent(Exception e) { + this.message = e.getMessage(); + this.clazz = e.getClass().getName(); + } + + public final String message; + public final String clazz; + + @Override + public String getType() { + return "exception"; + } +}