From cc7dece9a48c78556847c3350653354d60618f43 Mon Sep 17 00:00:00 2001 From: UnfamiliarLegacy <74633542+UnfamiliarLegacy@users.noreply.github.com> Date: Fri, 26 Nov 2021 20:07:42 +0100 Subject: [PATCH] Add shutdown hook to ensure system proxy is unregistered --- .../proxy/nitro/http/NitroHttpProxy.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/http/NitroHttpProxy.java b/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/http/NitroHttpProxy.java index 475d912..231abb7 100644 --- a/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/http/NitroHttpProxy.java +++ b/G-Earth/src/main/java/gearth/protocol/connection/proxy/nitro/http/NitroHttpProxy.java @@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class NitroHttpProxy { private static final String ADMIN_WARNING_KEY = "admin_warning_dialog"; + private static final AtomicBoolean SHUTDOWN_HOOK = new AtomicBoolean(); private final Authority authority; private final NitroOsFunctions osFunctions; @@ -89,6 +90,8 @@ public class NitroHttpProxy { } public boolean start() { + setupShutdownHook(); + try { proxyServer = DefaultHttpProxyServer.bootstrap() .withPort(NitroConstants.HTTP_PORT) @@ -133,4 +136,18 @@ public class NitroHttpProxy { proxyServer.stop(); proxyServer = null; } + + /** + * Ensure the system proxy is removed when G-Earth exits. + * Otherwise, users might complain that their browsers / discord stop working when closing G-Earth incorrectly. + */ + private static void setupShutdownHook() { + if (SHUTDOWN_HOOK.get()) { + return; + } + + if (SHUTDOWN_HOOK.compareAndSet(false, true)) { + Runtime.getRuntime().addShutdownHook(new Thread(() -> NitroOsFunctionsFactory.create().unregisterSystemProxy())); + } + } }