From 409fe5290e1e15f37ada83fe337057b0bf9392d4 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 6 Oct 2024 10:23:44 +0800 Subject: [PATCH] Implementing IsAdministrator for non-Windows --- v2rayN/ServiceLib/Common/Utils.cs | 53 ++++++++++++------- v2rayN/ServiceLib/ServiceLib.csproj | 1 + .../v2rayN.Desktop/Views/MainWindow.axaml.cs | 5 +- v2rayN/v2rayN/Common/WindowsUtils.cs | 26 ++------- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 2 +- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index f642a423..0b0bd448 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -7,6 +7,7 @@ using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; +using System.Security.Principal; using System.Text; using System.Text.RegularExpressions; @@ -701,24 +702,6 @@ namespace ServiceLib.Common return systemHosts; } - public static string GetExeName(string name) - { - if (IsWindows()) - { - return $"{name}.exe"; - } - else - { - return name; - } - } - - public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - - public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux); - - public static bool IsOSX() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX); - #endregion 杂项 #region TempPath @@ -864,5 +847,39 @@ namespace ServiceLib.Common } #endregion TempPath + + #region Platform + + public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + + public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + + public static bool IsOSX() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + + public static string GetExeName(string name) + { + if (IsWindows()) + { + return $"{name}.exe"; + } + else + { + return name; + } + } + + public static bool IsAdministrator() + { + if (IsWindows()) + { + return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); + } + else + { + return Mono.Unix.Native.Syscall.geteuid() == 0; + } + } + + #endregion Platform } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index 28d58e22..8ff61685 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -9,6 +9,7 @@ + diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index 16a879f0..7010f5aa 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -40,7 +40,7 @@ namespace v2rayN.Desktop.Views menuCheckUpdate.Click += MenuCheckUpdate_Click; menuBackupAndRestore.Click += MenuBackupAndRestore_Click; - var IsAdministrator = true;//WindowsUtils.IsAdministrator(); + var IsAdministrator = Utils.IsAdministrator(); MessageBus.Current.Listen(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x)); ViewModel = new MainWindowViewModel(IsAdministrator, UpdateViewHandler); Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel)); @@ -119,14 +119,13 @@ namespace v2rayN.Desktop.Views } }); + this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}"; if (Utils.IsWindows()) { - this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}"; menuGlobalHotkeySetting.IsVisible = false; } else { - this.Title = $"{Utils.GetVersion()}"; menuRebootAsAdmin.IsVisible = false; menuSettingsSetUWP.IsVisible = false; menuGlobalHotkeySetting.IsVisible = false; diff --git a/v2rayN/v2rayN/Common/WindowsUtils.cs b/v2rayN/v2rayN/Common/WindowsUtils.cs index 6e02c995..27a66f63 100644 --- a/v2rayN/v2rayN/Common/WindowsUtils.cs +++ b/v2rayN/v2rayN/Common/WindowsUtils.cs @@ -207,27 +207,7 @@ namespace v2rayN uint attributeSize = (uint)Marshal.SizeOf(attribute); DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, ref attribute, attributeSize); DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, ref attribute, attributeSize); - } - - /// - /// IsAdministrator - /// - /// - public static bool IsAdministrator() - { - try - { - WindowsIdentity current = WindowsIdentity.GetCurrent(); - WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current); - //WindowsBuiltInRole可以枚举出很多权限,例如系统用户、User、Guest等等 - return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - return false; - } - } + } /// /// 开机自动启动 @@ -241,7 +221,7 @@ namespace v2rayN var autoRunName = $"{AutoRunName}_{Utils.GetMD5(Utils.StartupPath())}"; //delete first RegWriteValue(AutoRunRegPath, autoRunName, ""); - if (IsAdministrator()) + if (Utils.IsAdministrator()) { AutoStart(autoRunName, "", ""); } @@ -249,7 +229,7 @@ namespace v2rayN if (run) { string exePath = Utils.GetExePath(); - if (IsAdministrator()) + if (Utils.IsAdministrator()) { AutoStart(autoRunName, exePath, ""); } diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index ea9d0cfb..7bdb8206 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -36,7 +36,7 @@ namespace v2rayN.Views menuCheckUpdate.Click += MenuCheckUpdate_Click; menuBackupAndRestore.Click += MenuBackupAndRestore_Click; - var IsAdministrator = WindowsUtils.IsAdministrator(); + var IsAdministrator = Utils.IsAdministrator(); MessageBus.Current.Listen(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x)); ViewModel = new MainWindowViewModel(IsAdministrator, UpdateViewHandler); Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));