Moved reporting system to CoreBot; JSON-base reports

This commit is contained in:
Anuken 2018-08-29 22:54:15 -04:00
parent 9a41399178
commit 28b9de2e7a
7 changed files with 41 additions and 123 deletions

View file

@ -40,7 +40,7 @@ public class Vars{
//discord group URL
public static final String discordURL = "https://discord.gg/BKADYds";
public static final String releasesURL = "https://api.github.com/repos/Anuken/Mindustry/releases";
public static final String crashReportURL = "http://localhost:8080/report";
public static final String crashReportURL = "http:/mindustry.us.to/report";
public static final int maxTextLength = 150;
public static final int maxNameLength = 40;
public static final float itemSize = 5f;

View file

@ -51,6 +51,7 @@ public class Control extends Module{
private Throwable error;
public Control(){
//TODO remove; only for testing
if(true) throw new RuntimeException("This should crash.");
saves = new Saves();

View file

@ -1,21 +1,26 @@
package io.anuke.mindustry.desktop;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonValue.ValueType;
import com.badlogic.gdx.utils.JsonWriter.OutputType;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.game.Version;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.core.Settings;
import io.anuke.ucore.util.Strings;
import io.anuke.ucore.util.Log;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.PrintWriter;
import java.io.StringWriter;
public class CrashHandler{
public static void handle(Throwable e){
e.printStackTrace();
//don't create crash logs for me (anuke), as it's expected
//also don't create logs for custom builds
if(System.getProperty("user.name").equals("anuke") || Version.build == -1) return;
boolean netActive = false, netServer = false;
//attempt to close connections, if applicable
@ -27,47 +32,38 @@ public class CrashHandler{
p.printStackTrace();
}
//don't create crash logs for me (anuke), as it's expected
//if(System.getProperty("user.name").equals("anuke")) return;
JsonValue value = new JsonValue(ValueType.object);
String header = "--CRASH REPORT--\n";
boolean fn = netActive, fs = netServer;
//add all relevant info, ignoring exceptions
ex(() -> value.addChild("build", new JsonValue(Version.build)));
ex(() -> value.addChild("net", new JsonValue(fn)));
ex(() -> value.addChild("server", new JsonValue(fs)));
ex(() -> value.addChild("os", new JsonValue(System.getProperty("os.name"))));
ex(() -> value.addChild("multithreading", new JsonValue(Settings.getBool("multithread"))));
ex(() -> value.addChild("trace", new JsonValue(parseException(e))));
Log.info("Sending crash report.");
//post to crash report URL
Net.http(Vars.crashReportURL, "POST", value.toJson(OutputType.json), r -> System.exit(1), t -> System.exit(1));
//sleep forever
try{ Thread.sleep(Long.MAX_VALUE); }catch(InterruptedException ignored){}
}
private static String parseException(Throwable e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
private static void ex(Runnable r){
try{
header += "--GAME INFO--\n";
header += "Build: " + Version.build + "\n";
header += "Net Active: " + netActive + "\n";
header += "Net Server: " + netServer + "\n";
header += "OS: " + System.getProperty("os.name") + "\n";
header += "Multithreading: " + Settings.getBool("multithread") + "\n----\n";
}catch(Throwable e4){
header += "--error getting additional info--\n";
e4.printStackTrace();
}
//parse exception
String result = header + Strings.parseFullException(e);
boolean failed = false;
String filename = "";
Net.http(Vars.crashReportURL, "POST", result, r -> {}, Throwable::printStackTrace);
//try to write it
try{
filename = "crash-report-" + new SimpleDateFormat("dd-MM-yy h.mm.ss").format(new Date()) + ".txt";
Files.write(Paths.get(System.getProperty("user.home"), filename), result.getBytes());
}catch(Throwable i){
i.printStackTrace();
failed = true;
}
try{
javax.swing.JOptionPane.showMessageDialog(null, "An error has occured: \n" + result + "\n\n" +
(!failed ? "A crash report has been written to " + Paths.get(System.getProperty("user.home"), filename).toFile().getAbsolutePath() + ".\nPlease send this file to the developer!"
: "Failed to generate crash report.\nPlease send an image of this crash log to the developer!"));
}catch(Throwable i){
i.printStackTrace();
//what now?
r.run();
}catch(Throwable t){
t.printStackTrace();
}
}
}

View file

@ -1,26 +0,0 @@
apply plugin: "java"
sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/" ]
project.ext.mainClassName = "io.anuke.mindustry.reporter.Launcher"
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
ignoreExitValue = true
}
task dist(type: Jar) {
dependsOn classes
from files(sourceSets.main.output.classesDirs)
from files(sourceSets.main.output.resourcesDir)
from {configurations.compile.collect {zipTree(it)}}
writeVersion()
manifest {
attributes 'Main-Class': project.mainClassName
}
}

View file

@ -1,41 +0,0 @@
package io.anuke.mindustry.reporter;
import com.sun.net.httpserver.HttpServer;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
public class Launcher{
private static final long REQUEST_TIME = 1000 * 6;
public static void main(String[] args) throws IOException{
ReportHandler handler = new ReportHandler();
HashMap<String, Long> rateLimit = new HashMap<>();
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/report", t -> {
String key = t.getRemoteAddress().getAddress().getHostName();
if(rateLimit.get(key) != null && (currentTimeMillis() - rateLimit.get(key)) < REQUEST_TIME){
rateLimit.put(key, currentTimeMillis());
out.println("connection " + key + " is being rate limited");
return;
}
rateLimit.put(key, currentTimeMillis());
byte[] bytes = new byte[t.getRequestBody().available()];
new DataInputStream(t.getRequestBody()).readFully(bytes);
handler.handle(new String(bytes));
t.sendResponseHeaders(200, 0);
});
server.setExecutor(null);
server.start();
out.println("server up");
}
}

View file

@ -1,10 +0,0 @@
package io.anuke.mindustry.reporter;
import static java.lang.System.out;
public class ReportHandler{
public void handle(String text){
out.println("recieved text: " + text);
}
}

View file

@ -1,2 +0,0 @@
./gradlew reporter:dist
java -jar reporter/build/libs/reporter-release.jar