Compare commits

...

5 commits

Author SHA1 Message Date
2dust
a108eaf34b Optimization and Improvement.
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run
Changed callback from synchronous Action<bool, string> to asynchronous Func<bool, string, Task>
2025-08-29 11:53:30 +08:00
2dust
da28c639b3 Optimization and Improvement.
Changed callback from synchronous Action<bool, string> to asynchronous Func<bool, string, Task>
2025-08-29 11:40:08 +08:00
2dust
8ef68127d4 Optimization and Improvement.
Changed callback from synchronous Action<bool, string> to asynchronous Func<bool, string, Task>
2025-08-29 10:53:57 +08:00
2dust
f39d966a33 Optimization and Improvement.
Changed callback from synchronous Action<bool, string> to asynchronous Func<bool, string, Task>
2025-08-29 10:31:09 +08:00
2dust
f83e83de13 Optimization and Improvement
Changed callback from synchronous Action<bool, string> to asynchronous Func<bool, string, Task>
2025-08-29 09:49:30 +08:00
14 changed files with 176 additions and 160 deletions

View file

@ -2,17 +2,18 @@ namespace ServiceLib.Handler;
public static class SubscriptionHandler
{
public static async Task UpdateProcess(Config config, string subId, bool blProxy, Action<bool, string> updateFunc)
public static async Task UpdateProcess(Config config, string subId, bool blProxy, Func<bool, string, Task> updateFunc)
{
updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart);
await updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart);
var subItem = await AppManager.Instance.SubItems();
if (subItem is not { Count: > 0 })
{
updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription);
await updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription);
return;
}
var successCount = 0;
foreach (var item in subItem)
{
try
@ -25,32 +26,35 @@ public static class SubscriptionHandler
var hashCode = $"{item.Remarks}->";
if (item.Enabled == false)
{
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}");
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}");
continue;
}
// Create download handler
var downloadHandle = CreateDownloadHandler(hashCode, updateFunc);
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
// Get all subscription content (main subscription + additional subscriptions)
var result = await DownloadAllSubscriptions(config, item, blProxy, downloadHandle);
// Process download result
await ProcessDownloadResult(config, item.Id, result, hashCode, updateFunc);
if (await ProcessDownloadResult(config, item.Id, result, hashCode, updateFunc))
{
successCount++;
}
updateFunc?.Invoke(false, "-------------------------------------------------------");
await updateFunc?.Invoke(false, "-------------------------------------------------------");
}
catch (Exception ex)
{
var hashCode = $"{item.Remarks}->";
Logging.SaveLog("UpdateSubscription", ex);
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgFailedImportSubscription}: {ex.Message}");
updateFunc?.Invoke(false, "-------------------------------------------------------");
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgFailedImportSubscription}: {ex.Message}");
await updateFunc?.Invoke(false, "-------------------------------------------------------");
}
}
updateFunc?.Invoke(true, $"{ResUI.MsgUpdateSubscriptionEnd}");
await updateFunc?.Invoke(successCount > 0, $"{ResUI.MsgUpdateSubscriptionEnd}");
}
private static bool IsValidSubscription(SubItem item, string subId)
@ -76,7 +80,7 @@ public static class SubscriptionHandler
return true;
}
private static DownloadService CreateDownloadHandler(string hashCode, Action<bool, string> updateFunc)
private static DownloadService CreateDownloadHandler(string hashCode, Func<bool, string, Task> updateFunc)
{
var downloadHandle = new DownloadService();
downloadHandle.Error += (sender2, args) =>
@ -181,23 +185,23 @@ public static class SubscriptionHandler
return result;
}
private static async Task ProcessDownloadResult(Config config, string id, string result, string hashCode, Action<bool, string> updateFunc)
private static async Task<bool> ProcessDownloadResult(Config config, string id, string result, string hashCode, Func<bool, string, Task> updateFunc)
{
if (result.IsNullOrEmpty())
{
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
return;
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
return false;
}
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
// If result is too short, display content directly
if (result.Length < 99)
{
updateFunc?.Invoke(false, $"{hashCode}{result}");
await updateFunc?.Invoke(false, $"{hashCode}{result}");
}
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartParsingSubscription}");
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartParsingSubscription}");
// Add servers to configuration
var ret = await ConfigHandler.AddBatchServers(config, result, id, true);
@ -208,9 +212,10 @@ public static class SubscriptionHandler
}
// Update completion message
updateFunc?.Invoke(false,
ret > 0
await updateFunc?.Invoke(false, ret > 0
? $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}"
: $"{hashCode}{ResUI.MsgFailedImportSubscription}");
return ret > 0;
}
}

View file

@ -35,7 +35,7 @@ public sealed class ClashApiManager
return null;
}
public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Action<ClashProxyModel?, string> updateFunc)
public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Func<ClashProxyModel?, string, Task> updateFunc)
{
Task.Run(async () =>
{
@ -79,12 +79,12 @@ public sealed class ClashApiManager
tasks.Add(Task.Run(async () =>
{
var result = await HttpClientHelper.Instance.TryGetAsync(url);
updateFunc?.Invoke(it, result);
await updateFunc?.Invoke(it, result);
}));
}
await Task.WhenAll(tasks);
await Task.Delay(1000);
updateFunc?.Invoke(null, "");
await updateFunc?.Invoke(null, "");
});
}

View file

@ -10,11 +10,11 @@ public class CoreAdminManager
private static readonly Lazy<CoreAdminManager> _instance = new(() => new());
public static CoreAdminManager Instance => _instance.Value;
private Config _config;
private Action<bool, string>? _updateFunc;
private Func<bool, string, Task>? _updateFunc;
private int _linuxSudoPid = -1;
private const string _tag = "CoreAdminHandler";
public async Task Init(Config config, Action<bool, string> updateFunc)
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
{
if (_config != null)
{
@ -26,9 +26,9 @@ public class CoreAdminManager
await Task.CompletedTask;
}
private void UpdateFunc(bool notify, string msg)
private async Task UpdateFunc(bool notify, string msg)
{
_updateFunc?.Invoke(notify, msg);
await _updateFunc?.Invoke(notify, msg);
}
public async Task<Process?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
@ -60,7 +60,7 @@ public class CoreAdminManager
{
if (e.Data.IsNotEmpty())
{
UpdateFunc(false, e.Data + Environment.NewLine);
_ = UpdateFunc(false, e.Data + Environment.NewLine);
}
}
@ -106,7 +106,7 @@ public class CoreAdminManager
.WithStandardInputPipe(PipeSource.FromString(AppManager.Instance.LinuxSudoPwd))
.ExecuteBufferedAsync();
UpdateFunc(false, result.StandardOutput.ToString());
await UpdateFunc(false, result.StandardOutput.ToString());
}
catch (Exception ex)
{

View file

@ -14,10 +14,10 @@ public class CoreManager
private Process? _process;
private Process? _processPre;
private bool _linuxSudo = false;
private Action<bool, string>? _updateFunc;
private Func<bool, string, Task>? _updateFunc;
private const string _tag = "CoreHandler";
public async Task Init(Config config, Action<bool, string> updateFunc)
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -63,7 +63,7 @@ public class CoreManager
{
if (node == null)
{
UpdateFunc(false, ResUI.CheckServerSettings);
await UpdateFunc(false, ResUI.CheckServerSettings);
return;
}
@ -71,13 +71,13 @@ public class CoreManager
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
if (result.Success != true)
{
UpdateFunc(true, result.Msg);
await UpdateFunc(true, result.Msg);
return;
}
UpdateFunc(false, $"{node.GetSummary()}");
UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await UpdateFunc(false, $"{node.GetSummary()}");
await UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await CoreStop();
await Task.Delay(100);
@ -91,7 +91,7 @@ public class CoreManager
await CoreStartPreService(node);
if (_process != null)
{
UpdateFunc(true, $"{node.GetSummary()}");
await UpdateFunc(true, $"{node.GetSummary()}");
}
}
@ -101,14 +101,14 @@ public class CoreManager
var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false));
var configPath = Utils.GetBinConfigPath(fileName);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
UpdateFunc(false, result.Msg);
await UpdateFunc(false, result.Msg);
if (result.Success != true)
{
return -1;
}
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
UpdateFunc(false, configPath);
await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await UpdateFunc(false, configPath);
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
var proc = await RunProcess(coreInfo, fileName, true, false);
@ -216,9 +216,9 @@ public class CoreManager
}
}
private void UpdateFunc(bool notify, string msg)
private async Task UpdateFunc(bool notify, string msg)
{
_updateFunc?.Invoke(notify, msg);
await _updateFunc?.Invoke(notify, msg);
}
#endregion Private
@ -230,7 +230,7 @@ public class CoreManager
var fileName = CoreInfoManager.Instance.GetCoreExecFile(coreInfo, out var msg);
if (fileName.IsNullOrEmpty())
{
UpdateFunc(false, msg);
await UpdateFunc(false, msg);
return null;
}
@ -251,7 +251,7 @@ public class CoreManager
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
UpdateFunc(mayNeedSudo, ex.Message);
await UpdateFunc(mayNeedSudo, ex.Message);
return null;
}
}
@ -284,7 +284,7 @@ public class CoreManager
{
if (e.Data.IsNotEmpty())
{
UpdateFunc(false, e.Data + Environment.NewLine);
_ = UpdateFunc(false, e.Data + Environment.NewLine);
}
}
proc.OutputDataReceived += dataHandler;

View file

@ -8,14 +8,14 @@ public class StatisticsManager
private Config _config;
private ServerStatItem? _serverStatItem;
private List<ServerStatItem> _lstServerStat;
private Action<ServerSpeedItem>? _updateFunc;
private Func<ServerSpeedItem, Task>? _updateFunc;
private StatisticsXrayService? _statisticsXray;
private StatisticsSingboxService? _statisticsSingbox;
private static readonly string _tag = "StatisticsHandler";
public List<ServerStatItem> ServerStat => _lstServerStat;
public async Task Init(Config config, Action<ServerSpeedItem> updateFunc)
public async Task Init(Config config, Func<ServerSpeedItem, Task> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -97,9 +97,9 @@ public class StatisticsManager
_lstServerStat = await SQLiteHelper.Instance.TableAsync<ServerStatItem>().ToListAsync();
}
private void UpdateServerStatHandler(ServerSpeedItem server)
private async Task UpdateServerStatHandler(ServerSpeedItem server)
{
_ = UpdateServerStat(server);
await UpdateServerStat(server);
}
private async Task UpdateServerStat(ServerSpeedItem server)
@ -123,7 +123,7 @@ public class StatisticsManager
server.TodayDown = _serverStatItem.TodayDown;
server.TotalUp = _serverStatItem.TotalUp;
server.TotalDown = _serverStatItem.TotalDown;
_updateFunc?.Invoke(server);
await _updateFunc?.Invoke(server);
}
private async Task GetServerStatItem(string indexId)

View file

@ -4,13 +4,18 @@ public class TaskManager
{
private static readonly Lazy<TaskManager> _instance = new(() => new());
public static TaskManager Instance => _instance.Value;
private Config _config;
private Func<bool, string, Task>? _updateFunc;
public void RegUpdateTask(Config config, Action<bool, string> updateFunc)
public void RegUpdateTask(Config config, Func<bool, string, Task> updateFunc)
{
Task.Run(() => ScheduledTasks(config, updateFunc));
_config = config;
_updateFunc = updateFunc;
Task.Run(ScheduledTasks);
}
private async Task ScheduledTasks(Config config, Action<bool, string> updateFunc)
private async Task ScheduledTasks()
{
Logging.SaveLog("Setup Scheduled Tasks");
@ -21,14 +26,14 @@ public class TaskManager
await Task.Delay(1000 * 60);
//Execute once 1 minute
await UpdateTaskRunSubscription(config, updateFunc);
await UpdateTaskRunSubscription();
//Execute once 20 minute
if (numOfExecuted % 20 == 0)
{
//Logging.SaveLog("Execute save config");
await ConfigHandler.SaveConfig(config);
await ConfigHandler.SaveConfig(_config);
await ProfileExManager.Instance.SaveTo();
}
@ -42,14 +47,14 @@ public class TaskManager
FileManager.DeleteExpiredFiles(Utils.GetTempPath(), DateTime.Now.AddMonths(-1));
//Check once 1 hour
await UpdateTaskRunGeo(config, numOfExecuted / 60, updateFunc);
await UpdateTaskRunGeo(numOfExecuted / 60);
}
numOfExecuted++;
}
}
private async Task UpdateTaskRunSubscription(Config config, Action<bool, string> updateFunc)
private async Task UpdateTaskRunSubscription()
{
var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds();
var lstSubs = (await AppManager.Instance.SubItems())?
@ -66,30 +71,30 @@ public class TaskManager
foreach (var item in lstSubs)
{
await SubscriptionHandler.UpdateProcess(config, item.Id, true, (success, msg) =>
await SubscriptionHandler.UpdateProcess(_config, item.Id, true, async (success, msg) =>
{
updateFunc?.Invoke(success, msg);
await _updateFunc?.Invoke(success, msg);
if (success)
{
Logging.SaveLog($"Update subscription end. {msg}");
}
});
item.UpdateTime = updateTime;
await ConfigHandler.AddSubItem(config, item);
await ConfigHandler.AddSubItem(_config, item);
await Task.Delay(1000);
}
}
private async Task UpdateTaskRunGeo(Config config, int hours, Action<bool, string> updateFunc)
private async Task UpdateTaskRunGeo(int hours)
{
if (config.GuiItem.AutoUpdateInterval > 0 && hours > 0 && hours % config.GuiItem.AutoUpdateInterval == 0)
if (_config.GuiItem.AutoUpdateInterval > 0 && hours > 0 && hours % _config.GuiItem.AutoUpdateInterval == 0)
{
Logging.SaveLog("Execute update geo files");
var updateHandle = new UpdateService();
await updateHandle.UpdateGeoFileAll(config, (success, msg) =>
await updateHandle.UpdateGeoFileAll(_config, async (success, msg) =>
{
updateFunc?.Invoke(false, msg);
await _updateFunc?.Invoke(false, msg);
});
}
}

View file

@ -15,7 +15,7 @@ public class DownloadService
private static readonly string _tag = "DownloadService";
public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action<bool, string> updateFunc)
public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Func<bool, string, Task> updateFunc)
{
try
{
@ -31,10 +31,10 @@ public class DownloadService
}
catch (Exception ex)
{
updateFunc?.Invoke(false, ex.Message);
await updateFunc?.Invoke(false, ex.Message);
if (ex.InnerException != null)
{
updateFunc?.Invoke(false, ex.InnerException.Message);
await updateFunc?.Invoke(false, ex.InnerException.Message);
}
}
return 0;

View file

@ -5,26 +5,20 @@ using System.Net.Sockets;
namespace ServiceLib.Services;
public class SpeedtestService
public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateFunc)
{
private static readonly string _tag = "SpeedtestService";
private Config? _config;
private Action<SpeedTestResult>? _updateFunc;
private readonly Config? _config = config;
private readonly Func<SpeedTestResult, Task>? _updateFunc = updateFunc;
private static readonly ConcurrentBag<string> _lstExitLoop = new();
public SpeedtestService(Config config, Action<SpeedTestResult> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
}
public void RunLoop(ESpeedActionType actionType, List<ProfileItem> selecteds)
{
Task.Run(async () =>
{
await RunAsync(actionType, selecteds);
await ProfileExManager.Instance.SaveTo();
UpdateFunc("", ResUI.SpeedtestingCompleted);
await UpdateFunc("", ResUI.SpeedtestingCompleted);
});
}
@ -43,7 +37,7 @@ public class SpeedtestService
var exitLoopKey = Utils.GetGuid(false);
_lstExitLoop.Add(exitLoopKey);
var lstSelected = GetClearItem(actionType, selecteds);
var lstSelected = await GetClearItem(actionType, selecteds);
switch (actionType)
{
@ -65,7 +59,7 @@ public class SpeedtestService
}
}
private List<ServerTestItem> GetClearItem(ESpeedActionType actionType, List<ProfileItem> selecteds)
private async Task<List<ServerTestItem>> GetClearItem(ESpeedActionType actionType, List<ProfileItem> selecteds)
{
var lstSelected = new List<ServerTestItem>();
foreach (var it in selecteds)
@ -97,17 +91,17 @@ public class SpeedtestService
{
case ESpeedActionType.Tcping:
case ESpeedActionType.Realping:
UpdateFunc(it.IndexId, ResUI.Speedtesting, "");
await UpdateFunc(it.IndexId, ResUI.Speedtesting, "");
ProfileExManager.Instance.SetTestDelay(it.IndexId, 0);
break;
case ESpeedActionType.Speedtest:
UpdateFunc(it.IndexId, "", ResUI.SpeedtestingWait);
await UpdateFunc(it.IndexId, "", ResUI.SpeedtestingWait);
ProfileExManager.Instance.SetTestSpeed(it.IndexId, 0);
break;
case ESpeedActionType.Mixedtest:
UpdateFunc(it.IndexId, ResUI.Speedtesting, ResUI.SpeedtestingWait);
await UpdateFunc(it.IndexId, ResUI.Speedtesting, ResUI.SpeedtestingWait);
ProfileExManager.Instance.SetTestDelay(it.IndexId, 0);
ProfileExManager.Instance.SetTestSpeed(it.IndexId, 0);
break;
@ -133,7 +127,7 @@ public class SpeedtestService
var responseTime = await GetTcpingTime(it.Address, it.Port);
ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime);
UpdateFunc(it.IndexId, responseTime.ToString());
await UpdateFunc(it.IndexId, responseTime.ToString());
}
catch (Exception ex)
{
@ -169,11 +163,11 @@ public class SpeedtestService
{
if (_lstExitLoop.Any(p => p == exitLoopKey) == false)
{
UpdateFunc("", ResUI.SpeedtestingSkip);
await UpdateFunc("", ResUI.SpeedtestingSkip);
return;
}
UpdateFunc("", string.Format(ResUI.SpeedtestingTestFailedPart, lstFailed.Count));
await UpdateFunc("", string.Format(ResUI.SpeedtestingTestFailedPart, lstFailed.Count));
if (pageSizeNext > _config.SpeedTestItem.MixedConcurrencyCount)
{
@ -239,7 +233,7 @@ public class SpeedtestService
{
if (_lstExitLoop.Any(p => p == exitLoopKey) == false)
{
UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip);
await UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip);
continue;
}
if (it.ConfigType == EConfigType.Custom)
@ -256,7 +250,7 @@ public class SpeedtestService
pid = await CoreManager.Instance.LoadCoreConfigSpeedtest(it);
if (pid < 0)
{
UpdateFunc(it.IndexId, "", ResUI.FailedToRunCore);
await UpdateFunc(it.IndexId, "", ResUI.FailedToRunCore);
}
else
{
@ -270,7 +264,7 @@ public class SpeedtestService
}
else
{
UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip);
await UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip);
}
}
}
@ -298,25 +292,25 @@ public class SpeedtestService
var responseTime = await HttpClientHelper.Instance.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10);
ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime);
UpdateFunc(it.IndexId, responseTime.ToString());
await UpdateFunc(it.IndexId, responseTime.ToString());
return responseTime;
}
private async Task DoSpeedTest(DownloadService downloadHandle, ServerTestItem it)
{
UpdateFunc(it.IndexId, "", ResUI.Speedtesting);
await UpdateFunc(it.IndexId, "", ResUI.Speedtesting);
var webProxy = new WebProxy($"socks5://{Global.Loopback}:{it.Port}");
var url = _config.SpeedTestItem.SpeedTestUrl;
var timeout = _config.SpeedTestItem.SpeedTestTimeout;
await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
await downloadHandle.DownloadDataAsync(url, webProxy, timeout, async (success, msg) =>
{
decimal.TryParse(msg, out var dec);
if (dec > 0)
{
ProfileExManager.Instance.SetTestSpeed(it.IndexId, dec);
}
UpdateFunc(it.IndexId, "", msg);
await UpdateFunc(it.IndexId, "", msg);
});
}
@ -371,9 +365,9 @@ public class SpeedtestService
return lstTest;
}
private void UpdateFunc(string indexId, string delay, string speed = "")
private async Task UpdateFunc(string indexId, string delay, string speed = "")
{
_updateFunc?.Invoke(new() { IndexId = indexId, Delay = delay, Speed = speed });
await _updateFunc?.Invoke(new() { IndexId = indexId, Delay = delay, Speed = speed });
if (indexId.IsNotEmpty() && speed.IsNotEmpty())
{
ProfileExManager.Instance.SetTestMessage(indexId, speed);

View file

@ -8,11 +8,11 @@ public class StatisticsSingboxService
private readonly Config _config;
private bool _exitFlag;
private ClientWebSocket? webSocket;
private Action<ServerSpeedItem>? _updateFunc;
private readonly Func<ServerSpeedItem, Task>? _updateFunc;
private string Url => $"ws://{Global.Loopback}:{AppManager.Instance.StatePort2}/traffic";
private static readonly string _tag = "StatisticsSingboxService";
public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
public StatisticsSingboxService(Config config, Func<ServerSpeedItem, Task> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -90,7 +90,7 @@ public class StatisticsSingboxService
{
ParseOutput(result, out var up, out var down);
_updateFunc?.Invoke(new ServerSpeedItem()
await _updateFunc?.Invoke(new ServerSpeedItem()
{
ProxyUp = (long)(up / 1000),
ProxyDown = (long)(down / 1000)

View file

@ -6,10 +6,10 @@ public class StatisticsXrayService
private ServerSpeedItem _serverSpeedItem = new();
private readonly Config _config;
private bool _exitFlag;
private Action<ServerSpeedItem>? _updateFunc;
private readonly Func<ServerSpeedItem, Task>? _updateFunc;
private string Url => $"{Global.HttpProtocol}{Global.Loopback}:{AppManager.Instance.StatePort}/debug/vars";
public StatisticsXrayService(Config config, Action<ServerSpeedItem> updateFunc)
public StatisticsXrayService(Config config, Func<ServerSpeedItem, Task> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -39,7 +39,7 @@ public class StatisticsXrayService
if (result != null)
{
var server = ParseOutput(result) ?? new ServerSpeedItem();
_updateFunc?.Invoke(server);
await _updateFunc?.Invoke(server);
}
}
catch

View file

@ -5,11 +5,11 @@ namespace ServiceLib.Services;
public class UpdateService
{
private Action<bool, string>? _updateFunc;
private Func<bool, string, Task>? _updateFunc;
private readonly int _timeout = 30;
private static readonly string _tag = "UpdateService";
public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
public async Task CheckUpdateGuiN(Config config, Func<bool, string, Task> updateFunc, bool preRelease)
{
_updateFunc = updateFunc;
var url = string.Empty;
@ -20,25 +20,25 @@ public class UpdateService
{
if (args.Success)
{
_updateFunc?.Invoke(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
_updateFunc?.Invoke(true, Utils.UrlEncode(fileName));
UpdateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
UpdateFunc(true, Utils.UrlEncode(fileName));
}
else
{
_updateFunc?.Invoke(false, args.Msg);
UpdateFunc(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
_updateFunc?.Invoke(false, args.GetException().Message);
UpdateFunc(false, args.GetException().Message);
};
_updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN));
await UpdateFunc(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN));
var result = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease);
if (result.Success)
{
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
_updateFunc?.Invoke(false, result.Msg);
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
await UpdateFunc(false, result.Msg);
url = result.Data?.ToString();
fileName = Utils.GetTempPath(Utils.GetGuid());
@ -46,11 +46,11 @@ public class UpdateService
}
else
{
_updateFunc?.Invoke(false, result.Msg);
await UpdateFunc(false, result.Msg);
}
}
public async Task CheckUpdateCore(ECoreType type, Config config, Action<bool, string> updateFunc, bool preRelease)
public async Task CheckUpdateCore(ECoreType type, Config config, Func<bool, string, Task> updateFunc, bool preRelease)
{
_updateFunc = updateFunc;
var url = string.Empty;
@ -61,34 +61,34 @@ public class UpdateService
{
if (args.Success)
{
_updateFunc?.Invoke(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
_updateFunc?.Invoke(false, ResUI.MsgUnpacking);
UpdateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
UpdateFunc(false, ResUI.MsgUnpacking);
try
{
_updateFunc?.Invoke(true, fileName);
UpdateFunc(true, fileName);
}
catch (Exception ex)
{
_updateFunc?.Invoke(false, ex.Message);
UpdateFunc(false, ex.Message);
}
}
else
{
_updateFunc?.Invoke(false, args.Msg);
UpdateFunc(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
_updateFunc?.Invoke(false, args.GetException().Message);
UpdateFunc(false, args.GetException().Message);
};
_updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, type));
await UpdateFunc(false, string.Format(ResUI.MsgStartUpdating, type));
var result = await CheckUpdateAsync(downloadHandle, type, preRelease);
if (result.Success)
{
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type));
_updateFunc?.Invoke(false, result.Msg);
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type));
await UpdateFunc(false, result.Msg);
url = result.Data?.ToString();
var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url);
@ -99,17 +99,17 @@ public class UpdateService
{
if (!result.Msg.IsNullOrEmpty())
{
_updateFunc?.Invoke(false, result.Msg);
await UpdateFunc(false, result.Msg);
}
}
}
public async Task UpdateGeoFileAll(Config config, Action<bool, string> updateFunc)
public async Task UpdateGeoFileAll(Config config, Func<bool, string, Task> updateFunc)
{
await UpdateGeoFiles(config, updateFunc);
await UpdateOtherFiles(config, updateFunc);
await UpdateSrsFileAll(config, updateFunc);
_updateFunc?.Invoke(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
await UpdateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
}
#region CheckUpdate private
@ -128,7 +128,7 @@ public class UpdateService
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
_updateFunc?.Invoke(false, ex.Message);
await UpdateFunc(false, ex.Message);
return new RetResult(false, ex.Message);
}
}
@ -212,7 +212,7 @@ public class UpdateService
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
_updateFunc?.Invoke(false, ex.Message);
await UpdateFunc(false, ex.Message);
return new SemanticVersion("");
}
}
@ -272,7 +272,7 @@ public class UpdateService
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
_updateFunc?.Invoke(false, ex.Message);
await UpdateFunc(false, ex.Message);
return new RetResult(false, ex.Message);
}
}
@ -333,7 +333,7 @@ public class UpdateService
#region Geo private
private async Task UpdateGeoFiles(Config config, Action<bool, string> updateFunc)
private async Task UpdateGeoFiles(Config config, Func<bool, string, Task> updateFunc)
{
_updateFunc = updateFunc;
@ -352,7 +352,7 @@ public class UpdateService
}
}
private async Task UpdateOtherFiles(Config config, Action<bool, string> updateFunc)
private async Task UpdateOtherFiles(Config config, Func<bool, string, Task> updateFunc)
{
//If it is not in China area, no update is required
if (config.ConstItem.GeoSourceUrl.IsNotEmpty())
@ -371,7 +371,7 @@ public class UpdateService
}
}
private async Task UpdateSrsFileAll(Config config, Action<bool, string> updateFunc)
private async Task UpdateSrsFileAll(Config config, Func<bool, string, Task> updateFunc)
{
_updateFunc = updateFunc;
@ -426,7 +426,7 @@ public class UpdateService
}
}
private async Task UpdateSrsFile(string type, string srsName, Config config, Action<bool, string> updateFunc)
private async Task UpdateSrsFile(string type, string srsName, Config config, Func<bool, string, Task> updateFunc)
{
var srsUrl = string.IsNullOrEmpty(config.ConstItem.SrsSourceUrl)
? Global.SingboxRulesetUrl
@ -439,7 +439,7 @@ public class UpdateService
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
}
private async Task DownloadGeoFile(string url, string fileName, string targetPath, Action<bool, string> updateFunc)
private async Task DownloadGeoFile(string url, string fileName, string targetPath, Func<bool, string, Task> updateFunc)
{
var tmpFileName = Utils.GetTempPath(Utils.GetGuid());
@ -448,7 +448,7 @@ public class UpdateService
{
if (args.Success)
{
_updateFunc?.Invoke(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName));
UpdateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName));
try
{
@ -457,26 +457,31 @@ public class UpdateService
File.Copy(tmpFileName, targetPath, true);
File.Delete(tmpFileName);
//_updateFunc?.Invoke(true, "");
//await UpdateFunc(true, "");
}
}
catch (Exception ex)
{
_updateFunc?.Invoke(false, ex.Message);
UpdateFunc(false, ex.Message);
}
}
else
{
_updateFunc?.Invoke(false, args.Msg);
UpdateFunc(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
_updateFunc?.Invoke(false, args.GetException().Message);
UpdateFunc(false, args.GetException().Message);
};
await downloadHandle.DownloadFileAsync(url, tmpFileName, true, _timeout);
}
#endregion Geo private
private async Task UpdateFunc(bool notify, string msg)
{
await _updateFunc?.Invoke(notify, msg);
}
}

View file

@ -89,9 +89,11 @@ public class CheckUpdateViewModel : MyReactiveObject
{
var item = _checkUpdateModel[k];
if (item.IsSelected != true)
{
continue;
}
UpdateView(item.CoreType, "...");
await UpdateView(item.CoreType, "...");
if (item.CoreType == _geo)
{
await CheckUpdateGeo();
@ -129,9 +131,9 @@ public class CheckUpdateViewModel : MyReactiveObject
private async Task CheckUpdateGeo()
{
void _updateUI(bool success, string msg)
async Task _updateUI(bool success, string msg)
{
UpdateView(_geo, msg);
await UpdateView(_geo, msg);
if (success)
{
UpdatedPlusPlus(_geo, "");
@ -146,12 +148,12 @@ public class CheckUpdateViewModel : MyReactiveObject
private async Task CheckUpdateN(bool preRelease)
{
void _updateUI(bool success, string msg)
async Task _updateUI(bool success, string msg)
{
UpdateView(_v2rayN, msg);
await UpdateView(_v2rayN, msg);
if (success)
{
UpdateView(_v2rayN, ResUI.OperationSuccess);
await UpdateView(_v2rayN, ResUI.OperationSuccess);
UpdatedPlusPlus(_v2rayN, msg);
}
}
@ -164,12 +166,12 @@ public class CheckUpdateViewModel : MyReactiveObject
private async Task CheckUpdateCore(CheckUpdateModel model, bool preRelease)
{
void _updateUI(bool success, string msg)
async Task _updateUI(bool success, string msg)
{
UpdateView(model.CoreType, msg);
await UpdateView(model.CoreType, msg);
if (success)
{
UpdateView(model.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore);
await UpdateView(model.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore);
UpdatedPlusPlus(model.CoreType, msg);
}
@ -193,7 +195,7 @@ public class CheckUpdateViewModel : MyReactiveObject
if (_lstUpdated.Any(x => x.CoreType == _v2rayN && x.IsFinished == true))
{
await Task.Delay(1000);
UpgradeN();
await UpgradeN();
}
await Task.Delay(1000);
_updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, true);
@ -212,7 +214,7 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
private void UpgradeN()
private async Task UpgradeN()
{
try
{
@ -223,14 +225,14 @@ public class CheckUpdateViewModel : MyReactiveObject
}
if (!Utils.UpgradeAppExists(out _))
{
UpdateView(_v2rayN, ResUI.UpgradeAppNotExistTip);
await UpdateView(_v2rayN, ResUI.UpgradeAppNotExistTip);
return;
}
Locator.Current.GetService<MainWindowViewModel>()?.UpgradeApp(fileName);
}
catch (Exception ex)
{
UpdateView(_v2rayN, ex.Message);
await UpdateView(_v2rayN, ex.Message);
}
}
@ -281,7 +283,7 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
UpdateView(item.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfully);
await UpdateView(item.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfully);
if (File.Exists(fileName))
{
@ -290,21 +292,24 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
private void UpdateView(string coreType, string msg)
private async Task UpdateView(string coreType, string msg)
{
var item = new CheckUpdateModel()
{
CoreType = coreType,
Remarks = msg,
};
_updateView?.Invoke(EViewAction.DispatcherCheckUpdate, item);
await _updateView?.Invoke(EViewAction.DispatcherCheckUpdate, item);
}
public void UpdateViewResult(CheckUpdateModel model)
{
var found = _checkUpdateModel.FirstOrDefault(t => t.CoreType == model.CoreType);
if (found == null)
{
return;
}
var itemCopy = JsonUtils.DeepCopy(found);
itemCopy.Remarks = model.Remarks;
_checkUpdateModel.Replace(found, itemCopy);

View file

@ -373,14 +373,14 @@ public class ClashProxiesViewModel : MyReactiveObject
private async Task ProxiesDelayTest(bool blAll = true)
{
ClashApiManager.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), (item, result) =>
ClashApiManager.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) =>
{
if (item == null || result.IsNullOrEmpty())
{
return;
}
_updateView?.Invoke(EViewAction.DispatcherProxiesDelayTest, new SpeedTestResult() { IndexId = item.Name, Delay = result });
await _updateView?.Invoke(EViewAction.DispatcherProxiesDelayTest, new SpeedTestResult() { IndexId = item.Name, Delay = result });
});
await Task.CompletedTask;
}

View file

@ -245,7 +245,7 @@ public class MainWindowViewModel : MyReactiveObject
#region Actions
private void UpdateHandler(bool notify, string msg)
private async Task UpdateHandler(bool notify, string msg)
{
NoticeManager.Instance.SendMessage(msg);
if (notify)
@ -254,7 +254,7 @@ public class MainWindowViewModel : MyReactiveObject
}
}
private void UpdateTaskHandler(bool success, string msg)
private async Task UpdateTaskHandler(bool success, string msg)
{
NoticeManager.Instance.SendMessageEx(msg);
if (success)
@ -263,7 +263,7 @@ public class MainWindowViewModel : MyReactiveObject
RefreshServers();
if (indexIdOld != _config.IndexId)
{
_ = Reload();
await Reload();
}
if (_config.UiItem.EnableAutoAdjustMainLvColWidth)
{
@ -272,13 +272,13 @@ public class MainWindowViewModel : MyReactiveObject
}
}
private void UpdateStatisticsHandler(ServerSpeedItem update)
private async Task UpdateStatisticsHandler(ServerSpeedItem update)
{
if (!_config.UiItem.ShowInTaskbar)
{
return;
}
_updateView?.Invoke(EViewAction.DispatcherStatistics, update);
await _updateView?.Invoke(EViewAction.DispatcherStatistics, update);
}
public void SetStatisticsResult(ServerSpeedItem update)
@ -596,7 +596,9 @@ public class MainWindowViewModel : MyReactiveObject
Locator.Current.GetService<ClashProxiesViewModel>()?.ProxiesReload();
}
else
{ TabMainSelectedIndex = 0; }
{
TabMainSelectedIndex = 0;
}
}
private async Task LoadCore()
@ -631,7 +633,7 @@ public class MainWindowViewModel : MyReactiveObject
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler);
await Reload();
}