Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/websocket/WebSocketService.java

233 lines
11 KiB
Java
Raw Normal View History

package pro.gravit.launchserver.websocket;
2018-10-01 11:07:47 +03:00
2019-06-03 10:58:10 +03:00
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;
2018-10-01 11:07:47 +03:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
2019-06-03 10:58:10 +03:00
2018-10-01 13:01:10 +03:00
import io.netty.channel.Channel;
2018-10-01 14:15:39 +03:00
import io.netty.channel.ChannelFutureListener;
2018-10-01 11:07:47 +03:00
import io.netty.channel.ChannelHandlerContext;
2018-10-01 13:01:10 +03:00
import io.netty.channel.group.ChannelGroup;
2018-10-01 11:07:47 +03:00
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import pro.gravit.launcher.events.ExceptionEvent;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.events.request.AuthRequestEvent;
import pro.gravit.launcher.events.request.ErrorRequestEvent;
import pro.gravit.launcher.hasher.HashedEntry;
import pro.gravit.launcher.hasher.HashedEntryAdapter;
import pro.gravit.launcher.request.JsonResultSerializeAdapter;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.RequestException;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.admin.ProxyRequest;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.socket.Client;
2019-06-03 10:58:10 +03:00
import pro.gravit.launchserver.websocket.json.JsonResponseAdapter;
import pro.gravit.launchserver.websocket.json.JsonResponseInterface;
import pro.gravit.launchserver.websocket.json.SimpleResponse;
import pro.gravit.launchserver.websocket.json.admin.AddLogListenerResponse;
import pro.gravit.launchserver.websocket.json.admin.ExecCommandResponse;
import pro.gravit.launchserver.websocket.json.admin.ProxyCommandResponse;
2019-06-03 10:58:10 +03:00
import pro.gravit.launchserver.websocket.json.auth.AuthResponse;
import pro.gravit.launchserver.websocket.json.auth.CheckServerResponse;
import pro.gravit.launchserver.websocket.json.auth.GetAvailabilityAuthResponse;
import pro.gravit.launchserver.websocket.json.auth.JoinServerResponse;
import pro.gravit.launchserver.websocket.json.auth.ProfilesResponse;
import pro.gravit.launchserver.websocket.json.auth.RestoreSessionResponse;
import pro.gravit.launchserver.websocket.json.auth.SetProfileResponse;
import pro.gravit.launchserver.websocket.json.profile.BatchProfileByUsername;
import pro.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse;
import pro.gravit.launchserver.websocket.json.profile.ProfileByUsername;
import pro.gravit.launchserver.websocket.json.secure.GetSecureTokenResponse;
import pro.gravit.launchserver.websocket.json.secure.VerifySecureTokenResponse;
import pro.gravit.launchserver.websocket.json.update.LauncherResponse;
import pro.gravit.launchserver.websocket.json.update.UpdateListResponse;
import pro.gravit.launchserver.websocket.json.update.UpdateResponse;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
2018-10-01 11:07:47 +03:00
2019-04-21 16:01:16 +03:00
@SuppressWarnings("rawtypes")
2018-10-01 11:07:47 +03:00
public class WebSocketService {
2018-10-01 13:01:10 +03:00
public final ChannelGroup channels;
2018-11-08 15:30:16 +03:00
2018-10-01 13:01:10 +03:00
public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder gson) {
this.channels = channels;
2018-10-01 11:07:47 +03:00
this.server = server;
this.gsonBuiler = gson;
this.gsonBuiler.registerTypeAdapter(JsonResponseInterface.class, new JsonResponseAdapter(this));
2019-04-03 16:27:40 +03:00
this.gsonBuiler.registerTypeAdapter(ResultInterface.class, new JsonResultSerializeAdapter());
this.gsonBuiler.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
this.gson = gsonBuiler.create();
2018-10-01 11:07:47 +03:00
}
private final LaunchServer server;
2018-11-08 15:30:16 +03:00
private static final HashMap<String, Class> responses = new HashMap<>();
2018-10-01 11:07:47 +03:00
private final Gson gson;
private final GsonBuilder gsonBuiler;
2018-10-01 11:07:47 +03:00
2019-05-15 14:11:22 +03:00
void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) {
2018-10-01 11:07:47 +03:00
String request = frame.text();
2018-10-01 11:36:19 +03:00
JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class);
2019-05-15 14:11:22 +03:00
if (server.config.netty.proxy.enabled) {
if (server.config.netty.proxy.requests.contains(response.getType())) {
UUID origRequestUUID = null;
2019-05-15 14:11:22 +03:00
if (response instanceof SimpleResponse) {
SimpleResponse simpleResponse = (SimpleResponse) response;
simpleResponse.server = server;
simpleResponse.service = this;
simpleResponse.ctx = ctx;
2019-05-15 14:11:22 +03:00
if (ip != null) simpleResponse.ip = ip;
2019-05-09 16:05:53 +03:00
else simpleResponse.ip = IOHelper.getIP(ctx.channel().remoteAddress());
origRequestUUID = simpleResponse.requestUUID;
}
LogHelper.debug("Proxy %s request", response.getType());
2019-05-15 14:11:22 +03:00
if (client.session == 0) client.session = new Random().nextLong();
ProxyRequest proxyRequest = new ProxyRequest(response, client.session);
2019-05-15 14:11:22 +03:00
if (response instanceof SimpleResponse) {
((SimpleResponse) response).requestUUID = proxyRequest.requestUUID;
}
proxyRequest.isCheckSign = client.checkSign;
try {
ResultInterface result = proxyRequest.request();
2019-05-15 14:11:22 +03:00
if (result instanceof AuthRequestEvent) {
LogHelper.debug("Client auth params get successful");
AuthRequestEvent authRequestEvent = (AuthRequestEvent) result;
client.isAuth = true;
client.session = authRequestEvent.session;
2019-05-15 14:11:22 +03:00
if (authRequestEvent.playerProfile != null)
client.username = authRequestEvent.playerProfile.username;
}
2019-05-15 14:11:22 +03:00
if (result instanceof Request && response instanceof SimpleResponse) {
((Request) result).requestUUID = origRequestUUID;
}
sendObject(ctx, result);
2019-05-15 14:11:22 +03:00
} catch (RequestException e) {
sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
} catch (Exception e) {
LogHelper.error(e);
RequestEvent event;
2019-05-15 14:11:22 +03:00
if (server.config.netty.sendExceptionEnabled) {
event = new ExceptionEvent(e);
2019-05-15 14:11:22 +03:00
} else {
event = new ErrorRequestEvent("Fatal server error. Contact administrator");
}
2019-05-15 14:11:22 +03:00
if (response instanceof SimpleResponse) {
event.requestUUID = ((SimpleResponse) response).requestUUID;
}
sendObject(ctx, event);
}
2019-05-09 16:05:53 +03:00
return;
}
}
2019-05-15 14:11:22 +03:00
process(ctx, response, client, ip);
}
2019-05-15 14:11:22 +03:00
void process(ChannelHandlerContext ctx, JsonResponseInterface response, Client client, String ip) {
if (response instanceof SimpleResponse) {
SimpleResponse simpleResponse = (SimpleResponse) response;
simpleResponse.server = server;
simpleResponse.service = this;
simpleResponse.ctx = ctx;
2019-05-15 14:11:22 +03:00
if (ip != null) simpleResponse.ip = ip;
2019-05-09 16:05:53 +03:00
else simpleResponse.ip = IOHelper.getIP(ctx.channel().remoteAddress());
}
2018-10-01 11:07:47 +03:00
try {
response.execute(ctx, client);
2018-11-08 15:30:16 +03:00
} catch (Exception e) {
2018-10-01 11:07:47 +03:00
LogHelper.error(e);
RequestEvent event;
2019-05-15 14:11:22 +03:00
if (server.config.netty.sendExceptionEnabled) {
event = new ExceptionEvent(e);
2019-05-15 14:11:22 +03:00
} else {
event = new ErrorRequestEvent("Fatal server error. Contact administrator");
}
2019-05-15 14:11:22 +03:00
if (response instanceof SimpleResponse) {
event.requestUUID = ((SimpleResponse) response).requestUUID;
}
sendObject(ctx, event);
2018-10-01 11:07:47 +03:00
}
}
2018-11-08 15:30:16 +03:00
public Class getResponseClass(String type) {
return responses.get(type);
}
2018-11-08 15:30:16 +03:00
public void registerResponse(String key, Class responseInterfaceClass) {
responses.put(key, responseInterfaceClass);
}
2018-11-08 15:30:16 +03:00
public void registerClient(Channel channel) {
2018-10-01 13:01:10 +03:00
channels.add(channel);
}
2018-11-08 15:30:16 +03:00
public void registerResponses() {
registerResponse("auth", AuthResponse.class);
2018-10-01 13:39:35 +03:00
registerResponse("checkServer", CheckServerResponse.class);
registerResponse("joinServer", JoinServerResponse.class);
registerResponse("profiles", ProfilesResponse.class);
registerResponse("launcher", LauncherResponse.class);
registerResponse("updateList", UpdateListResponse.class);
registerResponse("cmdExec", ExecCommandResponse.class);
registerResponse("setProfile", SetProfileResponse.class);
2019-02-17 10:24:20 +03:00
registerResponse("addLogListener", AddLogListenerResponse.class);
2019-02-20 12:55:21 +03:00
registerResponse("update", UpdateResponse.class);
registerResponse("restoreSession", RestoreSessionResponse.class);
registerResponse("batchProfileByUsername", BatchProfileByUsername.class);
registerResponse("profileByUsername", ProfileByUsername.class);
registerResponse("profileByUUID", ProfileByUUIDResponse.class);
2019-04-12 00:58:45 +03:00
registerResponse("getSecureToken", GetSecureTokenResponse.class);
registerResponse("verifySecureToken", VerifySecureTokenResponse.class);
2019-04-13 20:55:01 +03:00
registerResponse("getAvailabilityAuth", GetAvailabilityAuthResponse.class);
registerResponse("proxy", ProxyCommandResponse.class);
}
2018-11-08 15:30:16 +03:00
public void sendObject(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class)));
}
2019-04-03 16:27:40 +03:00
public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
2018-10-01 11:07:47 +03:00
}
2018-11-08 15:30:16 +03:00
public void sendObjectAll(Object obj) {
2019-05-15 14:11:22 +03:00
for (Channel ch : channels) {
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class)));
}
}
public void sendObjectAll(Object obj, Type type) {
2019-05-15 14:11:22 +03:00
for (Channel ch : channels) {
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
}
}
2018-11-08 15:30:16 +03:00
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class))).addListener(ChannelFutureListener.CLOSE);
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type))).addListener(ChannelFutureListener.CLOSE);
2018-10-01 14:15:39 +03:00
}
2018-11-08 15:30:16 +03:00
public void sendEvent(EventResult obj) {
2018-10-01 13:01:10 +03:00
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
}
2018-11-08 15:30:16 +03:00
public static class EventResult implements ResultInterface {
2018-10-01 13:01:10 +03:00
public EventResult() {
2018-10-01 13:01:10 +03:00
}
2018-11-08 15:30:16 +03:00
@Override
public String getType() {
return "event";
}
2018-10-01 13:01:10 +03:00
}
2018-11-08 15:30:16 +03:00
2018-10-01 11:07:47 +03:00
}