private void updateACLToolStripMenuItem_Click(object sender, EventArgs e) { var bak_State = State; var bak_StateText = StatusLabel.Text; StatusText(i18N.Translate("Starting update ACL")); using var client = new WebClient(); client.DownloadFileTaskAsync(Global.Settings.ACL, "bin\\default.acl"); client.DownloadFileCompleted += ((sender, args) => { try { if (args.Error == null) { NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, i18N.Translate("ACL updated successfully"), ToolTipIcon.Info); } else { Logging.Error("ACL 更新失败!" + args.Error); MessageBoxX.Show(i18N.Translate("ACL update failed") + "\n" + args.Error); } } finally { State = bak_State; StatusLabel.Text = bak_StateText; } }); }
private void updateACLWithProxyToolStripMenuItem_Click(object sender, EventArgs e) { updateACLWithProxyToolStripMenuItem.Enabled = false; // 当前 ServerComboBox 中至少有一项 if (ServerComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select a server first")); return; } MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; ControlButton.Text = "..."; Task.Run(() => { var mode = new Models.Mode { Remark = "ProxyUpdate", Type = 5 }; _mainController = new MainController(); _mainController.Start(ServerComboBox.SelectedItem as Models.Server, mode); using var client = new WebClient(); client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); StatusText(i18N.Translate("Updating in the background")); try { client.DownloadFile(Global.Settings.ACL, "bin\\default.acl"); NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, i18N.Translate("ACL updated successfully"), ToolTipIcon.Info); } catch (Exception e) { Logging.Error("使用代理更新 ACL 失败!" + e); MessageBoxX.Show(i18N.Translate("ACL update failed") + "\n" + e); } finally { State = State.Waiting; _mainController.Stop(); } }); }
private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e) { var bak_State = State; var bak_StateText = StatusLabel.Text; if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1) { Global.Settings.UseProxyToUpdateSubscription = false; } if (Global.Settings.UseProxyToUpdateSubscription) { // 当前 ServerComboBox 中至少有一项 if (ServerComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select a server first")); return; } MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; ControlButton.Text = "..."; } if (Global.Settings.SubscribeLink.Count > 0) { StatusText(i18N.Translate("Starting update subscription")); DeleteServerPictureBox.Enabled = false; UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false; Task.Run(() => { if (Global.Settings.UseProxyToUpdateSubscription) { var mode = new Models.Mode { Remark = "ProxyUpdate", Type = 5 }; _mainController = new MainController(); _mainController.Start(ServerComboBox.SelectedItem as Models.Server, mode); } foreach (var item in Global.Settings.SubscribeLink) { using var client = new WebClient(); try { if (!string.IsNullOrEmpty(item.UserAgent)) { client.Headers.Add("User-Agent", item.UserAgent); } else { client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"); } if (Global.Settings.UseProxyToUpdateSubscription) { client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); } var response = client.DownloadString(item.Link); try { response = ShareLink.URLSafeBase64Decode(response); } catch (Exception) { // ignored } Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList(); var result = ShareLink.Parse(response); if (result != null) { foreach (var x in result) { x.Group = item.Remark; } Global.Settings.Server.AddRange(result); NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, string.Format(i18N.Translate("Update {1} server(s) from {0}"), item.Remark, result.Count), ToolTipIcon.Info); } else { NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, string.Format(i18N.Translate("Update servers error from {0}"), item.Remark), ToolTipIcon.Error); } } catch (Exception) { } } InitServer(); DeleteServerPictureBox.Enabled = true; if (Global.Settings.UseProxyToUpdateSubscription) { MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; ControlButton.Text = i18N.Translate("Start"); _mainController.Stop(); NatTypeStatusLabel.Text = ""; } Configuration.Save(); StatusText(i18N.Translate("Subscription updated")); MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; State = bak_State; StatusLabel.Text = bak_StateText; }).ContinueWith(task => { BeginInvoke(new Action(() => { UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; })); }); NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, i18N.Translate("Updating in the background"), ToolTipIcon.Info); } else { MessageBoxX.Show(i18N.Translate("No subscription link")); } }