Skip to content

Commit dc2877d

Browse files
committed
Refactor UpdateSubscriptionProcess ,add SubscriptionHandler
1 parent 89d6af8 commit dc2877d

File tree

4 files changed

+136
-135
lines changed

4 files changed

+136
-135
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
namespace ServiceLib.Handler;
2+
3+
public class SubscriptionHandler
4+
{
5+
public static async Task UpdateProcess(Config config, string subId, bool blProxy, Action<bool, string> updateFunc)
6+
{
7+
updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart);
8+
var subItem = await AppHandler.Instance.SubItems();
9+
10+
if (subItem is not { Count: > 0 })
11+
{
12+
updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription);
13+
return;
14+
}
15+
16+
foreach (var item in subItem)
17+
{
18+
var id = item.Id.TrimEx();
19+
var url = item.Url.TrimEx();
20+
var userAgent = item.UserAgent.TrimEx();
21+
var hashCode = $"{item.Remarks}->";
22+
if (id.IsNullOrEmpty() || url.IsNullOrEmpty() || (subId.IsNotEmpty() && item.Id != subId))
23+
{
24+
//_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgNoValidSubscription}");
25+
continue;
26+
}
27+
if (!url.StartsWith(Global.HttpsProtocol) && !url.StartsWith(Global.HttpProtocol))
28+
{
29+
continue;
30+
}
31+
if (item.Enabled == false)
32+
{
33+
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}");
34+
continue;
35+
}
36+
37+
var downloadHandle = new DownloadService();
38+
downloadHandle.Error += (sender2, args) =>
39+
{
40+
updateFunc?.Invoke(false, $"{hashCode}{args.GetException().Message}");
41+
};
42+
43+
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
44+
45+
//one url
46+
url = Utils.GetPunycode(url);
47+
//convert
48+
if (item.ConvertTarget.IsNotEmpty())
49+
{
50+
var subConvertUrl = config.ConstItem.SubConvertUrl.IsNullOrEmpty() ? Global.SubConvertUrls.FirstOrDefault() : config.ConstItem.SubConvertUrl;
51+
url = string.Format(subConvertUrl!, Utils.UrlEncode(url));
52+
if (!url.Contains("target="))
53+
{
54+
url += string.Format("&target={0}", item.ConvertTarget);
55+
}
56+
if (!url.Contains("config="))
57+
{
58+
url += string.Format("&config={0}", Global.SubConvertConfig.FirstOrDefault());
59+
}
60+
}
61+
var result = await downloadHandle.TryDownloadString(url, blProxy, userAgent);
62+
if (blProxy && result.IsNullOrEmpty())
63+
{
64+
result = await downloadHandle.TryDownloadString(url, false, userAgent);
65+
}
66+
67+
//more url
68+
if (item.ConvertTarget.IsNullOrEmpty() && item.MoreUrl.TrimEx().IsNotEmpty())
69+
{
70+
if (result.IsNotEmpty() && Utils.IsBase64String(result))
71+
{
72+
result = Utils.Base64Decode(result);
73+
}
74+
75+
var lstUrl = item.MoreUrl.TrimEx().Split(",") ?? [];
76+
foreach (var it in lstUrl)
77+
{
78+
var url2 = Utils.GetPunycode(it);
79+
if (url2.IsNullOrEmpty())
80+
{
81+
continue;
82+
}
83+
84+
var result2 = await downloadHandle.TryDownloadString(url2, blProxy, userAgent);
85+
if (blProxy && result2.IsNullOrEmpty())
86+
{
87+
result2 = await downloadHandle.TryDownloadString(url2, false, userAgent);
88+
}
89+
if (result2.IsNotEmpty())
90+
{
91+
if (Utils.IsBase64String(result2))
92+
{
93+
result += Environment.NewLine + Utils.Base64Decode(result2);
94+
}
95+
else
96+
{
97+
result += Environment.NewLine + result2;
98+
}
99+
}
100+
}
101+
}
102+
103+
if (result.IsNullOrEmpty())
104+
{
105+
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
106+
}
107+
else
108+
{
109+
updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
110+
if (result?.Length < 99)
111+
{
112+
updateFunc?.Invoke(false, $"{hashCode}{result}");
113+
}
114+
115+
var ret = await ConfigHandler.AddBatchServers(config, result, id, true);
116+
if (ret <= 0)
117+
{
118+
Logging.SaveLog("FailedImportSubscription");
119+
Logging.SaveLog(result);
120+
}
121+
updateFunc?.Invoke(false,
122+
ret > 0
123+
? $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}"
124+
: $"{hashCode}{ResUI.MsgFailedImportSubscription}");
125+
}
126+
updateFunc?.Invoke(false, "-------------------------------------------------------");
127+
128+
//await ConfigHandler.DedupServerList(config, id);
129+
}
130+
131+
updateFunc?.Invoke(true, $"{ResUI.MsgUpdateSubscriptionEnd}");
132+
}
133+
}

v2rayN/ServiceLib/Handler/TaskHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ private async Task UpdateTaskRunSubscription(Config config, Action<bool, string>
6363
}
6464

6565
Logging.SaveLog("Execute update subscription");
66-
var updateHandle = new UpdateService();
6766

6867
foreach (var item in lstSubs)
6968
{
70-
await updateHandle.UpdateSubscriptionProcess(config, item.Id, true, (bool success, string msg) =>
69+
await SubscriptionHandler.UpdateProcess(config, item.Id, true, (bool success, string msg) =>
7170
{
7271
updateFunc?.Invoke(success, msg);
7372
if (success)

v2rayN/ServiceLib/Services/UpdateService.cs

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ServiceLib.Services;
66
public class UpdateService
77
{
88
private Action<bool, string>? _updateFunc;
9-
private int _timeout = 30;
9+
private readonly int _timeout = 30;
1010
private static readonly string _tag = "UpdateService";
1111

1212
public async Task CheckUpdateGuiN(Config config, Action<bool, string> updateFunc, bool preRelease)
@@ -104,137 +104,6 @@ public async Task CheckUpdateCore(ECoreType type, Config config, Action<bool, st
104104
}
105105
}
106106

107-
public async Task UpdateSubscriptionProcess(Config config, string subId, bool blProxy, Action<bool, string> updateFunc)
108-
{
109-
_updateFunc = updateFunc;
110-
111-
_updateFunc?.Invoke(false, ResUI.MsgUpdateSubscriptionStart);
112-
var subItem = await AppHandler.Instance.SubItems();
113-
114-
if (subItem is not { Count: > 0 })
115-
{
116-
_updateFunc?.Invoke(false, ResUI.MsgNoValidSubscription);
117-
return;
118-
}
119-
120-
foreach (var item in subItem)
121-
{
122-
var id = item.Id.TrimEx();
123-
var url = item.Url.TrimEx();
124-
var userAgent = item.UserAgent.TrimEx();
125-
var hashCode = $"{item.Remarks}->";
126-
if (id.IsNullOrEmpty() || url.IsNullOrEmpty() || (subId.IsNotEmpty() && item.Id != subId))
127-
{
128-
//_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgNoValidSubscription}");
129-
continue;
130-
}
131-
if (!url.StartsWith(Global.HttpsProtocol) && !url.StartsWith(Global.HttpProtocol))
132-
{
133-
continue;
134-
}
135-
if (item.Enabled == false)
136-
{
137-
_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSkipSubscriptionUpdate}");
138-
continue;
139-
}
140-
141-
var downloadHandle = new DownloadService();
142-
downloadHandle.Error += (sender2, args) =>
143-
{
144-
_updateFunc?.Invoke(false, $"{hashCode}{args.GetException().Message}");
145-
};
146-
147-
_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}");
148-
149-
//one url
150-
url = Utils.GetPunycode(url);
151-
//convert
152-
if (item.ConvertTarget.IsNotEmpty())
153-
{
154-
var subConvertUrl = config.ConstItem.SubConvertUrl.IsNullOrEmpty() ? Global.SubConvertUrls.FirstOrDefault() : config.ConstItem.SubConvertUrl;
155-
url = string.Format(subConvertUrl!, Utils.UrlEncode(url));
156-
if (!url.Contains("target="))
157-
{
158-
url += string.Format("&target={0}", item.ConvertTarget);
159-
}
160-
if (!url.Contains("config="))
161-
{
162-
url += string.Format("&config={0}", Global.SubConvertConfig.FirstOrDefault());
163-
}
164-
}
165-
var result = await downloadHandle.TryDownloadString(url, blProxy, userAgent);
166-
if (blProxy && result.IsNullOrEmpty())
167-
{
168-
result = await downloadHandle.TryDownloadString(url, false, userAgent);
169-
}
170-
171-
//more url
172-
if (item.ConvertTarget.IsNullOrEmpty() && item.MoreUrl.TrimEx().IsNotEmpty())
173-
{
174-
if (result.IsNotEmpty() && Utils.IsBase64String(result))
175-
{
176-
result = Utils.Base64Decode(result);
177-
}
178-
179-
var lstUrl = item.MoreUrl.TrimEx().Split(",") ?? [];
180-
foreach (var it in lstUrl)
181-
{
182-
var url2 = Utils.GetPunycode(it);
183-
if (url2.IsNullOrEmpty())
184-
{
185-
continue;
186-
}
187-
188-
var result2 = await downloadHandle.TryDownloadString(url2, blProxy, userAgent);
189-
if (blProxy && result2.IsNullOrEmpty())
190-
{
191-
result2 = await downloadHandle.TryDownloadString(url2, false, userAgent);
192-
}
193-
if (result2.IsNotEmpty())
194-
{
195-
if (Utils.IsBase64String(result2))
196-
{
197-
result += Environment.NewLine + Utils.Base64Decode(result2);
198-
}
199-
else
200-
{
201-
result += Environment.NewLine + result2;
202-
}
203-
}
204-
}
205-
}
206-
207-
if (result.IsNullOrEmpty())
208-
{
209-
_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}");
210-
}
211-
else
212-
{
213-
_updateFunc?.Invoke(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}");
214-
if (result?.Length < 99)
215-
{
216-
_updateFunc?.Invoke(false, $"{hashCode}{result}");
217-
}
218-
219-
var ret = await ConfigHandler.AddBatchServers(config, result, id, true);
220-
if (ret <= 0)
221-
{
222-
Logging.SaveLog("FailedImportSubscription");
223-
Logging.SaveLog(result);
224-
}
225-
_updateFunc?.Invoke(false,
226-
ret > 0
227-
? $"{hashCode}{ResUI.MsgUpdateSubscriptionEnd}"
228-
: $"{hashCode}{ResUI.MsgFailedImportSubscription}");
229-
}
230-
_updateFunc?.Invoke(false, "-------------------------------------------------------");
231-
232-
//await ConfigHandler.DedupServerList(config, id);
233-
}
234-
235-
_updateFunc?.Invoke(true, $"{ResUI.MsgUpdateSubscriptionEnd}");
236-
}
237-
238107
public async Task UpdateGeoFileAll(Config config, Action<bool, string> updateFunc)
239108
{
240109
await UpdateGeoFiles(config, updateFunc);

v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ private async Task SubSettingAsync()
477477

478478
public async Task UpdateSubscriptionProcess(string subId, bool blProxy)
479479
{
480-
await (new UpdateService()).UpdateSubscriptionProcess(_config, subId, blProxy, UpdateTaskHandler);
480+
await SubscriptionHandler.UpdateProcess(_config, subId, blProxy, UpdateTaskHandler);
481481
}
482482

483483
#endregion Subscription

0 commit comments

Comments
 (0)