v2rayN/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

630 lines
20 KiB
C#
Raw Normal View History

2023-01-01 11:42:01 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
2023-04-06 02:44:29 +00:00
using System.Diagnostics;
2023-01-01 11:42:01 +00:00
using System.Reactive;
using System.Reactive.Linq;
2024-08-20 06:15:29 +00:00
namespace ServiceLib.ViewModels
2023-01-01 11:42:01 +00:00
{
public class MainWindowViewModel : MyReactiveObject
2023-01-01 11:42:01 +00:00
{
#region Menu
//servers
public ReactiveCommand<Unit, Unit> AddVmessServerCmd { get; }
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> AddVlessServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddShadowsocksServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddSocksServerCmd { get; }
2024-04-10 05:32:50 +00:00
public ReactiveCommand<Unit, Unit> AddHttpServerCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> AddTrojanServerCmd { get; }
2023-11-28 08:07:53 +00:00
public ReactiveCommand<Unit, Unit> AddHysteria2ServerCmd { get; }
2023-12-19 08:03:30 +00:00
public ReactiveCommand<Unit, Unit> AddTuicServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddWireguardServerCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> AddCustomServerCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaClipboardCmd { get; }
public ReactiveCommand<Unit, Unit> AddServerViaScanCmd { get; }
2024-10-18 09:35:32 +00:00
public ReactiveCommand<Unit, Unit> AddServerViaImageCmd { get; }
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
//Subscription
public ReactiveCommand<Unit, Unit> SubSettingCmd { get; }
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> SubUpdateCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateViaProxyCmd { get; }
2023-01-08 01:24:09 +00:00
public ReactiveCommand<Unit, Unit> SubGroupUpdateCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> SubGroupUpdateViaProxyCmd { get; }
//Setting
public ReactiveCommand<Unit, Unit> OptionSettingCmd { get; }
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> RoutingSettingCmd { get; }
public ReactiveCommand<Unit, Unit> DNSSettingCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> GlobalHotkeySettingCmd { get; }
public ReactiveCommand<Unit, Unit> RebootAsAdminCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> ClearServerStatisticsCmd { get; }
2024-05-08 03:43:03 +00:00
public ReactiveCommand<Unit, Unit> OpenTheFileLocationCmd { get; }
2023-01-01 11:42:01 +00:00
//Presets
public ReactiveCommand<Unit, Unit> RegionalPresetDefaultCmd { get; }
2024-10-18 09:35:32 +00:00
public ReactiveCommand<Unit, Unit> RegionalPresetRussiaCmd { get; }
public ReactiveCommand<Unit, Unit> RegionalPresetIranCmd { get; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> ReloadCmd { get; }
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
[Reactive]
public bool BlReloadEnabled { get; set; }
[Reactive]
2024-07-18 09:39:11 +00:00
public bool ShowClashUI { get; set; }
2024-07-19 02:51:14 +00:00
[Reactive]
public int TabMainSelectedIndex { get; set; }
2024-10-12 07:45:21 +00:00
#endregion Menu
2023-01-01 11:42:01 +00:00
#region Init
2024-10-12 07:45:21 +00:00
public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
2023-01-01 11:42:01 +00:00
{
2024-10-07 01:51:41 +00:00
_config = AppHandler.Instance.Config;
2023-01-01 11:42:01 +00:00
_updateView = updateView;
2024-02-01 04:44:47 +00:00
#region WhenAnyValue && ReactiveCommand
2023-01-01 11:42:01 +00:00
//servers
AddVmessServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerAsync(true, EConfigType.VMess);
2023-01-01 11:42:01 +00:00
});
AddVlessServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerAsync(true, EConfigType.VLESS);
2023-01-01 11:42:01 +00:00
});
AddShadowsocksServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerAsync(true, EConfigType.Shadowsocks);
2023-01-01 11:42:01 +00:00
});
AddSocksServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerAsync(true, EConfigType.SOCKS);
2023-01-01 11:42:01 +00:00
});
AddHttpServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.HTTP);
});
AddTrojanServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerAsync(true, EConfigType.Trojan);
2023-01-01 11:42:01 +00:00
});
AddHysteria2ServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.Hysteria2);
});
AddTuicServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-12-19 08:03:30 +00:00
{
await AddServerAsync(true, EConfigType.TUIC);
2023-12-19 08:03:30 +00:00
});
AddWireguardServerCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerAsync(true, EConfigType.WireGuard);
});
AddCustomServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerAsync(true, EConfigType.Custom);
2023-01-01 11:42:01 +00:00
});
AddServerViaClipboardCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await AddServerViaClipboardAsync(null);
2023-01-01 11:42:01 +00:00
});
2024-08-22 11:51:10 +00:00
AddServerViaScanCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
2024-10-18 09:35:32 +00:00
await AddServerViaScanAsync();
});
AddServerViaImageCmd = ReactiveCommand.CreateFromTask(async () =>
{
await AddServerViaImageAsync();
2023-01-01 11:42:01 +00:00
});
//Subscription
SubSettingCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await SubSettingAsync();
2023-01-01 11:42:01 +00:00
});
2024-07-18 09:39:11 +00:00
SubUpdateCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await UpdateSubscriptionProcess("", false);
2023-01-01 11:42:01 +00:00
});
SubUpdateViaProxyCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await UpdateSubscriptionProcess("", true);
2023-01-01 11:42:01 +00:00
});
SubGroupUpdateCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await UpdateSubscriptionProcess(_config.SubIndexId, false);
2023-01-01 11:42:01 +00:00
});
SubGroupUpdateViaProxyCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await UpdateSubscriptionProcess(_config.SubIndexId, true);
2023-01-01 11:42:01 +00:00
});
//Setting
OptionSettingCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await OptionSettingAsync();
2023-01-01 11:42:01 +00:00
});
RoutingSettingCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await RoutingSettingAsync();
2023-01-01 11:42:01 +00:00
});
DNSSettingCmd = ReactiveCommand.CreateFromTask(async () =>
{
await DNSSettingAsync();
});
2024-08-22 11:51:10 +00:00
GlobalHotkeySettingCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
2024-08-22 11:51:10 +00:00
if (await _updateView?.Invoke(EViewAction.GlobalHotkeySettingWindow, null) == true)
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
2023-01-01 11:42:01 +00:00
}
});
RebootAsAdminCmd = ReactiveCommand.CreateFromTask(async () =>
2023-04-06 02:44:29 +00:00
{
await RebootAsAdmin();
2023-04-06 02:44:29 +00:00
});
ClearServerStatisticsCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await ClearServerStatistics();
2023-01-01 11:42:01 +00:00
});
OpenTheFileLocationCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await OpenTheFileLocation();
2023-01-01 11:42:01 +00:00
});
ReloadCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await Reload();
2023-01-01 11:42:01 +00:00
});
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
2024-10-21 05:46:13 +00:00
Init();
2023-01-01 11:42:01 +00:00
}
2024-10-21 01:45:33 +00:00
private async Task Init()
2023-01-01 11:42:01 +00:00
{
_config.UiItem.ShowInTaskbar = true;
2024-10-21 05:46:13 +00:00
2024-10-21 01:45:33 +00:00
await ConfigHandler.InitBuiltinRouting(_config);
await ConfigHandler.InitBuiltinDNS(_config);
await ProfileExHandler.Instance.Init();
await CoreHandler.Instance.Init(_config, UpdateHandler);
2024-10-12 07:45:21 +00:00
TaskHandler.Instance.RegUpdateTask(_config, UpdateTaskHandler);
2023-01-01 11:42:01 +00:00
if (_config.GuiItem.EnableStatistics)
2023-01-01 11:42:01 +00:00
{
2024-10-22 09:50:42 +00:00
await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
2023-01-01 11:42:01 +00:00
}
2024-10-21 01:45:33 +00:00
await Reload();
2024-10-21 05:46:13 +00:00
await AutoHideStartup();
2024-11-09 06:37:48 +00:00
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
#endregion Init
2023-01-01 11:42:01 +00:00
#region Actions
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
private void UpdateHandler(bool notify, string msg)
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.SendMessage(msg);
2024-03-07 03:51:45 +00:00
if (notify)
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(msg);
2024-03-07 03:51:45 +00:00
}
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2023-02-19 04:18:08 +00:00
private void UpdateTaskHandler(bool success, string msg)
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.SendMessageEx(msg);
2023-01-01 11:42:01 +00:00
if (success)
{
var indexIdOld = _config.IndexId;
2023-02-04 10:52:17 +00:00
RefreshServers();
if (indexIdOld != _config.IndexId)
2023-02-28 12:23:15 +00:00
{
Reload();
}
if (_config.UiItem.EnableAutoAdjustMainLvColWidth)
{
2024-08-30 04:59:43 +00:00
_updateView?.Invoke(EViewAction.AdjustMainLvColWidth, null);
}
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
private void UpdateStatisticsHandler(ServerSpeedItem update)
{
if (!_config.UiItem.ShowInTaskbar)
{
return;
}
_updateView?.Invoke(EViewAction.DispatcherStatistics, update);
}
public void SetStatisticsResult(ServerSpeedItem update)
2023-01-01 11:42:01 +00:00
{
try
{
2024-10-12 07:45:21 +00:00
Locator.Current.GetService<StatusBarViewModel>()?.UpdateStatistics(update);
2024-11-03 08:45:48 +00:00
if ((update.ProxyUp + update.ProxyDown) > 0 && DateTime.Now.Second % 9 == 0)
2024-07-18 09:39:11 +00:00
{
Locator.Current.GetService<ProfilesViewModel>()?.UpdateStatistics(update);
}
2023-01-01 11:42:01 +00:00
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
2024-08-22 11:51:10 +00:00
public async Task MyAppExitAsync(bool blWindowsShutDown)
2023-01-01 11:42:01 +00:00
{
try
{
2024-12-07 06:07:51 +00:00
Logging.SaveLog("MyAppExitAsync Begin");
MessageBus.Current.SendMessage("", EMsgCommand.AppExit.ToString());
2023-01-01 11:42:01 +00:00
2024-10-21 01:45:33 +00:00
await ConfigHandler.SaveConfig(_config);
2024-12-07 06:07:51 +00:00
await SysProxyHandler.UpdateSysProxy(_config, true);
2024-10-20 03:51:05 +00:00
await ProfileExHandler.Instance.SaveTo();
await StatisticsHandler.Instance.SaveTo();
2024-07-18 09:39:11 +00:00
StatisticsHandler.Instance.Close();
await CoreHandler.Instance.CoreStop();
2024-08-20 06:15:29 +00:00
2024-12-07 06:07:51 +00:00
Logging.SaveLog("MyAppExitAsync End");
2023-01-01 11:42:01 +00:00
}
catch { }
finally
{
if (!blWindowsShutDown)
{
_updateView?.Invoke(EViewAction.Shutdown, null);
}
2023-01-01 11:42:01 +00:00
}
}
public async Task UpgradeApp(string arg)
2024-09-06 10:37:46 +00:00
{
if (!Utils.UpgradeAppExists(out var fileName))
{
NoticeHandler.Instance.SendMessageAndEnqueue(ResUI.UpgradeAppNotExistTip);
Logging.SaveLog("UpgradeApp does not exist");
return;
}
2024-09-06 10:37:46 +00:00
Process process = new()
{
StartInfo = new ProcessStartInfo
{
2024-10-26 09:55:36 +00:00
UseShellExecute = true,
FileName = fileName,
Arguments = arg.AppendQuotes(),
2024-09-06 10:37:46 +00:00
WorkingDirectory = Utils.StartupPath()
}
};
process.Start();
if (process.Id > 0)
{
await MyAppExitAsync(false);
await MyAppExitAsync(false);
2024-09-06 10:37:46 +00:00
}
}
2024-10-12 07:45:21 +00:00
public void ShowHideWindow(bool? blShow)
{
_updateView?.Invoke(EViewAction.ShowHideWindow, blShow);
}
public void Shutdown()
{
_updateView?.Invoke(EViewAction.Shutdown, null);
}
2023-04-14 12:49:36 +00:00
#endregion Actions
2023-01-01 11:42:01 +00:00
#region Servers && Groups
2024-07-18 09:39:11 +00:00
private void RefreshServers()
2023-01-01 11:42:01 +00:00
{
2024-10-11 02:17:44 +00:00
MessageBus.Current.SendMessage("", EMsgCommand.RefreshProfiles.ToString());
2023-01-01 11:42:01 +00:00
}
2024-07-18 09:39:11 +00:00
private void RefreshSubscriptions()
2023-01-01 11:42:01 +00:00
{
2024-07-18 09:39:11 +00:00
Locator.Current.GetService<ProfilesViewModel>()?.RefreshSubscriptions();
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
#endregion Servers && Groups
2023-01-01 11:42:01 +00:00
#region Add Servers
2023-04-14 12:49:36 +00:00
2024-08-22 11:51:10 +00:00
public async Task AddServerAsync(bool blNew, EConfigType eConfigType)
2023-01-01 11:42:01 +00:00
{
2024-07-18 09:39:11 +00:00
ProfileItem item = new()
2023-01-01 11:42:01 +00:00
{
Subid = _config.SubIndexId,
ConfigType = eConfigType,
IsSub = false,
2024-07-18 09:39:11 +00:00
};
2023-01-01 11:42:01 +00:00
bool? ret = false;
if (eConfigType == EConfigType.Custom)
{
2024-08-22 11:51:10 +00:00
ret = await _updateView?.Invoke(EViewAction.AddServer2Window, item);
2023-01-01 11:42:01 +00:00
}
else
{
2024-08-22 11:51:10 +00:00
ret = await _updateView?.Invoke(EViewAction.AddServerWindow, item);
2023-01-01 11:42:01 +00:00
}
if (ret == true)
{
RefreshServers();
if (item.IndexId == _config.IndexId)
2023-01-01 11:42:01 +00:00
{
2024-10-21 01:45:33 +00:00
await Reload();
2023-01-01 11:42:01 +00:00
}
}
}
2024-08-22 11:51:10 +00:00
public async Task AddServerViaClipboardAsync(string? clipboardData)
2023-01-01 11:42:01 +00:00
{
2024-08-20 06:15:29 +00:00
if (clipboardData == null)
{
2024-08-22 11:51:10 +00:00
await _updateView?.Invoke(EViewAction.AddServerViaClipboard, null);
2024-08-20 06:15:29 +00:00
return;
}
int ret = await ConfigHandler.AddBatchServers(_config, clipboardData, _config.SubIndexId, false);
2023-01-01 11:42:01 +00:00
if (ret > 0)
{
2024-07-18 09:39:11 +00:00
RefreshSubscriptions();
2023-01-01 11:42:01 +00:00
RefreshServers();
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(string.Format(ResUI.SuccessfullyImportedServerViaClipboard, ret));
2023-01-01 11:42:01 +00:00
}
else
{
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
}
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2024-10-18 09:35:32 +00:00
public async Task AddServerViaScanAsync()
2024-10-12 07:45:21 +00:00
{
_updateView?.Invoke(EViewAction.ScanScreenTask, null);
}
2024-10-18 09:35:32 +00:00
public async Task ScanScreenResult(byte[]? bytes)
{
var result = QRCodeHelper.ParseBarcode(bytes);
await AddScanResultAsync(result);
}
public async Task AddServerViaImageAsync()
{
_updateView?.Invoke(EViewAction.ScanImageTask, null);
}
public async Task ScanImageResult(string fileName)
{
if (Utils.IsNullOrEmpty(fileName))
{
return;
}
var result = QRCodeHelper.ParseBarcode(fileName);
await AddScanResultAsync(result);
}
private async Task AddScanResultAsync(string? result)
2023-01-01 11:42:01 +00:00
{
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(result))
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.NoValidQRcodeFound);
2023-01-01 11:42:01 +00:00
}
else
{
int ret = await ConfigHandler.AddBatchServers(_config, result, _config.SubIndexId, false);
2023-01-01 11:42:01 +00:00
if (ret > 0)
{
2024-07-18 09:39:11 +00:00
RefreshSubscriptions();
2023-01-01 11:42:01 +00:00
RefreshServers();
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.SuccessfullyImportedServerViaScan);
2023-01-01 11:42:01 +00:00
}
else
{
NoticeHandler.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
2024-08-22 11:51:10 +00:00
private async Task SubSettingAsync()
2023-01-01 11:42:01 +00:00
{
2024-08-22 11:51:10 +00:00
if (await _updateView?.Invoke(EViewAction.SubSettingWindow, null) == true)
2023-01-01 11:42:01 +00:00
{
2024-07-18 09:39:11 +00:00
RefreshSubscriptions();
2023-01-01 11:42:01 +00:00
}
}
public async Task UpdateSubscriptionProcess(string subId, bool blProxy)
2023-02-08 11:20:44 +00:00
{
2024-10-20 03:51:05 +00:00
await (new UpdateService()).UpdateSubscriptionProcess(_config, subId, blProxy, UpdateTaskHandler);
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
#endregion Subscription
2023-01-01 11:42:01 +00:00
#region Setting
2024-08-22 11:51:10 +00:00
private async Task OptionSettingAsync()
2023-01-01 11:42:01 +00:00
{
2024-08-22 11:51:10 +00:00
var ret = await _updateView?.Invoke(EViewAction.OptionSettingWindow, null);
2023-01-01 11:42:01 +00:00
if (ret == true)
{
Locator.Current.GetService<StatusBarViewModel>()?.InboundDisplayStatus();
2024-10-21 01:45:33 +00:00
await Reload();
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
2024-08-22 11:51:10 +00:00
private async Task RoutingSettingAsync()
2023-01-01 11:42:01 +00:00
{
2024-08-22 11:51:10 +00:00
var ret = await _updateView?.Invoke(EViewAction.RoutingSettingWindow, null);
2023-01-01 11:42:01 +00:00
if (ret == true)
{
2024-10-21 01:45:33 +00:00
await ConfigHandler.InitBuiltinRouting(_config);
2024-10-12 07:45:21 +00:00
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
2024-10-21 01:45:33 +00:00
await Reload();
2023-01-01 11:42:01 +00:00
}
}
2023-04-27 08:20:13 +00:00
2024-08-22 11:51:10 +00:00
private async Task DNSSettingAsync()
{
2024-08-22 11:51:10 +00:00
var ret = await _updateView?.Invoke(EViewAction.DNSSettingWindow, null);
if (ret == true)
{
2024-10-21 01:45:33 +00:00
await Reload();
}
}
2023-01-01 11:42:01 +00:00
public async Task RebootAsAdmin(bool blAdmin = true)
2023-04-06 02:44:29 +00:00
{
try
{
2024-10-12 07:45:21 +00:00
ProcessStartInfo startInfo = new()
{
UseShellExecute = true,
Arguments = Global.RebootAs,
WorkingDirectory = Utils.StartupPath(),
FileName = Utils.GetExePath().AppendQuotes(),
Verb = blAdmin ? "runas" : null,
2024-10-12 07:45:21 +00:00
};
2023-04-06 02:44:29 +00:00
Process.Start(startInfo);
2024-10-12 07:45:21 +00:00
await MyAppExitAsync(false);
}
catch { }
2023-04-06 02:44:29 +00:00
}
private async Task ClearServerStatistics()
{
2024-10-21 01:45:33 +00:00
await StatisticsHandler.Instance.ClearAllServerStatistics();
RefreshServers();
}
private async Task OpenTheFileLocation()
{
2024-12-30 03:10:27 +00:00
var path = Utils.StartupPath();
if (Utils.IsWindows())
{
2024-12-30 03:10:27 +00:00
Utils.ProcessStart(path);
}
else if (Utils.IsLinux())
{
2024-12-30 03:10:27 +00:00
Utils.ProcessStart("nautilus", path);
}
else if (Utils.IsOSX())
{
2024-12-30 03:10:27 +00:00
Utils.ProcessStart("open", path);
}
}
2023-04-14 12:49:36 +00:00
#endregion Setting
2023-01-01 11:42:01 +00:00
2024-03-07 01:06:35 +00:00
#region core job
2023-01-01 11:42:01 +00:00
public async Task Reload()
2023-01-01 11:42:01 +00:00
{
2024-01-30 09:18:00 +00:00
BlReloadEnabled = false;
await LoadCore();
2024-10-12 07:45:21 +00:00
Locator.Current.GetService<StatusBarViewModel>()?.TestServerAvailability();
2024-10-16 01:20:05 +00:00
await SysProxyHandler.UpdateSysProxy(_config, false);
2024-10-21 01:45:33 +00:00
_updateView?.Invoke(EViewAction.DispatcherReload, null);
2023-01-01 11:42:01 +00:00
}
public void ReloadResult()
{
2024-10-16 01:20:05 +00:00
//Locator.Current.GetService<StatusBarViewModel>()?.ChangeSystemProxyAsync(_config.systemProxyItem.sysProxyType, false);
BlReloadEnabled = true;
2024-10-05 09:51:31 +00:00
ShowClashUI = _config.IsRunningCore(ECoreType.sing_box);
if (ShowClashUI)
{
Locator.Current.GetService<ClashProxiesViewModel>()?.ProxiesReload();
}
else { TabMainSelectedIndex = 0; }
}
2024-03-07 01:06:35 +00:00
private async Task LoadCore()
2023-01-01 11:42:01 +00:00
{
2024-10-21 01:45:33 +00:00
//if (_config.tunModeItem.enableTun)
//{
// Task.Delay(1000).Wait();
// WindowsUtils.RemoveTunDevice();
//}
2024-10-20 03:51:05 +00:00
await Task.Run(async () =>
2023-01-01 11:42:01 +00:00
{
2024-10-20 03:51:05 +00:00
var node = await ConfigHandler.GetDefaultServer(_config);
2024-10-21 01:45:33 +00:00
await CoreHandler.Instance.LoadCore(node);
2024-06-28 02:41:17 +00:00
});
2023-01-01 11:42:01 +00:00
}
2024-10-21 01:45:33 +00:00
public async Task CloseCore()
2023-01-01 11:42:01 +00:00
{
2024-10-24 07:35:37 +00:00
await ConfigHandler.SaveConfig(_config);
await CoreHandler.Instance.CoreStop();
2023-01-01 11:42:01 +00:00
}
2024-10-21 05:46:13 +00:00
private async Task AutoHideStartup()
2023-01-01 11:42:01 +00:00
{
if (_config.UiItem.AutoHideStartup)
2023-01-01 11:42:01 +00:00
{
2024-10-21 05:46:13 +00:00
ShowHideWindow(false);
2023-01-01 11:42:01 +00:00
}
}
2024-10-12 07:45:21 +00:00
#endregion core job
#region Presets
public async Task ApplyRegionalPreset(EPresetType type)
{
2024-10-20 03:51:05 +00:00
await ConfigHandler.ApplyRegionalPreset(_config, type);
2024-10-21 01:45:33 +00:00
await ConfigHandler.InitRouting(_config);
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
2024-10-24 07:35:37 +00:00
await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
2024-10-21 01:45:33 +00:00
await Reload();
}
2024-10-18 09:35:32 +00:00
#endregion Presets
2023-01-01 11:42:01 +00:00
}
2024-07-25 02:26:39 +00:00
}