T2CodeLib/src/main/java/net/t2code/t2codelib/SPIGOT/system/CreateReportLog.java

219 lines
11 KiB
Java

package net.t2code.t2codelib.SPIGOT.system;
import net.t2code.luckyBox.api.LuckyBoxAPI;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.minecraftVersion.T2CmcVersion;
import net.t2code.t2codelib.SPIGOT.api.plugins.T2CpluginCheck;
import net.t2code.t2codelib.Util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class CreateReportLog {
protected static void create(CommandSender sender) throws IOException {
T2Csend.sender(sender, Util.getPrefix() + " §6A DebugLog is created...");
String timeStampFile = new SimpleDateFormat("HH_mm_ss-dd_MM_yyyy").format(Calendar.getInstance().getTime());
File directory = new File(T2CodeLibMain.getPath() + "/DebugLogs");
if (!directory.exists()) {
directory.mkdir();
}
File file = new File(T2CodeLibMain.getPath(), "/DebugLogs/T2CodeLog.txt");
PrintWriter pWriter = null;
try {
pWriter = new PrintWriter(new FileWriter(file.getPath()));
String timeStamp = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy").format(Calendar.getInstance().getTime());
pWriter.println("Created on: " + timeStamp);
pWriter.println();
pWriter.println("Server Bukkit version: " + T2CmcVersion.getBukkitVersion());
pWriter.println("Server run on: " + T2CmcVersion.getMcVersion());
pWriter.println("Server NMS: " + T2CmcVersion.getNms());
pWriter.println();
pWriter.println("Online Mode: " + Bukkit.getOnlineMode());
pWriter.println("Worlds: " + Bukkit.getWorlds());
pWriter.println("OP-Player:");
for (OfflinePlayer player : Bukkit.getOperators()) {
pWriter.println(" - Player: " + player.getName() + " - " + player.getUniqueId());
}
pWriter.println();
if (Vault.vaultEnable) {
pWriter.println("Vault: " + Bukkit.getPluginManager().getPlugin("Vault").getName() + " - " + Bukkit.getPluginManager().getPlugin("Vault")
.getDescription().getVersion());
} else pWriter.println("Vault: not connected");
if (T2CodeLibMain.getEco() != null) {
String st = T2CodeLibMain.getEco().getName();
if (T2CodeLibMain.getEco().getName().equals("CMIEconomy")) st = "CMI";
if (Bukkit.getPluginManager().getPlugin(st) != null) {
pWriter.println("Economy: " + T2CodeLibMain.getEco().isEnabled() + " - " + st + " - " + Bukkit.getPluginManager().getPlugin(st).getDescription().getVersion());
} else pWriter.println("Economy: " + T2CodeLibMain.getEco().isEnabled() + " - " + st);
} else pWriter.println("Economy: not connected via vault");
if (T2CodeLibMain.getPerm() != null) {
if (Bukkit.getPluginManager().getPlugin(T2CodeLibMain.getPerm().getName()) != null) {
pWriter.println("Permission: " + T2CodeLibMain.getPerm().isEnabled() + " - " + T2CodeLibMain.getPerm().getName() + " - " + Bukkit.getPluginManager()
.getPlugin(T2CodeLibMain.getPerm().getName()).getDescription().getVersion());
} else pWriter.println("Permission: " + T2CodeLibMain.getPerm().isEnabled() + " - " + T2CodeLibMain.getPerm().getName());
} else pWriter.println("Permission: not connected via vault");
pWriter.println();
pWriter.println("Java: " + System.getProperty("java.version"));
pWriter.println("System: " + System.getProperty("os.name"));
pWriter.println("System: " + System.getProperty("os.version"));
pWriter.println("User Home: " + System.getProperty("user.home"));
pWriter.println();
pWriter.println("T2CodeLib: " + T2CodeLibMain.getPlugin().getDescription().getVersion());
pWriter.println();
if (T2CpluginCheck.luckyBox()) {
pWriter.println("T2C-PremiumPlugins: ");
pWriter.println("T2C-LuckyBox UID: " + LuckyBoxAPI.getUID());
pWriter.println("T2C-LuckyBox RID: " + LuckyBoxAPI.getRID());
pWriter.println("T2C-LuckyBox DID: " + LuckyBoxAPI.getDID());
pWriter.println("T2C-LuckyBox isP: " + LuckyBoxAPI.isP());
pWriter.println("T2C-LuckyBox isV: " + LuckyBoxAPI.isV());
pWriter.println();
}
pWriter.println("Plugins: ");
for (Plugin pl : Bukkit.getPluginManager().getPlugins()) {
pWriter.println(" - " + pl.getName() + " - " + pl.getDescription().getVersion() + " - Enabled: " + pl.isEnabled() + " - Autors: " + pl.getDescription()
.getAuthors() + " - Website: " + pl.getDescription().getWebsite());
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pWriter != null) {
pWriter.flush();
pWriter.close();
}
}
String filePath = T2CodeLibMain.getPath() + "/DebugLogs/T2CodeLog.txt";
String log = "logs/latest.log";
String zipPath = "plugins/T2CodeLib/DebugLogs/T2CLog-" + timeStampFile + ".zip";
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipPath))) {
File fileToZip = new File(filePath);
zip.putNextEntry(new ZipEntry(fileToZip.getName()));
Files.copy(fileToZip.toPath(), zip);
addFileToZip("", "logs/latest.log", zip, false);
for (String pl : Util.getT2cPlugins()) {
pluginToDebug(pl, zip);
}
zip.closeEntry();
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
file.delete();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://debug.t2code.net/uploadFile");
File zipFile = new File(zipPath);
FileBody bin = new FileBody(zipFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", bin);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
String responseString = EntityUtils.toString(resEntity, "UTF-8");
JSONObject obj = new JSONObject(responseString);
String downloadURL = obj.getString("fileDownloadUri");
String fileID = obj.getString("fileID");
String fileType = obj.getString("fileType");
Integer fileSize = obj.getInt("size");
zipFile.delete();
if (sender instanceof Player) {
T2Csend.sender(sender, Util.getPrefix() + "<green>A DebugLog zip has been created. You can download it <click:open_url:'url'><gold>here</gold></click>. Please enter the following key in the ticket: <gold>key</gold></green><br><dark_red>Do not share the download URL with anyone!</dark_red><br><click:run_command:'/t2code debug deleteReportLog key'><green>You can <b>delte </b>yor Debug-File by clicking me.</green></click>".replace("key",fileID).replace("url",downloadURL));
T2Csend.console(Util.getPrefix() + " §6A DebugLog zip has been created. You can download it here: url.\n§6Please enter the following key in the ticket: §ekey.\n§4Do not share the download URL with anyone!\nYou can delte yor Debug-File with the following command: /t2code debug deleteReportLog key.".replace("key",fileID).replace("url",downloadURL));
} else T2Csend.sender(sender, Util.getPrefix() + " §6A DebugLog zip has been created. You can download it here: url.\n§6Please enter the following key in the ticket: §ekey.\n§4Do not share the download URL with anyone!\nYou can delte yor Debug-File with the following command: /t2code debug deleteReportLog key.".replace("key",fileID).replace("url",downloadURL));
}
private static void pluginToDebug(String pluginName, ZipOutputStream zip) throws IOException {
if (T2CpluginCheck.pluginCheck(pluginName)) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
File plConfigs = new File(plugin.getDataFolder().getPath());
if (plConfigs.exists()) {
addFolderToZip("T2Code-Plugins", plugin.getDataFolder().getPath(), zip);
}
File f = new File("plugins/");
File[] fileArray = f.listFiles();
for (File config : fileArray) {
if (config.getName().contains(pluginName) && config.getName().contains(".jar")) {
addFileToZip("T2Code-Plugins", config.getPath(), zip, false);
}
}
}
}
private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {
File folder = new File(srcFolder);
if (folder.list() == null) {
addFileToZip(path + "/" + folder.getName(), srcFolder, zip, false);
} else if (folder.list().length == 0) {
addFileToZip(path, srcFolder, zip, true);
} else {
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
}
}
}
}
private static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag) throws IOException {
File folder = new File(srcFile);
if (flag) {
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
} else {
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
if (path.equals("")) {
zip.putNextEntry(new ZipEntry((folder.getName())));
} else {
zip.putNextEntry(new ZipEntry((path + "/" + folder.getName())));
}
while ((len = in.read(buf)) > 0) {
try {
zip.write(buf, 0, len);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
public static void delete(String arg) throws IOException {
URL url = new URL("https://debug.t2code.net/delte/" + arg);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
}
}