From ac21a0960fea4082d8726c576895034139adfcee Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 01:50:47 +0800 Subject: [PATCH 1/6] add config lock Add a lock on config form to avoid accidental changes --- shadowsocks-csharp/Model/Configuration.cs | 22 ++-- shadowsocks-csharp/View/ConfigForm.Designer.cs | 38 +++++- shadowsocks-csharp/View/ConfigForm.cs | 164 +++++++++++++++---------- 3 files changed, 147 insertions(+), 77 deletions(-) diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index d5acbe6d..78eb3c29 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -16,6 +16,8 @@ namespace Shadowsocks.Model public int index; public bool enabled; public bool isDefault; + public bool lockStatus; + public bool enableLog; private static string CONFIG_FILE = "gui-config.json"; @@ -33,11 +35,11 @@ namespace Shadowsocks.Model public static void CheckServer(Server server) { - checkPort(server.local_port); - checkPort(server.server_port); - checkPassword(server.password); - checkServer(server.server); - checkRemark(server.remarks); + CheckPort(server.local_port); + CheckPort(server.server_port); + CheckPassword(server.password); + CheckServer(server.server); + CheckRemark(server.remarks); } public static Configuration Load() @@ -106,7 +108,7 @@ namespace Shadowsocks.Model }; } - private static void assert(bool condition) + private static void Assert(bool condition) { if (!condition) { @@ -114,7 +116,7 @@ namespace Shadowsocks.Model } } - private static void checkPort(int port) + private static void CheckPort(int port) { if (port <= 0 || port > 65535) { @@ -122,7 +124,7 @@ namespace Shadowsocks.Model } } - private static void checkPassword(string password) + private static void CheckPassword(string password) { if (string.IsNullOrEmpty(password)) { @@ -130,7 +132,7 @@ namespace Shadowsocks.Model } } - private static void checkServer(string server) + private static void CheckServer(string server) { if (string.IsNullOrEmpty(server)) { @@ -138,7 +140,7 @@ namespace Shadowsocks.Model } } - private static void checkRemark(string remark) + private static void CheckRemark(string remark) { //remark is optional } diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 6d6f8881..3ad2e1f2 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -55,6 +55,7 @@ this.ConfigItem = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.editPACFileItem = new System.Windows.Forms.MenuItem(); + this.QRCodeItem = new System.Windows.Forms.MenuItem(); this.aboutItem = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.quitItem = new System.Windows.Forms.MenuItem(); @@ -63,11 +64,13 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); - this.QRCodeItem = new System.Windows.Forms.MenuItem(); + this.LockButton = new System.Windows.Forms.Button(); + this.panel4 = new System.Windows.Forms.Panel(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); this.ServerGroupBox.SuspendLayout(); + this.panel4.SuspendLayout(); this.SuspendLayout(); // // tableLayoutPanel1 @@ -329,6 +332,12 @@ this.editPACFileItem.Text = "Edit &PAC File..."; this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click); // + // QRCodeItem + // + this.QRCodeItem.Index = 4; + this.QRCodeItem.Text = "Show &QRCode..."; + this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); + // // aboutItem // this.aboutItem.Index = 5; @@ -396,11 +405,25 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // - // QRCodeItem + // LockButton // - this.QRCodeItem.Index = 4; - this.QRCodeItem.Text = "Show &QRCode..."; - this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); + this.LockButton.Location = new System.Drawing.Point(4, 4); + this.LockButton.Name = "LockButton"; + this.LockButton.Size = new System.Drawing.Size(89, 23); + this.LockButton.TabIndex = 6; + this.LockButton.Text = "&Lock"; + this.LockButton.UseVisualStyleBackColor = true; + this.LockButton.Click += new System.EventHandler(this.LockButton_Click); + // + // panel4 + // + this.panel4.AutoSize = true; + this.panel4.Controls.Add(this.LockButton); + this.panel4.Location = new System.Drawing.Point(12, 251); + this.panel4.Margin = new System.Windows.Forms.Padding(0); + this.panel4.Name = "panel4"; + this.panel4.Size = new System.Drawing.Size(192, 30); + this.panel4.TabIndex = 7; // // ConfigForm // @@ -409,6 +432,7 @@ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; this.ClientSize = new System.Drawing.Size(489, 286); + this.Controls.Add(this.panel4); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); @@ -420,6 +444,7 @@ this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Shadowsocks"; + this.TopMost = true; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ConfigForm_FormClosed); this.Load += new System.EventHandler(this.ConfigForm_Load); this.Shown += new System.EventHandler(this.ConfigForm_Shown); @@ -429,6 +454,7 @@ this.panel3.ResumeLayout(false); this.ServerGroupBox.ResumeLayout(false); this.ServerGroupBox.PerformLayout(); + this.panel4.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -470,6 +496,8 @@ private System.Windows.Forms.TextBox RemarksTextBox; private System.Windows.Forms.Label label6; private System.Windows.Forms.MenuItem QRCodeItem; + private System.Windows.Forms.Button LockButton; + private System.Windows.Forms.Panel panel4; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 174d10fd..17c5c0a0 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; +using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using System.Diagnostics; @@ -15,9 +16,10 @@ namespace Shadowsocks.View private ShadowsocksController controller; // this is a copy of configuration that we are working on - private Configuration modifiedConfiguration; - private int oldSelectedIndex = -1; - private bool isFirstRun; + private Configuration _modifiedConfiguration; + private int _oldSelectedIndex = -1; + private bool _isFirstRun; + private bool _lockStatus; public ConfigForm(ShadowsocksController controller) { @@ -29,12 +31,12 @@ namespace Shadowsocks.View controller.ConfigChanged += controller_ConfigChanged; controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; - loadCurrentConfiguration(); + LoadCurrentConfiguration(); } private void controller_ConfigChanged(object sender, EventArgs e) { - loadCurrentConfiguration(); + LoadCurrentConfiguration(); } private void controller_EnableStatusChanged(object sender, EventArgs e) @@ -50,18 +52,31 @@ namespace Shadowsocks.View } - private void showWindow() + private void ShowWindow() { this.Opacity = 1; this.Show(); IPTextBox.Focus(); + if (_lockStatus) + { + IPTextBox.Enabled = false; + ServerPortTextBox.Enabled = false; + PasswordTextBox.Enabled = false; + EncryptionSelect.Enabled = false; + ProxyPortTextBox.Enabled = false; + RemarksTextBox.Enabled = false; + AddButton.Enabled = false; + DeleteButton.Enabled = false; + _lockStatus = true; + LockButton.Text = "&Unlock Settings"; + } } - private bool saveOldSelectedServer() + private bool SaveOldSelectedServer() { try { - if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count) + if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) { return true; } @@ -75,7 +90,7 @@ namespace Shadowsocks.View remarks = RemarksTextBox.Text }; Configuration.CheckServer(server); - modifiedConfiguration.configs[oldSelectedIndex] = server; + _modifiedConfiguration.configs[_oldSelectedIndex] = server; return true; } catch (FormatException) @@ -89,17 +104,17 @@ namespace Shadowsocks.View return false; } - private void loadSelectedServer() + private void LoadSelectedServer() { - if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < modifiedConfiguration.configs.Count) + if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count) { - Server server = modifiedConfiguration.configs[ServersListBox.SelectedIndex]; + Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex]; IPTextBox.Text = server.server; ServerPortTextBox.Text = server.server_port.ToString(); PasswordTextBox.Text = server.password; ProxyPortTextBox.Text = server.local_port.ToString(); - EncryptionSelect.Text = server.method == null ? "aes-256-cfb" : server.method; + EncryptionSelect.Text = server.method ?? "aes-256-cfb"; RemarksTextBox.Text = server.remarks; ServerGroupBox.Visible = true; IPTextBox.Focus(); @@ -110,28 +125,29 @@ namespace Shadowsocks.View } } - private void loadConfiguration(Configuration configuration) + private void LoadConfiguration(Configuration configuration) { ServersListBox.Items.Clear(); - foreach (Server server in modifiedConfiguration.configs) + foreach (Server server in _modifiedConfiguration.configs) { ServersListBox.Items.Add(string.IsNullOrEmpty(server.server) ? "New server" : string.IsNullOrEmpty(server.remarks)? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")"); } } - private void loadCurrentConfiguration() + private void LoadCurrentConfiguration() { - modifiedConfiguration = controller.GetConfiguration(); - loadConfiguration(modifiedConfiguration); - oldSelectedIndex = modifiedConfiguration.index; - ServersListBox.SelectedIndex = modifiedConfiguration.index; - loadSelectedServer(); - - updateServersMenu(); - enableItem.Checked = modifiedConfiguration.enabled; + _modifiedConfiguration = controller.GetConfiguration(); + LoadConfiguration(_modifiedConfiguration); + _oldSelectedIndex = _modifiedConfiguration.index; + _lockStatus = _modifiedConfiguration.lockStatus; + ServersListBox.SelectedIndex = _modifiedConfiguration.index; + LoadSelectedServer(); + + UpdateServersMenu(); + enableItem.Checked = _modifiedConfiguration.enabled; } - private void updateServersMenu() + private void UpdateServersMenu() { var items = ServersItem.MenuItems; @@ -157,71 +173,68 @@ namespace Shadowsocks.View private void ConfigForm_Load(object sender, EventArgs e) { - if (!controller.GetConfiguration().isDefault) + if (controller == null || controller.GetConfiguration().isDefault) { - this.Opacity = 0; - BeginInvoke(new MethodInvoker(delegate - { - this.Hide(); - })); + _isFirstRun = true; } else { - isFirstRun = true; + this.Opacity = 0; + BeginInvoke(new MethodInvoker(delegate { this.Hide(); })); } } private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) { - if (oldSelectedIndex == ServersListBox.SelectedIndex) + if (_oldSelectedIndex == ServersListBox.SelectedIndex) { // we are moving back to oldSelectedIndex or doing a force move return; } - if (!saveOldSelectedServer()) + if (!SaveOldSelectedServer()) { // why this won't cause stack overflow? - ServersListBox.SelectedIndex = oldSelectedIndex; + ServersListBox.SelectedIndex = _oldSelectedIndex; return; } - loadSelectedServer(); - oldSelectedIndex = ServersListBox.SelectedIndex; + LoadSelectedServer(); + _oldSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!saveOldSelectedServer()) + if (!SaveOldSelectedServer()) { return; } Server server = Configuration.GetDefaultServer(); - modifiedConfiguration.configs.Add(server); - loadConfiguration(modifiedConfiguration); - ServersListBox.SelectedIndex = modifiedConfiguration.configs.Count - 1; - oldSelectedIndex = ServersListBox.SelectedIndex; + _modifiedConfiguration.configs.Add(server); + LoadConfiguration(_modifiedConfiguration); + ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1; + _oldSelectedIndex = ServersListBox.SelectedIndex; } private void DeleteButton_Click(object sender, EventArgs e) { - oldSelectedIndex = ServersListBox.SelectedIndex; - if (oldSelectedIndex >= 0 && oldSelectedIndex < modifiedConfiguration.configs.Count) + _oldSelectedIndex = ServersListBox.SelectedIndex; + if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count) { - modifiedConfiguration.configs.RemoveAt(oldSelectedIndex); + _modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex); } - if (oldSelectedIndex >= modifiedConfiguration.configs.Count) + if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count) { // can be -1 - oldSelectedIndex = modifiedConfiguration.configs.Count - 1; + _oldSelectedIndex = _modifiedConfiguration.configs.Count - 1; } - ServersListBox.SelectedIndex = oldSelectedIndex; - loadConfiguration(modifiedConfiguration); - ServersListBox.SelectedIndex = oldSelectedIndex; - loadSelectedServer(); + ServersListBox.SelectedIndex = _oldSelectedIndex; + LoadConfiguration(_modifiedConfiguration); + ServersListBox.SelectedIndex = _oldSelectedIndex; + LoadSelectedServer(); } private void Config_Click(object sender, EventArgs e) { - showWindow(); + ShowWindow(); } private void Quit_Click(object sender, EventArgs e) @@ -229,38 +242,39 @@ namespace Shadowsocks.View this.Close(); } - private void showFirstTimeBalloon() + private void ShowFirstTimeBalloon() { - if (isFirstRun) + if (_isFirstRun) { notifyIcon1.BalloonTipTitle = "Shadowsocks is here"; notifyIcon1.BalloonTipText = "You can turn on/off Shadowsocks in the context menu"; notifyIcon1.ShowBalloonTip(0); - isFirstRun = false; + _isFirstRun = false; } } private void OKButton_Click(object sender, EventArgs e) { - if (!saveOldSelectedServer()) + if (!SaveOldSelectedServer()) { return; } - if (modifiedConfiguration.configs.Count == 0) + if (_modifiedConfiguration.configs.Count == 0) { MessageBox.Show("Please add at least one server"); return; } - controller.SaveConfig(modifiedConfiguration); + _modifiedConfiguration.lockStatus = _lockStatus; + controller.SaveConfig(_modifiedConfiguration); this.Hide(); - showFirstTimeBalloon(); + ShowFirstTimeBalloon(); } private void CancelButton_Click(object sender, EventArgs e) { this.Hide(); - loadCurrentConfiguration(); - showFirstTimeBalloon(); + LoadCurrentConfiguration(); + ShowFirstTimeBalloon(); } private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e) @@ -275,7 +289,7 @@ namespace Shadowsocks.View private void notifyIcon1_DoubleClick(object sender, EventArgs e) { - showWindow(); + ShowWindow(); } @@ -307,5 +321,31 @@ namespace Shadowsocks.View { new QRCodeForm(controller.GetQRCodeForCurrentServer()).Show(); } + + private void ChangeLockStatus(bool lockStatus) + { + IPTextBox.Enabled = lockStatus; + ServerPortTextBox.Enabled = lockStatus; + PasswordTextBox.Enabled = lockStatus; + EncryptionSelect.Enabled = lockStatus; + ProxyPortTextBox.Enabled = lockStatus; + RemarksTextBox.Enabled = lockStatus; + AddButton.Enabled = lockStatus; + DeleteButton.Enabled = lockStatus; + _lockStatus = !lockStatus; + if (lockStatus) + { + LockButton.Text = "&Lock"; + } + else + { + LockButton.Text = "&Unlock"; + } + } + + private void LockButton_Click(object sender, EventArgs e) + { + ChangeLockStatus(_lockStatus); + } } } From 24614ba2a7c0c46092e59ad6bb1d15c5a398949e Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 02:04:11 +0800 Subject: [PATCH 2/6] add cancel button for config Now can use esc key to cancel, and don't change focus after change server from list --- shadowsocks-csharp/View/ConfigForm.Designer.cs | 11 ++++++----- shadowsocks-csharp/View/ConfigForm.cs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 3ad2e1f2..a661b65a 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -256,7 +256,7 @@ this.OKButton.Location = new System.Drawing.Point(4, 4); this.OKButton.Name = "OKButton"; this.OKButton.Size = new System.Drawing.Size(75, 23); - this.OKButton.TabIndex = 8; + this.OKButton.TabIndex = 6; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = true; this.OKButton.Click += new System.EventHandler(this.OKButton_Click); @@ -267,7 +267,7 @@ this.MyCancelButton.Location = new System.Drawing.Point(86, 4); this.MyCancelButton.Name = "MyCancelButton"; this.MyCancelButton.Size = new System.Drawing.Size(75, 23); - this.MyCancelButton.TabIndex = 9; + this.MyCancelButton.TabIndex = 7; this.MyCancelButton.Text = "Cancel"; this.MyCancelButton.UseVisualStyleBackColor = true; this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); @@ -371,7 +371,7 @@ this.DeleteButton.Location = new System.Drawing.Point(100, 4); this.DeleteButton.Name = "DeleteButton"; this.DeleteButton.Size = new System.Drawing.Size(89, 23); - this.DeleteButton.TabIndex = 7; + this.DeleteButton.TabIndex = 9; this.DeleteButton.Text = "&Delete"; this.DeleteButton.UseVisualStyleBackColor = true; this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click); @@ -381,7 +381,7 @@ this.AddButton.Location = new System.Drawing.Point(4, 4); this.AddButton.Name = "AddButton"; this.AddButton.Size = new System.Drawing.Size(89, 23); - this.AddButton.TabIndex = 6; + this.AddButton.TabIndex = 8; this.AddButton.Text = "&Add"; this.AddButton.UseVisualStyleBackColor = true; this.AddButton.Click += new System.EventHandler(this.AddButton_Click); @@ -410,7 +410,7 @@ this.LockButton.Location = new System.Drawing.Point(4, 4); this.LockButton.Name = "LockButton"; this.LockButton.Size = new System.Drawing.Size(89, 23); - this.LockButton.TabIndex = 6; + this.LockButton.TabIndex = 10; this.LockButton.Text = "&Lock"; this.LockButton.UseVisualStyleBackColor = true; this.LockButton.Click += new System.EventHandler(this.LockButton_Click); @@ -431,6 +431,7 @@ this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; + this.CancelButton = this.MyCancelButton; this.ClientSize = new System.Drawing.Size(489, 286); this.Controls.Add(this.panel4); this.Controls.Add(this.ServersListBox); diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 17c5c0a0..68cb861c 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -117,7 +117,7 @@ namespace Shadowsocks.View EncryptionSelect.Text = server.method ?? "aes-256-cfb"; RemarksTextBox.Text = server.remarks; ServerGroupBox.Visible = true; - IPTextBox.Focus(); + //IPTextBox.Focus(); } else { From b5eac341b32a938e7f17b830ceedca9ba1577956 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 02:10:43 +0800 Subject: [PATCH 3/6] revert --- shadowsocks-csharp/Model/Configuration.cs | 22 ++-- shadowsocks-csharp/View/ConfigForm.Designer.cs | 47 ++----- shadowsocks-csharp/View/ConfigForm.cs | 164 ++++++++++--------------- 3 files changed, 81 insertions(+), 152 deletions(-) diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 78eb3c29..d5acbe6d 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -16,8 +16,6 @@ namespace Shadowsocks.Model public int index; public bool enabled; public bool isDefault; - public bool lockStatus; - public bool enableLog; private static string CONFIG_FILE = "gui-config.json"; @@ -35,11 +33,11 @@ namespace Shadowsocks.Model public static void CheckServer(Server server) { - CheckPort(server.local_port); - CheckPort(server.server_port); - CheckPassword(server.password); - CheckServer(server.server); - CheckRemark(server.remarks); + checkPort(server.local_port); + checkPort(server.server_port); + checkPassword(server.password); + checkServer(server.server); + checkRemark(server.remarks); } public static Configuration Load() @@ -108,7 +106,7 @@ namespace Shadowsocks.Model }; } - private static void Assert(bool condition) + private static void assert(bool condition) { if (!condition) { @@ -116,7 +114,7 @@ namespace Shadowsocks.Model } } - private static void CheckPort(int port) + private static void checkPort(int port) { if (port <= 0 || port > 65535) { @@ -124,7 +122,7 @@ namespace Shadowsocks.Model } } - private static void CheckPassword(string password) + private static void checkPassword(string password) { if (string.IsNullOrEmpty(password)) { @@ -132,7 +130,7 @@ namespace Shadowsocks.Model } } - private static void CheckServer(string server) + private static void checkServer(string server) { if (string.IsNullOrEmpty(server)) { @@ -140,7 +138,7 @@ namespace Shadowsocks.Model } } - private static void CheckRemark(string remark) + private static void checkRemark(string remark) { //remark is optional } diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index a661b65a..6d6f8881 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -55,7 +55,6 @@ this.ConfigItem = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.editPACFileItem = new System.Windows.Forms.MenuItem(); - this.QRCodeItem = new System.Windows.Forms.MenuItem(); this.aboutItem = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.quitItem = new System.Windows.Forms.MenuItem(); @@ -64,13 +63,11 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); - this.LockButton = new System.Windows.Forms.Button(); - this.panel4 = new System.Windows.Forms.Panel(); + this.QRCodeItem = new System.Windows.Forms.MenuItem(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); this.ServerGroupBox.SuspendLayout(); - this.panel4.SuspendLayout(); this.SuspendLayout(); // // tableLayoutPanel1 @@ -256,7 +253,7 @@ this.OKButton.Location = new System.Drawing.Point(4, 4); this.OKButton.Name = "OKButton"; this.OKButton.Size = new System.Drawing.Size(75, 23); - this.OKButton.TabIndex = 6; + this.OKButton.TabIndex = 8; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = true; this.OKButton.Click += new System.EventHandler(this.OKButton_Click); @@ -267,7 +264,7 @@ this.MyCancelButton.Location = new System.Drawing.Point(86, 4); this.MyCancelButton.Name = "MyCancelButton"; this.MyCancelButton.Size = new System.Drawing.Size(75, 23); - this.MyCancelButton.TabIndex = 7; + this.MyCancelButton.TabIndex = 9; this.MyCancelButton.Text = "Cancel"; this.MyCancelButton.UseVisualStyleBackColor = true; this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); @@ -332,12 +329,6 @@ this.editPACFileItem.Text = "Edit &PAC File..."; this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click); // - // QRCodeItem - // - this.QRCodeItem.Index = 4; - this.QRCodeItem.Text = "Show &QRCode..."; - this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); - // // aboutItem // this.aboutItem.Index = 5; @@ -371,7 +362,7 @@ this.DeleteButton.Location = new System.Drawing.Point(100, 4); this.DeleteButton.Name = "DeleteButton"; this.DeleteButton.Size = new System.Drawing.Size(89, 23); - this.DeleteButton.TabIndex = 9; + this.DeleteButton.TabIndex = 7; this.DeleteButton.Text = "&Delete"; this.DeleteButton.UseVisualStyleBackColor = true; this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click); @@ -381,7 +372,7 @@ this.AddButton.Location = new System.Drawing.Point(4, 4); this.AddButton.Name = "AddButton"; this.AddButton.Size = new System.Drawing.Size(89, 23); - this.AddButton.TabIndex = 8; + this.AddButton.TabIndex = 6; this.AddButton.Text = "&Add"; this.AddButton.UseVisualStyleBackColor = true; this.AddButton.Click += new System.EventHandler(this.AddButton_Click); @@ -405,25 +396,11 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // - // LockButton - // - this.LockButton.Location = new System.Drawing.Point(4, 4); - this.LockButton.Name = "LockButton"; - this.LockButton.Size = new System.Drawing.Size(89, 23); - this.LockButton.TabIndex = 10; - this.LockButton.Text = "&Lock"; - this.LockButton.UseVisualStyleBackColor = true; - this.LockButton.Click += new System.EventHandler(this.LockButton_Click); - // - // panel4 + // QRCodeItem // - this.panel4.AutoSize = true; - this.panel4.Controls.Add(this.LockButton); - this.panel4.Location = new System.Drawing.Point(12, 251); - this.panel4.Margin = new System.Windows.Forms.Padding(0); - this.panel4.Name = "panel4"; - this.panel4.Size = new System.Drawing.Size(192, 30); - this.panel4.TabIndex = 7; + this.QRCodeItem.Index = 4; + this.QRCodeItem.Text = "Show &QRCode..."; + this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); // // ConfigForm // @@ -431,9 +408,7 @@ this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; - this.CancelButton = this.MyCancelButton; this.ClientSize = new System.Drawing.Size(489, 286); - this.Controls.Add(this.panel4); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); @@ -445,7 +420,6 @@ this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Shadowsocks"; - this.TopMost = true; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ConfigForm_FormClosed); this.Load += new System.EventHandler(this.ConfigForm_Load); this.Shown += new System.EventHandler(this.ConfigForm_Shown); @@ -455,7 +429,6 @@ this.panel3.ResumeLayout(false); this.ServerGroupBox.ResumeLayout(false); this.ServerGroupBox.PerformLayout(); - this.panel4.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -497,8 +470,6 @@ private System.Windows.Forms.TextBox RemarksTextBox; private System.Windows.Forms.Label label6; private System.Windows.Forms.MenuItem QRCodeItem; - private System.Windows.Forms.Button LockButton; - private System.Windows.Forms.Panel panel4; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 68cb861c..2631f529 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; -using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using System.Diagnostics; @@ -16,10 +15,9 @@ namespace Shadowsocks.View private ShadowsocksController controller; // this is a copy of configuration that we are working on - private Configuration _modifiedConfiguration; - private int _oldSelectedIndex = -1; - private bool _isFirstRun; - private bool _lockStatus; + private Configuration modifiedConfiguration; + private int oldSelectedIndex = -1; + private bool isFirstRun; public ConfigForm(ShadowsocksController controller) { @@ -31,12 +29,12 @@ namespace Shadowsocks.View controller.ConfigChanged += controller_ConfigChanged; controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; - LoadCurrentConfiguration(); + loadCurrentConfiguration(); } private void controller_ConfigChanged(object sender, EventArgs e) { - LoadCurrentConfiguration(); + loadCurrentConfiguration(); } private void controller_EnableStatusChanged(object sender, EventArgs e) @@ -52,31 +50,18 @@ namespace Shadowsocks.View } - private void ShowWindow() + private void showWindow() { this.Opacity = 1; this.Show(); IPTextBox.Focus(); - if (_lockStatus) - { - IPTextBox.Enabled = false; - ServerPortTextBox.Enabled = false; - PasswordTextBox.Enabled = false; - EncryptionSelect.Enabled = false; - ProxyPortTextBox.Enabled = false; - RemarksTextBox.Enabled = false; - AddButton.Enabled = false; - DeleteButton.Enabled = false; - _lockStatus = true; - LockButton.Text = "&Unlock Settings"; - } } - private bool SaveOldSelectedServer() + private bool saveOldSelectedServer() { try { - if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) + if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count) { return true; } @@ -90,7 +75,7 @@ namespace Shadowsocks.View remarks = RemarksTextBox.Text }; Configuration.CheckServer(server); - _modifiedConfiguration.configs[_oldSelectedIndex] = server; + modifiedConfiguration.configs[oldSelectedIndex] = server; return true; } catch (FormatException) @@ -104,17 +89,17 @@ namespace Shadowsocks.View return false; } - private void LoadSelectedServer() + private void loadSelectedServer() { - if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count) + if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < modifiedConfiguration.configs.Count) { - Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex]; + Server server = modifiedConfiguration.configs[ServersListBox.SelectedIndex]; IPTextBox.Text = server.server; ServerPortTextBox.Text = server.server_port.ToString(); PasswordTextBox.Text = server.password; ProxyPortTextBox.Text = server.local_port.ToString(); - EncryptionSelect.Text = server.method ?? "aes-256-cfb"; + EncryptionSelect.Text = server.method == null ? "aes-256-cfb" : server.method; RemarksTextBox.Text = server.remarks; ServerGroupBox.Visible = true; //IPTextBox.Focus(); @@ -125,29 +110,28 @@ namespace Shadowsocks.View } } - private void LoadConfiguration(Configuration configuration) + private void loadConfiguration(Configuration configuration) { ServersListBox.Items.Clear(); - foreach (Server server in _modifiedConfiguration.configs) + foreach (Server server in modifiedConfiguration.configs) { ServersListBox.Items.Add(string.IsNullOrEmpty(server.server) ? "New server" : string.IsNullOrEmpty(server.remarks)? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")"); } } - private void LoadCurrentConfiguration() + private void loadCurrentConfiguration() { - _modifiedConfiguration = controller.GetConfiguration(); - LoadConfiguration(_modifiedConfiguration); - _oldSelectedIndex = _modifiedConfiguration.index; - _lockStatus = _modifiedConfiguration.lockStatus; - ServersListBox.SelectedIndex = _modifiedConfiguration.index; - LoadSelectedServer(); - - UpdateServersMenu(); - enableItem.Checked = _modifiedConfiguration.enabled; + modifiedConfiguration = controller.GetConfiguration(); + loadConfiguration(modifiedConfiguration); + oldSelectedIndex = modifiedConfiguration.index; + ServersListBox.SelectedIndex = modifiedConfiguration.index; + loadSelectedServer(); + + updateServersMenu(); + enableItem.Checked = modifiedConfiguration.enabled; } - private void UpdateServersMenu() + private void updateServersMenu() { var items = ServersItem.MenuItems; @@ -173,68 +157,71 @@ namespace Shadowsocks.View private void ConfigForm_Load(object sender, EventArgs e) { - if (controller == null || controller.GetConfiguration().isDefault) + if (!controller.GetConfiguration().isDefault) { - _isFirstRun = true; + this.Opacity = 0; + BeginInvoke(new MethodInvoker(delegate + { + this.Hide(); + })); } else { - this.Opacity = 0; - BeginInvoke(new MethodInvoker(delegate { this.Hide(); })); + isFirstRun = true; } } private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) { - if (_oldSelectedIndex == ServersListBox.SelectedIndex) + if (oldSelectedIndex == ServersListBox.SelectedIndex) { // we are moving back to oldSelectedIndex or doing a force move return; } - if (!SaveOldSelectedServer()) + if (!saveOldSelectedServer()) { // why this won't cause stack overflow? - ServersListBox.SelectedIndex = _oldSelectedIndex; + ServersListBox.SelectedIndex = oldSelectedIndex; return; } - LoadSelectedServer(); - _oldSelectedIndex = ServersListBox.SelectedIndex; + loadSelectedServer(); + oldSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!saveOldSelectedServer()) { return; } Server server = Configuration.GetDefaultServer(); - _modifiedConfiguration.configs.Add(server); - LoadConfiguration(_modifiedConfiguration); - ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1; - _oldSelectedIndex = ServersListBox.SelectedIndex; + modifiedConfiguration.configs.Add(server); + loadConfiguration(modifiedConfiguration); + ServersListBox.SelectedIndex = modifiedConfiguration.configs.Count - 1; + oldSelectedIndex = ServersListBox.SelectedIndex; } private void DeleteButton_Click(object sender, EventArgs e) { - _oldSelectedIndex = ServersListBox.SelectedIndex; - if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count) + oldSelectedIndex = ServersListBox.SelectedIndex; + if (oldSelectedIndex >= 0 && oldSelectedIndex < modifiedConfiguration.configs.Count) { - _modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex); + modifiedConfiguration.configs.RemoveAt(oldSelectedIndex); } - if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count) + if (oldSelectedIndex >= modifiedConfiguration.configs.Count) { // can be -1 - _oldSelectedIndex = _modifiedConfiguration.configs.Count - 1; + oldSelectedIndex = modifiedConfiguration.configs.Count - 1; } - ServersListBox.SelectedIndex = _oldSelectedIndex; - LoadConfiguration(_modifiedConfiguration); - ServersListBox.SelectedIndex = _oldSelectedIndex; - LoadSelectedServer(); + ServersListBox.SelectedIndex = oldSelectedIndex; + loadConfiguration(modifiedConfiguration); + ServersListBox.SelectedIndex = oldSelectedIndex; + loadSelectedServer(); } private void Config_Click(object sender, EventArgs e) { - ShowWindow(); + showWindow(); } private void Quit_Click(object sender, EventArgs e) @@ -242,39 +229,38 @@ namespace Shadowsocks.View this.Close(); } - private void ShowFirstTimeBalloon() + private void showFirstTimeBalloon() { - if (_isFirstRun) + if (isFirstRun) { notifyIcon1.BalloonTipTitle = "Shadowsocks is here"; notifyIcon1.BalloonTipText = "You can turn on/off Shadowsocks in the context menu"; notifyIcon1.ShowBalloonTip(0); - _isFirstRun = false; + isFirstRun = false; } } private void OKButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!saveOldSelectedServer()) { return; } - if (_modifiedConfiguration.configs.Count == 0) + if (modifiedConfiguration.configs.Count == 0) { MessageBox.Show("Please add at least one server"); return; } - _modifiedConfiguration.lockStatus = _lockStatus; - controller.SaveConfig(_modifiedConfiguration); + controller.SaveConfig(modifiedConfiguration); this.Hide(); - ShowFirstTimeBalloon(); + showFirstTimeBalloon(); } private void CancelButton_Click(object sender, EventArgs e) { this.Hide(); - LoadCurrentConfiguration(); - ShowFirstTimeBalloon(); + loadCurrentConfiguration(); + showFirstTimeBalloon(); } private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e) @@ -289,7 +275,7 @@ namespace Shadowsocks.View private void notifyIcon1_DoubleClick(object sender, EventArgs e) { - ShowWindow(); + showWindow(); } @@ -321,31 +307,5 @@ namespace Shadowsocks.View { new QRCodeForm(controller.GetQRCodeForCurrentServer()).Show(); } - - private void ChangeLockStatus(bool lockStatus) - { - IPTextBox.Enabled = lockStatus; - ServerPortTextBox.Enabled = lockStatus; - PasswordTextBox.Enabled = lockStatus; - EncryptionSelect.Enabled = lockStatus; - ProxyPortTextBox.Enabled = lockStatus; - RemarksTextBox.Enabled = lockStatus; - AddButton.Enabled = lockStatus; - DeleteButton.Enabled = lockStatus; - _lockStatus = !lockStatus; - if (lockStatus) - { - LockButton.Text = "&Lock"; - } - else - { - LockButton.Text = "&Unlock"; - } - } - - private void LockButton_Click(object sender, EventArgs e) - { - ChangeLockStatus(_lockStatus); - } } } From 9f50a5a34b04b94da529cb4c490544e7287185b3 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 02:13:35 +0800 Subject: [PATCH 4/6] add cancel button for config Now can use esc key to cancel, and don't change focus after change server from list --- shadowsocks-csharp/View/ConfigForm.Designer.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 6d6f8881..b2d20a84 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -55,6 +55,7 @@ this.ConfigItem = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.editPACFileItem = new System.Windows.Forms.MenuItem(); + this.QRCodeItem = new System.Windows.Forms.MenuItem(); this.aboutItem = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.quitItem = new System.Windows.Forms.MenuItem(); @@ -63,7 +64,6 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); - this.QRCodeItem = new System.Windows.Forms.MenuItem(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); @@ -329,6 +329,12 @@ this.editPACFileItem.Text = "Edit &PAC File..."; this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click); // + // QRCodeItem + // + this.QRCodeItem.Index = 4; + this.QRCodeItem.Text = "Show &QRCode..."; + this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); + // // aboutItem // this.aboutItem.Index = 5; @@ -396,18 +402,13 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // - // QRCodeItem - // - this.QRCodeItem.Index = 4; - this.QRCodeItem.Text = "Show &QRCode..."; - this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); - // // ConfigForm // this.AcceptButton = this.OKButton; this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; + this.CancelButton = this.MyCancelButton; this.ClientSize = new System.Drawing.Size(489, 286); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); From fc53a072035909e5fa0aa89ab712bd83abfbb788 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 02:18:47 +0800 Subject: [PATCH 5/6] change function to C# style Change the first letter of function to upper case letter to fit C# code style --- shadowsocks-csharp/Controller/Local.cs | 30 +++++++------- shadowsocks-csharp/Controller/PACServer.cs | 28 ++++++------- shadowsocks-csharp/Controller/PolipoRunner.cs | 30 +++++++------- .../Controller/ShadowsocksController.cs | 32 +++++++-------- shadowsocks-csharp/Encrypt/EncryptorFactory.cs | 30 +++++++------- shadowsocks-csharp/Model/Configuration.cs | 20 ++++----- shadowsocks-csharp/View/ConfigForm.cs | 48 +++++++++++----------- 7 files changed, 109 insertions(+), 109 deletions(-) diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index b5db6c0c..cace2f57 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -13,7 +13,7 @@ namespace Shadowsocks.Controller { private Server config; //private Encryptor encryptor; - Socket listener; + Socket _listener; public Local(Server config) { this.config = config; @@ -25,24 +25,24 @@ namespace Shadowsocks.Controller try { // Create a TCP/IP socket. - listener = new Socket(AddressFamily.InterNetwork, + _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port); // Bind the socket to the local endpoint and listen for incoming connections. - listener.Bind(localEndPoint); - listener.Listen(100); + _listener.Bind(localEndPoint); + _listener.Listen(100); // Start an asynchronous socket to listen for connections. Console.WriteLine("Shadowsocks started"); - listener.BeginAccept( + _listener.BeginAccept( new AsyncCallback(AcceptCallback), - listener); + _listener); } catch(SocketException) { - listener.Close(); + _listener.Close(); throw; } @@ -50,7 +50,7 @@ namespace Shadowsocks.Controller public void Stop() { - listener.Close(); + _listener.Close(); } @@ -90,7 +90,7 @@ namespace Shadowsocks.Controller //handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, // new AsyncCallback(ReadCallback), state); } - catch (Exception) + catch { //Console.WriteLine(e.Message); } @@ -141,7 +141,7 @@ namespace Shadowsocks.Controller // Connect to the remote endpoint. remote.BeginConnect(remoteEP, - new AsyncCallback(connectCallback), null); + new AsyncCallback(ConnectCallback), null); } catch (Exception e) { @@ -182,7 +182,7 @@ namespace Shadowsocks.Controller ((IDisposable)encryptor).Dispose(); } - private void connectCallback(IAsyncResult ar) + private void ConnectCallback(IAsyncResult ar) { try { @@ -192,7 +192,7 @@ namespace Shadowsocks.Controller //Console.WriteLine("Socket connected to {0}", // remote.RemoteEndPoint.ToString()); - handshakeReceive(); + HandshakeReceive(); } catch (Exception e) { @@ -201,12 +201,12 @@ namespace Shadowsocks.Controller } } - private void handshakeReceive() + private void HandshakeReceive() { try { connection.BeginReceive(connetionRecvBuffer, 0, 256, 0, - new AsyncCallback(handshakeReceiveCallback), null); + new AsyncCallback(HandshakeReceiveCallback), null); } catch (Exception e) { @@ -215,7 +215,7 @@ namespace Shadowsocks.Controller } } - private void handshakeReceiveCallback(IAsyncResult ar) + private void HandshakeReceiveCallback(IAsyncResult ar) { try { diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index bedce2e4..eab2ff60 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -33,7 +33,7 @@ namespace Shadowsocks.Controller new AsyncCallback(AcceptCallback), listener); - watchPACFile(); + WatchPacFile(); } public string TouchPACFile() @@ -61,7 +61,7 @@ namespace Shadowsocks.Controller Socket conn = listener.EndAccept(ar); conn.BeginReceive(new byte[1024], 0, 1024, 0, - new AsyncCallback(receiveCallback), conn); + new AsyncCallback(ReceiveCallback), conn); } catch (Exception e) { @@ -69,7 +69,7 @@ namespace Shadowsocks.Controller } } - private string getPACContent() + private string GetPACContent() { if (File.Exists(PAC_FILE)) { @@ -92,17 +92,17 @@ namespace Shadowsocks.Controller return System.Text.Encoding.UTF8.GetString(buffer, 0, n); } } - watchPACFile(); + WatchPacFile(); } - private void receiveCallback(IAsyncResult ar) + private void ReceiveCallback(IAsyncResult ar) { Socket conn = (Socket)ar.AsyncState; try { int bytesRead = conn.EndReceive(ar); - string pac = getPACContent(); + string pac = GetPACContent(); string proxy = "PROXY 127.0.0.1:8123;"; @@ -118,7 +118,7 @@ Connection: Close ", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac; byte[] response = System.Text.Encoding.UTF8.GetBytes(text); - conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(sendCallback), conn); + conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), conn); } else { @@ -132,13 +132,13 @@ Connection: Close } } - private void sendCallback(IAsyncResult ar) + private void SendCallback(IAsyncResult ar) { Socket conn = (Socket)ar.AsyncState; conn.Shutdown(SocketShutdown.Send); } - private void watchPACFile() + private void WatchPacFile() { if (watcher != null) { @@ -147,14 +147,14 @@ Connection: Close watcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.Filter = PAC_FILE; - watcher.Changed += watcher_Changed; - watcher.Created += watcher_Changed; - watcher.Deleted += watcher_Changed; - watcher.Renamed += watcher_Changed; + watcher.Changed += Watcher_Changed; + watcher.Created += Watcher_Changed; + watcher.Deleted += Watcher_Changed; + watcher.Renamed += Watcher_Changed; watcher.EnableRaisingEvents = true; } - void watcher_Changed(object sender, FileSystemEventArgs e) + private void Watcher_Changed(object sender, FileSystemEventArgs e) { if (PACFileChanged != null) { diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index 1f9802cf..9f3c2187 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -11,11 +11,11 @@ namespace Shadowsocks.Controller { class PolipoRunner { - private Process process; + private Process _process; public void Start(Server config) { - if (process == null) + if (_process == null) { Process[] existingPolipo = Process.GetProcessesByName("ss_polipo"); foreach (Process p in existingPolipo) @@ -36,34 +36,34 @@ namespace Shadowsocks.Controller FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); - process = new Process(); + _process = new Process(); // Configure the process using the StartInfo properties. - process.StartInfo.FileName = temppath + "/ss_polipo.exe"; - process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\""; - process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.RedirectStandardError = true; + _process.StartInfo.FileName = temppath + "/ss_polipo.exe"; + _process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\""; + _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + _process.StartInfo.UseShellExecute = false; + _process.StartInfo.CreateNoWindow = true; + _process.StartInfo.RedirectStandardOutput = true; + _process.StartInfo.RedirectStandardError = true; //process.StandardOutput - process.Start(); + _process.Start(); } } public void Stop() { - if (process != null) + if (_process != null) { try { - process.Kill(); - process.WaitForExit(); + _process.Kill(); + _process.WaitForExit(); } catch (InvalidOperationException) { // do nothing } - process = null; + _process = null; } } } diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 4111d2b8..5af0bf38 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -14,7 +14,7 @@ namespace Shadowsocks.Controller private Local local; private PACServer pacServer; - private Configuration config; + private Configuration _config; private PolipoRunner polipoRunner; private bool stopped = false; @@ -31,10 +31,10 @@ namespace Shadowsocks.Controller public ShadowsocksController() { - config = Configuration.Load(); + _config = Configuration.Load(); polipoRunner = new PolipoRunner(); - polipoRunner.Start(config.GetCurrentServer()); - local = new Local(config.GetCurrentServer()); + polipoRunner.Start(_config.GetCurrentServer()); + local = new Local(_config.GetCurrentServer()); try { local.Start(); @@ -47,20 +47,20 @@ namespace Shadowsocks.Controller Console.WriteLine(e.Message); } - updateSystemProxy(); + UpdateSystemProxy(); } public void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); // some logic in configuration updated the config when saving, we need to read it again - config = Configuration.Load(); + _config = Configuration.Load(); local.Stop(); polipoRunner.Stop(); - polipoRunner.Start(config.GetCurrentServer()); + polipoRunner.Start(_config.GetCurrentServer()); - local = new Local(config.GetCurrentServer()); + local = new Local(_config.GetCurrentServer()); local.Start(); if (ConfigChanged != null) @@ -71,7 +71,7 @@ namespace Shadowsocks.Controller public Server GetCurrentServer() { - return config.GetCurrentServer(); + return _config.GetCurrentServer(); } // always return copy @@ -83,9 +83,9 @@ namespace Shadowsocks.Controller public void ToggleEnable(bool enabled) { - config.enabled = enabled; - updateSystemProxy(); - SaveConfig(config); + _config.enabled = enabled; + UpdateSystemProxy(); + SaveConfig(_config); if (EnableStatusChanged != null) { EnableStatusChanged(this, new EventArgs()); @@ -101,7 +101,7 @@ namespace Shadowsocks.Controller stopped = true; local.Stop(); polipoRunner.Stop(); - if (config.enabled) + if (_config.enabled) { SystemProxy.Disable(); } @@ -124,9 +124,9 @@ namespace Shadowsocks.Controller return "ss://" + base64; } - private void updateSystemProxy() + private void UpdateSystemProxy() { - if (config.enabled) + if (_config.enabled) { SystemProxy.Enable(); } @@ -138,7 +138,7 @@ namespace Shadowsocks.Controller private void pacServer_PACFileChanged(object sender, EventArgs e) { - updateSystemProxy(); + UpdateSystemProxy(); } } diff --git a/shadowsocks-csharp/Encrypt/EncryptorFactory.cs b/shadowsocks-csharp/Encrypt/EncryptorFactory.cs index 422e0c2e..59111a69 100644 --- a/shadowsocks-csharp/Encrypt/EncryptorFactory.cs +++ b/shadowsocks-csharp/Encrypt/EncryptorFactory.cs @@ -1,16 +1,16 @@  -namespace Shadowsocks.Encrypt -{ - public static class EncryptorFactory - { - public static IEncryptor GetEncryptor(string method, string password) - { - if (string.IsNullOrEmpty(method) || method.ToLowerInvariant() == "table") - { - return new TableEncryptor(method, password); - } - - return new PolarSSLEncryptor(method, password); - } - } -} +namespace Shadowsocks.Encrypt +{ + public static class EncryptorFactory + { + public static IEncryptor GetEncryptor(string method, string password) + { + if (string.IsNullOrEmpty(method) || method.ToLowerInvariant() == "table") + { + return new TableEncryptor(method, password); + } + + return new PolarSSLEncryptor(method, password); + } + } +} diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index d5acbe6d..c45bb9f4 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -33,11 +33,11 @@ namespace Shadowsocks.Model public static void CheckServer(Server server) { - checkPort(server.local_port); - checkPort(server.server_port); - checkPassword(server.password); - checkServer(server.server); - checkRemark(server.remarks); + CheckPort(server.local_port); + CheckPort(server.server_port); + CheckPassword(server.password); + CheckServer(server.server); + CheckRemark(server.remarks); } public static Configuration Load() @@ -106,7 +106,7 @@ namespace Shadowsocks.Model }; } - private static void assert(bool condition) + private static void Assert(bool condition) { if (!condition) { @@ -114,7 +114,7 @@ namespace Shadowsocks.Model } } - private static void checkPort(int port) + private static void CheckPort(int port) { if (port <= 0 || port > 65535) { @@ -122,7 +122,7 @@ namespace Shadowsocks.Model } } - private static void checkPassword(string password) + private static void CheckPassword(string password) { if (string.IsNullOrEmpty(password)) { @@ -130,7 +130,7 @@ namespace Shadowsocks.Model } } - private static void checkServer(string server) + private static void CheckServer(string server) { if (string.IsNullOrEmpty(server)) { @@ -138,7 +138,7 @@ namespace Shadowsocks.Model } } - private static void checkRemark(string remark) + private static void CheckRemark(string remark) { //remark is optional } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 2631f529..195d0bc9 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -29,12 +29,12 @@ namespace Shadowsocks.View controller.ConfigChanged += controller_ConfigChanged; controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; - loadCurrentConfiguration(); + LoadCurrentConfiguration(); } private void controller_ConfigChanged(object sender, EventArgs e) { - loadCurrentConfiguration(); + LoadCurrentConfiguration(); } private void controller_EnableStatusChanged(object sender, EventArgs e) @@ -50,14 +50,14 @@ namespace Shadowsocks.View } - private void showWindow() + private void ShowWindow() { this.Opacity = 1; this.Show(); IPTextBox.Focus(); } - private bool saveOldSelectedServer() + private bool SaveOldSelectedServer() { try { @@ -89,7 +89,7 @@ namespace Shadowsocks.View return false; } - private void loadSelectedServer() + private void LoadSelectedServer() { if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < modifiedConfiguration.configs.Count) { @@ -110,7 +110,7 @@ namespace Shadowsocks.View } } - private void loadConfiguration(Configuration configuration) + private void LoadConfiguration(Configuration configuration) { ServersListBox.Items.Clear(); foreach (Server server in modifiedConfiguration.configs) @@ -119,19 +119,19 @@ namespace Shadowsocks.View } } - private void loadCurrentConfiguration() + private void LoadCurrentConfiguration() { modifiedConfiguration = controller.GetConfiguration(); - loadConfiguration(modifiedConfiguration); + LoadConfiguration(modifiedConfiguration); oldSelectedIndex = modifiedConfiguration.index; ServersListBox.SelectedIndex = modifiedConfiguration.index; - loadSelectedServer(); + LoadSelectedServer(); - updateServersMenu(); + UpdateServersMenu(); enableItem.Checked = modifiedConfiguration.enabled; } - private void updateServersMenu() + private void UpdateServersMenu() { var items = ServersItem.MenuItems; @@ -178,25 +178,25 @@ namespace Shadowsocks.View // we are moving back to oldSelectedIndex or doing a force move return; } - if (!saveOldSelectedServer()) + if (!SaveOldSelectedServer()) { // why this won't cause stack overflow? ServersListBox.SelectedIndex = oldSelectedIndex; return; } - loadSelectedServer(); + LoadSelectedServer(); oldSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!saveOldSelectedServer()) + if (!SaveOldSelectedServer()) { return; } Server server = Configuration.GetDefaultServer(); modifiedConfiguration.configs.Add(server); - loadConfiguration(modifiedConfiguration); + LoadConfiguration(modifiedConfiguration); ServersListBox.SelectedIndex = modifiedConfiguration.configs.Count - 1; oldSelectedIndex = ServersListBox.SelectedIndex; } @@ -214,14 +214,14 @@ namespace Shadowsocks.View oldSelectedIndex = modifiedConfiguration.configs.Count - 1; } ServersListBox.SelectedIndex = oldSelectedIndex; - loadConfiguration(modifiedConfiguration); + LoadConfiguration(modifiedConfiguration); ServersListBox.SelectedIndex = oldSelectedIndex; - loadSelectedServer(); + LoadSelectedServer(); } private void Config_Click(object sender, EventArgs e) { - showWindow(); + ShowWindow(); } private void Quit_Click(object sender, EventArgs e) @@ -229,7 +229,7 @@ namespace Shadowsocks.View this.Close(); } - private void showFirstTimeBalloon() + private void ShowFirstTimeBalloon() { if (isFirstRun) { @@ -242,7 +242,7 @@ namespace Shadowsocks.View private void OKButton_Click(object sender, EventArgs e) { - if (!saveOldSelectedServer()) + if (!SaveOldSelectedServer()) { return; } @@ -253,14 +253,14 @@ namespace Shadowsocks.View } controller.SaveConfig(modifiedConfiguration); this.Hide(); - showFirstTimeBalloon(); + ShowFirstTimeBalloon(); } private void CancelButton_Click(object sender, EventArgs e) { this.Hide(); - loadCurrentConfiguration(); - showFirstTimeBalloon(); + LoadCurrentConfiguration(); + ShowFirstTimeBalloon(); } private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e) @@ -275,7 +275,7 @@ namespace Shadowsocks.View private void notifyIcon1_DoubleClick(object sender, EventArgs e) { - showWindow(); + ShowWindow(); } From efefe274a6d4adf532f860e09668fad7ee6143e9 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 02:27:44 +0800 Subject: [PATCH 6/6] handle no privilege error Popup an error window when don't have enough privilege to change Registry. --- shadowsocks-csharp/Controller/SystemProxy.cs | 49 ++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/shadowsocks-csharp/Controller/SystemProxy.cs b/shadowsocks-csharp/Controller/SystemProxy.cs index 803a326a..884c7260 100755 --- a/shadowsocks-csharp/Controller/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/SystemProxy.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using System.Windows.Forms; +using Microsoft.Win32; using System; using System.Collections.Generic; using System.Runtime.InteropServices; @@ -13,32 +14,52 @@ namespace Shadowsocks.Controller public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; public const int INTERNET_OPTION_REFRESH = 37; - static bool settingsReturn, refreshReturn; + static bool _settingsReturn, _refreshReturn; public static void NotifyIE() { // These lines implement the Interface in the beginning of program // They cause the OS to refresh the settings, causing IP to realy update - settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); - refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); + _settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); + _refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); } public static void Enable() { - RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); - registry.SetValue("ProxyEnable", 0); - registry.SetValue("ProxyServer", ""); - registry.SetValue("AutoConfigURL", "http://127.0.0.1:8090/pac?t=" + GetTimestamp(DateTime.Now)); - SystemProxy.NotifyIE(); + try + { + RegistryKey registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + true); + registry.SetValue("ProxyEnable", 0); + registry.SetValue("ProxyServer", ""); + registry.SetValue("AutoConfigURL", "http://127.0.0.1:8090/pac?t=" + GetTimestamp(DateTime.Now)); + SystemProxy.NotifyIE(); + } + catch (Exception) + { + MessageBox.Show("can not change registry!"); + throw; + } } public static void Disable() { - RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); - registry.SetValue("ProxyEnable", 0); - registry.SetValue("ProxyServer", ""); - registry.SetValue("AutoConfigURL", ""); - SystemProxy.NotifyIE(); + try + { + RegistryKey registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + true); + registry.SetValue("ProxyEnable", 0); + registry.SetValue("ProxyServer", ""); + registry.SetValue("AutoConfigURL", ""); + SystemProxy.NotifyIE(); + } + catch (Exception) + { + MessageBox.Show("can not change registry!"); + throw; + } } private static String GetTimestamp(DateTime value)