-
Notifications
You must be signed in to change notification settings - Fork 780
Admin checker #4393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShulkerSakura
wants to merge
16
commits into
HMCL-dev:main
Choose a base branch
from
ShulkerSakura:AdminChecker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Admin checker #4393
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
70461db
AssetsCleaner
ShulkerSakura 669dd6e
Style Check
ShulkerSakura eeb4303
Style Check
ShulkerSakura 5146f4f
Style Check
ShulkerSakura fa48ad7
添加indexes清理功能
ShulkerSakura 2317b69
添加indexes清理功能
ShulkerSakura 3d12c14
Merge branch 'HMCL-dev:main' into AssetsCleaner
ShulkerSakura 24e94a2
Merge branch 'HMCL-dev:main' into AssetsCleaner
ShulkerSakura bbc36ed
Added username length detection
ShulkerSakura 447583d
Delete test code
ShulkerSakura 052a950
Deleted testing asstes cleaner module
ShulkerSakura f77e47e
update
Glavo 90558eb
update
Glavo ca84140
Merge branch 'HMCL-dev:main' into 16CharacterDetection
ShulkerSakura cc17550
Add Admin Checker
ShulkerSakura c3dabfa
Style Check
ShulkerSakura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
HMCL/src/main/java/org/jackhuang/hmcl/util/AdminChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jackhuang.hmcl.util; | ||
|
||
import org.jackhuang.hmcl.util.platform.OperatingSystem; | ||
import org.jackhuang.hmcl.util.platform.windows.WinReg; | ||
|
||
public class AdminChecker { | ||
|
||
private AdminChecker() { | ||
} | ||
|
||
public static boolean isAdmin() { | ||
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { | ||
return isWindowsAdmin(); | ||
} else if (OperatingSystem.CURRENT_OS.isLinuxOrBSD() && OperatingSystem.CURRENT_OS == OperatingSystem.MACOS) { | ||
return isUnixRoot(); | ||
} else { | ||
// 未知系统,保守返回 false | ||
System.err.println("Unknown OS: " + OperatingSystem.CURRENT_OS); | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean isWindowsAdmin() { | ||
WinReg reg = WinReg.INSTANCE; | ||
try { | ||
return reg.exists(WinReg.HKEY.HKEY_USERS, "S-1-5-19"); | ||
} catch (Throwable t) { | ||
// 捕获 AccessException、JNA 错误等 | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean isUnixRoot() { | ||
try { | ||
ProcessBuilder pb = new ProcessBuilder("id", "-u"); | ||
Process process = pb.start(); | ||
java.util.Scanner scanner = new java.util.Scanner(process.getInputStream()); | ||
String uid = scanner.hasNext() ? scanner.next() : ""; | ||
scanner.close(); | ||
process.waitFor(); | ||
return "0".equals(uid.trim()); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这会在 HMCL 的关键启动路径上阻塞地等待外部程序,会严重拖慢启动速度。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此需求由报错群成员提出,用于判断一些运行问题权限问题是否由用户使用了管理员权限但又不承认,如果你认为实现的不妥可以讲讲我应该怎么做或给出你的方案