mirror of https://github.com/2dust/v2rayN
Implementing IsAdministrator for non-Windows
parent
75d86cf883
commit
409fe5290e
|
@ -7,6 +7,7 @@ using System.Net.Sockets;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using System.Security.Principal;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
@ -701,24 +702,6 @@ namespace ServiceLib.Common
|
||||||
return systemHosts;
|
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 杂项
|
#endregion 杂项
|
||||||
|
|
||||||
#region TempPath
|
#region TempPath
|
||||||
|
@ -864,5 +847,39 @@ namespace ServiceLib.Common
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion TempPath
|
#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
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Downloader" Version="3.2.0" />
|
<PackageReference Include="Downloader" Version="3.2.0" />
|
||||||
|
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
|
||||||
<PackageReference Include="ReactiveUI" Version="20.1.63" />
|
<PackageReference Include="ReactiveUI" Version="20.1.63" />
|
||||||
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
|
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
|
||||||
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
|
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
|
||||||
|
|
|
@ -40,7 +40,7 @@ namespace v2rayN.Desktop.Views
|
||||||
menuCheckUpdate.Click += MenuCheckUpdate_Click;
|
menuCheckUpdate.Click += MenuCheckUpdate_Click;
|
||||||
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;
|
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;
|
||||||
|
|
||||||
var IsAdministrator = true;//WindowsUtils.IsAdministrator();
|
var IsAdministrator = Utils.IsAdministrator();
|
||||||
MessageBus.Current.Listen<string>(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x));
|
MessageBus.Current.Listen<string>(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x));
|
||||||
ViewModel = new MainWindowViewModel(IsAdministrator, UpdateViewHandler);
|
ViewModel = new MainWindowViewModel(IsAdministrator, UpdateViewHandler);
|
||||||
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));
|
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())
|
if (Utils.IsWindows())
|
||||||
{
|
{
|
||||||
this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
|
|
||||||
menuGlobalHotkeySetting.IsVisible = false;
|
menuGlobalHotkeySetting.IsVisible = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.Title = $"{Utils.GetVersion()}";
|
|
||||||
menuRebootAsAdmin.IsVisible = false;
|
menuRebootAsAdmin.IsVisible = false;
|
||||||
menuSettingsSetUWP.IsVisible = false;
|
menuSettingsSetUWP.IsVisible = false;
|
||||||
menuGlobalHotkeySetting.IsVisible = false;
|
menuGlobalHotkeySetting.IsVisible = false;
|
||||||
|
|
|
@ -209,26 +209,6 @@ namespace v2rayN
|
||||||
DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, ref attribute, attributeSize);
|
DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, ref attribute, attributeSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// IsAdministrator
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 开机自动启动
|
/// 开机自动启动
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -241,7 +221,7 @@ namespace v2rayN
|
||||||
var autoRunName = $"{AutoRunName}_{Utils.GetMD5(Utils.StartupPath())}";
|
var autoRunName = $"{AutoRunName}_{Utils.GetMD5(Utils.StartupPath())}";
|
||||||
//delete first
|
//delete first
|
||||||
RegWriteValue(AutoRunRegPath, autoRunName, "");
|
RegWriteValue(AutoRunRegPath, autoRunName, "");
|
||||||
if (IsAdministrator())
|
if (Utils.IsAdministrator())
|
||||||
{
|
{
|
||||||
AutoStart(autoRunName, "", "");
|
AutoStart(autoRunName, "", "");
|
||||||
}
|
}
|
||||||
|
@ -249,7 +229,7 @@ namespace v2rayN
|
||||||
if (run)
|
if (run)
|
||||||
{
|
{
|
||||||
string exePath = Utils.GetExePath();
|
string exePath = Utils.GetExePath();
|
||||||
if (IsAdministrator())
|
if (Utils.IsAdministrator())
|
||||||
{
|
{
|
||||||
AutoStart(autoRunName, exePath, "");
|
AutoStart(autoRunName, exePath, "");
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace v2rayN.Views
|
||||||
menuCheckUpdate.Click += MenuCheckUpdate_Click;
|
menuCheckUpdate.Click += MenuCheckUpdate_Click;
|
||||||
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;
|
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;
|
||||||
|
|
||||||
var IsAdministrator = WindowsUtils.IsAdministrator();
|
var IsAdministrator = Utils.IsAdministrator();
|
||||||
MessageBus.Current.Listen<string>(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x));
|
MessageBus.Current.Listen<string>(Global.CommandSendSnackMsg).Subscribe(x => DelegateSnackMsg(x));
|
||||||
ViewModel = new MainWindowViewModel(IsAdministrator, UpdateViewHandler);
|
ViewModel = new MainWindowViewModel(IsAdministrator, UpdateViewHandler);
|
||||||
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));
|
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));
|
||||||
|
|
Loading…
Reference in New Issue