From ca963c638d6343b11bd6020d48fe1f4a8d7bc96b Mon Sep 17 00:00:00 2001 From: Student Main Date: Thu, 3 Sep 2020 16:18:37 +0800 Subject: [PATCH] sip008 controller support --- .../Controller/ShadowsocksController.cs | 39 +++++++++++++++++-- shadowsocks-csharp/Model/Configuration.cs | 11 ++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index ba7230d3..d407957c 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -7,6 +7,7 @@ using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; +using System.Threading.Tasks; using System.Web; using System.Windows.Forms; using NLog; @@ -449,7 +450,7 @@ namespace Shadowsocks.Controller #endregion #region Misc - + public void ToggleVerboseLogging(bool enabled) { _config.isVerboseLogging = enabled; @@ -490,7 +491,7 @@ namespace Shadowsocks.Controller ConfigChanged?.Invoke(this, new EventArgs()); } - + #endregion #region Statistic @@ -567,7 +568,7 @@ namespace Shadowsocks.Controller #endregion #region SIP003 - + private void StartPlugin() { var server = _config.GetCurrentServer(); @@ -685,5 +686,37 @@ namespace Shadowsocks.Controller #endregion + #region SIP008 + + public async Task UpdateOnlineConfig(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) + ); + _config.index = _config.configs.IndexOf(selected); + Configuration.Save(_config); + } + + 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)) + ); + _config.index = _config.configs.IndexOf(selected); + Configuration.Save(_config); + } + + #endregion } } diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 93c33365..f4d9e35c 100644 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net; using Newtonsoft.Json; using NLog; @@ -197,6 +198,7 @@ namespace Shadowsocks.Model public static void Save(Configuration config) { + config.configs = SortByOnlineConfig(config.configs); config.version = UpdateChecker.Version; if (config.index >= config.configs.Count) config.index = config.configs.Count - 1; @@ -230,6 +232,15 @@ namespace Shadowsocks.Model } } + public static List SortByOnlineConfig(IEnumerable servers) + { + var groups = servers.GroupBy(s => s.group); + List ret = new List(); + ret.AddRange(groups.Where(g => string.IsNullOrEmpty(g.Key)).SelectMany(g => g)); + ret.AddRange(groups.Where(g => !string.IsNullOrEmpty(g.Key)).SelectMany(g => g)); + return ret; + } + public static Server AddDefaultServerOrServer(Configuration config, Server server = null, int? index = null) { if (config?.configs != null)