From f46b1fb8b0ca112b95f951d00ed2a136de10c224 Mon Sep 17 00:00:00 2001 From: Student Main Date: Fri, 4 Sep 2020 17:29:18 +0800 Subject: [PATCH] online config deletion, and update when startup --- .../Service/OnlineConfigResolver.cs | 32 +++----- .../Controller/ShadowsocksController.cs | 56 +++++++++++--- shadowsocks-csharp/Program.cs | 11 ++- .../View/OnlineConfigForm.Designer.cs | 4 +- shadowsocks-csharp/View/OnlineConfigForm.cs | 73 ++++++++++++++----- 5 files changed, 120 insertions(+), 56 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs b/shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs index fe3e16c1..89ff2565 100644 --- a/shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs +++ b/shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs @@ -26,35 +26,21 @@ namespace Shadowsocks.Controller.Service httpClientHandler.Proxy = proxy; } - try - { - string str = await httpClient.GetStringAsync(url); - var ret = Get(str); - foreach (var item in ret) - { - item.group = url; - } - return ret; - } - catch (Exception e) + _ = Task.Delay(2000).ContinueWith(_ => httpClient.CancelPendingRequests()); + string str = await httpClient.GetStringAsync(url); + + var ret = Get(str); + foreach (var item in ret) { - logger.LogUsefulException(e); - return new List(); + item.group = url; } + return ret; } public static List Get(string json) { - try - { - var t = JToken.Parse(json); - return SearchJToken(t).ToList(); - } - catch (Exception e) - { - logger.LogUsefulException(e); - return new List(); - } + var t = JToken.Parse(json); + return SearchJToken(t).ToList(); } private static IEnumerable SearchJArray(JArray a) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index c2f8e0d4..935bf8ea 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -688,33 +688,56 @@ namespace Shadowsocks.Controller #region SIP008 - public async Task UpdateOnlineConfig(string url) + + public async Task UpdateOnlineConfigInternal(string url) { - var selected = GetCurrentServer(); var onlineServer = await OnlineConfigResolver.GetOnline(url, _config.WebProxy); _config.configs = Configuration.SortByOnlineConfig( _config.configs .Where(c => c.group != url) .Concat(onlineServer) ); + logger.Info($"updated {onlineServer.Count} server from {url}"); + return onlineServer.Count; + } + + public async Task UpdateOnlineConfig(string url) + { + var selected = GetCurrentServer(); + try + { + int count = await UpdateOnlineConfigInternal(url); + } + catch (Exception e) + { + logger.LogUsefulException(e); + return false; + } _config.index = _config.configs.IndexOf(selected); SaveConfig(_config); + return true; } - public async Task UpdateAllOnlineConfig() + public async Task UpdateAllOnlineConfig() { var selected = GetCurrentServer(); - var r = await Task.WhenAll( - _config.onlineConfigSource.Select(url => OnlineConfigResolver.GetOnline(url, _config.WebProxy)) - ); - var results = r.SelectMany(s => s); - _config.configs = Configuration.SortByOnlineConfig( - _config.configs - .Where(c => string.IsNullOrEmpty(c.group)) - .Concat(r.SelectMany(s => s)) - ); + int failCount = 0; + foreach (var url in _config.onlineConfigSource) + { + try + { + await UpdateOnlineConfigInternal(url); + } + catch (Exception e) + { + logger.LogUsefulException(e); + failCount++; + } + } + _config.index = _config.configs.IndexOf(selected); SaveConfig(_config); + return failCount; } public void SaveOnlineConfigSource(IEnumerable vs) @@ -723,6 +746,15 @@ namespace Shadowsocks.Controller SaveConfig(_config); } + public void RemoveOnlineConfig(string url) + { + _config.onlineConfigSource.RemoveAll(v => v == url); + _config.configs = Configuration.SortByOnlineConfig( + _config.configs.Where(c => c.group != url) + ); + SaveConfig(_config); + } + #endregion } } diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 4fb8fbbf..6da06f0f 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -123,7 +123,14 @@ namespace Shadowsocks HotKeys.Init(MainController); MainController.Start(); - #region IPC Handler and Arguement Process + // Update online config + Task.Run(async () => + { + await Task.Delay(10 * 1000); + await MainController.UpdateAllOnlineConfig(); + }); + +#region IPC Handler and Arguement Process IPCService ipcService = new IPCService(); Task.Run(() => ipcService.RunServer()); ipcService.OpenUrlRequested += (_1, e) => MainController.AskAddServerBySSURL(e.Url); @@ -132,7 +139,7 @@ namespace Shadowsocks { MainController.AskAddServerBySSURL(Options.OpenUrl); } - #endregion +#endregion Application.Run(); diff --git a/shadowsocks-csharp/View/OnlineConfigForm.Designer.cs b/shadowsocks-csharp/View/OnlineConfigForm.Designer.cs index f15503ce..d535b523 100644 --- a/shadowsocks-csharp/View/OnlineConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/OnlineConfigForm.Designer.cs @@ -153,6 +153,7 @@ this.OkButton.TabIndex = 7; this.OkButton.Text = "OK"; this.OkButton.UseVisualStyleBackColor = true; + this.OkButton.Click += new System.EventHandler(this.OkButton_Click); // // UpdateAllButton // @@ -180,6 +181,7 @@ this.CancelButton.TabIndex = 8; this.CancelButton.Text = "Cancel"; this.CancelButton.UseVisualStyleBackColor = true; + this.CancelButton.Click += new System.EventHandler(this.CancelButton_Click); // // OnlineConfigForm // @@ -188,7 +190,7 @@ this.ClientSize = new System.Drawing.Size(488, 465); this.Controls.Add(this.tableLayoutPanel1); this.Name = "OnlineConfigForm"; - this.Text = "OnlineConfigForm"; + this.Text = "Online config"; this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); this.ResumeLayout(false); diff --git a/shadowsocks-csharp/View/OnlineConfigForm.cs b/shadowsocks-csharp/View/OnlineConfigForm.cs index 0c23be10..4cbcf43e 100644 --- a/shadowsocks-csharp/View/OnlineConfigForm.cs +++ b/shadowsocks-csharp/View/OnlineConfigForm.cs @@ -1,14 +1,10 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Drawing; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; +using Shadowsocks.Properties; namespace Shadowsocks.View { @@ -17,13 +13,13 @@ namespace Shadowsocks.View private ShadowsocksController controller; private Configuration config; - private const string DefaultPrefix = "http://www.baidu.com/"; - public OnlineConfigForm(ShadowsocksController controller) { this.controller = controller; InitializeComponent(); LoadConfig(); + Icon = System.Drawing.Icon.FromHandle(Resources.ssw128.GetHicon()); + I18N.TranslateForm(this); } private void LoadConfig() @@ -38,6 +34,8 @@ namespace Shadowsocks.View } if (idx >= UrlListBox.Items.Count) idx = 0; + if (idx < 0 && UrlListBox.Items.Count > 0) idx = 0; + if (UrlListBox.Items.Count == 0) return; UrlListBox.SelectedIndex = idx; SelectItem(); } @@ -53,6 +51,7 @@ namespace Shadowsocks.View { var scheme = new Uri(UrlTextBox.Text).Scheme; if (scheme != "http" && scheme != "https") return false; + if (UrlListBox.Items.OfType().Contains(UrlTextBox.Text)) return false; } catch { @@ -61,31 +60,47 @@ namespace Shadowsocks.View return true; } - private bool Commit() + private void Commit() { + if (UrlListBox.SelectedIndex < 0) return; + if ((string)UrlListBox.SelectedItem == UrlTextBox.Text) + { + LoadConfig(); + return; + } + if (ValidateUrl()) { + UrlListBox.Items[UrlListBox.SelectedIndex] = UrlTextBox.Text; } - controller.SaveOnlineConfigSource(UrlListBox.Items.OfType().Where(s => !string.IsNullOrWhiteSpace(s))); + controller.SaveOnlineConfigSource(UrlListBox.Items.OfType().Where(s => !string.IsNullOrWhiteSpace(s)).Distinct()); LoadConfig(); - return true; + return; } private void AddButton_Click(object sender, EventArgs e) { + if (string.IsNullOrWhiteSpace(UrlTextBox.Text)) return; Commit(); + // string txt = UrlTextBox.Text; UrlListBox.Items.Add(""); - UrlTextBox.Text = DefaultPrefix; UrlListBox.SelectedIndex = UrlListBox.Items.Count - 1; + UrlTextBox.Text = ""; } - private void UpdateButton_Click(object sender, EventArgs e) + private async void UpdateButton_Click(object sender, EventArgs e) { // update content, also update online config Commit(); - - _ = controller.UpdateOnlineConfig(UrlTextBox.Text); + if (UrlListBox.Items.Count == 0) return; + tableLayoutPanel1.Enabled = false; + bool ok = await controller.UpdateOnlineConfig((string)UrlListBox.SelectedItem); + if (!ok) + { + MessageBox.Show(I18N.GetString("online config failed to update")); + } + tableLayoutPanel1.Enabled = true; } private void UrlListBox_SelectedIndexChanged(object sender, EventArgs e) @@ -99,14 +114,36 @@ namespace Shadowsocks.View private void DeleteButton_Click(object sender, EventArgs e) { - int idx = UrlListBox.SelectedIndex; - UrlListBox.Items.RemoveAt(idx); + if (UrlListBox.Items.Count == 0) return; + string url = (string)UrlListBox.SelectedItem; + if (!string.IsNullOrWhiteSpace(url)) + { + controller.RemoveOnlineConfig(url); + } + LoadConfig(); + } + + private async void UpdateAllButton_Click(object sender, EventArgs e) + { + if (UrlListBox.Items.Count == 0) return; + tableLayoutPanel1.Enabled = false; + int fail = await controller.UpdateAllOnlineConfig(); + if (fail > 0) + { + MessageBox.Show(I18N.GetString("{0} online config failed to update", fail)); + } + tableLayoutPanel1.Enabled = true; + } + + private void OkButton_Click(object sender, EventArgs e) + { Commit(); + Close(); } - private void UpdateAllButton_Click(object sender, EventArgs e) + private void CancelButton_Click(object sender, EventArgs e) { - _ = controller.UpdateAllOnlineConfig(); + Close(); } } }