Skip to content

Commit

Permalink
Bug fix for IsAdministrator
Browse files Browse the repository at this point in the history
This reverts commit 7618f9f.
  • Loading branch information
2dust committed Oct 13, 2024
1 parent e530789 commit 4eb443e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
14 changes: 14 additions & 0 deletions v2rayN/ServiceLib/Handler/AppHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
{
public sealed class AppHandler
{
#region Property

private static readonly Lazy<AppHandler> _instance = new(() => new());
private Config _config;
private int? _statePort;
private int? _statePort2;
private Job? _processJob;
private bool? _isAdministrator;
public static AppHandler Instance => _instance.Value;
public Config Config => _config;

Expand All @@ -28,6 +31,17 @@ public int StatePort2
}
}

public bool IsAdministrator
{
get
{
_isAdministrator ??= Utils.IsAdministrator();
return _isAdministrator.Value;
}
}

#endregion Property

#region Init

public AppHandler()
Expand Down
7 changes: 2 additions & 5 deletions v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace ServiceLib.ViewModels
{
public class StatusBarViewModel : MyReactiveObject
{
private bool _isAdministrator { get; set; }

#region ObservableCollection

private IObservableCollection<RoutingItem> _routingItems = new ObservableCollectionExtended<RoutingItem>();
Expand Down Expand Up @@ -119,8 +117,7 @@ public StatusBarViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
SelectedRouting = new();
SelectedServer = new();

_isAdministrator = Utils.IsAdministrator();
if (_config.tunModeItem.enableTun && _isAdministrator)
if (_config.tunModeItem.enableTun && AppHandler.Instance.IsAdministrator)
{
EnableTun = true;
}
Expand Down Expand Up @@ -414,7 +411,7 @@ private void DoEnableTun(bool c)
{
_config.tunModeItem.enableTun = EnableTun;
// When running as a non-administrator, reboot to administrator mode
if (EnableTun && !_isAdministrator)
if (EnableTun && !AppHandler.Instance.IsAdministrator)
{
_config.tunModeItem.enableTun = false;
Locator.Current.GetService<MainWindowViewModel>()?.RebootAsAdmin();
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public MainWindow()
}
});

this.Title = $"{Utils.GetVersion()} - {(Utils.IsAdministrator() ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
this.Title = $"{Utils.GetVersion()} - {(AppHandler.Instance.IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
if (Utils.IsWindows())
{
menuGlobalHotkeySetting.IsVisible = false;
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public MainWindow()
}
});

this.Title = $"{Utils.GetVersion()} - {(Utils.IsAdministrator() ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
this.Title = $"{Utils.GetVersion()} - {(AppHandler.Instance.IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";

if (!_config.guiItem.enableHWA)
{
Expand Down

0 comments on commit 4eb443e

Please sign in to comment.