Search in sources :

Example 36 with Document

use of de.dytanic.cloudnet.lib.utility.document.Document in project CloudNet by Dytanic.

the class WebsiteDownloadService method get.

@Override
public FullHttpResponse get(ChannelHandlerContext channelHandlerContext, QueryDecoder queryDecoder, PathProvider path, HttpRequest httpRequest) throws Exception {
    CloudNet.getLogger().debug("HTTP Request from " + channelHandlerContext.channel().remoteAddress());
    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") || !httpRequest.headers().contains("-Xvalue")) {
        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.toBytesAsUTF_8());
        return fullHttpResponse;
    }
    fullHttpResponse.headers().set("Content-Type", "application/octet-stream");
    switch(httpRequest.headers().get("-Xmessage")) {
        case "plugin":
            {
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                Path path1 = Paths.get("local/plugins/" + httpRequest.headers().get("-Xvalue") + ".jar");
                if (Files.exists(path1)) {
                    byte[] value = Files.readAllBytes(path1);
                    fullHttpResponse.headers().set("content-disposition", "attachment; filename = " + httpRequest.headers().get("-Xvalue") + ".jar");
                    fullHttpResponse.content().writeBytes(value);
                } else {
                    fullHttpResponse.headers().set("Content-Type", "application/json");
                    dataDocument.append("reason", Arrays.asList("cannot find file \"" + httpRequest.headers().get("-Xvalue") + "\""));
                    fullHttpResponse.content().writeBytes(dataDocument.toBytesAsUTF_8());
                }
            }
            break;
        case "template":
            {
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                Document document = Document.load(httpRequest.headers().get("-Xvalue"));
                if (document.contains("template") && document.contains("group") && Files.exists(Paths.get("local/templates/" + document.getString("group") + NetworkUtils.SLASH_STRING + document.getString("template")))) {
                    Path file = Paths.get("local/cache/" + (NetworkUtils.RANDOM.nextInt(Integer.MAX_VALUE - 1)) + ".zip");
                    if (!Files.exists(file))
                        Files.createFile(file);
                    String x = "local/templates/" + document.getString("group") + NetworkUtils.SLASH_STRING + document.getString("template");
                    File directory = new File(x);
                    directory.mkdirs();
                    if (directory.list() != null && directory.list().length == 0) {
                        new File(x + "/plugins").mkdirs();
                        try (FileWriter fileWriter = new FileWriter(new File(x + "/eula.txt"))) {
                            fileWriter.write("eula=true");
                            fileWriter.flush();
                        }
                    }
                    ZipConverter.convert(file, Paths.get(x));
                    byte[] value = Files.readAllBytes(file);
                    fullHttpResponse.headers().set("content-disposition", "attachment; filename = " + document.getString("template") + ".zip");
                    fullHttpResponse.content().writeBytes(value);
                    Files.deleteIfExists(file);
                } else {
                    fullHttpResponse.headers().set("Content-Type", "application/json");
                    dataDocument.append("reason", Arrays.asList("cannot find file \"" + httpRequest.headers().get("-Xvalue") + "\""));
                    fullHttpResponse.content().writeBytes(dataDocument.toBytesAsUTF_8());
                }
            }
            break;
        case "custom":
            {
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                String server = httpRequest.headers().get("-Xvalue");
                String x = "local/servers/" + server;
                if (!Files.exists(Paths.get(x)))
                    Files.createDirectories(Paths.get(x + "/plugins"));
                Path file = Paths.get("local/cache/" + NetworkUtils.RANDOM.nextLong() + ".zip");
                if (!Files.exists(file))
                    Files.createFile(file);
                ZipConverter.convert(file, Paths.get(x));
                byte[] value = Files.readAllBytes(file);
                fullHttpResponse.headers().set("content-disposition", "attachment; filename = " + server + ".zip");
                fullHttpResponse.content().writeBytes(value);
                Files.deleteIfExists(file);
            }
            break;
        default:
            break;
    }
    return fullHttpResponse;
}
Also used : Path(java.nio.file.Path) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Document(de.dytanic.cloudnet.lib.utility.document.Document) File(java.io.File)

Example 37 with Document

use of de.dytanic.cloudnet.lib.utility.document.Document 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 38 with Document

use of de.dytanic.cloudnet.lib.utility.document.Document in project CloudNet by Dytanic.

the class ChannelMessagingExample method sendCustomMessage.

public void sendCustomMessage() {
    CloudAPI.getInstance().getNetworkConnection().sendPacket(// send a custom channel message to all
    new PacketOutCustomChannelMessage("some-sub-channel-for-proxy", "handle", new Document("foo", "bar")));
    // send a custom channel message to all proxys
    CloudAPI.getInstance().sendCustomSubProxyMessage("some-sub-channel-for-proxy", "handle", new Document("foo", "bar"));
}
Also used : PacketOutCustomChannelMessage(de.dytanic.cloudnet.api.network.packet.out.PacketOutCustomChannelMessage) Document(de.dytanic.cloudnet.lib.utility.document.Document)

Example 39 with Document

use of de.dytanic.cloudnet.lib.utility.document.Document in project CloudNet by Dytanic.

the class CloudCoreExample method onLoad.

@Override
public void onLoad() {
    if (!getUtilFile().exists()) {
        saveUtils(new Document("myutil configuration", "hello world!"));
    }
    if (!getConfigFile().exists()) {
        getConfig().set("myconfiguration", "foo bar");
        saveConfig();
    }
}
Also used : Document(de.dytanic.cloudnet.lib.utility.document.Document)

Example 40 with Document

use of de.dytanic.cloudnet.lib.utility.document.Document in project CloudNet by Dytanic.

the class ProxiedListener method handleServerConnect.

@EventHandler(priority = EventPriority.HIGHEST)
public void handleServerConnect(ServerConnectEvent event) {
    if (event.getPlayer().getServer() == null) {
        String fallback = CloudProxy.getInstance().fallback(event.getPlayer());
        ProxiedPlayerFallbackEvent proxiedPlayerFallbackEvent = new ProxiedPlayerFallbackEvent(event.getPlayer(), CloudAPI.getInstance().getOnlinePlayer(event.getPlayer().getUniqueId()), ProxiedPlayerFallbackEvent.FallbackType.SERVER_KICK, fallback);
        ProxyServer.getInstance().getPluginManager().callEvent(proxiedPlayerFallbackEvent);
        fallback = proxiedPlayerFallbackEvent.getFallback();
        if (fallback != null) {
            event.setTarget(ProxyServer.getInstance().getServerInfo(fallback));
            CloudAPI.getInstance().getNetworkConnection().getChannel().writeAndFlush(new PacketOutCustomSubChannelMessage(DefaultType.BUKKIT, event.getTarget().getName(), "cloudnet_internal", "server_connect_request", new Document("uniqueId", event.getPlayer().getUniqueId())));
            NetworkUtils.sleepUninterruptedly(6);
        } else
            event.setCancelled(true);
    } else {
        CloudAPI.getInstance().getNetworkConnection().getChannel().writeAndFlush(new PacketOutCustomSubChannelMessage(DefaultType.BUKKIT, event.getTarget().getName(), "cloudnet_internal", "server_connect_request", new Document("uniqueId", event.getPlayer().getUniqueId())));
        NetworkUtils.sleepUninterruptedly(6);
    }
}
Also used : ProxiedPlayerFallbackEvent(de.dytanic.cloudnet.bridge.event.proxied.ProxiedPlayerFallbackEvent) Document(de.dytanic.cloudnet.lib.utility.document.Document) EventHandler(net.md_5.bungee.event.EventHandler)

Aggregations

Document (de.dytanic.cloudnet.lib.utility.document.Document)69 DatabaseDocument (de.dytanic.cloudnet.lib.database.DatabaseDocument)26 TypeToken (com.google.gson.reflect.TypeToken)7 ArrayList (java.util.ArrayList)7 File (java.io.File)6 UUID (java.util.UUID)6 OfflinePlayer (de.dytanic.cloudnet.lib.player.OfflinePlayer)5 ServerGroup (de.dytanic.cloudnet.lib.server.ServerGroup)5 ServerConfig (de.dytanic.cloudnet.lib.server.ServerConfig)4 ServerInfo (de.dytanic.cloudnet.lib.server.info.ServerInfo)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ConnectableAddress (de.dytanic.cloudnet.lib.ConnectableAddress)3 CloudPlayer (de.dytanic.cloudnet.lib.player.CloudPlayer)3 Template (de.dytanic.cloudnet.lib.server.template.Template)3 ServerInstallablePlugin (de.dytanic.cloudnet.lib.service.plugin.ServerInstallablePlugin)3 SimpledUser (de.dytanic.cloudnet.lib.user.SimpledUser)3 Acceptable (de.dytanic.cloudnet.lib.utility.Acceptable)3 MinecraftServer (de.dytanic.cloudnetcore.network.components.MinecraftServer)3 IOException (java.io.IOException)3