v2rayN/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

601 lines
18 KiB
C#
Raw Normal View History

2025-01-30 09:10:05 +00:00
using System.Reactive;
2025-08-30 10:01:01 +00:00
using System.Reactive.Concurrency;
2023-01-01 11:42:01 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
namespace ServiceLib.ViewModels;
public class MainWindowViewModel : MyReactiveObject
2023-01-01 11:42:01 +00:00
{
#region Menu
2023-01-01 11:42:01 +00:00
//servers
public ReactiveCommand<Unit, Unit> AddVmessServerCmd { get; }
2023-04-14 12:49:36 +00:00
public ReactiveCommand<Unit, Unit> AddVlessServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddShadowsocksServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddSocksServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddHttpServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddTrojanServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddHysteria2ServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddTuicServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddWireguardServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddAnytlsServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddCustomServerCmd { get; }
2025-09-10 15:44:42 +00:00
public ReactiveCommand<Unit, Unit> AddPolicyGroupServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddProxyChainServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaClipboardCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaScanCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaImageCmd { get; }
2023-04-14 12:49:36 +00:00
//Subscription
public ReactiveCommand<Unit, Unit> SubSettingCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> SubUpdateCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateViaProxyCmd { get; }
public ReactiveCommand<Unit, Unit> SubGroupUpdateCmd { get; }
public ReactiveCommand<Unit, Unit> SubGroupUpdateViaProxyCmd { get; }
2023-04-14 12:49:36 +00:00
//Setting
public ReactiveCommand<Unit, Unit> OptionSettingCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> RoutingSettingCmd { get; }
public ReactiveCommand<Unit, Unit> DNSSettingCmd { get; }
public ReactiveCommand<Unit, Unit> FullConfigTemplateCmd { get; }
public ReactiveCommand<Unit, Unit> GlobalHotkeySettingCmd { get; }
public ReactiveCommand<Unit, Unit> RebootAsAdminCmd { get; }
public ReactiveCommand<Unit, Unit> ClearServerStatisticsCmd { get; }
public ReactiveCommand<Unit, Unit> OpenTheFileLocationCmd { get; }
2024-10-18 09:35:32 +00:00
//Presets
public ReactiveCommand<Unit, Unit> RegionalPresetDefaultCmd { get; }
public ReactiveCommand<Unit, Unit> RegionalPresetRussiaCmd { get; }
public ReactiveCommand<Unit, Unit> RegionalPresetIranCmd { get; }
2023-04-14 12:49:36 +00:00
public ReactiveCommand<Unit, Unit> ReloadCmd { get; }
2023-01-01 11:42:01 +00:00
[Reactive]
public bool BlReloadEnabled { get; set; }
[Reactive]
public bool ShowClashUI { get; set; }
2024-07-19 02:51:14 +00:00
[Reactive]
public int TabMainSelectedIndex { get; set; }
2023-01-01 11:42:01 +00:00
#endregion Menu
2025-01-14 09:05:13 +00:00
private bool _hasNextReloadJob = false;
2023-01-01 11:42:01 +00:00
#region Init
2023-01-01 11:42:01 +00:00
public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
{
2025-08-17 09:31:55 +00:00
_config = AppManager.Instance.Config;
_updateView = updateView;
2023-01-01 11:42:01 +00:00
#region WhenAnyValue && ReactiveCommand
2023-01-01 11:42:01 +00:00
//servers
AddVmessServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.VMess);
});
AddVlessServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.VLESS);
});
AddShadowsocksServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.Shadowsocks);
});
AddSocksServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.SOCKS);
});
AddHttpServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.HTTP);
});
AddTrojanServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.Trojan);
});
AddHysteria2ServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.Hysteria2);
});
AddTuicServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.TUIC);
});
AddWireguardServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.WireGuard);
});
AddAnytlsServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.Anytls);
});
AddCustomServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.Custom);
});
2025-09-10 15:44:42 +00:00
AddPolicyGroupServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.PolicyGroup);
});
AddProxyChainServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.ProxyChain);
});
AddServerViaClipboardCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerViaClipboardAsync(null);
});
AddServerViaScanCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerViaScanAsync();
});
AddServerViaImageCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerViaImageAsync();
});
2024-07-18 09:39:11 +00:00
//Subscription
SubSettingCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SubSettingAsync();
});
2023-01-01 11:42:01 +00:00
SubUpdateCmd = ReactiveCommand.CreateFromTask(async () =>
{
await UpdateSubscriptionProcess("", false);
});
SubUpdateViaProxyCmd = ReactiveCommand.CreateFromTask(async () =>
{
await UpdateSubscriptionProcess("", true);
});
SubGroupUpdateCmd = ReactiveCommand.CreateFromTask(async () =>
{
await UpdateSubscriptionProcess(_config.SubIndexId, false);
});
SubGroupUpdateViaProxyCmd = ReactiveCommand.CreateFromTask(async () =>
{
await UpdateSubscriptionProcess(_config.SubIndexId, true);
});
2023-01-01 11:42:01 +00:00
//Setting
OptionSettingCmd = ReactiveCommand.CreateFromTask(async () =>
{
await OptionSettingAsync();
});
RoutingSettingCmd = ReactiveCommand.CreateFromTask(async () =>
{
await RoutingSettingAsync();
});
DNSSettingCmd = ReactiveCommand.CreateFromTask(async () =>
{
await DNSSettingAsync();
});
FullConfigTemplateCmd = ReactiveCommand.CreateFromTask(async () =>
{
await FullConfigTemplateAsync();
});
GlobalHotkeySettingCmd = ReactiveCommand.CreateFromTask(async () =>
{
if (await _updateView?.Invoke(EViewAction.GlobalHotkeySettingWindow, null) == true)
2023-01-01 11:42:01 +00:00
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.OperationSuccess);
}
});
RebootAsAdminCmd = ReactiveCommand.CreateFromTask(async () =>
{
await RebootAsAdmin();
});
ClearServerStatisticsCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ClearServerStatistics();
});
OpenTheFileLocationCmd = ReactiveCommand.CreateFromTask(async () =>
{
await OpenTheFileLocation();
});
2023-01-01 11:42:01 +00:00
ReloadCmd = ReactiveCommand.CreateFromTask(async () =>
{
await Reload();
});
RegionalPresetDefaultCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ApplyRegionalPreset(EPresetType.Default);
});
RegionalPresetRussiaCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ApplyRegionalPreset(EPresetType.Russia);
});
RegionalPresetIranCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ApplyRegionalPreset(EPresetType.Iran);
});
2024-02-01 04:44:47 +00:00
#endregion WhenAnyValue && ReactiveCommand
2023-01-01 11:42:01 +00:00
_ = Init();
}
2024-10-21 05:46:13 +00:00
private async Task Init()
{
_config.UiItem.ShowInTaskbar = true;
2023-01-01 11:42:01 +00:00
await ConfigHandler.InitBuiltinRouting(_config);
await ConfigHandler.InitBuiltinDNS(_config);
await ConfigHandler.InitBuiltinFullConfigTemplate(_config);
2025-08-17 09:31:55 +00:00
await ProfileExManager.Instance.Init();
2025-09-06 12:12:04 +00:00
await ProfileGroupItemManager.Instance.Init();
2025-08-17 09:31:55 +00:00
await CoreManager.Instance.Init(_config, UpdateHandler);
TaskManager.Instance.RegUpdateTask(_config, UpdateTaskHandler);
2023-01-01 11:42:01 +00:00
if (_config.GuiItem.EnableStatistics || _config.GuiItem.DisplayRealTimeSpeed)
{
2025-08-17 09:31:55 +00:00
await StatisticsManager.Instance.Init(_config, UpdateStatisticsHandler);
2023-01-01 11:42:01 +00:00
}
await RefreshServers();
2023-04-14 12:49:36 +00:00
BlReloadEnabled = true;
await Reload();
await AutoHideStartup();
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
}
#endregion Init
2023-01-01 11:42:01 +00:00
#region Actions
2023-04-14 12:49:36 +00:00
private async Task UpdateHandler(bool notify, string msg)
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.SendMessage(msg);
if (notify)
2023-01-01 11:42:01 +00:00
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(msg);
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
private async Task UpdateTaskHandler(bool success, string msg)
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.SendMessageEx(msg);
if (success)
2023-01-01 11:42:01 +00:00
{
var indexIdOld = _config.IndexId;
await RefreshServers();
if (indexIdOld != _config.IndexId)
2023-01-01 11:42:01 +00:00
{
await Reload();
}
if (_config.UiItem.EnableAutoAdjustMainLvColWidth)
{
2025-08-30 12:36:16 +00:00
AppEvents.AdjustMainLvColWidthRequested.OnNext(Unit.Default);
2023-01-01 11:42:01 +00:00
}
}
}
2023-04-14 12:49:36 +00:00
private async Task UpdateStatisticsHandler(ServerSpeedItem update)
{
if (!_config.UiItem.ShowInTaskbar)
{
return;
}
2025-08-30 10:01:01 +00:00
AppEvents.DispatcherStatisticsRequested.OnNext(update);
}
2023-04-14 12:49:36 +00:00
public void ShowHideWindow(bool? blShow)
{
_updateView?.Invoke(EViewAction.ShowHideWindow, blShow);
}
2023-01-01 11:42:01 +00:00
#endregion Actions
2023-01-01 11:42:01 +00:00
#region Servers && Groups
2023-01-01 11:42:01 +00:00
private async Task RefreshServers()
{
AppEvents.ProfilesRefreshRequested.OnNext(Unit.Default);
await Task.Delay(200);
}
private void RefreshSubscriptions()
{
Locator.Current.GetService<ProfilesViewModel>()?.RefreshSubscriptions();
}
2023-01-01 11:42:01 +00:00
#endregion Servers && Groups
2023-04-14 12:49:36 +00:00
#region Add Servers
public async Task AddServerAsync(bool blNew, EConfigType eConfigType)
{
ProfileItem item = new()
2023-01-01 11:42:01 +00:00
{
Subid = _config.SubIndexId,
ConfigType = eConfigType,
IsSub = false,
};
2023-01-01 11:42:01 +00:00
bool? ret = false;
if (eConfigType == EConfigType.Custom)
{
ret = await _updateView?.Invoke(EViewAction.AddServer2Window, item);
2023-01-01 11:42:01 +00:00
}
2025-09-10 15:44:42 +00:00
else if (eConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain)
{
ret = await _updateView?.Invoke(EViewAction.AddGroupServerWindow, item);
}
else
2023-01-01 11:42:01 +00:00
{
ret = await _updateView?.Invoke(EViewAction.AddServerWindow, item);
}
if (ret == true)
{
await RefreshServers();
if (item.IndexId == _config.IndexId)
{
await Reload();
}
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
public async Task AddServerViaClipboardAsync(string? clipboardData)
{
if (clipboardData == null)
2024-10-12 07:45:21 +00:00
{
await _updateView?.Invoke(EViewAction.AddServerViaClipboard, null);
return;
2024-10-12 07:45:21 +00:00
}
var ret = await ConfigHandler.AddBatchServers(_config, clipboardData, _config.SubIndexId, false);
if (ret > 0)
2024-10-18 09:35:32 +00:00
{
RefreshSubscriptions();
await RefreshServers();
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(string.Format(ResUI.SuccessfullyImportedServerViaClipboard, ret));
2024-10-18 09:35:32 +00:00
}
else
2024-10-18 09:35:32 +00:00
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.OperationFailed);
2024-10-18 09:35:32 +00:00
}
}
2024-10-18 09:35:32 +00:00
public async Task AddServerViaScanAsync()
{
_updateView?.Invoke(EViewAction.ScanScreenTask, null);
await Task.CompletedTask;
}
public async Task ScanScreenResult(byte[]? bytes)
{
2025-08-17 08:26:13 +00:00
var result = QRCodeUtils.ParseBarcode(bytes);
await AddScanResultAsync(result);
}
public async Task AddServerViaImageAsync()
{
_updateView?.Invoke(EViewAction.ScanImageTask, null);
await Task.CompletedTask;
}
2024-10-18 09:35:32 +00:00
public async Task ScanImageResult(string fileName)
{
if (fileName.IsNullOrEmpty())
{
return;
2024-10-18 09:35:32 +00:00
}
2025-08-17 08:26:13 +00:00
var result = QRCodeUtils.ParseBarcode(fileName);
await AddScanResultAsync(result);
}
private async Task AddScanResultAsync(string? result)
{
if (result.IsNullOrEmpty())
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.NoValidQRcodeFound);
}
else
2023-01-01 11:42:01 +00:00
{
var ret = await ConfigHandler.AddBatchServers(_config, result, _config.SubIndexId, false);
if (ret > 0)
2023-01-01 11:42:01 +00:00
{
RefreshSubscriptions();
await RefreshServers();
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.SuccessfullyImportedServerViaScan);
2023-01-01 11:42:01 +00:00
}
else
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.OperationFailed);
2023-01-01 11:42:01 +00:00
}
}
}
2023-04-14 12:49:36 +00:00
#endregion Add Servers
2023-01-01 11:42:01 +00:00
#region Subscription
2023-01-01 11:42:01 +00:00
private async Task SubSettingAsync()
{
if (await _updateView?.Invoke(EViewAction.SubSettingWindow, null) == true)
2023-01-01 11:42:01 +00:00
{
RefreshSubscriptions();
2023-01-01 11:42:01 +00:00
}
}
2023-01-01 11:42:01 +00:00
public async Task UpdateSubscriptionProcess(string subId, bool blProxy)
{
2025-08-27 09:14:24 +00:00
await Task.Run(async () => await SubscriptionHandler.UpdateProcess(_config, subId, blProxy, UpdateTaskHandler));
}
2023-01-01 11:42:01 +00:00
#endregion Subscription
2023-01-01 11:42:01 +00:00
#region Setting
2023-01-01 11:42:01 +00:00
private async Task OptionSettingAsync()
{
var ret = await _updateView?.Invoke(EViewAction.OptionSettingWindow, null);
if (ret == true)
2023-01-01 11:42:01 +00:00
{
Locator.Current.GetService<StatusBarViewModel>()?.InboundDisplayStatus();
await Reload();
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
private async Task RoutingSettingAsync()
{
var ret = await _updateView?.Invoke(EViewAction.RoutingSettingWindow, null);
if (ret == true)
2023-01-01 11:42:01 +00:00
{
await ConfigHandler.InitBuiltinRouting(_config);
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
await Reload();
2023-01-01 11:42:01 +00:00
}
}
2023-04-27 08:20:13 +00:00
private async Task DNSSettingAsync()
{
var ret = await _updateView?.Invoke(EViewAction.DNSSettingWindow, null);
if (ret == true)
{
await Reload();
}
}
private async Task FullConfigTemplateAsync()
{
var ret = await _updateView?.Invoke(EViewAction.FullConfigTemplateWindow, null);
if (ret == true)
{
await Reload();
}
}
public async Task RebootAsAdmin()
{
ProcUtils.RebootAsAdmin();
2025-09-02 09:12:38 +00:00
await AppManager.Instance.AppExitAsync(true);
}
private async Task ClearServerStatistics()
{
2025-08-17 09:31:55 +00:00
await StatisticsManager.Instance.ClearAllServerStatistics();
await RefreshServers();
}
2023-01-01 11:42:01 +00:00
private async Task OpenTheFileLocation()
{
var path = Utils.StartupPath();
if (Utils.IsWindows())
2023-04-06 02:44:29 +00:00
{
ProcUtils.ProcessStart(path);
2023-04-06 02:44:29 +00:00
}
else if (Utils.IsLinux())
{
ProcUtils.ProcessStart("xdg-open", path);
}
else if (Utils.IsOSX())
{
ProcUtils.ProcessStart("open", path);
}
await Task.CompletedTask;
}
#endregion Setting
2023-01-01 11:42:01 +00:00
#region core job
2023-01-01 11:42:01 +00:00
public async Task Reload()
{
//If there are unfinished reload job, marked with next job.
if (!BlReloadEnabled)
2023-01-01 11:42:01 +00:00
{
_hasNextReloadJob = true;
return;
}
2024-01-30 09:18:00 +00:00
BlReloadEnabled = false;
2024-10-21 01:45:33 +00:00
await Task.Run(async () =>
{
await LoadCore();
await SysProxyHandler.UpdateSysProxy(_config, false);
await Task.Delay(1000);
});
Locator.Current.GetService<StatusBarViewModel>()?.TestServerAvailability();
2025-01-14 09:05:13 +00:00
2025-08-30 10:01:01 +00:00
RxApp.MainThreadScheduler.Schedule(() => _ = ReloadResult());
2023-01-01 11:42:01 +00:00
BlReloadEnabled = true;
if (_hasNextReloadJob)
{
_hasNextReloadJob = false;
await Reload();
}
}
2025-08-30 10:01:01 +00:00
public async Task ReloadResult()
{
// BlReloadEnabled = true;
//Locator.Current.GetService<StatusBarViewModel>()?.ChangeSystemProxyAsync(_config.systemProxyItem.sysProxyType, false);
ShowClashUI = _config.IsRunningCore(ECoreType.sing_box);
if (ShowClashUI)
2023-01-01 11:42:01 +00:00
{
Locator.Current.GetService<ClashProxiesViewModel>()?.ProxiesReload();
2023-01-01 11:42:01 +00:00
}
else
{
TabMainSelectedIndex = 0;
}
}
2023-01-01 11:42:01 +00:00
private async Task LoadCore()
{
var node = await ConfigHandler.GetDefaultServer(_config);
2025-08-17 09:31:55 +00:00
await CoreManager.Instance.LoadCore(node);
}
public async Task CloseCore()
{
await ConfigHandler.SaveConfig(_config);
2025-08-17 09:31:55 +00:00
await CoreManager.Instance.CoreStop();
}
2023-01-01 11:42:01 +00:00
private async Task AutoHideStartup()
{
if (_config.UiItem.AutoHideStartup)
2023-01-01 11:42:01 +00:00
{
ShowHideWindow(false);
2023-01-01 11:42:01 +00:00
}
await Task.CompletedTask;
}
2023-01-01 11:42:01 +00:00
#endregion core job
#region Presets
public async Task ApplyRegionalPreset(EPresetType type)
{
await ConfigHandler.ApplyRegionalPreset(_config, type);
await ConfigHandler.InitRouting(_config);
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler);
await Reload();
2023-01-01 11:42:01 +00:00
}
#endregion Presets
2025-01-30 09:10:05 +00:00
}