Initial proof-of-concept for crash report server

This commit is contained in:
Anuken
2018-08-29 22:09:55 -04:00
parent db47b9a877
commit 9a41399178
10 changed files with 95 additions and 4 deletions
+26
View File
@@ -0,0 +1,26 @@
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
}
}
@@ -0,0 +1,41 @@
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");
}
}
@@ -0,0 +1,10 @@
package io.anuke.mindustry.reporter;
import static java.lang.System.out;
public class ReportHandler{
public void handle(String text){
out.println("recieved text: " + text);
}
}