From 6889e9ba7e8eec614e23a1d6aaace01ac2695d6d Mon Sep 17 00:00:00 2001 From: database64128 Date: Thu, 10 Sep 2020 14:44:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Cleanup=20and=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Deserialize the configuration using a constructor to set default values and initialize objects - Re-enable the UI when online config update failed --- shadowsocks-csharp/Model/Configuration.cs | 91 +++++++++++---------- shadowsocks-csharp/View/OnlineConfigForm.cs | 6 +- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index f4d9e35c..60df5410 100644 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -21,7 +21,6 @@ namespace Shadowsocks.Model public List onlineConfigSource; - // when strategy is set, index is ignored public string strategy; public int index; @@ -30,30 +29,69 @@ namespace Shadowsocks.Model public bool shareOverLan; public bool isDefault; public int localPort; - public bool portableMode = true; + public bool portableMode; public bool showPluginOutput; public string pacUrl; public bool useOnlinePac; - public bool secureLocalPac = true; + public bool secureLocalPac; public bool availabilityStatistics; public bool autoCheckUpdate; public bool checkPreRelease; public bool isVerboseLogging; // hidden options - public bool isIPv6Enabled = false; // for experimental ipv6 support + public bool isIPv6Enabled; // for experimental ipv6 support public bool generateLegacyUrl = false; // for pre-sip002 url compatibility public string geositeUrl; // for custom geosite source (and rule group) - public string geositeGroup = "geolocation-!cn"; - public bool geositeBlacklistMode = true; - + public string geositeGroup; + public bool geositeBlacklistMode; //public NLogConfig.LogLevel logLevel; public LogViewerConfig logViewer; public ProxyConfig proxy; public HotkeyConfig hotkey; + [JsonIgnore] + public bool updated; + + public Configuration() + { + version = UpdateChecker.Version; + strategy = ""; + index = 0; + global = false; + enabled = false; + shareOverLan = false; + isDefault = true; + localPort = 1080; + portableMode = true; + showPluginOutput = false; + pacUrl = ""; + useOnlinePac = false; + secureLocalPac = true; + availabilityStatistics = false; + autoCheckUpdate = false; + checkPreRelease = false; + isVerboseLogging = false; + + // hidden options + isIPv6Enabled = false; + generateLegacyUrl = false; + geositeUrl = ""; + geositeGroup = "geolocation-!cn"; + geositeBlacklistMode = true; + + logViewer = new LogViewerConfig(); + proxy = new ProxyConfig(); + hotkey = new HotkeyConfig(); + + updated = false; + + configs = new List(); + onlineConfigSource = new List(); + } + [JsonIgnore] NLogConfig nLogConfig; @@ -64,10 +102,6 @@ namespace Shadowsocks.Model private static readonly NLogConfig.LogLevel verboseLogLevel = NLogConfig.LogLevel.Debug; #endif - - [JsonIgnore] - public bool updated = false; - [JsonIgnore] public string localHost => GetLocalHost(); private string GetLocalHost() @@ -90,7 +124,6 @@ namespace Shadowsocks.Model localPort) : null; - public static void CheckServer(Server server) { CheckServer(server.server); @@ -125,23 +158,10 @@ namespace Shadowsocks.Model config.updated = true; } - if (config.configs == null) - config.configs = new List(); - if (config.onlineConfigSource == null) - config.onlineConfigSource = new List(); - if (config.configs.Count == 0) config.configs.Add(GetDefaultServer()); - if (config.localPort == 0) - config.localPort = 1080; - if (config.index == -1 && config.strategy == null) + if (config.index == -1 && string.IsNullOrEmpty(config.strategy)) config.index = 0; - if (config.logViewer == null) - config.logViewer = new LogViewerConfig(); - if (config.proxy == null) - config.proxy = new ProxyConfig(); - if (config.hotkey == null) - config.hotkey = new HotkeyConfig(); if (!System.Net.Sockets.Socket.OSSupportsIPv6) { config.isIPv6Enabled = false; // disable IPv6 if os not support @@ -154,20 +174,8 @@ namespace Shadowsocks.Model { if (!(e is FileNotFoundException)) logger.LogUsefulException(e); - config = new Configuration - { - index = 0, - isDefault = true, - localPort = 1080, - autoCheckUpdate = true, - configs = new List() - { - GetDefaultServer() - }, - logViewer = new LogViewerConfig(), - proxy = new ProxyConfig(), - hotkey = new HotkeyConfig(), - }; + config = new Configuration(); + config.configs.Add(GetDefaultServer()); } try @@ -199,12 +207,11 @@ 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; if (config.index < -1) config.index = -1; - if (config.index == -1 && config.strategy == null) + if (config.index == -1 && string.IsNullOrEmpty(config.strategy)) config.index = 0; config.isDefault = false; try diff --git a/shadowsocks-csharp/View/OnlineConfigForm.cs b/shadowsocks-csharp/View/OnlineConfigForm.cs index ab69546d..fca0ed57 100644 --- a/shadowsocks-csharp/View/OnlineConfigForm.cs +++ b/shadowsocks-csharp/View/OnlineConfigForm.cs @@ -82,11 +82,10 @@ namespace Shadowsocks.View private void AddButton_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(UrlTextBox.Text)) return; - Commit(); - // string txt = UrlTextBox.Text; - UrlListBox.Items.Add(""); + UrlListBox.Items.Add(UrlTextBox.Text); UrlListBox.SelectedIndex = UrlListBox.Items.Count - 1; UrlTextBox.Text = ""; + Commit(); } private async void UpdateButton_Click(object sender, EventArgs e) @@ -101,6 +100,7 @@ namespace Shadowsocks.View if (!ok) { MessageBox.Show(I18N.GetString("online config failed to update")); + tableLayoutPanel1.Enabled = true; return; } if (old != current) controller.RemoveOnlineConfig(old);