Rip anonymous GitHub gists. :( Use ViaVersion dump! :D

This commit is contained in:
Matsv 2018-03-29 20:42:11 +02:00
parent 721297bf21
commit af53a96f67

View File

@ -33,9 +33,10 @@ public class DumpSubCmd extends ViaSubCommand {
return "Dump information about your server, this is helpful if you report bugs."; return "Dump information about your server, this is helpful if you report bugs.";
} }
@Override @Override
public boolean execute(final ViaCommandSender sender, String[] args) { public boolean execute(final ViaCommandSender sender, String[] args) {
VersionInfo version = new VersionInfo( final VersionInfo version = new VersionInfo(
System.getProperty("java.version"), System.getProperty("java.version"),
System.getProperty("os.name"), System.getProperty("os.name"),
ProtocolRegistry.SERVER_PROTOCOL, ProtocolRegistry.SERVER_PROTOCOL,
@ -53,58 +54,47 @@ public class DumpSubCmd extends ViaSubCommand {
@Override @Override
public void run() { public void run() {
HttpURLConnection con; HttpURLConnection con = null;
try { try {
con = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection(); con = (HttpURLConnection) new URL("https://dump.viaversion.com/documents").openConnection();
} catch (IOException e) { } catch (IOException e) {
sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); 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 Gist", e); Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to ViaVersion Dump", e);
return; return;
} }
try { try {
con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Content-Type", "text/plain");
con.addRequestProperty("User-Agent", "ViaVersion"); con.addRequestProperty("User-Agent", "ViaVersion/" + version.getPluginVersion());
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setDoOutput(true); con.setDoOutput(true);
OutputStream out = con.getOutputStream(); OutputStream out = con.getOutputStream();
String contents = GsonUtil.getGsonBuilder().setPrettyPrinting().create().toJson(template); out.write(GsonUtil.getGsonBuilder().setPrettyPrinting().create().toJson(template).getBytes(Charset.forName("UTF-8")));
// 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(); out.close();
if (con.getResponseCode() == 429) {
sender.sendMessage(ChatColor.RED + "You can only paste ones every minute to protect our systems.");
return;
}
String rawOutput = CharStreams.toString(new InputStreamReader(con.getInputStream())); String rawOutput = CharStreams.toString(new InputStreamReader(con.getInputStream()));
con.getInputStream().close(); con.getInputStream().close();
JsonObject output = GsonUtil.getGson().fromJson(rawOutput, JsonObject.class); 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")) sender.sendMessage(ChatColor.GREEN + "We've made a dump with useful information, report your issue and provide this url: " + getUrl(output.get("key").getAsString()));
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: " + output.get("html_url").getAsString());
} catch (Exception e) { } catch (Exception e) {
sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); 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 { try {
if (con.getResponseCode() == 403) { if (con.getResponseCode() < 200 || con.getResponseCode() > 400) {
// Ensure 403 is due to rate limit being hit String rawOutput = CharStreams.toString(new InputStreamReader(con.getErrorStream()));
if("0".equals(con.getHeaderField("X-RateLimit-Remaining"))) { con.getErrorStream().close();
Via.getPlatform().getLogger().log(Level.WARNING, "You may only create 60 dumps per hour, please try again later."); Via.getPlatform().getLogger().log(Level.WARNING, "Page returned: " + rawOutput);
return;
}
} }
Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Gist", e);
} catch (IOException e1) { } catch (IOException e1) {
Via.getPlatform().getLogger().log(Level.WARNING, "Failed to capture further info", e1); Via.getPlatform().getLogger().log(Level.WARNING, "Failed to capture further info", e1);
} }
@ -114,4 +104,8 @@ public class DumpSubCmd extends ViaSubCommand {
return true; return true;
} }
private String getUrl(String id) {
return String.format("https://dump.viaversion.com/%s", id);
}
} }