Search in sources :

Example 11 with ProxyServer

use of de.dytanic.cloudnetcore.network.components.ProxyServer in project CloudNet by Dytanic.

the class PacketInCustomSubChannelMessage method handleInput.

@Override
public void handleInput(Document data, PacketSender packetSender) {
    DefaultType defaultType = data.getObject("defaultType", DefaultType.class);
    String channel = data.getString("channel");
    String message = data.getString("message");
    Document document = data.getDocument("value");
    if (defaultType.equals(DefaultType.BUKKIT)) {
        if (data.contains("serverId")) {
            MinecraftServer minecraftServer = CloudNet.getInstance().getServer(data.getString("serverId"));
            if (minecraftServer != null) {
                minecraftServer.sendPacket(new PacketOutCustomSubChannelMessage(channel, message, document));
                return;
            }
            CloudServer cloudServer = CloudNet.getInstance().getCloudGameServer(data.getString("serverId"));
            if (cloudServer != null) {
                cloudServer.sendPacket(new PacketOutCustomSubChannelMessage(channel, message, document));
            }
        } else {
            CloudNet.getInstance().getNetworkManager().sendAll(new PacketOutCustomSubChannelMessage(channel, message, document), new ChannelFilter() {

                @Override
                public boolean accept(INetworkComponent networkComponent) {
                    return networkComponent instanceof MinecraftServer || networkComponent instanceof CloudServer;
                }
            });
        }
    } else {
        if (data.contains("serverId")) {
            ProxyServer proxyServer = CloudNet.getInstance().getProxy(data.getString("serverId"));
            if (proxyServer != null) {
                proxyServer.sendPacket(new PacketOutCustomSubChannelMessage(channel, message, document));
            }
        } else {
            CloudNet.getInstance().getNetworkManager().sendToProxy(new PacketOutCustomSubChannelMessage(channel, message, document));
        }
    }
}
Also used : ChannelFilter(de.dytanic.cloudnetcore.network.components.util.ChannelFilter) PacketOutCustomSubChannelMessage(de.dytanic.cloudnetcore.network.packet.out.PacketOutCustomSubChannelMessage) INetworkComponent(de.dytanic.cloudnetcore.network.components.INetworkComponent) DefaultType(de.dytanic.cloudnet.lib.DefaultType) CloudServer(de.dytanic.cloudnetcore.network.components.CloudServer) Document(de.dytanic.cloudnet.lib.utility.document.Document) MinecraftServer(de.dytanic.cloudnetcore.network.components.MinecraftServer) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Example 12 with ProxyServer

use of de.dytanic.cloudnetcore.network.components.ProxyServer in project CloudNet by Dytanic.

the class WebsiteUtils method get.

@Override
public FullHttpResponse get(ChannelHandlerContext channelHandlerContext, QueryDecoder queryDecoder, PathProvider path, HttpRequest httpRequest) throws Exception {
    FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.UNAUTHORIZED);
    fullHttpResponse.headers().set("Content-Type", "application/json");
    Document dataDocument = new Document("success", false).append("reason", new ArrayList<>()).append("response", new Document());
    if (!httpRequest.headers().contains("-Xcloudnet-user") || (!httpRequest.headers().contains("-Xcloudnet-token") && !httpRequest.headers().contains("-Xcloudnet-password")) || !httpRequest.headers().contains("-Xmessage")) {
        dataDocument.append("reason", Arrays.asList("-Xcloudnet-user, -Xcloudnet-token or -Xmessage not found!"));
        fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
        return fullHttpResponse;
    }
    if (httpRequest.headers().contains("-Xcloudnet-token") ? !CloudNet.getInstance().authorization(httpRequest.headers().get("-Xcloudnet-user"), httpRequest.headers().get("-Xcloudnet-token")) : !CloudNet.getInstance().authorizationPassword(httpRequest.headers().get("-Xcloudnet-user"), httpRequest.headers().get("-Xcloudnet-password"))) {
        dataDocument.append("reason", Arrays.asList("failed authorization!"));
        fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
        return fullHttpResponse;
    }
    User user = CloudNet.getInstance().getUser(httpRequest.headers().get("-Xcloudnet-user"));
    switch(httpRequest.headers().get("-Xmessage").toLowerCase()) {
        case "serverinfos":
            {
                if (!user.getPermissions().contains("cloudnet.web.serverinfos") && !user.getPermissions().contains("*")) {
                    dataDocument.append("reason", Arrays.asList("permission denied!"));
                    fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                    fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                    return fullHttpResponse;
                }
                dataDocument.append("success", true);
                Document response = new Document();
                for (MinecraftServer minecraftServer : CloudNet.getInstance().getServers().values()) {
                    response.append(minecraftServer.getServiceId().getServerId(), minecraftServer.getServerInfo());
                }
                dataDocument.append("response", response);
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                return fullHttpResponse;
            }
        case "proxyinfos":
            {
                if (!user.getPermissions().contains("cloudnet.web.proxyinfos") && !user.getPermissions().contains("*")) {
                    dataDocument.append("reason", Arrays.asList("permission denied!"));
                    fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                    fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                    return fullHttpResponse;
                }
                dataDocument.append("success", true);
                Document response = new Document();
                for (ProxyServer minecraftServer : CloudNet.getInstance().getProxys().values()) {
                    response.append(minecraftServer.getServiceId().getServerId(), minecraftServer.getProxyInfo());
                }
                dataDocument.append("response", response);
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                return fullHttpResponse;
            }
        case "onlinePlayers":
            if (!user.getPermissions().contains("cloudnet.web.onlineplayers") && !user.getPermissions().contains("*")) {
                dataDocument.append("reason", Arrays.asList("permission denied!"));
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                return fullHttpResponse;
            }
            dataDocument.append("success", true);
            Document response = new Document();
            for (CloudPlayer cloudPlayer : CloudNet.getInstance().getNetworkManager().getOnlinePlayers().values()) {
                response.append(cloudPlayer.getUniqueId().toString(), cloudPlayer);
            }
            dataDocument.append("response", response);
            fullHttpResponse.setStatus(HttpResponseStatus.OK);
            fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
            return fullHttpResponse;
        case "statistic":
            if (!user.getPermissions().contains("cloudnet.web.statistic") && !user.getPermissions().contains("*")) {
                dataDocument.append("reason", Arrays.asList("permission denied!"));
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                return fullHttpResponse;
            }
            dataDocument.append("success", true).append("response", StatisticManager.getInstance().getStatistics());
            fullHttpResponse.setStatus(HttpResponseStatus.OK);
            fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
            return fullHttpResponse;
        case "cloudnetwork":
            if (!user.getPermissions().contains("cloudnet.web.cloudnetwork") && !user.getPermissions().contains("*")) {
                dataDocument.append("reason", Arrays.asList("permission denied!"));
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                return fullHttpResponse;
            }
            dataDocument.append("success", true).append("response", CloudNet.getInstance().getNetworkManager().newCloudNetwork());
            fullHttpResponse.setStatus(HttpResponseStatus.OK);
            fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
            return fullHttpResponse;
        case "startServer":
            if (!user.getPermissions().contains("cloudnet.web.startserver") && !user.getPermissions().contains("*")) {
                dataDocument.append("reason", Arrays.asList("permission denied!"));
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                return fullHttpResponse;
            }
            dataDocument.append("success", true);
            fullHttpResponse.setStatus(HttpResponseStatus.OK);
            fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
            if (httpRequest.headers().contains("-Xvalue")) {
                String group = httpRequest.headers().get("-Xvalue");
                CloudNet.getInstance().getScheduler().runTaskSync(new Runnable() {

                    @Override
                    public void run() {
                        CloudNet.getInstance().startGameServer(CloudNet.getInstance().getServerGroup(group));
                    }
                });
            }
            return fullHttpResponse;
        case "startProxy":
            if (!user.getPermissions().contains("cloudnet.web.startproxy") && !user.getPermissions().contains("*")) {
                dataDocument.append("reason", Arrays.asList("permission denied!"));
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                fullHttpResponse.setStatus(HttpResponseStatus.FORBIDDEN);
                return fullHttpResponse;
            }
            dataDocument.append("success", true);
            fullHttpResponse.setStatus(HttpResponseStatus.OK);
            fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
            if (httpRequest.headers().contains("-Xvalue")) {
                String group = httpRequest.headers().get("-Xvalue");
                CloudNet.getInstance().getScheduler().runTaskSync(new Runnable() {

                    @Override
                    public void run() {
                        CloudNet.getInstance().startProxy(CloudNet.getInstance().getProxyGroup(group));
                    }
                });
            }
            return fullHttpResponse;
        default:
            {
                dataDocument.append("success", true).append("reason", Arrays.asList("No available -Xmessage command found!"));
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
                return fullHttpResponse;
            }
    }
}
Also used : User(de.dytanic.cloudnet.lib.user.User) ArrayList(java.util.ArrayList) CloudPlayer(de.dytanic.cloudnet.lib.player.CloudPlayer) Document(de.dytanic.cloudnet.lib.utility.document.Document) MinecraftServer(de.dytanic.cloudnetcore.network.components.MinecraftServer) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Example 13 with ProxyServer

use of de.dytanic.cloudnetcore.network.components.ProxyServer in project CloudNet by Dytanic.

the class PacketInPlayerLoginRequest method handleInput.

@Override
public void handleInput(Document data, PacketSender packetSender) {
    if (!(packetSender instanceof ProxyServer) && packetUniqueId != null)
        return;
    PlayerConnection playerConnection = data.getObject("playerConnection", new TypeToken<PlayerConnection>() {
    }.getType());
    CloudNet.getInstance().getNetworkManager().handlePlayerLoginRequest(((ProxyServer) packetSender), playerConnection, packetUniqueId);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) PlayerConnection(de.dytanic.cloudnet.lib.player.PlayerConnection) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Example 14 with ProxyServer

use of de.dytanic.cloudnetcore.network.components.ProxyServer in project CloudNet by Dytanic.

the class PacketInRemoveProxy method handleInput.

@Override
public void handleInput(Document data, PacketSender packetSender) {
    if (!(packetSender instanceof Wrapper))
        return;
    Wrapper cn = (Wrapper) packetSender;
    ProxyInfo proxyInfo = data.getObject("proxyInfo", new TypeToken<ProxyInfo>() {
    }.getType());
    if (cn.getProxys().containsKey(proxyInfo.getServiceId().getServerId())) {
        ProxyServer minecraftServer = cn.getProxys().get(proxyInfo.getServiceId().getServerId());
        if (minecraftServer.getChannel() != null)
            minecraftServer.getChannel().close();
        cn.getProxys().remove(proxyInfo.getServiceId().getServerId());
        CloudNet.getInstance().getNetworkManager().handleProxyRemove(minecraftServer);
        CloudNet.getInstance().getScreenProvider().handleDisableScreen(proxyInfo.getServiceId());
    }
}
Also used : Wrapper(de.dytanic.cloudnetcore.network.components.Wrapper) ProxyInfo(de.dytanic.cloudnet.lib.server.info.ProxyInfo) TypeToken(com.google.gson.reflect.TypeToken) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Example 15 with ProxyServer

use of de.dytanic.cloudnetcore.network.components.ProxyServer in project CloudNet by Dytanic.

the class CommandCmd method onExecuteCommand.

@Override
public void onExecuteCommand(CommandSender sender, String[] args) {
    if (args.length > 1) {
        for (Wrapper wrapper : CloudNet.getInstance().getWrappers().values()) {
            for (MinecraftServer minecraftServer : wrapper.getServers().values()) {
                if (minecraftServer.getServiceId().getServerId().equalsIgnoreCase(args[0])) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (short i = 1; i < args.length; i++) {
                        stringBuilder.append(args[i]).append(NetworkUtils.SPACE_STRING);
                    }
                    minecraftServer.getWrapper().writeServerCommand(stringBuilder.substring(0, stringBuilder.length() - 1), minecraftServer.getServerInfo());
                    sender.sendMessage("Sending command to " + minecraftServer.getServiceId().getServerId() + " with [\"" + stringBuilder.substring(0, stringBuilder.length() - 1) + "\"]");
                    return;
                }
            }
            for (ProxyServer minecraftServer : wrapper.getProxys().values()) {
                if (minecraftServer.getServiceId().getServerId().equalsIgnoreCase(args[0])) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (short i = 1; i < args.length; i++) {
                        stringBuilder.append(args[i]).append(NetworkUtils.SPACE_STRING);
                    }
                    minecraftServer.getWrapper().writeProxyCommand(stringBuilder.substring(0, stringBuilder.length() - 1), minecraftServer.getProxyInfo());
                    sender.sendMessage("Sending command to " + minecraftServer.getServiceId().getServerId() + " with [\"" + stringBuilder.substring(0, stringBuilder.length() - 1) + "\"]");
                    return;
                }
            }
        }
    } else {
        sender.sendMessage("cmd <name> <command> | Executes a command, either from a proxy or game server");
    }
}
Also used : Wrapper(de.dytanic.cloudnetcore.network.components.Wrapper) MinecraftServer(de.dytanic.cloudnetcore.network.components.MinecraftServer) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Aggregations

ProxyServer (de.dytanic.cloudnetcore.network.components.ProxyServer)16 MinecraftServer (de.dytanic.cloudnetcore.network.components.MinecraftServer)12 Wrapper (de.dytanic.cloudnetcore.network.components.Wrapper)9 TypeToken (com.google.gson.reflect.TypeToken)4 CloudServer (de.dytanic.cloudnetcore.network.components.CloudServer)4 DefaultType (de.dytanic.cloudnet.lib.DefaultType)2 CloudPlayer (de.dytanic.cloudnet.lib.player.CloudPlayer)2 ProxyInfo (de.dytanic.cloudnet.lib.server.info.ProxyInfo)2 ServiceId (de.dytanic.cloudnet.lib.service.ServiceId)2 Document (de.dytanic.cloudnet.lib.utility.document.Document)2 AuthLoginResult (de.dytanic.cloudnet.lib.network.auth.AuthLoginResult)1 PacketOutAuthResult (de.dytanic.cloudnet.lib.network.auth.packetio.PacketOutAuthResult)1 PlayerConnection (de.dytanic.cloudnet.lib.player.PlayerConnection)1 ProxyProcessMeta (de.dytanic.cloudnet.lib.server.ProxyProcessMeta)1 User (de.dytanic.cloudnet.lib.user.User)1 CollectionWrapper (de.dytanic.cloudnet.lib.utility.CollectionWrapper)1 WrapperChannelDisconnectEvent (de.dytanic.cloudnetcore.api.event.network.WrapperChannelDisconnectEvent)1 CloudNetClient (de.dytanic.cloudnetcore.network.CloudNetClient)1 CloudNetClientAuth (de.dytanic.cloudnetcore.network.CloudNetClientAuth)1 INetworkComponent (de.dytanic.cloudnetcore.network.components.INetworkComponent)1