mirror of https://github.com/2dust/v2rayN
Refactor UpdateSubscriptionProcess ,add SubscriptionHandler
parent
89d6af8fc9
commit
dc2877d817
|
@ -0,0 +1,133 @@
|
||||||
|
namespace ServiceLib.Handler;
|
||||||
|
|
||||||
|
public class SubscriptionHandler
|
||||||
|
{
|
||||||
|
public static async Task UpdateProcess(Config config, string subId, bool blProxy, Action<bool, string> 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}");
|
||||||
|
}
|
||||||
|
}
|
|
@ -63,11 +63,10 @@ public class TaskHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
Logging.SaveLog("Execute update subscription");
|
Logging.SaveLog("Execute update subscription");
|
||||||
var updateHandle = new UpdateService();
|
|
||||||
|
|
||||||
foreach (var item in lstSubs)
|
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);
|
updateFunc?.Invoke(success, msg);
|
||||||
if (success)
|
if (success)
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace ServiceLib.Services;
|
||||||
public class UpdateService
|
public class UpdateService
|
||||||
{
|
{
|
||||||
private Action<bool, string>? _updateFunc;
|
private Action<bool, string>? _updateFunc;
|
||||||
private int _timeout = 30;
|
private readonly int _timeout = 30;
|
||||||
private static readonly string _tag = "UpdateService";
|
private static readonly string _tag = "UpdateService";
|
||||||
|
|
||||||
public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
|
public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
|
||||||
|
@ -104,137 +104,6 @@ public class UpdateService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateSubscriptionProcess(Config config, string subId, bool blProxy, Action<bool, string> 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<bool, string> updateFunc)
|
public async Task UpdateGeoFileAll(Config config, Action<bool, string> updateFunc)
|
||||||
{
|
{
|
||||||
await UpdateGeoFiles(config, updateFunc);
|
await UpdateGeoFiles(config, updateFunc);
|
||||||
|
|
|
@ -477,7 +477,7 @@ public class MainWindowViewModel : MyReactiveObject
|
||||||
|
|
||||||
public async Task UpdateSubscriptionProcess(string subId, bool blProxy)
|
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
|
#endregion Subscription
|
||||||
|
|
Loading…
Reference in New Issue