2022-11-14 00:06:44 +00:00
package net.t2code.t2codelib.SPIGOT.system.cmd ;
2021-11-28 01:13:21 +00:00
2022-01-20 17:19:39 +00:00
import net.t2code.luckyBox.api.LuckyBoxAPI ;
2022-10-25 13:03:07 +00:00
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend ;
import net.t2code.t2codelib.SPIGOT.api.minecraftVersion.T2CmcVersion ;
import net.t2code.t2codelib.SPIGOT.api.plugins.T2CpluginCheck ;
2022-11-14 00:06:44 +00:00
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain ;
import net.t2code.t2codelib.SPIGOT.system.Vault ;
2022-10-25 13:03:07 +00:00
import net.t2code.t2codelib.Util ;
2022-11-10 17:27:55 +00:00
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 ;
2021-11-28 01:13:21 +00:00
import org.bukkit.Bukkit ;
2022-01-21 21:04:26 +00:00
import org.bukkit.OfflinePlayer ;
2022-11-14 17:45:54 +00:00
import org.bukkit.Server ;
2021-11-28 01:13:21 +00:00
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
import org.bukkit.plugin.Plugin ;
2022-11-10 17:27:55 +00:00
import org.json.JSONObject ;
2022-11-14 17:45:54 +00:00
2021-11-28 01:13:21 +00:00
import java.io.* ;
2022-11-10 17:27:55 +00:00
import java.net.HttpURLConnection ;
import java.net.URL ;
2021-11-28 01:13:21 +00:00
import java.nio.file.Files ;
2022-11-14 23:36:25 +00:00
import java.nio.file.Paths ;
2021-11-28 01:13:21 +00:00
import java.text.SimpleDateFormat ;
2023-06-19 19:17:49 +00:00
import java.util.ArrayList ;
2021-11-28 01:13:21 +00:00
import java.util.Calendar ;
2023-06-19 19:17:49 +00:00
import java.util.Collections ;
import java.util.Comparator ;
2021-11-28 01:13:21 +00:00
import java.util.zip.ZipEntry ;
import java.util.zip.ZipOutputStream ;
public class CreateReportLog {
2022-11-14 23:36:25 +00:00
protected static void create ( CommandSender sender , Boolean confirmUpload ) throws IOException {
2022-10-25 13:03:07 +00:00
T2Csend . sender ( sender , Util . getPrefix ( ) + " §6A DebugLog is created... " ) ;
2021-11-28 01:13:21 +00:00
String timeStampFile = new SimpleDateFormat ( " HH_mm_ss-dd_MM_yyyy " ) . format ( Calendar . getInstance ( ) . getTime ( ) ) ;
2022-10-25 13:03:07 +00:00
File directory = new File ( T2CodeLibMain . getPath ( ) + " /DebugLogs " ) ;
2022-11-14 21:37:36 +00:00
File directoryTemp = new File ( " T2CDebugLogsTemp " ) ;
2021-11-28 01:13:21 +00:00
if ( ! directory . exists ( ) ) {
directory . mkdir ( ) ;
}
2022-11-14 21:37:36 +00:00
if ( ! directoryTemp . exists ( ) ) {
directoryTemp . mkdir ( ) ;
}
2021-11-28 01:13:21 +00:00
2022-11-14 21:37:36 +00:00
File file = new File ( " T2CDebugLogsTemp/T2CodeLog.txt " ) ;
2021-11-28 01:13:21 +00:00
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 ( ) ) ;
2022-11-14 17:45:54 +00:00
pWriter . println ( " Created on: " + timeStamp + " - from: " + sender . getName ( ) ) ;
2021-11-28 01:13:21 +00:00
pWriter . println ( ) ;
2022-10-25 13:03:07 +00:00
pWriter . println ( " Server Bukkit version: " + T2CmcVersion . getBukkitVersion ( ) ) ;
pWriter . println ( " Server run on: " + T2CmcVersion . getMcVersion ( ) ) ;
pWriter . println ( " Server NMS: " + T2CmcVersion . getNms ( ) ) ;
2021-11-28 01:13:21 +00:00
pWriter . println ( " Online Mode: " + Bukkit . getOnlineMode ( ) ) ;
2022-11-14 17:45:54 +00:00
pWriter . println ( " Bungee Mode: " + T2CodeLibMain . getIsBungee ( ) ) ;
pWriter . println ( " Server Port: " + Bukkit . getServer ( ) . getPort ( ) ) ;
pWriter . println ( ) ;
2021-11-28 01:13:21 +00:00
pWriter . println ( " Worlds: " + Bukkit . getWorlds ( ) ) ;
2022-01-21 21:04:26 +00:00
pWriter . println ( " OP-Player: " ) ;
2022-02-09 13:40:27 +00:00
for ( OfflinePlayer player : Bukkit . getOperators ( ) ) {
2022-11-10 20:21:44 +00:00
pWriter . println ( " - " + player . getName ( ) + " - " + player . getUniqueId ( ) ) ;
2022-01-21 21:04:26 +00:00
}
2021-11-28 01:13:21 +00:00
pWriter . println ( ) ;
if ( Vault . vaultEnable ) {
2022-10-25 13:03:07 +00:00
pWriter . println ( " Vault: " + Bukkit . getPluginManager ( ) . getPlugin ( " Vault " ) . getName ( ) + " - " + Bukkit . getPluginManager ( ) . getPlugin ( " Vault " )
. getDescription ( ) . getVersion ( ) ) ;
2021-11-28 01:13:21 +00:00
} else pWriter . println ( " Vault: not connected " ) ;
2022-10-25 13:03:07 +00:00
if ( T2CodeLibMain . getEco ( ) ! = null ) {
String st = T2CodeLibMain . getEco ( ) . getName ( ) ;
if ( T2CodeLibMain . getEco ( ) . getName ( ) . equals ( " CMIEconomy " ) ) st = " CMI " ;
2022-02-09 13:40:27 +00:00
if ( Bukkit . getPluginManager ( ) . getPlugin ( st ) ! = null ) {
2022-10-25 13:03:07 +00:00
pWriter . println ( " Economy: " + T2CodeLibMain . getEco ( ) . isEnabled ( ) + " - " + st + " - " + Bukkit . getPluginManager ( ) . getPlugin ( st ) . getDescription ( ) . getVersion ( ) ) ;
} else pWriter . println ( " Economy: " + T2CodeLibMain . getEco ( ) . isEnabled ( ) + " - " + st ) ;
2021-11-28 01:13:21 +00:00
} else pWriter . println ( " Economy: not connected via vault " ) ;
2022-10-25 13:03:07 +00:00
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 ( ) ) ;
2021-11-28 01:13:21 +00:00
} 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 ( ) ;
2022-10-25 13:03:07 +00:00
pWriter . println ( " T2CodeLib: " + T2CodeLibMain . getPlugin ( ) . getDescription ( ) . getVersion ( ) ) ;
2021-11-28 01:13:21 +00:00
pWriter . println ( ) ;
2022-10-25 13:03:07 +00:00
if ( T2CpluginCheck . luckyBox ( ) ) {
2022-01-20 17:19:39 +00:00
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 ( ) ) ;
2022-11-03 18:14:44 +00:00
pWriter . println ( " T2C-LuckyBox isP: " + LuckyBoxAPI . isP ( ) ) ;
pWriter . println ( " T2C-LuckyBox isV: " + LuckyBoxAPI . isV ( ) ) ;
2022-01-20 17:19:39 +00:00
pWriter . println ( ) ;
}
2023-06-19 19:17:49 +00:00
pWriter . println ( " OfflinePlayers amount: " + Bukkit . getOfflinePlayers ( ) . length ) ;
2022-11-10 20:21:44 +00:00
pWriter . println ( " OfflinePlayers: " ) ;
2023-06-19 19:17:49 +00:00
ArrayList < OfflinePlayer > players = new ArrayList < > ( ) ;
Collections . addAll ( players , Bukkit . getOfflinePlayers ( ) ) ;
players . sort ( Comparator . comparing ( OfflinePlayer : : getName ) ) ;
for ( OfflinePlayer player : players ) {
2022-11-10 20:21:44 +00:00
pWriter . println ( " - " + player . getName ( ) + " - " + player . getUniqueId ( ) ) ;
}
2023-06-19 19:17:49 +00:00
2022-11-10 20:21:44 +00:00
pWriter . println ( ) ;
pWriter . println ( " Plugins amount: " + Bukkit . getPluginManager ( ) . getPlugins ( ) . length ) ;
2021-11-28 01:13:21 +00:00
pWriter . println ( " Plugins: " ) ;
2023-06-19 19:17:49 +00:00
ArrayList < Plugin > plugins = new ArrayList < > ( ) ;
Collections . addAll ( plugins , Bukkit . getPluginManager ( ) . getPlugins ( ) ) ;
plugins . sort ( Comparator . comparing ( Plugin : : getName ) ) ;
for ( Plugin pl : plugins ) {
2023-06-30 15:59:29 +00:00
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 ( ) ) ;
2021-11-28 01:13:21 +00:00
}
2023-06-19 19:17:49 +00:00
2021-11-28 01:13:21 +00:00
} catch ( IOException ioe ) {
ioe . printStackTrace ( ) ;
} finally {
if ( pWriter ! = null ) {
pWriter . flush ( ) ;
pWriter . close ( ) ;
}
}
2022-11-14 21:37:36 +00:00
String filePath = " T2CDebugLogsTemp/T2CodeLog.txt " ;
2021-11-28 01:13:21 +00:00
String log = " logs/latest.log " ;
2022-11-14 21:37:36 +00:00
String zipPath = " T2CDebugLogsTemp/T2CLog- " + timeStampFile + " .zip " ;
2021-11-28 01:13:21 +00:00
try ( ZipOutputStream zip = new ZipOutputStream ( new FileOutputStream ( zipPath ) ) ) {
File fileToZip = new File ( filePath ) ;
zip . putNextEntry ( new ZipEntry ( fileToZip . getName ( ) ) ) ;
Files . copy ( fileToZip . toPath ( ) , zip ) ;
2022-04-16 14:56:16 +00:00
addFileToZip ( " " , " logs/latest.log " , zip , false ) ;
2022-04-01 00:23:26 +00:00
2023-06-30 15:59:29 +00:00
// for (String pl : Util.getT2cPlugins()) {
// pluginToDebug(pl, zip);
// }
2022-12-29 21:02:05 +00:00
pluginToDebug ( zip ) ; //todo überprüfen
2021-11-28 01:13:21 +00:00
zip . closeEntry ( ) ;
zip . close ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
file . delete ( ) ;
2022-11-14 23:36:25 +00:00
File zipFile = new File ( zipPath ) ;
2022-11-20 19:17:43 +00:00
if ( ! confirmUpload ) {
Files . move ( Paths . get ( zipPath ) , Paths . get ( directory + " / " + zipFile . getName ( ) ) ) ;
zipFile . delete ( ) ;
directoryTemp . delete ( ) ;
2022-11-14 23:36:25 +00:00
if ( sender instanceof Player ) {
2022-11-20 19:17:43 +00:00
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 ( ) ) ;
2022-11-14 23:36:25 +00:00
}
2022-11-20 19:17:43 +00:00
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 ( ) ) ;
2022-11-14 23:36:25 +00:00
return ;
}
2022-11-10 17:27:55 +00:00
HttpClient httpclient = new DefaultHttpClient ( ) ;
HttpPost httppost = new HttpPost ( " https://debug.t2code.net/uploadFile " ) ;
2022-11-14 23:36:25 +00:00
2022-11-10 17:27:55 +00:00
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 ( ) ;
2022-11-14 21:37:36 +00:00
directoryTemp . delete ( ) ;
2021-11-28 01:13:21 +00:00
if ( sender instanceof Player ) {
2022-11-14 21:59:17 +00:00
T2Csend . sender ( sender , ( " [prefix] <green>A DebugLog zip has been created. " +
2022-11-15 01:17:26 +00:00
" <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> " +
2023-12-07 15:58:39 +00:00
" <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> " +
2022-11-15 01:17:26 +00:00
" <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> " )
2022-11-20 19:17:43 +00:00
. replace ( " [key] " , fileID ) . replace ( " [url] " , downloadURL ) . replace ( " [prefix] " , Util . getPrefix ( ) ) ) ;
2022-11-14 13:16:46 +00:00
}
2022-11-15 01:17:26 +00:00
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> " +
2023-12-07 15:58:39 +00:00
" <br><dark_red>You can delete your Debug-File with the following command:</dark_red> <red>/t2code debug deleteReportLog [key]</red>. " )
2022-11-14 17:45:54 +00:00
. replace ( " [key] " , fileID ) . replace ( " [url] " , downloadURL ) ) ;
2022-11-14 09:04:53 +00:00
ReportLogStorage . add ( fileID ) ;
2021-11-28 01:13:21 +00:00
}
2022-04-16 15:09:19 +00:00
private static void pluginToDebug ( String pluginName , ZipOutputStream zip ) throws IOException {
2022-10-25 13:03:07 +00:00
if ( T2CpluginCheck . pluginCheck ( pluginName ) ) {
2021-11-28 01:13:21 +00:00
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 ) {
2022-04-16 15:09:19 +00:00
if ( config . getName ( ) . contains ( pluginName ) & & config . getName ( ) . contains ( " .jar " ) ) {
2021-11-28 01:13:21 +00:00
addFileToZip ( " T2Code-Plugins " , config . getPath ( ) , zip , false ) ;
}
}
}
}
2023-06-30 15:59:29 +00:00
2022-12-29 21:02:05 +00:00
private static void pluginToDebug ( ZipOutputStream zip ) throws IOException {
2023-06-30 15:59:29 +00:00
for ( Plugin plugin : Bukkit . getPluginManager ( ) . getPlugins ( ) ) {
2022-12-29 21:02:05 +00:00
String plName = plugin . getDescription ( ) . getName ( ) ;
2023-06-30 15:59:29 +00:00
if ( plName . contains ( " T2C- " ) | | Util . getT2cPlugins ( ) . contains ( plName ) | | plugin . getDescription ( ) . getAuthors ( ) . contains ( " JaTiTV " ) ) {
2022-12-29 21:02:05 +00:00
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 ( ) ) ;
2023-06-30 15:59:29 +00:00
addFileToZip ( " T2Code-Plugins " , f . getPath ( ) , zip , false ) ;
2022-12-29 21:02:05 +00:00
}
}
}
2021-11-28 01:13:21 +00:00
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 ) {
2021-12-22 21:44:04 +00:00
try {
zip . write ( buf , 0 , len ) ;
2022-02-09 13:40:27 +00:00
} catch ( Exception ex ) {
2021-12-22 21:44:04 +00:00
ex . printStackTrace ( ) ;
}
2021-11-28 01:13:21 +00:00
}
}
}
}
2022-11-10 17:27:55 +00:00
2022-11-14 09:04:53 +00:00
public static void delete ( String arg , CommandSender sender ) throws IOException {
URL url = new URL ( " https://debug.t2code.net/delete/ " + arg ) ;
2022-11-10 17:27:55 +00:00
HttpURLConnection con = ( HttpURLConnection ) url . openConnection ( ) ;
con . setRequestMethod ( " GET " ) ;
2022-11-14 09:04:53 +00:00
int response = con . getResponseCode ( ) ;
2022-11-14 17:45:54 +00:00
if ( response = = 200 ) {
2022-11-14 09:04:53 +00:00
ReportLogStorage . remove ( arg ) ;
2022-11-14 17:45:54 +00:00
T2Csend . sender ( sender , " <green>Deleted report: <yellow> " + arg ) ;
} else {
T2Csend . sender ( sender , " <green>Error while deleting report: <yellow> " + arg ) ;
2022-11-14 09:04:53 +00:00
}
con . disconnect ( ) ;
2022-11-10 17:27:55 +00:00
}
2021-11-28 01:13:21 +00:00
}