Compare commits

..

No commits in common. "a108eaf34bef55029422f4d617079a0ec66ea18d" and "abdafc9b3be60cc26a0a09b206384f37b31d01d3" have entirely different histories.

14 changed files with 160 additions and 176 deletions

View file

@ -2,18 +2,17 @@ namespace ServiceLib.Handler;
public static class SubscriptionHandler
{
public static async Task UpdateProcess(Config config, string subId, bool blProxy, Func<bool, string, Task> updateFunc)
public static async Task UpdateProcess(Config config, string subId, bool blProxy, Action<bool, string> updateFunc)
{
await updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart);
updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart);
var subItem = await AppManager.Instance.SubItems();
if (subItem is not { Count: > 0 })
{
await updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription);
updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription);
return;
}
var successCount = 0;
foreach (var item in subItem)
{
try
@ -26,35 +25,32 @@ public static class SubscriptionHandler
var hashCode = $"{item.Remarks}->";
if (item.Enabled == false)
{
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}");
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}");
continue;
}
// Create download handler
var downloadHandle = CreateDownloadHandler(hashCode, updateFunc);
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
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
if (await ProcessDownloadResult(config, item.Id, result, hashCode, updateFunc))
{
successCount++;
}
await ProcessDownloadResult(config, item.Id, result, hashCode, updateFunc);
await updateFunc?.Invoke(false, "-------------------------------------------------------");
updateFunc?.Invoke(false, "-------------------------------------------------------");
}
catch (Exception ex)
{
var hashCode = $"{item.Remarks}->";
Logging.SaveLog("UpdateSubscription", ex);
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgFailedImportSubscription}: {ex.Message}");
await updateFunc?.Invoke(false, "-------------------------------------------------------");
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgFailedImportSubscription}: {ex.Message}");
updateFunc?.Invoke(false, "-------------------------------------------------------");
}
}
await updateFunc?.Invoke(successCount > 0, $"{ResUI.MsgUpdateSubscriptionEnd}");
updateFunc?.Invoke(true, $"{ResUI.MsgUpdateSubscriptionEnd}");
}
private static bool IsValidSubscription(SubItem item, string subId)
@ -80,7 +76,7 @@ public static class SubscriptionHandler
return true;
}
private static DownloadService CreateDownloadHandler(string hashCode, Func<bool, string, Task> updateFunc)
private static DownloadService CreateDownloadHandler(string hashCode, Action<bool, string> updateFunc)
{
var downloadHandle = new DownloadService();
downloadHandle.Error += (sender2, args) =>
@ -185,23 +181,23 @@ public static class SubscriptionHandler
return result;
}
private static async Task<bool> ProcessDownloadResult(Config config, string id, string result, string hashCode, Func<bool, string, Task> updateFunc)
private static async Task ProcessDownloadResult(Config config, string id, string result, string hashCode, Action<bool, string> updateFunc)
{
if (result.IsNullOrEmpty())
{
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
return false;
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
return;
}
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
// If result is too short, display content directly
if (result.Length < 99)
{
await updateFunc?.Invoke(false, $"{hashCode}{result}");
updateFunc?.Invoke(false, $"{hashCode}{result}");
}
await updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartParsingSubscription}");
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartParsingSubscription}");
// Add servers to configuration
var ret = await ConfigHandler.AddBatchServers(config, result, id, true);
@ -212,10 +208,9 @@ public static class SubscriptionHandler
}
// Update completion message
await updateFunc?.Invoke(false, ret > 0
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, Func<ClashProxyModel?, string, Task> updateFunc)
public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Action<ClashProxyModel?, string> updateFunc)
{
Task.Run(async () =>
{
@ -79,12 +79,12 @@ public sealed class ClashApiManager
tasks.Add(Task.Run(async () =>
{
var result = await HttpClientHelper.Instance.TryGetAsync(url);
await updateFunc?.Invoke(it, result);
updateFunc?.Invoke(it, result);
}));
}
await Task.WhenAll(tasks);
await Task.Delay(1000);
await updateFunc?.Invoke(null, "");
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 Func<bool, string, Task>? _updateFunc;
private Action<bool, string>? _updateFunc;
private int _linuxSudoPid = -1;
private const string _tag = "CoreAdminHandler";
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
public async Task Init(Config config, Action<bool, string> updateFunc)
{
if (_config != null)
{
@ -26,9 +26,9 @@ public class CoreAdminManager
await Task.CompletedTask;
}
private async Task UpdateFunc(bool notify, string msg)
private void UpdateFunc(bool notify, string msg)
{
await _updateFunc?.Invoke(notify, msg);
_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();
await UpdateFunc(false, result.StandardOutput.ToString());
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 Func<bool, string, Task>? _updateFunc;
private Action<bool, string>? _updateFunc;
private const string _tag = "CoreHandler";
public async Task Init(Config config, Func<bool, string, Task> updateFunc)
public async Task Init(Config config, Action<bool, string> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -63,7 +63,7 @@ public class CoreManager
{
if (node == null)
{
await UpdateFunc(false, ResUI.CheckServerSettings);
UpdateFunc(false, ResUI.CheckServerSettings);
return;
}
@ -71,13 +71,13 @@ public class CoreManager
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
if (result.Success != true)
{
await UpdateFunc(true, result.Msg);
UpdateFunc(true, result.Msg);
return;
}
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")));
UpdateFunc(false, $"{node.GetSummary()}");
UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
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)
{
await UpdateFunc(true, $"{node.GetSummary()}");
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);
await UpdateFunc(false, result.Msg);
UpdateFunc(false, result.Msg);
if (result.Success != true)
{
return -1;
}
await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await UpdateFunc(false, configPath);
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
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 async Task UpdateFunc(bool notify, string msg)
private void UpdateFunc(bool notify, string msg)
{
await _updateFunc?.Invoke(notify, msg);
_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())
{
await UpdateFunc(false, msg);
UpdateFunc(false, msg);
return null;
}
@ -251,7 +251,7 @@ public class CoreManager
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
await UpdateFunc(mayNeedSudo, ex.Message);
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 Func<ServerSpeedItem, Task>? _updateFunc;
private Action<ServerSpeedItem>? _updateFunc;
private StatisticsXrayService? _statisticsXray;
private StatisticsSingboxService? _statisticsSingbox;
private static readonly string _tag = "StatisticsHandler";
public List<ServerStatItem> ServerStat => _lstServerStat;
public async Task Init(Config config, Func<ServerSpeedItem, Task> updateFunc)
public async Task Init(Config config, Action<ServerSpeedItem> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -97,9 +97,9 @@ public class StatisticsManager
_lstServerStat = await SQLiteHelper.Instance.TableAsync<ServerStatItem>().ToListAsync();
}
private async Task UpdateServerStatHandler(ServerSpeedItem server)
private void UpdateServerStatHandler(ServerSpeedItem server)
{
await UpdateServerStat(server);
_ = 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;
await _updateFunc?.Invoke(server);
_updateFunc?.Invoke(server);
}
private async Task GetServerStatItem(string indexId)

View file

@ -4,18 +4,13 @@ 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, Func<bool, string, Task> updateFunc)
public void RegUpdateTask(Config config, Action<bool, string> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
Task.Run(ScheduledTasks);
Task.Run(() => ScheduledTasks(config, updateFunc));
}
private async Task ScheduledTasks()
private async Task ScheduledTasks(Config config, Action<bool, string> updateFunc)
{
Logging.SaveLog("Setup Scheduled Tasks");
@ -26,14 +21,14 @@ public class TaskManager
await Task.Delay(1000 * 60);
//Execute once 1 minute
await UpdateTaskRunSubscription();
await UpdateTaskRunSubscription(config, updateFunc);
//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();
}
@ -47,14 +42,14 @@ public class TaskManager
FileManager.DeleteExpiredFiles(Utils.GetTempPath(), DateTime.Now.AddMonths(-1));
//Check once 1 hour
await UpdateTaskRunGeo(numOfExecuted / 60);
await UpdateTaskRunGeo(config, numOfExecuted / 60, updateFunc);
}
numOfExecuted++;
}
}
private async Task UpdateTaskRunSubscription()
private async Task UpdateTaskRunSubscription(Config config, Action<bool, string> updateFunc)
{
var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds();
var lstSubs = (await AppManager.Instance.SubItems())?
@ -71,30 +66,30 @@ public class TaskManager
foreach (var item in lstSubs)
{
await SubscriptionHandler.UpdateProcess(_config, item.Id, true, async (success, msg) =>
await SubscriptionHandler.UpdateProcess(config, item.Id, true, (success, msg) =>
{
await _updateFunc?.Invoke(success, msg);
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(int hours)
private async Task UpdateTaskRunGeo(Config config, int hours, Action<bool, string> updateFunc)
{
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, async (success, msg) =>
await updateHandle.UpdateGeoFileAll(config, (success, msg) =>
{
await _updateFunc?.Invoke(false, msg);
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, Func<bool, string, Task> updateFunc)
public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action<bool, string> updateFunc)
{
try
{
@ -31,10 +31,10 @@ public class DownloadService
}
catch (Exception ex)
{
await updateFunc?.Invoke(false, ex.Message);
updateFunc?.Invoke(false, ex.Message);
if (ex.InnerException != null)
{
await updateFunc?.Invoke(false, ex.InnerException.Message);
updateFunc?.Invoke(false, ex.InnerException.Message);
}
}
return 0;

View file

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

View file

@ -5,11 +5,11 @@ namespace ServiceLib.Services;
public class UpdateService
{
private Func<bool, string, Task>? _updateFunc;
private Action<bool, string>? _updateFunc;
private readonly int _timeout = 30;
private static readonly string _tag = "UpdateService";
public async Task CheckUpdateGuiN(Config config, Func<bool, string, Task> updateFunc, bool preRelease)
public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
{
_updateFunc = updateFunc;
var url = string.Empty;
@ -20,25 +20,25 @@ public class UpdateService
{
if (args.Success)
{
UpdateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
UpdateFunc(true, Utils.UrlEncode(fileName));
_updateFunc?.Invoke(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
_updateFunc?.Invoke(true, Utils.UrlEncode(fileName));
}
else
{
UpdateFunc(false, args.Msg);
_updateFunc?.Invoke(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
UpdateFunc(false, args.GetException().Message);
_updateFunc?.Invoke(false, args.GetException().Message);
};
await UpdateFunc(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN));
_updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN));
var result = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease);
if (result.Success)
{
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
await UpdateFunc(false, result.Msg);
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
_updateFunc?.Invoke(false, result.Msg);
url = result.Data?.ToString();
fileName = Utils.GetTempPath(Utils.GetGuid());
@ -46,11 +46,11 @@ public class UpdateService
}
else
{
await UpdateFunc(false, result.Msg);
_updateFunc?.Invoke(false, result.Msg);
}
}
public async Task CheckUpdateCore(ECoreType type, Config config, Func<bool, string, Task> updateFunc, bool preRelease)
public async Task CheckUpdateCore(ECoreType type, Config config, Action<bool, string> updateFunc, bool preRelease)
{
_updateFunc = updateFunc;
var url = string.Empty;
@ -61,34 +61,34 @@ public class UpdateService
{
if (args.Success)
{
UpdateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
UpdateFunc(false, ResUI.MsgUnpacking);
_updateFunc?.Invoke(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
_updateFunc?.Invoke(false, ResUI.MsgUnpacking);
try
{
UpdateFunc(true, fileName);
_updateFunc?.Invoke(true, fileName);
}
catch (Exception ex)
{
UpdateFunc(false, ex.Message);
_updateFunc?.Invoke(false, ex.Message);
}
}
else
{
UpdateFunc(false, args.Msg);
_updateFunc?.Invoke(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
UpdateFunc(false, args.GetException().Message);
_updateFunc?.Invoke(false, args.GetException().Message);
};
await UpdateFunc(false, string.Format(ResUI.MsgStartUpdating, type));
_updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, type));
var result = await CheckUpdateAsync(downloadHandle, type, preRelease);
if (result.Success)
{
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type));
await UpdateFunc(false, result.Msg);
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type));
_updateFunc?.Invoke(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())
{
await UpdateFunc(false, result.Msg);
_updateFunc?.Invoke(false, result.Msg);
}
}
}
public async Task UpdateGeoFileAll(Config config, Func<bool, string, Task> updateFunc)
public async Task UpdateGeoFileAll(Config config, Action<bool, string> updateFunc)
{
await UpdateGeoFiles(config, updateFunc);
await UpdateOtherFiles(config, updateFunc);
await UpdateSrsFileAll(config, updateFunc);
await UpdateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
_updateFunc?.Invoke(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
}
#region CheckUpdate private
@ -128,7 +128,7 @@ public class UpdateService
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
await UpdateFunc(false, ex.Message);
_updateFunc?.Invoke(false, ex.Message);
return new RetResult(false, ex.Message);
}
}
@ -212,7 +212,7 @@ public class UpdateService
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
await UpdateFunc(false, ex.Message);
_updateFunc?.Invoke(false, ex.Message);
return new SemanticVersion("");
}
}
@ -272,7 +272,7 @@ public class UpdateService
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
await UpdateFunc(false, ex.Message);
_updateFunc?.Invoke(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, Func<bool, string, Task> updateFunc)
private async Task UpdateGeoFiles(Config config, Action<bool, string> updateFunc)
{
_updateFunc = updateFunc;
@ -352,7 +352,7 @@ public class UpdateService
}
}
private async Task UpdateOtherFiles(Config config, Func<bool, string, Task> updateFunc)
private async Task UpdateOtherFiles(Config config, Action<bool, string> 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, Func<bool, string, Task> updateFunc)
private async Task UpdateSrsFileAll(Config config, Action<bool, string> updateFunc)
{
_updateFunc = updateFunc;
@ -426,7 +426,7 @@ public class UpdateService
}
}
private async Task UpdateSrsFile(string type, string srsName, Config config, Func<bool, string, Task> updateFunc)
private async Task UpdateSrsFile(string type, string srsName, Config config, Action<bool, string> 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, Func<bool, string, Task> updateFunc)
private async Task DownloadGeoFile(string url, string fileName, string targetPath, Action<bool, string> updateFunc)
{
var tmpFileName = Utils.GetTempPath(Utils.GetGuid());
@ -448,7 +448,7 @@ public class UpdateService
{
if (args.Success)
{
UpdateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName));
_updateFunc?.Invoke(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName));
try
{
@ -457,31 +457,26 @@ public class UpdateService
File.Copy(tmpFileName, targetPath, true);
File.Delete(tmpFileName);
//await UpdateFunc(true, "");
//_updateFunc?.Invoke(true, "");
}
}
catch (Exception ex)
{
UpdateFunc(false, ex.Message);
_updateFunc?.Invoke(false, ex.Message);
}
}
else
{
UpdateFunc(false, args.Msg);
_updateFunc?.Invoke(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
UpdateFunc(false, args.GetException().Message);
_updateFunc?.Invoke(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,11 +89,9 @@ public class CheckUpdateViewModel : MyReactiveObject
{
var item = _checkUpdateModel[k];
if (item.IsSelected != true)
{
continue;
}
await UpdateView(item.CoreType, "...");
UpdateView(item.CoreType, "...");
if (item.CoreType == _geo)
{
await CheckUpdateGeo();
@ -131,9 +129,9 @@ public class CheckUpdateViewModel : MyReactiveObject
private async Task CheckUpdateGeo()
{
async Task _updateUI(bool success, string msg)
void _updateUI(bool success, string msg)
{
await UpdateView(_geo, msg);
UpdateView(_geo, msg);
if (success)
{
UpdatedPlusPlus(_geo, "");
@ -148,12 +146,12 @@ public class CheckUpdateViewModel : MyReactiveObject
private async Task CheckUpdateN(bool preRelease)
{
async Task _updateUI(bool success, string msg)
void _updateUI(bool success, string msg)
{
await UpdateView(_v2rayN, msg);
UpdateView(_v2rayN, msg);
if (success)
{
await UpdateView(_v2rayN, ResUI.OperationSuccess);
UpdateView(_v2rayN, ResUI.OperationSuccess);
UpdatedPlusPlus(_v2rayN, msg);
}
}
@ -166,12 +164,12 @@ public class CheckUpdateViewModel : MyReactiveObject
private async Task CheckUpdateCore(CheckUpdateModel model, bool preRelease)
{
async Task _updateUI(bool success, string msg)
void _updateUI(bool success, string msg)
{
await UpdateView(model.CoreType, msg);
UpdateView(model.CoreType, msg);
if (success)
{
await UpdateView(model.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore);
UpdateView(model.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore);
UpdatedPlusPlus(model.CoreType, msg);
}
@ -195,7 +193,7 @@ public class CheckUpdateViewModel : MyReactiveObject
if (_lstUpdated.Any(x => x.CoreType == _v2rayN && x.IsFinished == true))
{
await Task.Delay(1000);
await UpgradeN();
UpgradeN();
}
await Task.Delay(1000);
_updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, true);
@ -214,7 +212,7 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
private async Task UpgradeN()
private void UpgradeN()
{
try
{
@ -225,14 +223,14 @@ public class CheckUpdateViewModel : MyReactiveObject
}
if (!Utils.UpgradeAppExists(out _))
{
await UpdateView(_v2rayN, ResUI.UpgradeAppNotExistTip);
UpdateView(_v2rayN, ResUI.UpgradeAppNotExistTip);
return;
}
Locator.Current.GetService<MainWindowViewModel>()?.UpgradeApp(fileName);
}
catch (Exception ex)
{
await UpdateView(_v2rayN, ex.Message);
UpdateView(_v2rayN, ex.Message);
}
}
@ -283,7 +281,7 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
await UpdateView(item.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfully);
UpdateView(item.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfully);
if (File.Exists(fileName))
{
@ -292,24 +290,21 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
private async Task UpdateView(string coreType, string msg)
private void UpdateView(string coreType, string msg)
{
var item = new CheckUpdateModel()
{
CoreType = coreType,
Remarks = msg,
};
await _updateView?.Invoke(EViewAction.DispatcherCheckUpdate, item);
_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(), async (item, result) =>
ClashApiManager.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), (item, result) =>
{
if (item == null || result.IsNullOrEmpty())
{
return;
}
await _updateView?.Invoke(EViewAction.DispatcherProxiesDelayTest, new SpeedTestResult() { IndexId = item.Name, Delay = result });
_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 async Task UpdateHandler(bool notify, string msg)
private void UpdateHandler(bool notify, string msg)
{
NoticeManager.Instance.SendMessage(msg);
if (notify)
@ -254,7 +254,7 @@ public class MainWindowViewModel : MyReactiveObject
}
}
private async Task UpdateTaskHandler(bool success, string msg)
private void UpdateTaskHandler(bool success, string msg)
{
NoticeManager.Instance.SendMessageEx(msg);
if (success)
@ -263,7 +263,7 @@ public class MainWindowViewModel : MyReactiveObject
RefreshServers();
if (indexIdOld != _config.IndexId)
{
await Reload();
_ = Reload();
}
if (_config.UiItem.EnableAutoAdjustMainLvColWidth)
{
@ -272,13 +272,13 @@ public class MainWindowViewModel : MyReactiveObject
}
}
private async Task UpdateStatisticsHandler(ServerSpeedItem update)
private void UpdateStatisticsHandler(ServerSpeedItem update)
{
if (!_config.UiItem.ShowInTaskbar)
{
return;
}
await _updateView?.Invoke(EViewAction.DispatcherStatistics, update);
_updateView?.Invoke(EViewAction.DispatcherStatistics, update);
}
public void SetStatisticsResult(ServerSpeedItem update)
@ -596,9 +596,7 @@ public class MainWindowViewModel : MyReactiveObject
Locator.Current.GetService<ClashProxiesViewModel>()?.ProxiesReload();
}
else
{
TabMainSelectedIndex = 0;
}
{ TabMainSelectedIndex = 0; }
}
private async Task LoadCore()
@ -633,7 +631,7 @@ public class MainWindowViewModel : MyReactiveObject
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler);
await Reload();
}