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);
}
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);
}
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;
}
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;
}
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)));
}
}
Aggregations