From ac8d1bf295c875fc17ea418734ca37bbb277b8c4 Mon Sep 17 00:00:00 2001 From: Ender <33337309+e3ndr@users.noreply.github.com> Date: Tue, 24 Mar 2020 02:56:04 -0500 Subject: [PATCH] Automatically add loopback exemption to Windows --- .../platform/standalone/GeyserBootstrap.java | 14 +++--- .../geysermc/platform/standalone/Util.java | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/Util.java diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java index 1ba111e7..0b70df58 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/GeyserBootstrap.java @@ -25,18 +25,18 @@ package org.geysermc.platform.standalone; +import java.io.File; +import java.io.IOException; +import java.util.UUID; + import org.geysermc.common.PlatformType; import org.geysermc.common.bootstrap.IGeyserBootstrap; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.utils.FileUtils; import org.geysermc.platform.standalone.console.GeyserLogger; -import java.io.File; -import java.io.IOException; -import java.util.UUID; - public class GeyserBootstrap implements IGeyserBootstrap { - + private GeyserConfiguration geyserConfig; private GeyserLogger geyserLogger; @@ -49,7 +49,9 @@ public class GeyserBootstrap implements IGeyserBootstrap { @Override public void onEnable() { geyserLogger = new GeyserLogger(); - + + Util.checkLoopback(geyserLogger); + try { File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString())); geyserConfig = FileUtils.loadConfig(configFile, GeyserConfiguration.class); diff --git a/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/Util.java b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/Util.java new file mode 100644 index 00000000..753b3b24 --- /dev/null +++ b/bootstrap/standalone/src/main/java/org/geysermc/platform/standalone/Util.java @@ -0,0 +1,44 @@ +package org.geysermc.platform.standalone; + +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.OpenOption; +import java.nio.file.Paths; + +import org.geysermc.platform.standalone.console.GeyserLogger; + +public class Util { + private static final String checkExemption = "powershell -Command \"CheckNetIsolation LoopbackExempt -s\""; // Java's Exec feature runs as CMD, NetIsolation is only accessible from PowerShell. + private static final String loopbackCommand = "powershell -Command \"CheckNetIsolation LoopbackExempt -a -n='Microsoft.MinecraftUWP_8wekyb3d8bbwe'\""; + private static final String startScript = "powershell -Command \"Start-Process 'cmd' -ArgumentList /c,%temp%/loopback_minecraft.bat -Verb runAs\""; + + public static void checkLoopback(GeyserLogger geyserLogger) { + if (System.getProperty("os.name").equalsIgnoreCase("Windows 10")) { + try { + Process process = Runtime.getRuntime().exec(checkExemption); + InputStream is = process.getInputStream(); + StringBuilder sb = new StringBuilder(); + + while (process.isAlive()) { + if (is.available() != 0) { + sb.append((char) is.read()); + } + } + + String result = sb.toString(); + + if (!result.contains("minecraftuwp")) { + Files.write(Paths.get(System.getenv("temp") + "/loopback_minecraft.bat"), loopbackCommand.getBytes(), new OpenOption[0]); + process = Runtime.getRuntime().exec(startScript); + + geyserLogger.warning("Added loopback exemption to Windows!"); + } + } catch (Exception e) { + e.printStackTrace(); + + geyserLogger.error("Couldn't auto add loopback exemption to Windows!"); + } + } + } + +}