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

167 lines
7.4 KiB
Java
Raw Normal View History

package pro.gravit.launchserver.socket;
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;
2018-10-01 11:07:47 +03:00
import com.google.gson.Gson;
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;
2019-08-13 21:02:33 +03:00
import io.netty.channel.group.ChannelMatchers;
2018-10-01 11:07:47 +03:00
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.events.ExceptionEvent;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.events.request.ErrorRequestEvent;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.launchserver.socket.response.admin.AddLogListenerResponse;
import pro.gravit.launchserver.socket.response.admin.ExecCommandResponse;
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import pro.gravit.launchserver.socket.response.auth.CheckServerResponse;
import pro.gravit.launchserver.socket.response.auth.GetAvailabilityAuthResponse;
import pro.gravit.launchserver.socket.response.auth.JoinServerResponse;
import pro.gravit.launchserver.socket.response.auth.ProfilesResponse;
import pro.gravit.launchserver.socket.response.auth.RegisterResponse;
import pro.gravit.launchserver.socket.response.auth.RestoreSessionResponse;
import pro.gravit.launchserver.socket.response.auth.SetProfileResponse;
import pro.gravit.launchserver.socket.response.profile.BatchProfileByUsername;
import pro.gravit.launchserver.socket.response.profile.ProfileByUUIDResponse;
import pro.gravit.launchserver.socket.response.profile.ProfileByUsername;
import pro.gravit.launchserver.socket.response.secure.GetSecureTokenResponse;
import pro.gravit.launchserver.socket.response.secure.VerifySecureTokenResponse;
import pro.gravit.launchserver.socket.response.update.LauncherResponse;
import pro.gravit.launchserver.socket.response.update.UpdateListResponse;
import pro.gravit.launchserver.socket.response.update.UpdateResponse;
import pro.gravit.utils.ProviderMap;
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;
public static ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>();
2018-11-08 15:30:16 +03:00
public WebSocketService(ChannelGroup channels, LaunchServer server) {
2018-10-01 13:01:10 +03:00
this.channels = channels;
2018-10-01 11:07:47 +03:00
this.server = server;
//this.gsonBuiler.registerTypeAdapter(WebSocketServerResponse.class, new JsonResponseAdapter(this));
//this.gsonBuiler.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
//this.gsonBuiler.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
this.gson = Launcher.gsonManager.gson;
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;
public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) {
2018-10-01 11:07:47 +03:00
String request = frame.text();
WebSocketServerResponse response = gson.fromJson(request, WebSocketServerResponse.class);
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, WebSocketServerResponse response, Client client, String ip) {
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());
}
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 registerClient(Channel channel) {
2018-10-01 13:01:10 +03:00
channels.add(channel);
}
2018-11-08 15:30:16 +03:00
public static void registerResponses() {
providers.register("auth", AuthResponse.class);
providers.register("checkServer", CheckServerResponse.class);
providers.register("joinServer", JoinServerResponse.class);
providers.register("profiles", ProfilesResponse.class);
providers.register("launcher", LauncherResponse.class);
providers.register("updateList", UpdateListResponse.class);
providers.register("cmdExec", ExecCommandResponse.class);
providers.register("setProfile", SetProfileResponse.class);
providers.register("addLogListener", AddLogListenerResponse.class);
providers.register("update", UpdateResponse.class);
providers.register("restoreSession", RestoreSessionResponse.class);
providers.register("batchProfileByUsername", BatchProfileByUsername.class);
providers.register("profileByUsername", ProfileByUsername.class);
providers.register("profileByUUID", ProfileByUUIDResponse.class);
providers.register("getSecureToken", GetSecureTokenResponse.class);
providers.register("verifySecureToken", VerifySecureTokenResponse.class);
providers.register("getAvailabilityAuth", GetAvailabilityAuthResponse.class);
providers.register("register", RegisterResponse.class);
}
2018-11-08 15:30:16 +03:00
public void sendObject(ChannelHandlerContext ctx, Object obj) {
2019-08-13 21:02:33 +03:00
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), ctx.voidPromise());
}
2019-04-03 16:27:40 +03:00
public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
2019-08-13 21:02:33 +03:00
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ctx.voidPromise());
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) {
2019-08-13 21:02:33 +03:00
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), ch.voidPromise());
}
}
public void sendObjectAll(Object obj, Type type) {
2019-05-15 14:11:22 +03:00
for (Channel ch : channels) {
2019-08-13 21:02:33 +03:00
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ch.voidPromise());
}
}
2018-11-08 15:30:16 +03:00
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
2019-07-01 14:34:03 +03:00
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE);
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) {
2019-07-01 14:34:03 +03:00
ctx.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) {
2019-08-13 21:02:33 +03:00
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)), ChannelMatchers.all(), true);
2018-10-01 13:01:10 +03:00
}
2018-11-08 15:30:16 +03:00
public static class EventResult implements WebSocketEvent {
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
}