[FEATURE] Advanced Client API

This commit is contained in:
Gravit 2020-11-21 21:30:21 +07:00
parent 5e048905cc
commit 2688270fa0
No known key found for this signature in database
GPG key ID: 98A079490768CCE5
2 changed files with 94 additions and 2 deletions

View file

@ -8,6 +8,8 @@
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import java.util.Base64;
public class ClientsCommand extends Command { public class ClientsCommand extends Command {
public ClientsCommand(LaunchServer server) { public ClientsCommand(LaunchServer server) {
super(server); super(server);
@ -34,8 +36,12 @@ public void invoke(String... args) {
LogHelper.info("Channel %s | connectUUID %s | checkSign %s", ip, frameHandler.getConnectUUID(), client.checkSign ? "true" : "false"); LogHelper.info("Channel %s | connectUUID %s | checkSign %s", ip, frameHandler.getConnectUUID(), client.checkSign ? "true" : "false");
else { else {
LogHelper.info("Client name %s | ip %s | connectUUID %s", client.username == null ? "null" : client.username, ip, frameHandler.getConnectUUID()); LogHelper.info("Client name %s | ip %s | connectUUID %s", client.username == null ? "null" : client.username, ip, frameHandler.getConnectUUID());
LogHelper.subInfo("userUUID: %s | session %s", client.uuid == null ? "null" : client.uuid.toString(), client.session == null ? "null" : client.session);
LogHelper.subInfo("Data: checkSign %s | auth_id %s", client.checkSign ? "true" : "false", LogHelper.subInfo("Data: checkSign %s | auth_id %s", client.checkSign ? "true" : "false",
client.auth_id); client.auth_id);
if(client.trustLevel != null) {
LogHelper.subInfo("trustLevel | key %s | pubkey %s", client.trustLevel.keyChecked ? "checked" : "unchecked", client.trustLevel.publicKey == null ? "null" : Base64.getEncoder().encode(client.trustLevel.publicKey));
}
LogHelper.subInfo("Permissions: %s (permissions %d | flags %d)", client.permissions == null ? "null" : client.permissions.toString(), client.permissions == null ? 0 : client.permissions.permissions, client.permissions == null ? 0 : client.permissions.flags); LogHelper.subInfo("Permissions: %s (permissions %d | flags %d)", client.permissions == null ? "null" : client.permissions.toString(), client.permissions == null ? 0 : client.permissions.permissions, client.permissions == null ? 0 : client.permissions.flags);
} }
})); }));

View file

@ -11,8 +11,10 @@
import pro.gravit.launcher.events.ExceptionEvent; import pro.gravit.launcher.events.ExceptionEvent;
import pro.gravit.launcher.events.RequestEvent; import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.events.request.ErrorRequestEvent; import pro.gravit.launcher.events.request.ErrorRequestEvent;
import pro.gravit.launcher.events.request.ExitRequestEvent;
import pro.gravit.launcher.request.WebSocketEvent; import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler; import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler;
import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse; import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
@ -36,8 +38,10 @@
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class WebSocketService { public class WebSocketService {
public static final ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>(); public static final ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>();
@ -68,12 +72,12 @@ public WebSocketService(ChannelGroup channels, LaunchServer server) {
} }
public void forEachActiveChannels(BiConsumer<Channel, WebSocketFrameHandler> callback) { public void forEachActiveChannels(BiConsumer<Channel, WebSocketFrameHandler> callback) {
channels.forEach((channel) -> { for(Channel channel : channels) {
if (channel == null || channel.pipeline() == null) return; if (channel == null || channel.pipeline() == null) return;
WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class); WebSocketFrameHandler wsHandler = channel.pipeline().get(WebSocketFrameHandler.class);
if (wsHandler == null) return; if (wsHandler == null) return;
callback.accept(channel, wsHandler); callback.accept(channel, wsHandler);
}); };
} }
public static void registerResponses() { public static void registerResponses() {
@ -208,6 +212,88 @@ public void sendObjectAll(Object obj, Type type) {
} }
} }
public void sendObjectToUUID(UUID userUuid, Object obj, Type type) {
for (Channel ch : channels) {
if (ch == null || ch.pipeline() == null) continue;
WebSocketFrameHandler wsHandler = ch.pipeline().get(WebSocketFrameHandler.class);
if (wsHandler == null) continue;
Client client = wsHandler.getClient();
if(client == null || !userUuid.equals(client.uuid)) continue;
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ch.voidPromise());
}
}
public void updateDaoObject(UUID userUuid, User daoObject, Consumer<Channel> callback) {
for(Channel ch : channels) {
if (ch == null || ch.pipeline() == null) continue;
WebSocketFrameHandler wsHandler = ch.pipeline().get(WebSocketFrameHandler.class);
if (wsHandler == null) continue;
Client client = wsHandler.getClient();
if(client == null || client.daoObject == null || !userUuid.equals(client.uuid)) continue;
client.daoObject = daoObject;
if(callback != null) callback.accept(ch);
}
}
public Channel getChannelFromConnectUUID(UUID connectUuid) {
for(Channel ch : channels) {
if (ch == null || ch.pipeline() == null) continue;
WebSocketFrameHandler wsHandler = ch.pipeline().get(WebSocketFrameHandler.class);
if (wsHandler == null) continue;
if(connectUuid.equals(wsHandler.getConnectUUID())) {
return ch;
}
}
return null;
}
public boolean kickByUserUUID(UUID userUuid, boolean isClose) {
boolean result = false;
for(Channel ch : channels) {
if (ch == null || ch.pipeline() == null) continue;
WebSocketFrameHandler wsHandler = ch.pipeline().get(WebSocketFrameHandler.class);
if (wsHandler == null) continue;
Client client = wsHandler.getClient();
if(client == null || client.daoObject == null || !userUuid.equals(client.uuid)) continue;
ExitResponse.exit(server, wsHandler, ch, ExitRequestEvent.ExitReason.SERVER);
if(isClose) ch.close();
result = true;
}
return result;
}
public boolean kickByConnectUUID(UUID connectUuid, boolean isClose) {
for(Channel ch : channels) {
if (ch == null || ch.pipeline() == null) continue;
WebSocketFrameHandler wsHandler = ch.pipeline().get(WebSocketFrameHandler.class);
if (wsHandler == null) continue;
if(connectUuid.equals(wsHandler.getConnectUUID())) {
ExitResponse.exit(server, wsHandler, ch, ExitRequestEvent.ExitReason.SERVER);
if(isClose) ch.close();
return true;
}
}
return false;
}
public boolean kickByIP(String ip, boolean isClose) {
boolean result = false;
for(Channel ch : channels) {
if (ch == null || ch.pipeline() == null) continue;
WebSocketFrameHandler wsHandler = ch.pipeline().get(WebSocketFrameHandler.class);
if(wsHandler == null) continue;
String clientIp;
if(wsHandler.context != null && wsHandler.context.ip != null) clientIp = wsHandler.context.ip;
else clientIp = IOHelper.getIP(ch.remoteAddress());
if(ip.equals(clientIp)) {
ExitResponse.exit(server, wsHandler, ch, ExitRequestEvent.ExitReason.SERVER);
if(isClose) ch.close();
result = true;
}
}
return result;
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) { public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE); ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE);
} }