diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java index 3cad7c16a..91ec1931c 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java @@ -55,42 +55,56 @@ public class DumpSubCmd extends ViaSubCommand { HttpURLConnection con = null; try { - con = (HttpURLConnection) new URL("http://hastebin.com/documents").openConnection(); + con = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection(); } catch (IOException e) { sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); - Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Hastebin", e); + Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Gist", e); return; } try { - con.setRequestProperty("Content-Type", "text/plain"); - // Bypass cloudflare :( - con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); + con.setRequestProperty("Content-Type", "application/jsom"); + con.addRequestProperty("User-Agent", "ViaVersion"); con.setRequestMethod("POST"); con.setDoOutput(true); OutputStream out = con.getOutputStream(); - out.write(GsonUtil.getGsonBuilder().setPrettyPrinting().create().toJson(template).getBytes(Charset.forName("UTF-8"))); + String contents = GsonUtil.getGsonBuilder().setPrettyPrinting().create().toJson(template); + // Create payload + JsonObject payload = new JsonObject(); + payload.addProperty("description", "ViaVersion Dump"); + payload.addProperty("public", "false"); + // Create file contents + JsonObject file = new JsonObject(); + file.addProperty("content", contents); + // Create file list + JsonObject files = new JsonObject(); + files.add("dump.json", file); + payload.add("files", files); + // Write to stream + out.write(GsonUtil.getGson().toJson(payload).getBytes(Charset.forName("UTF-8"))); out.close(); String rawOutput = CharStreams.toString(new InputStreamReader(con.getInputStream())); con.getInputStream().close(); - System.out.println("Raw output: " + rawOutput); JsonObject output = GsonUtil.getGson().fromJson(rawOutput, JsonObject.class); - if (!output.has("key")) - throw new InvalidObjectException("Key is not given in Hastebin output"); + if (!output.has("html_url")) + throw new InvalidObjectException("URL is not given in Gist output"); - sender.sendMessage(ChatColor.GREEN + "We've made a dump with useful information, report your issue and provide this url: " + getUrl(output.get("key").getAsString())); + sender.sendMessage(ChatColor.GREEN + "We've made a dump with useful information, report your issue and provide this url: " + output.get("html_url").getAsString()); } catch (Exception e) { sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); - Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Hastebin", e); try { - if (con.getResponseCode() < 200 || con.getResponseCode() > 400) { - String rawOutput = CharStreams.toString(new InputStreamReader(con.getErrorStream())); - con.getErrorStream().close(); - Via.getPlatform().getLogger().log(Level.WARNING, "Page returned: " + rawOutput); + if (con.getResponseCode() == 403) { + // Ensure 403 is due to rate limit being hit + if("0".equals(con.getHeaderField("X-RateLimit-Remaining"))) { + Via.getPlatform().getLogger().log(Level.WARNING, "You may only create 60 dumps per hour, please try again later."); + return; + } } + Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Gist", e); + } catch (IOException e1) { Via.getPlatform().getLogger().log(Level.WARNING, "Failed to capture further info", e1); } @@ -100,8 +114,4 @@ public class DumpSubCmd extends ViaSubCommand { return true; } - - private String getUrl(String id) { - return String.format("http://hastebin.com/%s.json", id); - } }