1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 18:52:55 +08:00

Perform elevated check asynchronously, use a separate function w/ switch statement

This commit is contained in:
Christine Chen 2021-04-26 22:37:08 -04:00
parent c3bad1d4c5
commit a2723f3f57
3 changed files with 35 additions and 17 deletions

View File

@ -9,7 +9,7 @@ using System.Reflection;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Microsoft.Win32;
using osu.Desktop.Admin;
using osu.Desktop.Security;
using osu.Desktop.Overlays;
using osu.Framework.Platform;
using osu.Game;

View File

@ -11,7 +11,7 @@ using osu.Game.Graphics;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Desktop.Admin
namespace osu.Desktop.Security
{
/// <summary>
/// Checks if the game is running with elevated privileges (as admin in Windows, root in Unix) and displays a warning notification if so.
@ -21,36 +21,54 @@ namespace osu.Desktop.Admin
[Resolved]
protected NotificationOverlay Notifications { get; private set; }
private bool elevated;
protected override void LoadComplete()
{
base.LoadComplete();
bool elevated = false;
if (OperatingSystem.IsWindows())
{
var windowsIdentity = WindowsIdentity.GetCurrent();
var windowsPrincipal = new WindowsPrincipal(windowsIdentity);
elevated = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
else if (RuntimeInfo.IsUnix)
{
elevated = Mono.Unix.Native.Syscall.geteuid() == 0;
}
if (!elevated)
return;
Notifications.Post(new ElevatedPrivilegesNotification());
}
[BackgroundDependencyLoader]
private void load()
{
elevated = isElevated();
}
private bool isElevated()
{
switch (RuntimeInfo.OS)
{
case RuntimeInfo.Platform.Windows:
{
if (!OperatingSystem.IsWindows()) return false;
var windowsIdentity = WindowsIdentity.GetCurrent();
var windowsPrincipal = new WindowsPrincipal(windowsIdentity);
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
case RuntimeInfo.Platform.macOS:
case RuntimeInfo.Platform.Linux:
return Mono.Unix.Native.Syscall.geteuid() == 0;
default:
return false;
}
}
private class ElevatedPrivilegesNotification : SimpleNotification
{
public override bool IsImportant => true;
public ElevatedPrivilegesNotification()
{
Text = $"Running osu! as {(RuntimeInfo.IsUnix ? "root" : "administrator")} does not improve performance and poses a security risk. Please run the game normally.";
Text = $"Running osu! as {(RuntimeInfo.IsUnix ? "root" : "administrator")} does not improve performance and poses a security risk. Please run the game as a normal user.";
}
[BackgroundDependencyLoader]

View File

@ -25,7 +25,7 @@
</ItemGroup>
<ItemGroup Label="Package References">
<PackageReference Include="Microsoft.NETCore.Targets" Version="5.0.0" />
<PackageReference Include="Mono.Posix.NETStandard" Version="5.20.1-preview" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="System.IO.Packaging" Version="5.0.0" />
<PackageReference Include="ppy.squirrel.windows" Version="1.9.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" />