diff --git a/README.md b/README.md index 72d19c0..4d780fa 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,13 @@ BetterCrashes is a port of VanillaFix's crash improvement features to Minecraft 1.7.10. -BetterCrashes makes Minecraft return you to the main menu when it crashes,and it can upload your Crash-Report to -[Ubuntu Pastebin](http://paste.ubuntu.com/) for send the link easier. +BetterCrashes makes Minecraft return you to the main menu when it crashes, and it can upload your crash report to +[mclo.gs](https://mclo.gs/) for easy sharing. -Most of the code comes from VanillaFix, I just make them work on Minecraft 1.7.10. +Most of the code comes from VanillaFix, I just made it work on Minecraft 1.7.10. Requires [SpongeMixins](https://github.com/GTNewHorizons/SpongeMixins) mod to work ## Credits + Thanks to VanillaFix and original author, vfyjxf diff --git a/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java b/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java index 33fba68..b3a7025 100644 --- a/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java +++ b/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java @@ -15,6 +15,7 @@ public class BetterCrashesConfig { public static boolean isGTNH = Loader.isModLoaded("dreamcraft"); public static int crashLogLimitClient = 30; public static int crashLogLimitServer = 30; + public static String crashLogPasteService; public static void init(File file) { config = new Configuration(file); @@ -38,6 +39,11 @@ public class BetterCrashesConfig { 0, Integer.MAX_VALUE, "Maximum number of crash logs generated per restart for server. Suppresses too many logs generated by continuous crashes."); + crashLogPasteService = config.getString( + "crashLogPasteService", + GENERAL, + "mclo.gs", + "Service to use for uploading crashlogs. Currently, only mclo.gs is currently supported."); if (config.hasChanged()) { config.save(); diff --git a/src/main/java/vfyjxf/bettercrashes/upload/CrashReportUpload.java b/src/main/java/vfyjxf/bettercrashes/upload/CrashReportUpload.java new file mode 100644 index 0000000..c71dd0c --- /dev/null +++ b/src/main/java/vfyjxf/bettercrashes/upload/CrashReportUpload.java @@ -0,0 +1,31 @@ +package vfyjxf.bettercrashes.upload; + +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import vfyjxf.bettercrashes.BetterCrashes; +import vfyjxf.bettercrashes.BetterCrashesConfig; + +public class CrashReportUpload { + + private static final Map services = new HashMap<>(); + private static final String DEFAULT_SERVICE = "mclo.gs"; + + static { + services.put("mclo.gs", new MclogsUploadService()); + } + + public static URL uploadToPastebin(String crashReport) throws IOException { + String service = BetterCrashesConfig.crashLogPasteService; + IUploadService selectedService = services.get(service); + if (selectedService == null) { + BetterCrashes.logger.warn( + String.format("Unknown upload service \"%s\", falling back to \"%s\"", service, DEFAULT_SERVICE)); + selectedService = services.get(DEFAULT_SERVICE); + } + + return selectedService.upload(crashReport, null); + } +} diff --git a/src/main/java/vfyjxf/bettercrashes/upload/IUploadService.java b/src/main/java/vfyjxf/bettercrashes/upload/IUploadService.java new file mode 100644 index 0000000..2621f94 --- /dev/null +++ b/src/main/java/vfyjxf/bettercrashes/upload/IUploadService.java @@ -0,0 +1,17 @@ +package vfyjxf.bettercrashes.upload; + +import java.io.IOException; +import java.net.URL; + +public interface IUploadService { + + /** + * Upload crash report to pastebin service + * + * @param contents crash report + * @param clientIdentifier + * @return URL pointing to the uploaded crash report + * @throws IOException + */ + URL upload(String contents, String clientIdentifier) throws IOException; +} diff --git a/src/main/java/vfyjxf/bettercrashes/upload/MclogsUploadService.java b/src/main/java/vfyjxf/bettercrashes/upload/MclogsUploadService.java new file mode 100644 index 0000000..68f43ac --- /dev/null +++ b/src/main/java/vfyjxf/bettercrashes/upload/MclogsUploadService.java @@ -0,0 +1,63 @@ +package vfyjxf.bettercrashes.upload; + +import static vfyjxf.bettercrashes.BetterCrashes.NAME; +import static vfyjxf.bettercrashes.BetterCrashes.VERSION; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class MclogsUploadService implements IUploadService { + + private static final String MCLOGS_UPLOAD_URL = "https://api.mclo.gs/1/log"; + + @Override + public URL upload(String contents, String clientIdentifier) throws IOException { + URL resultURL = null; + + URL pasteURL = new URL(MCLOGS_UPLOAD_URL); + HttpURLConnection connection = null; + try { + connection = (HttpURLConnection) pasteURL.openConnection(); + connection.setInstanceFollowRedirects(false); + connection.setRequestMethod("POST"); + connection.setUseCaches(false); + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + connection.setRequestProperty("Accept", "application/json"); + connection.setRequestProperty( + "User-Agent", + String.format("%s/%s%s", NAME, VERSION, (clientIdentifier != null) ? " " + clientIdentifier : "")); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setReadTimeout(20000); + connection.setConnectTimeout(50000); + + try (PrintWriter out = new PrintWriter(connection.getOutputStream())) { + String params = "content=" + URLEncoder.encode(contents, StandardCharsets.UTF_8.name()); + out.write(params); + out.flush(); + } + + try (BufferedReader in = new BufferedReader( + new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { + Gson gson = new Gson(); + JsonObject response = gson.fromJson(in, JsonObject.class); + if (response.has("success") && response.get("success").getAsBoolean()) { + resultURL = new URL(response.get("url").getAsString()); + } + } + } finally { + if (connection != null) connection.disconnect(); + } + + return resultURL; + } +} diff --git a/src/main/java/vfyjxf/bettercrashes/utils/CrashReportUpload.java b/src/main/java/vfyjxf/bettercrashes/utils/CrashReportUpload.java deleted file mode 100644 index d28effa..0000000 --- a/src/main/java/vfyjxf/bettercrashes/utils/CrashReportUpload.java +++ /dev/null @@ -1,33 +0,0 @@ -package vfyjxf.bettercrashes.utils; - -import java.io.IOException; -import java.io.PrintWriter; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; - -public class CrashReportUpload { - - public static String uploadToUbuntuPastebin(String url, String crashReport) throws IOException { - URL pasteUrl = new URL(url); - HttpURLConnection connection = (HttpURLConnection) pasteUrl.openConnection(); - connection.setInstanceFollowRedirects(false); - connection.setRequestMethod("POST"); - connection.setUseCaches(false); - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setReadTimeout(1000000); - connection.setConnectTimeout(500000); - PrintWriter out = new PrintWriter(connection.getOutputStream()); - String params = "poster=CrashReport&syntax=text&content=" + URLEncoder.encode(crashReport, "UTF-8"); - out.write(params); - out.flush(); - String resultUrl = null; - resultUrl = connection.getHeaderField("Location"); - if (resultUrl == null) { - resultUrl = connection.getHeaderField("location"); - } - return url + resultUrl; - } -} diff --git a/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java b/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java index a05ff64..ffb329c 100644 --- a/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java +++ b/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java @@ -10,6 +10,7 @@ import static vfyjxf.bettercrashes.BetterCrashesConfig.isGTNH; import java.io.IOException; import java.lang.reflect.Field; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -31,6 +32,7 @@ import cpw.mods.fml.common.ModContainer; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import vfyjxf.bettercrashes.BetterCrashesConfig; +import vfyjxf.bettercrashes.upload.CrashReportUpload; @SideOnly(Side.CLIENT) public abstract class GuiProblemScreen extends GuiScreen { @@ -51,7 +53,7 @@ public abstract class GuiProblemScreen extends GuiScreen { } protected final CrashReport report; - private volatile String hasteLink = null; + private volatile URL pasteLink = null; private String modListString; protected static final List UNSUPPORTED_MOD_IDS = Arrays.asList(); protected List detectedUnsupportedModNames; @@ -105,7 +107,7 @@ public abstract class GuiProblemScreen extends GuiScreen { } } if (button.id == 2) { - if (hasteLink == null) { + if (pasteLink == null) { button.enabled = false; button.displayString = I18n.format("bettercrashes.gui.common.uploading"); Thread thread = new Thread("BetterCrashes report uploading") { @@ -113,8 +115,7 @@ public abstract class GuiProblemScreen extends GuiScreen { @Override public void run() { try { - hasteLink = CrashReportUpload - .uploadToUbuntuPastebin("https://paste.ubuntu.com", report.getCompleteReport()); + pasteLink = CrashReportUpload.uploadToPastebin(report.getCompleteReport()); synchronized (button) { button.enabled = true; button.displayString = I18n.format("bettercrashes.gui.common.success"); @@ -130,11 +131,11 @@ public abstract class GuiProblemScreen extends GuiScreen { }; thread.start(); } else { - CrashUtils.openBrowser(hasteLink); + CrashUtils.openBrowser(pasteLink.toString()); } - if (hasteLink != null) { - setClipboardString(hasteLink); + if (pasteLink != null) { + setClipboardString(pasteLink.toString()); } } if (button.id == 3) {