[FEATURE] Реализация Exit

This commit is contained in:
Gravit 2020-03-01 13:58:38 +07:00
parent de47594083
commit 437da3de59
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 145 additions and 0 deletions

View file

@ -40,6 +40,11 @@ public Client getOrNewClient(long session) {
return clientSet.computeIfAbsent(session, Client::new); return clientSet.computeIfAbsent(session, Client::new);
} }
public Client removeClient(long session)
{
return clientSet.remove(session);
}
public void updateClient(long session) { public void updateClient(long session) {
Client c = clientSet.get(session); Client c = clientSet.get(session);

View file

@ -123,6 +123,7 @@ public static void registerResponses() {
providers.register("getAvailabilityAuth", GetAvailabilityAuthResponse.class); providers.register("getAvailabilityAuth", GetAvailabilityAuthResponse.class);
providers.register("register", RegisterResponse.class); providers.register("register", RegisterResponse.class);
providers.register("setPassword", SetPasswordResponse.class); providers.register("setPassword", SetPasswordResponse.class);
providers.register("exit", ExitResponse.class);
} }
public void sendObject(ChannelHandlerContext ctx, Object obj) { public void sendObject(ChannelHandlerContext ctx, Object obj) {

View file

@ -0,0 +1,89 @@
package pro.gravit.launchserver.socket.response.auth;
import io.netty.channel.ChannelHandlerContext;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.events.request.ExitRequestEvent;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler;
import pro.gravit.launchserver.socket.response.SimpleResponse;
public class ExitResponse extends SimpleResponse {
public boolean exitAll;
public String username;
@Override
public String getType() {
return "exit";
}
@Override
public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
if(username != null && ( !client.isAuth || client.permissions == null || !client.permissions.canAdmin ))
{
sendError("Permissions denied");
return;
}
if(username == null)
{
if(client.session == 0 && exitAll)
{
sendError("Session invalid");
return;
}
WebSocketFrameHandler handler = ctx.pipeline().get(WebSocketFrameHandler.class);
if(handler == null)
{
sendError("Exit internal error");
return;
}
Client newClient = new Client(0);
newClient.isSecure = client.isSecure;
newClient.checkSign = client.checkSign;
handler.setClient(newClient);
if(exitAll)
{
service.channels.forEach((channel) -> {
if(channel == null || channel.pipeline() == null) return;
WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class);
if(wsHandler == null || wsHandler == handler) return;
Client chClient = wsHandler.getClient();
if(client.isAuth && client.username != null)
{
if(!chClient.isAuth || !client.username.equals(chClient.username)) return;
}
else
{
if(chClient.session != client.session) return;
}
Client newCusClient = new Client(0);
newCusClient.isSecure = chClient.isSecure;
newCusClient.checkSign = chClient.checkSign;
wsHandler.setClient(newCusClient);
if(chClient.session != 0) server.sessionManager.removeClient(chClient.session);
ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER);
event.requestUUID = RequestEvent.eventUUID;
wsHandler.service.sendObject(channel, event);
});
}
sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.CLIENT));
}
else
{
service.channels.forEach((channel -> {
if(channel == null || channel.pipeline() == null) return;
WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class);
if(wsHandler == null) return;
Client chClient = wsHandler.getClient();
if(!chClient.isAuth || !username.equals(chClient.username)) return;
Client newCusClient = new Client(0);
newCusClient.isSecure = chClient.isSecure;
newCusClient.checkSign = chClient.checkSign;
wsHandler.setClient(newCusClient);
if(chClient.session != 0) server.sessionManager.removeClient(chClient.session);
ExitRequestEvent event = new ExitRequestEvent(ExitRequestEvent.ExitReason.SERVER);
event.requestUUID = RequestEvent.eventUUID;
wsHandler.service.sendObject(channel, event);
}));
sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.NO_EXIT));
}
}
}

View file

@ -0,0 +1,20 @@
package pro.gravit.launcher.events.request;
import pro.gravit.launcher.events.RequestEvent;
public class ExitRequestEvent extends RequestEvent {
public enum ExitReason
{
SERVER, CLIENT, TIMEOUT, NO_EXIT
}
public final ExitReason reason;
public ExitRequestEvent(ExitReason reason) {
this.reason = reason;
}
@Override
public String getType() {
return "exit";
}
}

View file

@ -0,0 +1,29 @@
package pro.gravit.launcher.request.auth;
import pro.gravit.launcher.events.request.ExitRequestEvent;
import pro.gravit.launcher.request.Request;
public class ExitRequest extends Request<ExitRequestEvent> {
public final boolean exitAll;
public final String username;
public ExitRequest() {
this.exitAll = false;
this.username = null;
}
public ExitRequest(boolean exitAll) {
this.exitAll = exitAll;
this.username = null;
}
public ExitRequest(boolean exitAll, String username) {
this.exitAll = exitAll;
this.username = username;
}
@Override
public String getType() {
return "exit";
}
}

View file

@ -107,6 +107,7 @@ public void registerResults() {
results.register("setpassword", SetPasswordRequestEvent.class); results.register("setpassword", SetPasswordRequestEvent.class);
results.register("notification", NotificationEvent.class); results.register("notification", NotificationEvent.class);
results.register("signal", SignalEvent.class); results.register("signal", SignalEvent.class);
results.register("exit", ExitRequestEvent.class);
} }
public void waitIfNotConnected() { public void waitIfNotConnected() {