From 0eb0e8c9e64d5bacda8d9ea39832d18fe6a61197 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Fri, 7 Nov 2014 23:53:37 +0800 Subject: [PATCH] use gui-config.json from shadowsocks-gui --- shadowsocks-csharp/Controller/Local.cs | 6 +- shadowsocks-csharp/Controller/PolipoRunner.cs | 2 +- .../Controller/ShadowsocksController.cs | 24 ++-- shadowsocks-csharp/Model/Config.cs | 117 ------------------ shadowsocks-csharp/Model/Configuration.cs | 133 +++++++++++++++++++++ shadowsocks-csharp/Model/Server.cs | 20 ++++ shadowsocks-csharp/View/ConfigForm.cs | 25 ++-- shadowsocks-csharp/shadowsocks-csharp.csproj | 5 +- shadowsocks-csharp/simple-json | 2 +- 9 files changed, 190 insertions(+), 144 deletions(-) delete mode 100755 shadowsocks-csharp/Model/Config.cs create mode 100755 shadowsocks-csharp/Model/Configuration.cs create mode 100755 shadowsocks-csharp/Model/Server.cs diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 4ba3be6f..a40c8119 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -11,10 +11,10 @@ namespace shadowsocks_csharp.Controller class Local { - private Config config; + private Server config; //private Encryptor encryptor; Socket listener; - public Local(Config config) + public Local(Server config) { this.config = config; //this.encryptor = new Encryptor(config.method, config.password); @@ -102,7 +102,7 @@ namespace shadowsocks_csharp.Controller { //public Encryptor encryptor; public IEncryptor encryptor; - public Config config; + public Server config; // Client socket. public Socket remote; public Socket connection; diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index 0263b6d0..875a0c7b 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -13,7 +13,7 @@ namespace shadowsocks_csharp.Controller { private Process process; - public void Start(Config config) + public void Start(Server config) { if (process == null) { diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index e321a3b0..ef95c282 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -14,7 +14,7 @@ namespace shadowsocks_csharp.Controller private Local local; private PACServer pacServer; - private Config config; + private Configuration config; private PolipoRunner polipoRunner; private bool stopped = false; @@ -38,10 +38,10 @@ namespace shadowsocks_csharp.Controller public ShadowsocksController() { - config = Config.Load(); + config = Configuration.Load(); polipoRunner = new PolipoRunner(); - polipoRunner.Start(config); - local = new Local(config); + polipoRunner.Start(config.GetCurrentServer()); + local = new Local(config.GetCurrentServer()); try { local.Start(); @@ -57,16 +57,16 @@ namespace shadowsocks_csharp.Controller updateSystemProxy(); } - public void SaveConfig(Config newConfig) + public void SaveConfig(Configuration newConfig) { - Config.Save(newConfig); + Configuration.Save(newConfig); config = newConfig; local.Stop(); polipoRunner.Stop(); - polipoRunner.Start(config); + polipoRunner.Start(config.GetCurrentServer()); - local = new Local(config); + local = new Local(config.GetCurrentServer()); local.Start(); if (ConfigChanged != null) @@ -75,11 +75,17 @@ namespace shadowsocks_csharp.Controller } } - public Config GetConfig() + public Server GetCurrentServer() + { + return config.GetCurrentServer(); + } + + public Configuration GetConfiguration() { return config; } + public void ToggleEnable(bool enabled) { config.enabled = enabled; diff --git a/shadowsocks-csharp/Model/Config.cs b/shadowsocks-csharp/Model/Config.cs deleted file mode 100755 index 0b962113..00000000 --- a/shadowsocks-csharp/Model/Config.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.IO; -using System.Diagnostics; -using SimpleJson; - -namespace shadowsocks_csharp.Model -{ - [Serializable] - public class Config - { - public bool enabled; - public string server; - public int server_port; - public int local_port; - public string password; - public string method; - - public bool isDefault; - - private static void assert(bool condition) - { - if(!condition) - { - throw new Exception("assertion failure"); - } - } - - public static Config Load() - { - try - { - using (StreamReader sr = new StreamReader(File.OpenRead(@"config.json"))) - { - Config config = SimpleJson.SimpleJson.DeserializeObject(sr.ReadToEnd()); - assert(!string.IsNullOrEmpty(config.server)); - assert(!string.IsNullOrEmpty(config.password)); - assert(config.local_port > 0); - assert(config.server_port > 0); - config.isDefault = false; - return config; - } - } - catch (Exception e) - { - if (!(e is FileNotFoundException)) - { - Console.WriteLine(e); - } - return new Config - { - server = "127.0.0.1", - server_port = 8388, - local_port = 1080, - password = "barfoo!", - method = "table", - enabled = true, - isDefault = true - }; - } - } - - private static void checkPort(int port) - { - if (port <= 0 || port > 65535) - { - throw new ArgumentException("port out of range"); - } - } - - private static void checkPassword(string password) - { - if (string.IsNullOrEmpty(password)) - { - throw new ArgumentException("password can not be blank"); - } - } - - private static void checkServer(string server) - { - if (string.IsNullOrEmpty(server)) - { - throw new ArgumentException("server can not be blank"); - } - } - - public static void Save(Config config) - { - checkPort(config.local_port); - checkPort(config.server_port); - checkPassword(config.password); - checkServer(config.server); - try - { - using (StreamWriter sw = new StreamWriter(File.Open(@"config.json", FileMode.Create))) - { - string jsonString = SimpleJson.SimpleJson.SerializeObject(new - { - server = config.server, - server_port = config.server_port, - local_port = config.local_port, - password = config.password, - method = config.method, - enabled = config.enabled - }); - sw.Write(jsonString); - sw.Flush(); - } - } - catch (IOException e) - { - Console.Error.WriteLine(e); - } - } - } -} diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs new file mode 100755 index 00000000..fdc7294f --- /dev/null +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace shadowsocks_csharp.Model +{ + [Serializable] + public class Configuration + { + public Configuration() + { + } + + public List configs; + public int index; + public bool enabled; + public bool isDefault; + + private static string CONFIG_FILE = "gui-config.json"; + + public Server GetCurrentServer() + { + if (index >= 0 && index < configs.Count) + { + return configs[index]; + } + else + { + return getDefaultServer(); + } + } + + public static void CheckServer(Server server) + { + checkPort(server.local_port); + checkPort(server.server_port); + checkPassword(server.password); + checkServer(server.server); + } + + public static Configuration Load() + { + try + { + var json = "[0,1,2]"; + var result = SimpleJson.SimpleJson.DeserializeObject>(json); + + string configContent = File.ReadAllText(CONFIG_FILE); + Configuration config = SimpleJson.SimpleJson.DeserializeObject(configContent); + config.isDefault = false; + return config; + } + catch (Exception e) + { + if (!(e is FileNotFoundException)) + { + Console.WriteLine(e); + } + return new Configuration + { + index = 0, + configs = new List() + { + getDefaultServer() + } + }; + } + } + + public static void Save(Configuration config) + { + try + { + config.isDefault = false; + using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create))) + { + string jsonString = SimpleJson.SimpleJson.SerializeObject(config); + sw.Write(jsonString); + sw.Flush(); + } + } + catch (IOException e) + { + Console.Error.WriteLine(e); + } + } + + private static Server getDefaultServer() + { + return new Server() + { + server = "", + server_port = 8388, + local_port = 1080, + method = "aes-256-cfb", + password = "" + }; + } + + private static void assert(bool condition) + { + if (!condition) + { + throw new Exception("assertion failure"); + } + } + + private static void checkPort(int port) + { + if (port <= 0 || port > 65535) + { + throw new ArgumentException("port out of range"); + } + } + + private static void checkPassword(string password) + { + if (string.IsNullOrEmpty(password)) + { + throw new ArgumentException("password can not be blank"); + } + } + + private static void checkServer(string server) + { + if (string.IsNullOrEmpty(server)) + { + throw new ArgumentException("server can not be blank"); + } + } + } +} diff --git a/shadowsocks-csharp/Model/Server.cs b/shadowsocks-csharp/Model/Server.cs new file mode 100755 index 00000000..f2d737ad --- /dev/null +++ b/shadowsocks-csharp/Model/Server.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.Diagnostics; +using SimpleJson; + +namespace shadowsocks_csharp.Model +{ + [Serializable] + public class Server + { + public string server; + public int server_port; + public int local_port; + public string password; + public string method; + + } +} diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 1ea370a4..4547d45a 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -53,20 +53,20 @@ namespace shadowsocks_csharp.View private void updateUI() { - Config config = controller.GetConfig(); + Server server = controller.GetCurrentServer(); - textBox1.Text = config.server; - textBox2.Text = config.server_port.ToString(); - textBox3.Text = config.password; - textBox4.Text = config.local_port.ToString(); - comboBox1.Text = config.method == null ? "aes-256-cfb" : config.method; + textBox1.Text = server.server; + textBox2.Text = server.server_port.ToString(); + textBox3.Text = server.password; + textBox4.Text = server.local_port.ToString(); + comboBox1.Text = server.method == null ? "aes-256-cfb" : server.method; - enableItem.Checked = config.enabled; + enableItem.Checked = controller.GetConfiguration().enabled; } private void Form1_Load(object sender, EventArgs e) { - if (!controller.GetConfig().isDefault) + if (!controller.GetConfiguration().isDefault) { this.Opacity = 0; BeginInvoke(new MethodInvoker(delegate @@ -90,15 +90,18 @@ namespace shadowsocks_csharp.View { try { - Config config = new Config + Server server = new Server { server = textBox1.Text, server_port = int.Parse(textBox2.Text), password = textBox3.Text, local_port = int.Parse(textBox4.Text), - method = comboBox1.Text, - isDefault = false + method = comboBox1.Text }; + Configuration config = controller.GetConfiguration(); + config.configs.Clear(); + config.configs.Add(server); + config.index = 0; controller.SaveConfig(config); this.Hide(); } diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 28deb6db..854836e7 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -60,7 +60,7 @@ true bin\x86\Debug\ - TRACE;DEBUG;SIMPLE_JSON_NO_LINQ_EXPRESSION + TRACE;DEBUG full x86 prompt @@ -89,7 +89,8 @@ - + + Form diff --git a/shadowsocks-csharp/simple-json b/shadowsocks-csharp/simple-json index 8ef093ac..663c62a7 160000 --- a/shadowsocks-csharp/simple-json +++ b/shadowsocks-csharp/simple-json @@ -1 +1 @@ -Subproject commit 8ef093ac7b18a9f48fb7a8e41085501c7e35e46c +Subproject commit 663c62a77ab3318f04ddb9449b2b882364709625