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

154 lines
6.5 KiB
Java
Raw Normal View History

package ru.gravit.launchserver.websocket;
2018-10-01 11:07:47 +03:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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 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;
import ru.gravit.launcher.request.ResultInterface;
2018-10-01 11:07:47 +03:00
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.websocket.json.EchoResponse;
import ru.gravit.launchserver.websocket.json.JsonResponseAdapter;
import ru.gravit.launchserver.websocket.json.JsonResponseInterface;
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.auth.*;
import ru.gravit.launchserver.websocket.json.profile.BatchProfileByUsername;
import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse;
import ru.gravit.launchserver.websocket.json.profile.ProfileByUsername;
2019-04-12 00:58:45 +03:00
import ru.gravit.launchserver.websocket.json.secure.GetSecureTokenResponse;
import ru.gravit.launchserver.websocket.json.secure.VerifySecureTokenResponse;
import ru.gravit.launchserver.websocket.json.update.LauncherResponse;
import ru.gravit.launchserver.websocket.json.update.UpdateListResponse;
import ru.gravit.launchserver.websocket.json.update.UpdateResponse;
2018-10-01 11:07:47 +03:00
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type;
2019-01-15 06:35:39 +03:00
import java.util.HashMap;
@SuppressWarnings({"unused", "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
2018-11-08 15:30:16 +03:00
void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client) {
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);
if(response instanceof SimpleResponse)
{
SimpleResponse simpleResponse = (SimpleResponse) response;
simpleResponse.server = server;
simpleResponse.service = this;
simpleResponse.ctx = ctx;
}
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;
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);
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("echo", EchoResponse.class);
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);
}
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 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
}