From f1cd39babd337f119c749e0ac558869f580f960c Mon Sep 17 00:00:00 2001 From: Gravit Date: Fri, 3 May 2019 23:07:37 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE][EXPERIMENTAL]=20LaunchServer=20=D0=B2?= =?UTF-8?q?=20=D0=BA=D0=B0=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=B5=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 10 ++++ .../ru/gravit/launchserver/socket/Client.java | 1 + .../websocket/LauncherNettyServer.java | 16 +++++++ .../websocket/WebSocketService.java | 46 +++++++++++++++++++ .../websocket/json/JsonResponseInterface.java | 3 +- .../json/admin/ProxyCommandResponse.java | 25 ++++++++++ .../websocket/json/auth/AuthResponse.java | 6 +++ .../ru/gravit/launcher/ClientPermissions.java | 2 + .../launcher/request/admin/ProxyRequest.java | 20 ++++++++ .../launcher/request/auth/AuthRequest.java | 18 +++++++- 10 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ProxyCommandResponse.java create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ProxyRequest.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 11719c05..0062d39e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -283,12 +283,22 @@ public class NettyConfig { public NettyPerformanceConfig performance; public NettyBindAddress[] binds; public LogLevel logLevel = LogLevel.DEBUG; + public NettyProxyConfig proxy = new NettyProxyConfig(); } public class NettyPerformanceConfig { public int bossThread; public int workerThread; } + public class NettyProxyConfig + { + public boolean enabled; + public String address = "ws://localhost:9275/api"; + public String login = "login"; + public String password = "password"; + public String auth_id = "std"; + public ArrayList requests = new ArrayList<>(); + } public class NettyBindAddress { public String address; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/Client.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/Client.java index 7a609f37..bf529a65 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/Client.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/Client.java @@ -8,6 +8,7 @@ public class Client { public long session; + public boolean proxy; public String auth_id; public long timestamp; public Type type; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java index 811db49a..0b9d53f9 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/LauncherNettyServer.java @@ -13,8 +13,12 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler; import io.netty.handler.logging.LoggingHandler; +import ru.gravit.launcher.request.Request; +import ru.gravit.launcher.request.auth.AuthRequest; +import ru.gravit.launcher.request.websockets.StandartClientWebSocketService; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.websocket.fileserver.FileServerHandler; +import ru.gravit.utils.helper.LogHelper; import java.net.InetSocketAddress; @@ -45,6 +49,18 @@ public void initChannel(NioSocketChannel ch) { pipeline.addLast(new WebSocketFrameHandler()); } }); + if(config.proxy.enabled) + { + LogHelper.info("Connect to main server %s"); + Request.service = StandartClientWebSocketService.initWebSockets(config.proxy.address, false); + AuthRequest authRequest = new AuthRequest(config.proxy.login, config.proxy.password, config.proxy.auth_id, AuthRequest.ConnectTypes.PROXY); + authRequest.initProxy = true; + try { + authRequest.request(); + } catch (Exception e) { + LogHelper.error(e); + } + } } public ChannelFuture bind(InetSocketAddress address) { 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 0ece2748..08d9505c 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketService.java @@ -13,7 +13,9 @@ import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; import ru.gravit.launcher.request.JsonResultSerializeAdapter; +import ru.gravit.launcher.request.RequestException; import ru.gravit.launcher.request.ResultInterface; +import ru.gravit.launcher.request.admin.ProxyRequest; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.websocket.json.JsonResponseAdapter; @@ -21,6 +23,7 @@ import ru.gravit.launchserver.websocket.json.SimpleResponse; import ru.gravit.launchserver.websocket.json.admin.AddLogListenerResponse; import ru.gravit.launchserver.websocket.json.admin.ExecCommandResponse; +import ru.gravit.launchserver.websocket.json.admin.ProxyCommandResponse; import ru.gravit.launchserver.websocket.json.auth.*; import ru.gravit.launchserver.websocket.json.profile.BatchProfileByUsername; import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse; @@ -57,6 +60,48 @@ public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) { String request = frame.text(); JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class); + if(server.config.netty.proxy.enabled) + { + if(server.config.netty.proxy.requests.contains(response.getType())) + { + if(response instanceof SimpleResponse) + { + SimpleResponse simpleResponse = (SimpleResponse) response; + simpleResponse.server = server; + simpleResponse.service = this; + simpleResponse.ctx = ctx; + } + LogHelper.debug("Proxy %s request", response.getType()); + ProxyRequest proxyRequest = new ProxyRequest(response, 0); + try { + ResultInterface result = proxyRequest.request(); + sendObject(ctx, result); + } catch (RequestException e) + { + sendObject(ctx, new ErrorRequestEvent(e.getMessage())); + } catch (Exception e) { + LogHelper.error(e); + RequestEvent event; + if(server.config.netty.sendExceptionEnabled) + { + 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); + } + } + } + process(ctx,response, client); + } + void process(ChannelHandlerContext ctx, JsonResponseInterface response, Client client) + { if(response instanceof SimpleResponse) { SimpleResponse simpleResponse = (SimpleResponse) response; @@ -115,6 +160,7 @@ public void registerResponses() { registerResponse("getSecureToken", GetSecureTokenResponse.class); registerResponse("verifySecureToken", VerifySecureTokenResponse.class); registerResponse("getAvailabilityAuth", GetAvailabilityAuthResponse.class); + registerResponse("proxy", ProxyCommandResponse.class); } public void sendObject(ChannelHandlerContext ctx, Object obj) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/JsonResponseInterface.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/JsonResponseInterface.java index 58f74d51..cd54596c 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/JsonResponseInterface.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/JsonResponseInterface.java @@ -1,9 +1,10 @@ package ru.gravit.launchserver.websocket.json; import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launchserver.socket.Client; -public interface JsonResponseInterface { +public interface JsonResponseInterface extends RequestInterface { String getType(); void execute(ChannelHandlerContext ctx, Client client) throws Exception; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ProxyCommandResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ProxyCommandResponse.java new file mode 100644 index 00000000..9b372b86 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ProxyCommandResponse.java @@ -0,0 +1,25 @@ +package ru.gravit.launchserver.websocket.json.admin; + +import io.netty.channel.ChannelHandlerContext; +import ru.gravit.launchserver.socket.Client; +import ru.gravit.launchserver.websocket.json.JsonResponseInterface; +import ru.gravit.launchserver.websocket.json.SimpleResponse; + +public class ProxyCommandResponse extends SimpleResponse { + public JsonResponseInterface response; + public long session; + @Override + public void execute(ChannelHandlerContext ctx, Client client) throws Exception { + if(!client.proxy) { + sendError("Proxy server error"); + return; + } + Client real_client = server.sessionManager.getOrNewClient(session); + response.execute(ctx, real_client); + } + + @Override + public String getType() { + return "proxy"; + } +} 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 2a1c67d4..d066cf71 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 @@ -45,6 +45,7 @@ public AuthResponse(String login, String password, String auth_id, OshiHWID hwid } public String auth_id; + public boolean initProxy; public ConnectTypes authType; public OshiHWID hwid; @@ -120,6 +121,11 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti LaunchServer.server.sessionManager.addClient(clientData); result.session = clientData.session; } + if(initProxy) + { + if(!clientData.permissions.canProxy) throw new AuthException("initProxy not allow"); + clientData.proxy = true; + } if(LaunchServer.server.config.protectHandler.allowGetAccessToken(context)) { UUID uuid = pair.handler.auth(aresult); diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/ClientPermissions.java b/LauncherAPI/src/main/java/ru/gravit/launcher/ClientPermissions.java index 740c9ff6..8e0e4bb2 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/ClientPermissions.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/ClientPermissions.java @@ -20,6 +20,8 @@ public class ClientPermissions { public boolean canUSR3; @LauncherAPI public boolean canBot; + @LauncherAPI + public boolean canProxy; public ClientPermissions(HInput input) throws IOException { this(input.readLong()); diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ProxyRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ProxyRequest.java new file mode 100644 index 00000000..84d27219 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ProxyRequest.java @@ -0,0 +1,20 @@ +package ru.gravit.launcher.request.admin; + +import ru.gravit.launcher.request.Request; +import ru.gravit.launcher.request.ResultInterface; +import ru.gravit.launcher.request.websockets.RequestInterface; + +public class ProxyRequest extends Request implements RequestInterface { + public RequestInterface response; + public long session; + + public ProxyRequest(RequestInterface response, long session) { + this.response = response; + this.session = session; + } + + @Override + public String getType() { + return "proxy"; + } +} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java index 2200d67c..e694a7b8 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java @@ -23,6 +23,10 @@ public final class AuthRequest extends Request implements Requ private final boolean getSession; @LauncherNetworkAPI private final ConnectTypes authType; + @LauncherNetworkAPI + public boolean initProxy; + @LauncherNetworkAPI + public String password; public enum ConnectTypes { @LauncherNetworkAPI @@ -30,7 +34,9 @@ public enum ConnectTypes { @LauncherNetworkAPI CLIENT, @LauncherNetworkAPI - BOT + BOT, + @LauncherNetworkAPI + PROXY } @LauncherAPI @@ -75,6 +81,16 @@ public AuthRequest(String login, byte[] encryptedPassword, String auth_id, Conne this.customText = ""; this.getSession = false; } + public AuthRequest(String login, String password, String auth_id, ConnectTypes authType) { + this.login = login; + this.password = password; + this.encryptedPassword = null; + this.auth_id = auth_id; + this.authType = authType; + this.hwid = null; + this.customText = ""; + this.getSession = false; + } @Override public String getType() {