Automatically add loopback exemption to Windows

This commit is contained in:
Ender 2020-03-24 02:56:04 -05:00
parent bbf6683bd8
commit ac8d1bf295
2 changed files with 52 additions and 6 deletions

View file

@ -25,18 +25,18 @@
package org.geysermc.platform.standalone; 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.PlatformType;
import org.geysermc.common.bootstrap.IGeyserBootstrap; import org.geysermc.common.bootstrap.IGeyserBootstrap;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.utils.FileUtils; import org.geysermc.connector.utils.FileUtils;
import org.geysermc.platform.standalone.console.GeyserLogger; import org.geysermc.platform.standalone.console.GeyserLogger;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class GeyserBootstrap implements IGeyserBootstrap { public class GeyserBootstrap implements IGeyserBootstrap {
private GeyserConfiguration geyserConfig; private GeyserConfiguration geyserConfig;
private GeyserLogger geyserLogger; private GeyserLogger geyserLogger;
@ -49,7 +49,9 @@ public class GeyserBootstrap implements IGeyserBootstrap {
@Override @Override
public void onEnable() { public void onEnable() {
geyserLogger = new GeyserLogger(); geyserLogger = new GeyserLogger();
Util.checkLoopback(geyserLogger);
try { try {
File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString())); File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
geyserConfig = FileUtils.loadConfig(configFile, GeyserConfiguration.class); geyserConfig = FileUtils.loadConfig(configFile, GeyserConfiguration.class);

View file

@ -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!");
}
}
}
}