mirror of
https://github.com/ppy/osu.git
synced 2025-02-16 01:42:54 +08:00
Merge pull request #12589 from Cublibre/admin-notification
Send a notification if game is run as admin/root
This commit is contained in:
commit
aaf65b64f5
@ -9,6 +9,7 @@ using System.Reflection;
|
|||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
|
using osu.Desktop.Security;
|
||||||
using osu.Desktop.Overlays;
|
using osu.Desktop.Overlays;
|
||||||
using osu.Framework.Platform;
|
using osu.Framework.Platform;
|
||||||
using osu.Game;
|
using osu.Game;
|
||||||
@ -113,6 +114,8 @@ namespace osu.Desktop
|
|||||||
|
|
||||||
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
|
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
|
||||||
LoadComponentAsync(new GameplayWinKeyBlocker(), Add);
|
LoadComponentAsync(new GameplayWinKeyBlocker(), Add);
|
||||||
|
|
||||||
|
LoadComponentAsync(new ElevatedPrivilegesChecker(), Add);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ScreenChanged(IScreen lastScreen, IScreen newScreen)
|
protected override void ScreenChanged(IScreen lastScreen, IScreen newScreen)
|
||||||
|
83
osu.Desktop/Security/ElevatedPrivilegesChecker.cs
Normal file
83
osu.Desktop/Security/ElevatedPrivilegesChecker.cs
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Security.Principal;
|
||||||
|
using osu.Framework;
|
||||||
|
using osu.Framework.Allocation;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Sprites;
|
||||||
|
using osu.Game.Graphics;
|
||||||
|
using osu.Game.Overlays;
|
||||||
|
using osu.Game.Overlays.Notifications;
|
||||||
|
|
||||||
|
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.
|
||||||
|
/// </summary>
|
||||||
|
public class ElevatedPrivilegesChecker : Component
|
||||||
|
{
|
||||||
|
[Resolved]
|
||||||
|
private NotificationOverlay notifications { get; set; }
|
||||||
|
|
||||||
|
private bool elevated;
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load()
|
||||||
|
{
|
||||||
|
elevated = checkElevated();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void LoadComplete()
|
||||||
|
{
|
||||||
|
base.LoadComplete();
|
||||||
|
|
||||||
|
if (elevated)
|
||||||
|
notifications.Post(new ElevatedPrivilegesNotification());
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool checkElevated()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
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, may break integrations and poses a security risk. Please run the game as a normal user.";
|
||||||
|
}
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load(OsuColour colours, NotificationOverlay notificationOverlay)
|
||||||
|
{
|
||||||
|
Icon = FontAwesome.Solid.ShieldAlt;
|
||||||
|
IconBackgound.Colour = colours.YellowDark;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -25,6 +25,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup Label="Package References">
|
<ItemGroup Label="Package References">
|
||||||
<PackageReference Include="Microsoft.NETCore.Targets" Version="5.0.0" />
|
<PackageReference Include="Microsoft.NETCore.Targets" Version="5.0.0" />
|
||||||
|
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
|
||||||
<PackageReference Include="System.IO.Packaging" Version="5.0.0" />
|
<PackageReference Include="System.IO.Packaging" Version="5.0.0" />
|
||||||
<PackageReference Include="ppy.squirrel.windows" Version="1.9.0.5" />
|
<PackageReference Include="ppy.squirrel.windows" Version="1.9.0.5" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" />
|
||||||
|
Loading…
Reference in New Issue
Block a user