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

306 lines
16 KiB
Java

package net.t2code.t2codelib.SPIGOT.system.cmd;
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.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.SPIGOT.system.Vault;
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.Server;
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.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class CreateReportLog {
protected static void create(CommandSender sender, Boolean confirmUpload) 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");
File directoryTemp = new File("T2CDebugLogsTemp");
if (!directory.exists()) {
directory.mkdir();
}
if (!directoryTemp.exists()) {
directoryTemp.mkdir();
}
File file = new File("T2CDebugLogsTemp/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 + " - from: " + sender.getName());
pWriter.println();
pWriter.println("Server Bukkit version: " + T2CmcVersion.getBukkitVersion());
pWriter.println("Server run on: " + T2CmcVersion.getMcVersion());
pWriter.println("Server NMS: " + T2CmcVersion.getNms());
pWriter.println("Online Mode: " + Bukkit.getOnlineMode());
pWriter.println("Bungee Mode: " + T2CodeLibMain.getIsBungee());
pWriter.println("Server Port: " + Bukkit.getServer().getPort());
pWriter.println();
pWriter.println("Worlds: " + Bukkit.getWorlds());
pWriter.println("OP-Player:");
for (OfflinePlayer player : Bukkit.getOperators()) {
pWriter.println(" - " + 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("OfflinePlayers amount: " + Bukkit.getOfflinePlayers().length);
pWriter.println("OfflinePlayers: ");
ArrayList<OfflinePlayer> players = new ArrayList<>();
Collections.addAll(players, Bukkit.getOfflinePlayers());
players.sort(Comparator.comparing(OfflinePlayer::getName));
for (OfflinePlayer player : players) {
pWriter.println(" - " + player.getName() + " - " + player.getUniqueId());
}
pWriter.println();
pWriter.println("Plugins amount: " + Bukkit.getPluginManager().getPlugins().length);
pWriter.println("Plugins: ");
ArrayList<Plugin> plugins = new ArrayList<>();
Collections.addAll(plugins, Bukkit.getPluginManager().getPlugins());
plugins.sort(Comparator.comparing(Plugin::getName));
for (Plugin pl : plugins) {
pWriter.println(" - " + pl.getName() +
" - " + pl.getDescription().getVersion() +
" - Enabled: " + pl.isEnabled() +
" - Autors: " + pl.getDescription().getAuthors() +
" - SoftDepend: " + pl.getDescription().getSoftDepend() +
" - Depend: " + pl.getDescription().getDepend() +
" - LoadBefore: " + pl.getDescription().getLoadBefore() +
" - Website: " + pl.getDescription().getWebsite());
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pWriter != null) {
pWriter.flush();
pWriter.close();
}
}
String filePath = "T2CDebugLogsTemp/T2CodeLog.txt";
String log = "logs/latest.log";
String zipPath = "T2CDebugLogsTemp/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);
// }
pluginToDebug(zip); //todo überprüfen
zip.closeEntry();
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
file.delete();
File zipFile = new File(zipPath);
if (!confirmUpload) {
Files.move(Paths.get(zipPath), Paths.get(directory + "/" + zipFile.getName()));
zipFile.delete();
directoryTemp.delete();
if (sender instanceof Player) {
T2Csend.sender(sender, Util.getPrefix() + " §6A DebugLog zip has been created. you can find it on in the files on your server under the path: §e" + directory + "/" + zipFile.getName());
}
T2Csend.console(Util.getPrefix() + " §6A DebugLog zip has been created. you can find it on in the files on your server under the path: §e" + directory + "/" + zipFile.getName());
return;
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://debug.t2code.net/uploadFile");
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();
directoryTemp.delete();
if (sender instanceof Player) {
T2Csend.sender(sender, ("[prefix] <green>A DebugLog zip has been created." +
"<br>[prefix] <green>You can download it <click:open_url:'[url]'><gold><hover:show_text:'<yellow>Download the debug file</yellow>'>here</hover></gold></click>." +
"<br>[prefix] <green>Please enter the following key in the ticket: <gold><click:copy_to_clipboard:[key]><hover:show_text:'<yellow>Copy to clipboard</yellow>'>[key]</hover></click></gold></green>" +
"<br>[prefix] <dark_red>Do not share the download URL with anyone!</dark_red>" +
"<br>[prefix] <click:run_command:'/t2code debug deleteReportLog [key]'><hover:show_text:'<yellow>Click to delete</yellow>'><green>You can <b>delete</b> your Debug-File by clicking me.</green>" +
"<br>[prefix] <color:#910d06>(If you do not delete the debug file, it will be deleted automatically after <red>14</red> days!)</color></click></hover>")
.replace("[key]", fileID).replace("[url]", downloadURL).replace("[prefix]", Util.getPrefix()));
}
T2Csend.console(Util.getPrefix() + (" <gold>A DebugLog zip has been created. You can download it here:</gold> <yellow>[url]</yellow>" +
"<br><gold>Please enter the following key in the ticket:</gold> <yellow>[key]</yellow>." +
"<br><dark_red>Do not share the download URL with anyone!</dark_red>" +
"<br><color:#910d06>(If you do not delete the debug file, it will be deleted automatically after <red>14</red> days!)</color>" +
"<br><dark_red>You can delete your Debug-File with the following command:</dark_red> <red>/t2code debug deleteReportLog [key]</red>.")
.replace("[key]", fileID).replace("[url]", downloadURL));
ReportLogStorage.add(fileID);
}
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 pluginToDebug(ZipOutputStream zip) throws IOException {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
String plName = plugin.getDescription().getName();
if (plName.contains("T2C-") || Util.getT2cPlugins().contains(plName) || plugin.getDescription().getAuthors().contains("JaTiTV")) {
File plConfigs = new File(plugin.getDataFolder().getPath());
if (plConfigs.exists()) {
addFolderToZip("T2Code-Plugins", plugin.getDataFolder().getPath(), zip);
}
File f = new File(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
addFileToZip("T2Code-Plugins", f.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, CommandSender sender) throws IOException {
URL url = new URL("https://debug.t2code.net/delete/" + arg);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int response = con.getResponseCode();
if (response == 200) {
ReportLogStorage.remove(arg);
T2Csend.sender(sender, "<green>Deleted report: <yellow>" + arg);
} else {
T2Csend.sender(sender, "<green>Error while deleting report: <yellow>" + arg);
}
con.disconnect();
}
}