Browse Source

🧹 Cleanup and fixes

- Deserialize the configuration using a constructor to set default values and initialize objects
- Re-enable the UI when online config update failed
tags/4.2.1.0
database64128 Student Main 4 years ago
parent
commit
6889e9ba7e
No known key found for this signature in database GPG Key ID: AA78519C208C8742
2 changed files with 52 additions and 45 deletions
  1. +49
    -42
      shadowsocks-csharp/Model/Configuration.cs
  2. +3
    -3
      shadowsocks-csharp/View/OnlineConfigForm.cs

+ 49
- 42
shadowsocks-csharp/Model/Configuration.cs View File

@@ -21,7 +21,6 @@ namespace Shadowsocks.Model
public List<string> 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<Server>();
onlineConfigSource = new List<string>();
}
[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<Server>();
if (config.onlineConfigSource == null)
config.onlineConfigSource = new List<string>();
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<Server>()
{
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


+ 3
- 3
shadowsocks-csharp/View/OnlineConfigForm.cs View File

@@ -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);


Loading…
Cancel
Save