From dc2877d8176a92e0d25592e5fd695609a8f53f83 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 14 Aug 2025 20:59:15 +0800 Subject: [PATCH] =?UTF-8?q?Refactor=20UpdateSubscriptionProcess=20?= =?UTF-8?q?=EF=BC=8Cadd=20SubscriptionHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ServiceLib/Handler/SubscriptionHandler.cs | 133 ++++++++++++++++++ v2rayN/ServiceLib/Handler/TaskHandler.cs | 3 +- v2rayN/ServiceLib/Services/UpdateService.cs | 133 +----------------- .../ViewModels/MainWindowViewModel.cs | 2 +- 4 files changed, 136 insertions(+), 135 deletions(-) create mode 100644 v2rayN/ServiceLib/Handler/SubscriptionHandler.cs diff --git a/v2rayN/ServiceLib/Handler/SubscriptionHandler.cs b/v2rayN/ServiceLib/Handler/SubscriptionHandler.cs new file mode 100644 index 00000000..b06700f8 --- /dev/null +++ b/v2rayN/ServiceLib/Handler/SubscriptionHandler.cs @@ -0,0 +1,133 @@ +namespace ServiceLib.Handler; + +public class SubscriptionHandler +{ + public static async Task UpdateProcess(Config config, string subId, bool blProxy, Action updateFunc) + { + updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart); + var subItem = await AppHandler.Instance.SubItems(); + + if (subItem is not { Count: > 0 }) + { + updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription); + return; + } + + foreach (var item in subItem) + { + var id = item.Id.TrimEx(); + var url = item.Url.TrimEx(); + var userAgent = item.UserAgent.TrimEx(); + var hashCode = $"{item.Remarks}->"; + if (id.IsNullOrEmpty() || url.IsNullOrEmpty() || (subId.IsNotEmpty() && item.Id != subId)) + { + //_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgNoValidSubscription}"); + continue; + } + if (!url.StartsWith(Global.HttpsProtocol) && !url.StartsWith(Global.HttpProtocol)) + { + continue; + } + if (item.Enabled == false) + { + updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}"); + continue; + } + + var downloadHandle = new DownloadService(); + downloadHandle.Error += (sender2, args) => + { + updateFunc?.Invoke(false, $"{hashCode}{args.GetException().Message}"); + }; + + updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}"); + + //one url + url = Utils.GetPunycode(url); + //convert + if (item.ConvertTarget.IsNotEmpty()) + { + var subConvertUrl = config.ConstItem.SubConvertUrl.IsNullOrEmpty() ? Global.SubConvertUrls.FirstOrDefault() : config.ConstItem.SubConvertUrl; + url = string.Format(subConvertUrl!, Utils.UrlEncode(url)); + if (!url.Contains("target=")) + { + url += string.Format("&target={0}", item.ConvertTarget); + } + if (!url.Contains("config=")) + { + url += string.Format("&config={0}", Global.SubConvertConfig.FirstOrDefault()); + } + } + var result = await downloadHandle.TryDownloadString(url, blProxy, userAgent); + if (blProxy && result.IsNullOrEmpty()) + { + result = await downloadHandle.TryDownloadString(url, false, userAgent); + } + + //more url + if (item.ConvertTarget.IsNullOrEmpty() && item.MoreUrl.TrimEx().IsNotEmpty()) + { + if (result.IsNotEmpty() && Utils.IsBase64String(result)) + { + result = Utils.Base64Decode(result); + } + + var lstUrl = item.MoreUrl.TrimEx().Split(",") ?? []; + foreach (var it in lstUrl) + { + var url2 = Utils.GetPunycode(it); + if (url2.IsNullOrEmpty()) + { + continue; + } + + var result2 = await downloadHandle.TryDownloadString(url2, blProxy, userAgent); + if (blProxy && result2.IsNullOrEmpty()) + { + result2 = await downloadHandle.TryDownloadString(url2, false, userAgent); + } + if (result2.IsNotEmpty()) + { + if (Utils.IsBase64String(result2)) + { + result += Environment.NewLine + Utils.Base64Decode(result2); + } + else + { + result += Environment.NewLine + result2; + } + } + } + } + + if (result.IsNullOrEmpty()) + { + updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}"); + } + else + { + updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}"); + if (result?.Length < 99) + { + updateFunc?.Invoke(false, $"{hashCode}{result}"); + } + + var ret = await ConfigHandler.AddBatchServers(config, result, id, true); + if (ret <= 0) + { + Logging.SaveLog("FailedImportSubscription"); + Logging.SaveLog(result); + } + updateFunc?.Invoke(false, + ret > 0 + ? $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}" + : $"{hashCode}{ResUI.MsgFailedImportSubscription}"); + } + updateFunc?.Invoke(false, "-------------------------------------------------------"); + + //await ConfigHandler.DedupServerList(config, id); + } + + updateFunc?.Invoke(true, $"{ResUI.MsgUpdateSubscriptionEnd}"); + } +} diff --git a/v2rayN/ServiceLib/Handler/TaskHandler.cs b/v2rayN/ServiceLib/Handler/TaskHandler.cs index ef1d4e38..b49570be 100644 --- a/v2rayN/ServiceLib/Handler/TaskHandler.cs +++ b/v2rayN/ServiceLib/Handler/TaskHandler.cs @@ -63,11 +63,10 @@ public class TaskHandler } Logging.SaveLog("Execute update subscription"); - var updateHandle = new UpdateService(); foreach (var item in lstSubs) { - await updateHandle.UpdateSubscriptionProcess(config, item.Id, true, (bool success, string msg) => + await SubscriptionHandler.UpdateProcess(config, item.Id, true, (bool success, string msg) => { updateFunc?.Invoke(success, msg); if (success) diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index 831ef2c3..b60ddc6c 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -6,7 +6,7 @@ namespace ServiceLib.Services; public class UpdateService { private Action? _updateFunc; - private int _timeout = 30; + private readonly int _timeout = 30; private static readonly string _tag = "UpdateService"; public async Task CheckUpdateGuiN(Config config, Action updateFunc, bool preRelease) @@ -104,137 +104,6 @@ public class UpdateService } } - public async Task UpdateSubscriptionProcess(Config config, string subId, bool blProxy, Action updateFunc) - { - _updateFunc = updateFunc; - - _updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart); - var subItem = await AppHandler.Instance.SubItems(); - - if (subItem is not { Count: > 0 }) - { - _updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription); - return; - } - - foreach (var item in subItem) - { - var id = item.Id.TrimEx(); - var url = item.Url.TrimEx(); - var userAgent = item.UserAgent.TrimEx(); - var hashCode = $"{item.Remarks}->"; - if (id.IsNullOrEmpty() || url.IsNullOrEmpty() || (subId.IsNotEmpty() && item.Id != subId)) - { - //_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgNoValidSubscription}"); - continue; - } - if (!url.StartsWith(Global.HttpsProtocol) && !url.StartsWith(Global.HttpProtocol)) - { - continue; - } - if (item.Enabled == false) - { - _updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}"); - continue; - } - - var downloadHandle = new DownloadService(); - downloadHandle.Error += (sender2, args) => - { - _updateFunc?.Invoke(false, $"{hashCode}{args.GetException().Message}"); - }; - - _updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}"); - - //one url - url = Utils.GetPunycode(url); - //convert - if (item.ConvertTarget.IsNotEmpty()) - { - var subConvertUrl = config.ConstItem.SubConvertUrl.IsNullOrEmpty() ? Global.SubConvertUrls.FirstOrDefault() : config.ConstItem.SubConvertUrl; - url = string.Format(subConvertUrl!, Utils.UrlEncode(url)); - if (!url.Contains("target=")) - { - url += string.Format("&target={0}", item.ConvertTarget); - } - if (!url.Contains("config=")) - { - url += string.Format("&config={0}", Global.SubConvertConfig.FirstOrDefault()); - } - } - var result = await downloadHandle.TryDownloadString(url, blProxy, userAgent); - if (blProxy && result.IsNullOrEmpty()) - { - result = await downloadHandle.TryDownloadString(url, false, userAgent); - } - - //more url - if (item.ConvertTarget.IsNullOrEmpty() && item.MoreUrl.TrimEx().IsNotEmpty()) - { - if (result.IsNotEmpty() && Utils.IsBase64String(result)) - { - result = Utils.Base64Decode(result); - } - - var lstUrl = item.MoreUrl.TrimEx().Split(",") ?? []; - foreach (var it in lstUrl) - { - var url2 = Utils.GetPunycode(it); - if (url2.IsNullOrEmpty()) - { - continue; - } - - var result2 = await downloadHandle.TryDownloadString(url2, blProxy, userAgent); - if (blProxy && result2.IsNullOrEmpty()) - { - result2 = await downloadHandle.TryDownloadString(url2, false, userAgent); - } - if (result2.IsNotEmpty()) - { - if (Utils.IsBase64String(result2)) - { - result += Environment.NewLine + Utils.Base64Decode(result2); - } - else - { - result += Environment.NewLine + result2; - } - } - } - } - - if (result.IsNullOrEmpty()) - { - _updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}"); - } - else - { - _updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}"); - if (result?.Length < 99) - { - _updateFunc?.Invoke(false, $"{hashCode}{result}"); - } - - var ret = await ConfigHandler.AddBatchServers(config, result, id, true); - if (ret <= 0) - { - Logging.SaveLog("FailedImportSubscription"); - Logging.SaveLog(result); - } - _updateFunc?.Invoke(false, - ret > 0 - ? $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}" - : $"{hashCode}{ResUI.MsgFailedImportSubscription}"); - } - _updateFunc?.Invoke(false, "-------------------------------------------------------"); - - //await ConfigHandler.DedupServerList(config, id); - } - - _updateFunc?.Invoke(true, $"{ResUI.MsgUpdateSubscriptionEnd}"); - } - public async Task UpdateGeoFileAll(Config config, Action updateFunc) { await UpdateGeoFiles(config, updateFunc); diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 8355996d..8578036d 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -477,7 +477,7 @@ public class MainWindowViewModel : MyReactiveObject public async Task UpdateSubscriptionProcess(string subId, bool blProxy) { - await (new UpdateService()).UpdateSubscriptionProcess(_config, subId, blProxy, UpdateTaskHandler); + await SubscriptionHandler.UpdateProcess(_config, subId, blProxy, UpdateTaskHandler); } #endregion Subscription