Skip to content

Commit

Permalink
[FIX] UnsafeCommandPack, кое-что дофикшено в Sentry.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaxar163 committed Jun 20, 2020
1 parent c587f70 commit 995d358
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package pro.gravit.launchermodules.sentryl;

import com.google.gson.annotations.SerializedName;
import pro.gravit.launcher.LauncherInject;

public class Config {
@SerializedName("dsn")
@LauncherInject("modules.sentry.dsn")
public String dsn = "YOUR_DSN";
@SerializedName("captureAll")
@LauncherInject("modules.sentry.captureAll")
public boolean captureAll = false;

@LauncherInject("modules.sentry.setThreadExcpectionHandler")
public boolean setThreadExcpectionHandler = false;
public static Object getDefault() {
return new Config();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pro.gravit.launchermodules.sentryl;

import io.sentry.Sentry;
import pro.gravit.launcher.LauncherInject;
import io.sentry.event.Event;
import io.sentry.event.EventBuilder;
import io.sentry.event.interfaces.ExceptionInterface;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
Expand All @@ -19,21 +21,55 @@ public ModuleImpl() {

@Override
public void init(LauncherInitContext initContext) {

registerEvent(this::clientInit, PreConfigPhase.class);
registerEvent(this::preInit, PreConfigPhase.class);
}
@LauncherInject("modules.sentry.dsn")
public String dsn = "YOUR_DSN";
@LauncherInject("modules.sentry.captureAll")
public boolean captureAll = false;
private void clientInit(PreConfigPhase phase) {

private void preInit(PreConfigPhase phase) {
Config c = new Config();
try {
Sentry.init(dsn);
if (captureAll)
Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Sentry.init(c.dsn);
// this code will never throw anything :)
LogHelper.addExcCallback(Sentry::capture);
if (c.captureAll)
LogHelper.addOutput(Sentry::capture, LogHelper.OutputTypes.PLAIN);
if (c.setThreadExcpectionHandler)
Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler(defaultHandler));
} catch (Throwable e) {
LogHelper.error(e);
}
}

}

class CustomUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private final Thread.UncaughtExceptionHandler defaultExceptionHandler;

CustomUncaughtExceptionHandler(Thread.UncaughtExceptionHandler defaultExceptionHandler) {
this.defaultExceptionHandler = defaultExceptionHandler;
}

@Override
public void uncaughtException(Thread thread, Throwable thrown) {
if (thrown == null) return;
EventBuilder eventBuilder = new EventBuilder()
.withMessage(thrown.getMessage())
.withLevel(Event.Level.FATAL)
.withExtra("thread", thread != null ? thread.getName() : "ERR_nullThreadName")
.withSentryInterface(new ExceptionInterface(thrown));

try {
Sentry.capture(eventBuilder);
} catch (Exception e) {
LogHelper.error(e);
}

// taken from ThreadGroup#uncaughtException
if (defaultExceptionHandler != null) {
// call the original handler
defaultExceptionHandler.uncaughtException(thread, thrown);
} else if (!(thrown instanceof ThreadDeath)) {
System.err.print("Exception in thread \"" + thread.getName() + "\" ");
thrown.printStackTrace(System.err);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.sentry.Sentry;
import io.sentry.event.Event;
import io.sentry.event.EventBuilder;
import io.sentry.event.interfaces.ExceptionInterface;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
Expand Down Expand Up @@ -39,17 +42,55 @@ public void preInit(PreConfigPhase phase) {
c = new Config();
IOHelper.write(p, IOHelper.encode(GSON_P.toJson(c, Config.class)));
}
Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Sentry.init(c.dsn);
// this code will never throw anything :)
LogHelper.addExcCallback(Sentry::capture);
if (c.captureAll)
LogHelper.addOutput(Sentry::capture, LogHelper.OutputTypes.PLAIN);
if (c.setThreadExcpectionHandler)
Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler(defaultHandler));
} catch (Throwable e) {
LogHelper.error(e);
}
}

public static class Config {
String dsn = "YOUR_DSN";
boolean captureAll = false;
public String dsn = "YOUR_DSN";
public boolean captureAll = false;
public boolean setThreadExcpectionHandler = false;
}
}

class CustomUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private final Thread.UncaughtExceptionHandler defaultExceptionHandler;

CustomUncaughtExceptionHandler(Thread.UncaughtExceptionHandler defaultExceptionHandler) {
this.defaultExceptionHandler = defaultExceptionHandler;
}

@Override
public void uncaughtException(Thread thread, Throwable thrown) {
if (thrown == null) return;
EventBuilder eventBuilder = new EventBuilder()
.withMessage(thrown.getMessage())
.withLevel(Event.Level.FATAL)
.withExtra("thread", thread != null ? thread.getName() : "ERR_nullThreadName")
.withSentryInterface(new ExceptionInterface(thrown));

try {
Sentry.capture(eventBuilder);
} catch (Exception e) {
LogHelper.error(e);
}

// taken from ThreadGroup#uncaughtException
if (defaultExceptionHandler != null) {
// call the original handler
defaultExceptionHandler.uncaughtException(thread, thrown);
} else if (!(thrown instanceof ThreadDeath)) {
System.err.print("Exception in thread \"" + thread.getName() + "\" ");
thrown.printStackTrace(System.err);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public void invoke(String... args) throws Exception {
fetchNatives(clientDir.resolve("natives"), info.natives);
LogHelper.subInfo("Natives downloaded!");
f.get();
e.awaitTermination(4, TimeUnit.HOURS);
e.shutdown();
e.awaitTermination(4, TimeUnit.HOURS);
e.shutdownNow();
// Finished
LogHelper.subInfo("Client downloaded!");
server.syncUpdatesDir(Collections.singleton(dirName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public void invoke(String... args) throws Exception {
CompletableFuture.allOf(d.runDownloadList(d.sortFiles(applies, 4), AssetDownloader.getBase(), assetDir, e)).thenAccept((v) -> {
LogHelper.subInfo("Asset successfully downloaded: '%s'", dirName);
}).get();
e.awaitTermination(4, TimeUnit.HOURS);
e.shutdown();
e.awaitTermination(4, TimeUnit.HOURS);
e.shutdownNow();
// Finished
server.syncUpdatesDir(Collections.singleton(dirName));
}
Expand Down

0 comments on commit 995d358

Please sign in to comment.