Search in sources :

Example 11 with Document

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

the class StatisticManager method addStartedServers.

public void addStartedServers() {
    if (!statistic)
        return;
    Document document = database.getDocument(NAME);
    if (!document.contains("startedServers")) {
        document.append("startedServers", 0L);
    }
    document.append("startedServers", document.getLong("startedServers") + 1L);
}
Also used : Document(de.dytanic.cloudnet.lib.utility.document.Document) DatabaseDocument(de.dytanic.cloudnet.lib.database.DatabaseDocument)

Example 12 with Document

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

the class StatisticManager method addStartedProxys.

public void addStartedProxys() {
    if (!statistic)
        return;
    Document document = database.getDocument(NAME);
    if (!document.contains("startedProxys")) {
        document.append("startedProxys", 0);
    }
    document.append("startedProxys", document.getInt("startedProxys") + 1);
}
Also used : Document(de.dytanic.cloudnet.lib.utility.document.Document) DatabaseDocument(de.dytanic.cloudnet.lib.database.DatabaseDocument)

Example 13 with Document

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

the class WebsiteAuthorization 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");
    fullHttpResponse.headers().set("Access-Control-Allow-Origin", "*");
    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"))) {
        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;
    }
    dataDocument.append("success", true);
    fullHttpResponse.content().writeBytes(dataDocument.convertToJsonString().getBytes(StandardCharsets.UTF_8));
    return fullHttpResponse;
}
Also used : ArrayList(java.util.ArrayList) Document(de.dytanic.cloudnet.lib.utility.document.Document)

Example 14 with Document

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

the class WebsiteDeployment method post.

@Override
public FullHttpResponse post(ChannelHandlerContext channelHandlerContext, QueryDecoder queryDecoder, PathProvider path, HttpRequest httpRequest) throws Exception {
    CloudNet.getLogger().debug("HTTP Request from " + channelHandlerContext.channel().remoteAddress());
    if (!(httpRequest instanceof FullHttpRequest))
        return null;
    FullHttpRequest fullHttpRequest = ((FullHttpRequest) httpRequest);
    FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.UNAUTHORIZED);
    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;
    }
    switch(httpRequest.headers().get("-Xmessage").toLowerCase()) {
        case "plugin":
            {
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                String pluginName = httpRequest.headers().get("-Xvalue");
                File file = new File(new StringBuilder("local/plugins/").append(pluginName).append(".jar").substring(0));
                if (file.getParentFile().mkdirs())
                    ;
                file.createNewFile();
                try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                    fileOutputStream.write(fullHttpRequest.content().array());
                }
                System.out.println("Plugin deployed [\"" + pluginName + "\"]");
            }
            break;
        case "template":
            {
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                Document document = Document.load(httpRequest.headers().get("-Xvalue"));
                if (document.contains("template") && document.contains("group")) {
                    File file = new File("local/templates/" + document.getString("group") + NetworkUtils.SLASH_STRING + document.getString("template") + NetworkUtils.SLASH_STRING + document.getString("template") + ".zip");
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                    try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                        fileOutputStream.write(fullHttpRequest.content().readBytes(fullHttpRequest.content().readableBytes()).array());
                        fileOutputStream.flush();
                    }
                    ZipFile zipFile = new ZipFile(file);
                    StringBuilder stringBuilder = new StringBuilder("local/templates/").append(document.getString("group")).append(NetworkUtils.SLASH_STRING).append(document.getString("template"));
                    for (ZipEntry zipEntry : Collections.list(zipFile.entries())) extractEntry(zipFile, zipEntry, stringBuilder.toString());
                    file.delete();
                    System.out.println("Template deployed [\"" + document.getString("template") + "\"] for the group [\"" + document.getString("group") + "\"]");
                }
            }
            break;
        case "custom":
            {
                fullHttpResponse.setStatus(HttpResponseStatus.OK);
                String payload = fullHttpRequest.headers().get("-Xvalue");
                File file = new File("local/servers/" + payload + "/payload.zip");
                file.getParentFile().mkdirs();
                file.createNewFile();
                try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                    fileOutputStream.write(fullHttpRequest.content().readBytes(fullHttpRequest.content().readableBytes()).array());
                    fileOutputStream.flush();
                }
                ZipFile zipFile = new ZipFile(file);
                for (ZipEntry zipEntry : Collections.list(zipFile.entries())) extractEntry(zipFile, zipEntry, "local/servers/" + payload);
                file.delete();
                System.out.println("Custom server deployed \"" + payload + "\"");
            }
            break;
    }
    return fullHttpResponse;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) Document(de.dytanic.cloudnet.lib.utility.document.Document) ZipFile(java.util.zip.ZipFile)

Example 15 with Document

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

the class PacketAPIInNameUUID method handleInput.

@Override
public void handleInput(Document data, PacketSender packetSender) {
    if (data.contains("uniqueId")) {
        UUID uniqueId = data.getObject("uniqueId", new TypeToken<UUID>() {
        }.getType());
        String name = CloudNet.getInstance().getDbHandlers().getNameToUUIDDatabase().get(uniqueId);
        packetSender.sendPacket(getResult(new Document("name", name)));
    } else {
        String name = data.getString("name");
        UUID uniqueId = CloudNet.getInstance().getDbHandlers().getNameToUUIDDatabase().get(name);
        packetSender.sendPacket(getResult(new Document("uniqueId", uniqueId)));
    }
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) UUID(java.util.UUID) Document(de.dytanic.cloudnet.lib.utility.document.Document)

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