Switch to IncendoPaster "library"

This commit is contained in:
dordsor21 2020-12-19 16:18:57 +00:00
parent e08fda0ac2
commit 7444c643a1
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
4 changed files with 11 additions and 300 deletions

View File

@ -69,6 +69,7 @@ dependencies {
exclude("com.sk89q.worldedit.worldedit-libs", "core") exclude("com.sk89q.worldedit.worldedit-libs", "core")
} }
"compile"("org.bstats:bstats-bukkit:1.7") "compile"("org.bstats:bstats-bukkit:1.7")
"compile"("com.intellectualsites.paster:Paster:1.0-SNAPSHOT")
// Third party // Third party
"implementation"("com.github.InventivetalentDev:MapManager:1.7.+") { isTransitive = false } "implementation"("com.github.InventivetalentDev:MapManager:1.7.+") { isTransitive = false }
"implementation"("com.github.TechFortress:GriefPrevention:16.+") { isTransitive = false } "implementation"("com.github.TechFortress:GriefPrevention:16.+") { isTransitive = false }
@ -119,6 +120,9 @@ tasks.named<ShadowJar>("shadowJar") {
relocate("org.bstats", "com.boydti.metrics") { relocate("org.bstats", "com.boydti.metrics") {
include(dependency("org.bstats:bstats-bukkit:1.7")) include(dependency("org.bstats:bstats-bukkit:1.7"))
} }
relocate("com.intellectualsites.paster", "com.boydti.fawe.paster") {
include(dependency("com.intellectualsites.paster:Paster:1.0-SNAPSHOT"))
}
} }
} }

View File

@ -59,6 +59,7 @@ dependencies {
"compile"("com.plotsquared:PlotSquared-Core:5.12.2") { "compile"("com.plotsquared:PlotSquared-Core:5.12.2") {
isTransitive = false isTransitive = false
} }
"api"("com.intellectualsites.paster:Paster:1.0-SNAPSHOT")
} }
tasks.named<Test>("test") { tasks.named<Test>("test") {

View File

@ -1,298 +0,0 @@
package com.boydti.fawe.util;
import com.boydti.fawe.Fawe;
import com.google.common.base.Charsets;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sk89q.worldedit.util.paste.Paster;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/**
* Single class paster for the Incendo paste service
*
* @author Sauilitired
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final class IncendoPaster implements Paster {
/**
* Upload service URL
*/
public static final String UPLOAD_PATH = "https://athion.net/ISPaster/paste/upload";
/**
* Valid paste applications
*/
public static final Collection<String>
VALID_APPLICATIONS = Arrays
.asList("plotsquared", "fastasyncworldedit", "incendopermissions", "kvantum");
private final Collection<PasteFile> files = new ArrayList<>();
private final String pasteApplication;
/**
* Construct a new paster
*
* @param pasteApplication The application that is sending the paste
*/
public IncendoPaster(final String pasteApplication) {
if (pasteApplication == null || pasteApplication.isEmpty()) {
throw new IllegalArgumentException("paste application cannot be null, nor empty");
}
if (!VALID_APPLICATIONS.contains(pasteApplication.toLowerCase(Locale.ROOT))) {
throw new IllegalArgumentException(
String.format("Unknown application name: %s", pasteApplication));
}
this.pasteApplication = pasteApplication;
}
@Override
public Callable<URL> paste(String content) {
return new PasteTask(content);
}
private final class PasteTask implements Callable<URL> {
private PasteTask(String content) {}
@Override
public URL call() throws Exception {
return new URL(debugPaste());
}
}
/**
* Get an immutable collection containing all the files that have been added to this paster
*
* @return Unmodifiable collection
*/
public final Collection<PasteFile> getFiles() {
return Collections.unmodifiableCollection(this.files);
}
/**
* Add a file to the paster
*
* @param file File to paste
*/
public void addFile(final PasteFile file) {
if (file == null) {
throw new IllegalArgumentException("File cannot be null");
}
// Check to see that no duplicate files are submitted
for (final PasteFile pasteFile : this.files) {
if (pasteFile.fileName.equalsIgnoreCase(file.getFileName())) {
throw new IllegalArgumentException(String.format("Found duplicate file with name %s",
file.getFileName()));
}
}
this.files.add(file);
}
/**
* Create a JSON string from the submitted information
*
* @return compiled JSON string
*/
private String toJsonString() {
final StringBuilder builder = new StringBuilder("{\n");
builder.append("\"paste_application\": \"").append(this.pasteApplication).append("\",\n\"files\": \"");
Iterator<PasteFile> fileIterator = this.files.iterator();
while (fileIterator.hasNext()) {
final PasteFile file = fileIterator.next();
builder.append(file.getFileName());
if (fileIterator.hasNext()) {
builder.append(",");
}
}
builder.append("\",\n");
fileIterator = this.files.iterator();
while (fileIterator.hasNext()) {
final PasteFile file = fileIterator.next();
builder.append("\"file-").append(file.getFileName()).append("\": \"")
.append(file.getContent().replaceAll("\"", "\\\\\"")).append("\"");
if (fileIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("\n}");
return builder.toString();
}
/**
* Upload the paste and return the status message
*
* @return Status message
* @throws Throwable any and all exceptions
*/
public final String upload() throws Throwable {
final URL url = new URL(UPLOAD_PATH);
final URLConnection connection = url.openConnection();
final HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
final byte[] content = toJsonString().getBytes(Charsets.UTF_8);
httpURLConnection.setFixedLengthStreamingMode(content.length);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Accept", "*/*");
httpURLConnection.connect();
try (final OutputStream stream = httpURLConnection.getOutputStream()) {
stream.write(content);
}
if (!httpURLConnection.getResponseMessage().contains("OK")) {
throw new IllegalStateException(String.format("Server returned status: %d %s",
httpURLConnection.getResponseCode(), httpURLConnection.getResponseMessage()));
}
final StringBuilder input = new StringBuilder();
try (final BufferedReader inputStream = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) {
String line;
while ((line = inputStream.readLine()) != null) {
input.append(line).append("\n");
}
}
return input.toString();
}
/**
* Simple class that represents a paste file
*/
public static class PasteFile {
private final String fileName;
private final String content;
/**
* Construct a new paste file
*
* @param fileName File name, cannot be empty, nor null
* @param content File content, cannot be empty, nor null
*/
public PasteFile(final String fileName, final String content) {
if (fileName == null || fileName.isEmpty()) {
throw new IllegalArgumentException("file name cannot be null, nor empty");
}
if (content == null || content.isEmpty()) {
throw new IllegalArgumentException("content cannot be null, nor empty");
}
this.fileName = fileName;
this.content = content;
}
/**
* Get the file name
*
* @return File name
*/
public String getFileName() {
return this.fileName;
}
/**
* Get the file content as a single string
*
* @return File content
*/
public String getContent() {
return this.content;
}
}
public static String debugPaste() throws IOException {
final IncendoPaster incendoPaster = new IncendoPaster("fastasyncworldedit");
StringBuilder b = new StringBuilder();
b.append(
"# Welcome to this paste\n# It is meant to provide us at IntellectualSites with better information about your "
+ "problem\n");
b.append("\n# Server Information\n");
b.append(Fawe.imp().getDebugInfo());
b.append("\n# YAY! Now, let's see what we can find in your JVM\n");
Runtime runtime = Runtime.getRuntime();
RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
b.append("Uptime: ").append(TimeUnit.MINUTES.convert(rb.getUptime(), TimeUnit.MILLISECONDS))
.append(" minutes").append('\n');
b.append("JVM Flags: ").append(rb.getInputArguments()).append('\n');
b.append("Free Memory: ").append(runtime.freeMemory() / 1024 / 1024).append(" MB").append('\n');
b.append("Max Memory: ").append(runtime.maxMemory() / 1024 / 1024).append(" MB").append('\n');
b.append("Total Memory: ").append(runtime.totalMemory() / 1024 / 1024).append(" MB").append('\n');
b.append("Available Processors: ").append(runtime.availableProcessors()).append('\n');
b.append("Java Name: ").append(rb.getVmName()).append('\n');
b.append("Java Version: '").append(System.getProperty("java.version")).append("'\n");
b.append("Java Vendor: '").append(System.getProperty("java.vendor")).append("'\n");
b.append("Operating System: '").append(System.getProperty("os.name")).append("'\n");
b.append("OS Version: ").append(System.getProperty("os.version")).append('\n');
b.append("OS Arch: ").append(System.getProperty("os.arch")).append('\n');
b.append("# Okay :D Great. You are now ready to create your bug report!");
b.append("\n# You can do so at https://github.com/IntellectualSites/FastAsyncWorldEdit/issues");
b.append("\n# or via our Discord at https://discord.gg/KxkjDVg");
incendoPaster.addFile(new IncendoPaster.PasteFile("information", b.toString()));
try {
final File logFile = new File(Fawe.imp().getDirectory(), "../../logs/latest.log");
final String file;
if (Files.size(logFile.toPath()) > 14_000_000) {
file = "too big :(";
} else {
file = readFile(logFile);
}
incendoPaster.addFile(new IncendoPaster.PasteFile("latest.log", file));
} catch (IOException ignored) {
}
incendoPaster.addFile(new PasteFile("config.yml", readFile(new File(Fawe.imp().getDirectory(), "config.yml"))));
incendoPaster.addFile(new PasteFile("config-legacy.yml", readFile(new File(Fawe.imp().getDirectory(), "config-legacy.yml"))));
final String rawResponse;
try {
rawResponse = incendoPaster.upload();
} catch (Throwable throwable) {
throw new IOException(String.format("Failed to upload files: %s", throwable.getMessage()), throwable);
}
final JsonObject jsonObject = new JsonParser().parse(rawResponse).getAsJsonObject();
if (jsonObject.has("created")) {
final String pasteId = jsonObject.get("paste_id").getAsString();
return String.format("https://athion.net/ISPaster/paste/view/%s", pasteId);
} else {
throw new IOException(String.format("Failed to upload files: %s",
jsonObject.get("response").getAsString()));
}
}
private static String readFile(final File file) throws IOException {
final StringBuilder content = new StringBuilder();
final List<String> lines = new ArrayList<>();
try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
}
for (int i = Math.max(0, lines.size() - 1000); i < lines.size(); i++) {
content.append(lines.get(i)).append("\n");
}
return content.toString();
}
}

View File

@ -22,7 +22,7 @@ package com.sk89q.worldedit.command;
import com.boydti.fawe.Fawe; import com.boydti.fawe.Fawe;
import com.boydti.fawe.FaweVersion; import com.boydti.fawe.FaweVersion;
import com.boydti.fawe.config.Settings; import com.boydti.fawe.config.Settings;
import com.boydti.fawe.util.IncendoPaster; import com.intellectualsites.paster.IncendoPaster;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
@ -47,6 +47,7 @@ import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.ArgFlag; import org.enginehub.piston.annotation.param.ArgFlag;
import org.enginehub.piston.annotation.param.Switch; import org.enginehub.piston.annotation.param.Switch;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@ -139,7 +140,10 @@ public class WorldEditCommands {
public void report(Actor actor) throws WorldEditException { public void report(Actor actor) throws WorldEditException {
String dest; String dest;
try { try {
dest = IncendoPaster.debugPaste(); final File logFile = new File(Fawe.imp().getDirectory(), "../../logs/latest.log");
final File config = new File(Fawe.imp().getDirectory(), "config.yml");
final File legacyConfig = new File(Fawe.imp().getDirectory(), "config-legacy.yml");
dest = IncendoPaster.debugPaste(logFile, Fawe.imp().getDebugInfo(), config, legacyConfig);
} catch (IOException e) { } catch (IOException e) {
actor.printInfo(TextComponent.of(e.getMessage())); actor.printInfo(TextComponent.of(e.getMessage()));
return; return;