diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 1e193158..113242a7 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -80,6 +80,23 @@ namespace Shadowsocks.Controller SaveConfig(_config); } + public bool AddServerBySSURL(string ssURL) + { + try + { + var server = new Server(ssURL); + _config.configs.Add(server); + _config.index = _config.configs.Count - 1; + SaveConfig(_config); + return true; + } + catch (Exception e) + { + Logging.LogUsefulException(e); + return false; + } + } + public void ToggleEnable(bool enabled) { _config.enabled = enabled; diff --git a/shadowsocks-csharp/Data/cn.txt b/shadowsocks-csharp/Data/cn.txt index 9f8c6da1..e091b276 100644 --- a/shadowsocks-csharp/Data/cn.txt +++ b/shadowsocks-csharp/Data/cn.txt @@ -3,12 +3,13 @@ Enable=启用代理 Mode=代理模式 PAC=PAC 模式 Global=全局模式 -Servers=服务器选择 +Servers=服务器 Edit Servers...=编辑服务器... Start on Boot=开机启动 Share over LAN=在局域网共享代理 Edit PAC File...=编辑 PAC 文件... Show QRCode...=显示二维码... +Scan QRCode from Screen...=扫描屏幕上的二维码... Show Logs...=显示日志... About...=关于... Quit=退出 diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 63b4c750..4b6e0ecb 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -94,15 +94,7 @@ namespace Shadowsocks.Model public static Server GetDefaultServer() { - return new Server() - { - server = "", - server_port = 8388, - local_port = 1080, - method = "aes-256-cfb", - password = "", - remarks = "" - }; + return new Server(); } private static void Assert(bool condition) diff --git a/shadowsocks-csharp/Model/Server.cs b/shadowsocks-csharp/Model/Server.cs index 382efcbe..b4486db9 100755 --- a/shadowsocks-csharp/Model/Server.cs +++ b/shadowsocks-csharp/Model/Server.cs @@ -5,6 +5,7 @@ using System.IO; using System.Diagnostics; using SimpleJson; using Shadowsocks.Controller; +using System.Text.RegularExpressions; namespace Shadowsocks.Model { @@ -33,5 +34,41 @@ namespace Shadowsocks.Model return remarks + " (" + server + ":" + server_port + ")"; } } + + public Server() + { + this.server = ""; + this.server_port = 8388; + this.local_port = 1080; + this.method = "aes-256-cfb"; + this.password = ""; + this.remarks = ""; + } + + public Server(string ssURL) : this() + { + string[] r1 = Regex.Split(ssURL, "ss://", RegexOptions.IgnoreCase); + string base64 = r1[1].ToString(); + byte[] bytes = null; + for (var i = 0; i < 3; i++) { + try + { + bytes = System.Convert.FromBase64String(base64); + } + catch (FormatException) + { + base64 += "="; + } + } + if (bytes == null) + { + throw new FormatException(); + } + string[] parts = Encoding.UTF8.GetString(bytes).Split(new char[2] { ':', '@' }); + this.method = parts[0].ToString(); + this.password = parts[1].ToString(); + this.server = parts[2].ToString(); + this.server_port = int.Parse(parts[3].ToString()); + } } } diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index a4d43563..4a514ac0 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -135,7 +135,9 @@ namespace Shadowsocks.View }), this.ServersItem = CreateMenuGroup("Servers", new MenuItem[] { this.SeperatorItem = new MenuItem("-"), - this.ConfigItem = CreateMenuItem("Edit Servers...", new EventHandler(this.Config_Click)) + this.ConfigItem = CreateMenuItem("Edit Servers...", new EventHandler(this.Config_Click)), + CreateMenuItem("Show QRCode...", new EventHandler(this.QRCodeItem_Click)), + CreateMenuItem("Scan QRCode from Screen...", new EventHandler(this.ScanQRCodeItem_Click)) }), new MenuItem("-"), this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)), @@ -143,8 +145,6 @@ namespace Shadowsocks.View CreateMenuItem("Edit PAC File...", new EventHandler(this.EditPACFileItem_Click)), CreateMenuItem("Update PAC from GFWList", new EventHandler(this.UpdatePACFromGFWListItem_Click)), new MenuItem("-"), - CreateMenuItem("Show QRCode...", new EventHandler(this.QRCodeItem_Click)), - CreateMenuItem("Scan QRCode...", new EventHandler(this.ScanQRCodeItem_Click)), CreateMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)), CreateMenuItem("About...", new EventHandler(this.AboutItem_Click)), new MenuItem("-"), @@ -228,8 +228,10 @@ namespace Shadowsocks.View private void UpdateServersMenu() { var items = ServersItem.MenuItems; - - items.Clear(); + while (items[0] != SeperatorItem) + { + items.RemoveAt(0); + } Configuration configuration = controller.GetConfiguration(); for (int i = 0; i < configuration.configs.Count; i++) @@ -238,10 +240,8 @@ namespace Shadowsocks.View MenuItem item = new MenuItem(server.FriendlyName()); item.Tag = i; item.Click += AServerItem_Click; - items.Add(item); + items.Add(i, item); } - items.Add(SeperatorItem); - items.Add(ConfigItem); if (configuration.index >= 0 && configuration.index < configuration.configs.Count) { @@ -384,8 +384,14 @@ namespace Shadowsocks.View var result = reader.Decode(image); if (result != null) { - Console.WriteLine(result.Text); + var success = controller.AddServerBySSURL(result.Text); + if (success) + { + ShowConfigForm(); + return; + } } + MessageBox.Show(I18N.GetString("Failed to scan QRCode")); } }