[FEATURE] Реализована обработка ошибок в WebSockets

This commit is contained in:
Gravit 2019-02-10 17:54:19 +07:00
parent 982a6f0342
commit 956cef2cd6
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
13 changed files with 59 additions and 27 deletions

View file

@ -99,19 +99,6 @@ public void sendEvent(EventResult obj) {
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj))); channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
} }
public static class ErrorResult implements ResultInterface {
public ErrorResult(String error) {
this.error = error;
}
public final String error;
@Override
public String getType() {
return "error";
}
}
public static class SuccessResult { public static class SuccessResult {
public SuccessResult(String requesttype) { public SuccessResult(String requesttype) {
this.requesttype = requesttype; this.requesttype = requesttype;

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.admin; package ru.gravit.launchserver.socket.websocket.json.admin;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
@ -17,11 +18,11 @@ public String getType() {
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) {
if (!client.isAuth) { if (!client.isAuth) {
service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return; return;
} }
if (!client.permissions.canAdmin) { if (!client.permissions.canAdmin) {
service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return; return;
} }
LaunchServer.server.commandHandler.eval(cmd, false); LaunchServer.server.commandHandler.eval(cmd, false);

View file

@ -3,6 +3,7 @@
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.OshiHWID; import ru.gravit.launcher.OshiHWID;
import ru.gravit.launcher.events.request.AuthRequestEvent; import ru.gravit.launcher.events.request.AuthRequestEvent;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
@ -113,7 +114,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,aresult.username,client); result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,aresult.username,client);
service.sendObject(ctx, result); service.sendObject(ctx, result);
} catch (AuthException | HWIDException e) { } catch (AuthException | HWIDException e) {
service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
} }
} }

View file

@ -2,6 +2,7 @@
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.CheckServerRequestEvent; import ru.gravit.launcher.events.request.CheckServerRequestEvent;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
import ru.gravit.launchserver.response.profile.ProfileByUUIDResponse; import ru.gravit.launchserver.response.profile.ProfileByUUIDResponse;
@ -28,11 +29,11 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
if(result.uuid != null) if(result.uuid != null)
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,result.uuid,username,client); result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,result.uuid,username,client);
} catch (AuthException e) { } catch (AuthException e) {
service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
return; return;
} catch (Exception e) { } catch (Exception e) {
LogHelper.error(e); LogHelper.error(e);
service.sendObject(ctx, new WebSocketService.ErrorResult("Internal authHandler error")); service.sendObject(ctx, new ErrorRequestEvent("Internal authHandler error"));
return; return;
} }
service.sendObject(ctx, new CheckServerRequestEvent()); service.sendObject(ctx, new CheckServerRequestEvent());

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.auth; package ru.gravit.launchserver.socket.websocket.json.auth;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.events.request.JoinServerRequestEvent; import ru.gravit.launcher.events.request.JoinServerRequestEvent;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
@ -25,11 +26,11 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
try { try {
success = LaunchServer.server.config.authHandler.joinServer(username, accessToken, serverID); success = LaunchServer.server.config.authHandler.joinServer(username, accessToken, serverID);
} catch (AuthException e) { } catch (AuthException e) {
service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
return; return;
} catch (Exception e) { } catch (Exception e) {
LogHelper.error(e); LogHelper.error(e);
service.sendObject(ctx, new WebSocketService.ErrorResult("Internal authHandler error")); service.sendObject(ctx, new ErrorRequestEvent("Internal authHandler error"));
return; return;
} }
service.sendObject(ctx, new JoinServerRequestEvent(success)); service.sendObject(ctx, new JoinServerRequestEvent(success));

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.auth; package ru.gravit.launchserver.socket.websocket.json.auth;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.events.request.ProfilesRequestEvent;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
@ -20,7 +21,7 @@ public String getType() {
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
if(!client.checkSign) if(!client.checkSign)
{ {
service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return; return;
} }
service.sendObject(ctx, new ProfilesRequestEvent((List<ClientProfile>) LaunchServer.server.getProfiles())); service.sendObject(ctx, new ProfilesRequestEvent((List<ClientProfile>) LaunchServer.server.getProfiles()));

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.auth; package ru.gravit.launchserver.socket.websocket.json.auth;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.Client;
@ -20,14 +21,14 @@ public String getType() {
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
if(!client.isAuth) if(!client.isAuth)
{ {
service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return; return;
} }
Collection<ClientProfile> profiles = LaunchServer.server.getProfiles(); Collection<ClientProfile> profiles = LaunchServer.server.getProfiles();
for (ClientProfile p : profiles) { for (ClientProfile p : profiles) {
if (p.getTitle().equals(this.client)) { if (p.getTitle().equals(this.client)) {
if (!p.isWhitelistContains(client.username)) { if (!p.isWhitelistContains(client.username)) {
service.sendObject(ctx, new WebSocketService.ErrorResult(LaunchServer.server.config.whitelistRejectString)); service.sendObject(ctx, new ErrorRequestEvent(LaunchServer.server.config.whitelistRejectString));
return; return;
} }
client.profile = p; client.profile = p;
@ -35,6 +36,6 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
break; break;
} }
} }
service.sendObject(ctx, new WebSocketService.ErrorResult("Profile not found")); service.sendObject(ctx, new ErrorRequestEvent("Profile not found"));
} }
} }

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.update; package ru.gravit.launchserver.socket.websocket.json.update;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.events.request.LauncherRequestEvent; import ru.gravit.launcher.events.request.LauncherRequestEvent;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.Client;
@ -52,7 +53,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
} else { } else {
service.sendObjectAndClose(ctx, new LauncherRequestEvent(true, EXE_URL)); service.sendObjectAndClose(ctx, new LauncherRequestEvent(true, EXE_URL));
} }
} else service.sendObject(ctx, new WebSocketService.ErrorResult("Request launcher type error")); } else service.sendObject(ctx, new ErrorRequestEvent("Request launcher type error"));
} }

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.update; package ru.gravit.launchserver.socket.websocket.json.update;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.events.request.UpdateListRequestEvent; import ru.gravit.launcher.events.request.UpdateListRequestEvent;
import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launcher.hasher.HashedDir;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
@ -19,7 +20,7 @@ public String getType() {
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) {
if (!client.isAuth) { if (!client.isAuth) {
service.sendObject(ctx, new WebSocketService.ErrorResult("Access denied")); service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return; return;
} }
HashedDir hdir = LaunchServer.server.updatesDirMap.get(dir).object; HashedDir hdir = LaunchServer.server.updatesDirMap.get(dir).object;

View file

@ -86,6 +86,7 @@ public void registerResults() {
registerResult("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class); registerResult("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class);
registerResult("profiles", ProfilesRequestEvent.class); registerResult("profiles", ProfilesRequestEvent.class);
registerResult("updateList", UpdateListRequestEvent.class); registerResult("updateList", UpdateListRequestEvent.class);
registerResult("error", ErrorRequestEvent.class);
} }
public void registerHandler(EventHandler eventHandler) public void registerHandler(EventHandler eventHandler)

View file

@ -2,6 +2,8 @@
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.request.RequestException;
import ru.gravit.launcher.request.ResultInterface; import ru.gravit.launcher.request.ResultInterface;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -24,6 +26,11 @@ public static ResultInterface sendRequest(RequestInterface request) throws IOExc
} }
ResultInterface result = e.result; ResultInterface result = e.result;
waitEventHandler.requests.remove(e); waitEventHandler.requests.remove(e);
if(e.result.getType().equals("error"))
{
ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) e.result;
throw new RequestException(errorRequestEvent.error);
}
return result; return result;
} }
public static void initWebSockets(String address, int port) public static void initWebSockets(String address, int port)

View file

@ -1,6 +1,7 @@
package ru.gravit.launcher.request.websockets; package ru.gravit.launcher.request.websockets;
import ru.gravit.launcher.request.ResultInterface; import ru.gravit.launcher.request.ResultInterface;
import ru.gravit.utils.helper.LogHelper;
import java.util.HashSet; import java.util.HashSet;
@ -8,10 +9,13 @@ public class WaitEventHandler implements ClientWebSocketService.EventHandler {
public HashSet<ResultEvent> requests = new HashSet<>(); public HashSet<ResultEvent> requests = new HashSet<>();
@Override @Override
public void process(ResultInterface result) { public void process(ResultInterface result) {
LogHelper.debug("Processing event %s type", result.getType());
for(ResultEvent r : requests) for(ResultEvent r : requests)
{ {
if(r.type.equals(result.getType())) LogHelper.subDebug("Processing %s", r.type);
if(r.type.equals(result.getType()) || result.getType().equals("error"))
{ {
LogHelper.debug("Event %s type", r.type);
synchronized (r) synchronized (r)
{ {
r.result = result; r.result = result;

View file

@ -0,0 +1,25 @@
package ru.gravit.launcher.events.request;
import ru.gravit.launcher.request.ResultInterface;
import ru.gravit.utils.event.EventInterface;
import java.util.UUID;
public class ErrorRequestEvent implements ResultInterface, EventInterface {
public static UUID uuid = UUID.fromString("0af22bc7-aa01-4881-bdbb-dc62b3cdac96");
public ErrorRequestEvent(String error) {
this.error = error;
}
public final String error;
@Override
public String getType() {
return "error";
}
@Override
public UUID getUUID() {
return null;
}
}