-
Notifications
You must be signed in to change notification settings - Fork 975
Expand file tree
/
Copy pathCheckUpdates.cs
More file actions
343 lines (312 loc) · 14.5 KB
/
CheckUpdates.cs
File metadata and controls
343 lines (312 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace subs_check.win.gui
{
public partial class CheckUpdates : Form
{
// 添加一个属性用于存储和传递文本内容
public string UrlContent { get; set; }
public System.Windows.Forms.ComboBox.ObjectCollection githubProxys { get; set; }
public string githubProxy { get; set; }
string githubProxyURL;
public string 当前subsCheck版本号 { get; set; }
public string 当前GUI版本号 { get; set; }
public string 最新GUI版本号 { get; set; }
public CheckUpdates()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
timer1.Enabled = true;
if (githubProxys != null)
{
comboBox1.Items.Clear();
foreach (var item in githubProxys)
{
comboBox1.Items.Add(item);
}
}
if (!string.IsNullOrEmpty(githubProxy)) comboBox1.Text = githubProxy;
}
private async void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
label3.Text = 最新GUI版本号;
label4.Text = 当前GUI版本号;
label5.Text = 当前subsCheck版本号;
if (comboBox1.Text == "自动选择")
{
// 创建不包含"自动选择"的代理列表
List<string> proxyItems = new List<string>();
for (int j = 0; j < comboBox1.Items.Count; j++)
{
string proxyItem = comboBox1.Items[j].ToString();
if (proxyItem != "自动选择")
proxyItems.Add(proxyItem);
}
// 随机打乱列表顺序
Random random = new Random();
proxyItems = proxyItems.OrderBy(x => random.Next()).ToList();
// 异步检测可用代理
githubProxyURL = await DetectGitHubProxyAsync(proxyItems);
}
else
{
githubProxyURL = $"https://{comboBox1.Text}/";
}
if (最新GUI版本号 != 当前GUI版本号)
{
// 检查当前目录下是否存在 Upgrade.exe
string upgradeExePath = System.IO.Path.Combine(Application.StartupPath, "Upgrade.exe");
if (System.IO.File.Exists(upgradeExePath))
{
button1.Text = "立即更新";
button1.Enabled = true;
}
else
{
button1.Text = "缺少更新程序";
button1.Enabled = false;
}
}
else
{
button1.Text = "已是最新版本";
button1.Enabled = false;
}
using (HttpClient client = new HttpClient())
{
try
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36 (KHTML, like Gecko) cmliu/SubsCheck-Win-GUI");
client.Timeout = TimeSpan.FromSeconds(30); // 增加超时时间以适应下载需求
string url = "https://api.github.com/repos/beck-8/subs-check/releases/latest";
string 备用url = "https://api.github.cmliussss.net/repos/beck-8/subs-check/releases/latest";
HttpResponseMessage response = null;
string responseBody = null;
JObject json = null;
// 先尝试主URL
try
{
response = await client.GetAsync(url);
// 如果主URL请求成功返回有效数据
if (response.IsSuccessStatusCode)
{
responseBody = await response.Content.ReadAsStringAsync();
json = JObject.Parse(responseBody);
Console.WriteLine("成功从主API获取版本信息");
}
// 如果主URL请求不成功但没有抛出异常
else
{
Console.WriteLine($"主API请求失败 HTTP {(int)response.StatusCode},尝试备用API...");
response = await client.GetAsync(备用url);
if (response.IsSuccessStatusCode)
{
responseBody = await response.Content.ReadAsStringAsync();
json = JObject.Parse(responseBody);
Console.WriteLine("成功从备用API获取版本信息");
}
else
{
Console.WriteLine($"备用API也请求失败: HTTP {(int)response.StatusCode}", true);
return; // 两个URL都失败,提前退出
}
}
}
// 捕获网络请求异常(如连接超时、无法解析域名等)
catch (HttpRequestException ex)
{
Console.WriteLine($"主API请求出错: {ex.Message},尝试备用API...");
try
{
response = await client.GetAsync(备用url);
if (response.IsSuccessStatusCode)
{
responseBody = await response.Content.ReadAsStringAsync();
json = JObject.Parse(responseBody);
Console.WriteLine("成功从备用API获取版本信息");
}
else
{
Console.WriteLine($"备用API也请求失败: HTTP {(int)response.StatusCode}", true);
return; // 备用URL也失败,提前退出
}
}
catch (Exception backupEx)
{
Console.WriteLine($"备用API请求也出错: {backupEx.Message}", true);
return; // 连备用URL也异常,提前退出
}
}
// 捕获JSON解析异常
catch (Newtonsoft.Json.JsonException ex)
{
Console.WriteLine($"解析JSON数据出错: {ex.Message}", true);
try
{
response = await client.GetAsync(备用url);
if (response.IsSuccessStatusCode)
{
responseBody = await response.Content.ReadAsStringAsync();
json = JObject.Parse(responseBody);
Console.WriteLine("成功从备用API获取版本信息");
}
}
catch (Exception backupEx)
{
Console.WriteLine($"备用API请求也出错: {backupEx.Message}", true);
return; // 连备用URL也有问题,提前退出
}
}
// 捕获其他所有异常
catch (Exception ex)
{
Console.WriteLine($"获取版本信息时出现未预期的错误: {ex.Message}", true);
try
{
response = await client.GetAsync(备用url);
if (response.IsSuccessStatusCode)
{
responseBody = await response.Content.ReadAsStringAsync();
json = JObject.Parse(responseBody);
Console.WriteLine("成功从备用URL获取版本信息");
}
}
catch (Exception backupEx)
{
//控制台打印错误
Console.WriteLine($"备用API请求也出错: {backupEx.Message}", true);
return; // 连备用URL也有问题,提前退出
}
}
// 如果成功获取了JSON数据,继续处理
if (json != null)
{
string latestVersion = json["tag_name"].ToString();
label6.Text = latestVersion;
if (当前subsCheck版本号 != latestVersion)
{
button2.Text = "立即更新";
button2.Enabled = true;
}
else
{
button2.Text = "已是最新版本";
button2.Enabled = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show($"下载 subs-check.exe 时出错: {ex.Message}\n\n请前往 https://github.com/beck-8/subs-check/releases 自行下载!",
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
// 创建专用方法用于异步检测GitHub代理
private async Task<string> DetectGitHubProxyAsync(List<string> proxyItems)
{
string detectedProxyURL = "";
// 遍历随机排序后的代理列表
foreach (string proxyItem in proxyItems)
{
string checkUrl = $"https://{proxyItem}/https://raw.githubusercontent.com/cmliu/SubsCheck-Win-GUI/master/packages.config";
try
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(5); // 设置5秒超时
// 添加User-Agent头,避免被拒绝访问
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36 (KHTML, like Gecko) cmliu/SubsCheck-Win-GUI");
// 使用异步方式
HttpResponseMessage response = await client.GetAsync(checkUrl);
if (response.IsSuccessStatusCode)
{
// 找到可用代理
detectedProxyURL = $"https://{proxyItem}/";
break;
}
}
}
catch (Exception ex)
{
// 记录错误但继续尝试下一个
Console.WriteLine($"Error: {ex.Message}");
}
}
return detectedProxyURL;
}
private void button2_Click(object sender, EventArgs e)
{
// 设置对话框结果为OK,表示用户点击了"立即更新"按钮
this.DialogResult = DialogResult.OK;
// 关闭窗口
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
//下载链接
string downloadURL = $"{githubProxyURL}https://github.com/cmliu/SubsCheck-Win-GUI/releases/download/{最新GUI版本号}/SubsCheck_Win_GUI.zip";
//目标文件
string downloadEXE = "subs-check.win.gui.exe";
try
{
// 获取应用程序目录
string executablePath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
// 创建 Upgrade.ini 文件路径
string iniFilePath = System.IO.Path.Combine(executablePath, "Upgrade.ini");
// 准备 INI 文件内容
string iniContent =
"[Upgrade]\r\n" +
$"DownloadURL={downloadURL}\r\n" +
$"TargetFile={downloadEXE}\r\n";
// 写入文件(如果文件已存在会被覆盖)
System.IO.File.WriteAllText(iniFilePath, iniContent);
DialogResult result = MessageBox.Show(
$"发现新版本: {最新GUI版本号}\n\n" +
"· 点击【确定】将下载并安装更新\n" +
"· 更新过程中程序会自动关闭并重启\n" +
"· 更新完成后所有设置将保持不变\n\n" +
"是否立即更新到最新版本?",
"发现新版本",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
// 检查目标文件是否存在
string targetFilePath = System.IO.Path.Combine(Application.StartupPath, "Upgrade.exe");
if (System.IO.File.Exists(targetFilePath))
{
// 使用Process.Start异步启动应用程序
System.Diagnostics.Process.Start(targetFilePath);
this.Close();
}
else
{
MessageBox.Show("更新程序 Upgrade.exe 不存在!",
"错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"写入更新信息时出错: {ex.Message}",
"错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}