Use ProcessBuilder

This commit is contained in:
Noah van der Aa 2021-10-07 19:45:58 +02:00
parent fae9cdb6d8
commit 8745f0fe3f
No known key found for this signature in database
GPG Key ID: 547D90BC6FF753CF

View File

@ -1,4 +1,4 @@
From 932721f8e83afa12ad09f503b2adace9b619e758 Mon Sep 17 00:00:00 2001 From 763d9ab434a3fcf6dcf0ebd2218e55cbb22333e2 Mon Sep 17 00:00:00 2001
From: Noah van der Aa <ndvdaa@gmail.com> From: Noah van der Aa <ndvdaa@gmail.com>
Date: Thu, 30 Sep 2021 16:59:18 +0200 Date: Thu, 30 Sep 2021 16:59:18 +0200
Subject: [PATCH] Add root/admin user detection Subject: [PATCH] Add root/admin user detection
@ -12,10 +12,10 @@ Co-authored-by: egg82 <eggys82@gmail.com>
diff --git a/api/src/main/java/io/github/waterfallmc/waterfall/utils/ServerEnvironment.java b/api/src/main/java/io/github/waterfallmc/waterfall/utils/ServerEnvironment.java diff --git a/api/src/main/java/io/github/waterfallmc/waterfall/utils/ServerEnvironment.java b/api/src/main/java/io/github/waterfallmc/waterfall/utils/ServerEnvironment.java
new file mode 100644 new file mode 100644
index 00000000..32791b72 index 00000000..1ec9fe05
--- /dev/null --- /dev/null
+++ b/api/src/main/java/io/github/waterfallmc/waterfall/utils/ServerEnvironment.java +++ b/api/src/main/java/io/github/waterfallmc/waterfall/utils/ServerEnvironment.java
@@ -0,0 +1,31 @@ @@ -0,0 +1,34 @@
+package io.github.waterfallmc.waterfall.utils; +package io.github.waterfallmc.waterfall.utils;
+ +
+import java.io.BufferedReader; +import java.io.BufferedReader;
@ -28,8 +28,10 @@ index 00000000..32791b72
+ static { + static {
+ boolean isWindows = System.getProperty("os.name").startsWith("Windows"); + boolean isWindows = System.getProperty("os.name").startsWith("Windows");
+ boolean isAdmin = false; + boolean isAdmin = false;
+ String[] command = isWindows ? new String[]{"reg", "query", "reg query \"HKU\\S-1-5-19\"" } : new String[]{"id", "-u" };
+
+ try { + try {
+ Process process = Runtime.getRuntime().exec(isWindows ? "reg query \"HKU\\S-1-5-19\"" : "id -u " + System.getProperty("user.name")); + Process process = new ProcessBuilder(command).start();
+ process.waitFor(); + process.waitFor();
+ if (isWindows) { + if (isWindows) {
+ isAdmin = process.exitValue() == 0; + isAdmin = process.exitValue() == 0;
@ -40,6 +42,7 @@ index 00000000..32791b72
+ } + }
+ } catch (InterruptedException | IOException ignored) { + } catch (InterruptedException | IOException ignored) {
+ } + }
+
+ RUNNING_AS_ROOT_OR_ADMIN = isAdmin; + RUNNING_AS_ROOT_OR_ADMIN = isAdmin;
+ } + }
+ +