From a03aae85d35480a1e213f4c898c2295b2adeda82 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 04:39:57 +0800 Subject: [PATCH 01/13] add open on lan option Add an option to choose whether to open on lan or not. --- shadowsocks-csharp/Controller/Local.cs | 39 +++++++++++++--------- shadowsocks-csharp/Controller/PACServer.cs | 37 ++++++++++++++++---- shadowsocks-csharp/Controller/PolipoRunner.cs | 9 +++++ .../Controller/ShadowsocksController.cs | 13 ++++++++ shadowsocks-csharp/Model/Configuration.cs | 1 + shadowsocks-csharp/View/ConfigForm.Designer.cs | 14 ++++++++ shadowsocks-csharp/View/ConfigForm.cs | 2 ++ 7 files changed, 93 insertions(+), 22 deletions(-) diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index cace2f57..b27e237f 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -12,6 +12,7 @@ namespace Shadowsocks.Controller class Local { private Server config; + public bool openOnLan; //private Encryptor encryptor; Socket _listener; public Local(Server config) @@ -27,7 +28,15 @@ namespace Shadowsocks.Controller // Create a TCP/IP socket. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port); + IPEndPoint localEndPoint = null; + if (openOnLan) + { + localEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), config.local_port); + } + else + { + localEndPoint = new IPEndPoint(IPAddress.Loopback, config.local_port); + } // Bind the socket to the local endpoint and listen for incoming connections. _listener.Bind(localEndPoint); @@ -229,7 +238,7 @@ namespace Shadowsocks.Controller // reject socks 4 response = new byte[]{ 0, 91 }; } - connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(handshakeSendCallback), null); + connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null); } else { @@ -243,7 +252,7 @@ namespace Shadowsocks.Controller } } - private void handshakeSendCallback(IAsyncResult ar) + private void HandshakeSendCallback(IAsyncResult ar) { try { @@ -275,7 +284,7 @@ namespace Shadowsocks.Controller if (bytesRead > 0) { byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 }; - connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(startPipe), null); + connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(StartPipe), null); } else { @@ -290,15 +299,15 @@ namespace Shadowsocks.Controller } - private void startPipe(IAsyncResult ar) + private void StartPipe(IAsyncResult ar) { try { connection.EndReceive(ar); remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeRemoteReceiveCallback), null); + new AsyncCallback(PipeRemoteReceiveCallback), null); connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeConnectionReceiveCallback), null); + new AsyncCallback(PipeConnectionReceiveCallback), null); } catch (Exception e) { @@ -307,7 +316,7 @@ namespace Shadowsocks.Controller } } - private void pipeRemoteReceiveCallback(IAsyncResult ar) + private void PipeRemoteReceiveCallback(IAsyncResult ar) { try @@ -318,7 +327,7 @@ namespace Shadowsocks.Controller { int bytesToSend; encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend); - connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(pipeConnectionSendCallback), null); + connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null); } else { @@ -333,7 +342,7 @@ namespace Shadowsocks.Controller } } - private void pipeConnectionReceiveCallback(IAsyncResult ar) + private void PipeConnectionReceiveCallback(IAsyncResult ar) { try @@ -344,7 +353,7 @@ namespace Shadowsocks.Controller { int bytesToSend; encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend); - remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(pipeRemoteSendCallback), null); + remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null); } else { @@ -358,13 +367,13 @@ namespace Shadowsocks.Controller } } - private void pipeRemoteSendCallback(IAsyncResult ar) + private void PipeRemoteSendCallback(IAsyncResult ar) { try { remote.EndSend(ar); connection.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeConnectionReceiveCallback), null); + new AsyncCallback(PipeConnectionReceiveCallback), null); } catch (Exception e) { @@ -373,13 +382,13 @@ namespace Shadowsocks.Controller } } - private void pipeConnectionSendCallback(IAsyncResult ar) + private void PipeConnectionSendCallback(IAsyncResult ar) { try { connection.EndSend(ar); remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeRemoteReceiveCallback), null); + new AsyncCallback(PipeRemoteReceiveCallback), null); } catch (Exception e) { diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index eab2ff60..a58aaf1c 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -13,8 +13,9 @@ namespace Shadowsocks.Controller class PACServer { private static string PAC_FILE = "pac.txt"; + public bool openOnLan; - Socket listener; + Socket _listener; FileSystemWatcher watcher; public event EventHandler PACFileChanged; @@ -22,20 +23,42 @@ namespace Shadowsocks.Controller public void Start() { // Create a TCP/IP socket. - listener = new Socket(AddressFamily.InterNetwork, + _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - IPEndPoint localEndPoint = new IPEndPoint(0, 8090); + _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + IPEndPoint localEndPoint = null; + if (openOnLan) + { + localEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8090); + } + else + { + localEndPoint = new IPEndPoint(IPAddress.Loopback, 8090); + } // Bind the socket to the local endpoint and listen for incoming connections. - listener.Bind(localEndPoint); - listener.Listen(100); - listener.BeginAccept( + _listener.Bind(localEndPoint); + _listener.Listen(100); + _listener.BeginAccept( new AsyncCallback(AcceptCallback), - listener); + _listener); WatchPacFile(); } + public void Stop() + { + try + { + _listener.Close(); + } + catch (Exception) + { + + throw; + } + } + public string TouchPACFile() { if (File.Exists(PAC_FILE)) diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index 9f3c2187..e0fdf411 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -12,6 +12,7 @@ namespace Shadowsocks.Controller class PolipoRunner { private Process _process; + public bool openOnLan; public void Start(Server config) { @@ -33,6 +34,14 @@ namespace Shadowsocks.Controller string temppath = Path.GetTempPath(); string polipoConfig = Resources.polipo_config; polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", config.local_port.ToString()); + if (openOnLan) + { + polipoConfig = polipoConfig.Replace("\"127.0.0.1\"", "\"0.0.0.0\""); + } + else + { + polipoConfig = polipoConfig.Replace("\"0.0.0.0\"", "\"127.0.0.1\""); + } FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 5af0bf38..1285c2e8 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -17,6 +17,7 @@ namespace Shadowsocks.Controller private Configuration _config; private PolipoRunner polipoRunner; private bool stopped = false; + private bool openOnLan; public class PathEventArgs : EventArgs { @@ -32,14 +33,19 @@ namespace Shadowsocks.Controller public ShadowsocksController() { _config = Configuration.Load(); + openOnLan = _config.openOnLan; polipoRunner = new PolipoRunner(); + polipoRunner.openOnLan = openOnLan; polipoRunner.Start(_config.GetCurrentServer()); local = new Local(_config.GetCurrentServer()); + local.openOnLan = openOnLan; + try { local.Start(); pacServer = new PACServer(); pacServer.PACFileChanged += pacServer_PACFileChanged; + pacServer.openOnLan = openOnLan; pacServer.Start(); } catch (Exception e) @@ -55,14 +61,21 @@ namespace Shadowsocks.Controller Configuration.Save(newConfig); // some logic in configuration updated the config when saving, we need to read it again _config = Configuration.Load(); + openOnLan = _config.openOnLan; local.Stop(); polipoRunner.Stop(); + polipoRunner.openOnLan = openOnLan; polipoRunner.Start(_config.GetCurrentServer()); local = new Local(_config.GetCurrentServer()); + local.openOnLan = openOnLan; local.Start(); + pacServer.Stop(); + pacServer.openOnLan = openOnLan; + pacServer.Start(); + if (ConfigChanged != null) { ConfigChanged(this, new EventArgs()); diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index c45bb9f4..09222e19 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -16,6 +16,7 @@ namespace Shadowsocks.Model public int index; public bool enabled; public bool isDefault; + public bool openOnLan; private static string CONFIG_FILE = "gui-config.json"; diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index b2d20a84..841ecccb 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -64,6 +64,7 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); + this.OpenOnLanBox = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); @@ -402,6 +403,16 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // + // OpenOnLanBox + // + this.OpenOnLanBox.AutoSize = true; + this.OpenOnLanBox.Location = new System.Drawing.Point(16, 260); + this.OpenOnLanBox.Name = "OpenOnLanBox"; + this.OpenOnLanBox.Size = new System.Drawing.Size(90, 17); + this.OpenOnLanBox.TabIndex = 7; + this.OpenOnLanBox.Text = "Open On Lan"; + this.OpenOnLanBox.UseVisualStyleBackColor = true; + // // ConfigForm // this.AcceptButton = this.OKButton; @@ -410,6 +421,7 @@ this.AutoSize = true; this.CancelButton = this.MyCancelButton; this.ClientSize = new System.Drawing.Size(489, 286); + this.Controls.Add(this.OpenOnLanBox); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); @@ -418,6 +430,7 @@ this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; + this.MinimizeBox = false; this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Shadowsocks"; @@ -471,6 +484,7 @@ private System.Windows.Forms.TextBox RemarksTextBox; private System.Windows.Forms.Label label6; private System.Windows.Forms.MenuItem QRCodeItem; + private System.Windows.Forms.CheckBox OpenOnLanBox; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 195d0bc9..988c69b8 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -129,6 +129,7 @@ namespace Shadowsocks.View UpdateServersMenu(); enableItem.Checked = modifiedConfiguration.enabled; + OpenOnLanBox.Checked = modifiedConfiguration.openOnLan; } private void UpdateServersMenu() @@ -251,6 +252,7 @@ namespace Shadowsocks.View MessageBox.Show("Please add at least one server"); return; } + modifiedConfiguration.openOnLan = OpenOnLanBox.Checked; controller.SaveConfig(modifiedConfiguration); this.Hide(); ShowFirstTimeBalloon(); From 835265b1cd4a6c1dfca1d2db5215c467ff3b6264 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 05:46:08 +0800 Subject: [PATCH 02/13] add log switch and modify detector Add an option to select whether to open log or not. Add a detector to check whether the server setting changed. --- .../Controller/ShadowsocksController.cs | 29 +++++- shadowsocks-csharp/Model/Configuration.cs | 2 + shadowsocks-csharp/Program.cs | 14 --- shadowsocks-csharp/View/ConfigForm.Designer.cs | 36 ++++--- shadowsocks-csharp/View/ConfigForm.cs | 107 +++++++++++++-------- 5 files changed, 122 insertions(+), 66 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 1285c2e8..82b4557c 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -1,4 +1,5 @@ -using Shadowsocks.Model; +using System.IO; +using Shadowsocks.Model; using System; using System.Collections.Generic; using System.Text; @@ -33,6 +34,11 @@ namespace Shadowsocks.Controller public ShadowsocksController() { _config = Configuration.Load(); + if (_config.enableLog) + { + SetLog(); + } + openOnLan = _config.openOnLan; polipoRunner = new PolipoRunner(); polipoRunner.openOnLan = openOnLan; @@ -59,6 +65,10 @@ namespace Shadowsocks.Controller public void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); + if (newConfig.noChange && newConfig.openOnLan == openOnLan) + { + return; + } // some logic in configuration updated the config when saving, we need to read it again _config = Configuration.Load(); openOnLan = _config.openOnLan; @@ -154,5 +164,22 @@ namespace Shadowsocks.Controller UpdateSystemProxy(); } + private void SetLog() + { + try + { + FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); + TextWriter tmp = Console.Out; + StreamWriter sw = new StreamWriter(fs); + sw.AutoFlush = true; + Console.SetOut(sw); + Console.SetError(sw); + } + catch (IOException e) + { + Console.WriteLine(e.ToString()); + } + } + } } diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 09222e19..a501f117 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -17,6 +17,8 @@ namespace Shadowsocks.Model public bool enabled; public bool isDefault; public bool openOnLan; + public bool enableLog; + public bool noChange; private static string CONFIG_FILE = "gui-config.json"; diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 516bdf80..1e0bf71a 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -47,26 +47,12 @@ namespace Shadowsocks } LoadLibrary(dllPath); - try - { - FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); - TextWriter tmp = Console.Out; - StreamWriter sw = new StreamWriter(fs); - sw.AutoFlush = true; - Console.SetOut(sw); - Console.SetError(sw); - } - catch (IOException e) - { - Console.WriteLine(e.ToString()); - } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ShadowsocksController controller = new ShadowsocksController(); // TODO run without a main form to save RAM Application.Run(new ConfigForm(controller)); - } } } diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 841ecccb..4ac5052b 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -64,7 +64,8 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); - this.OpenOnLanBox = new System.Windows.Forms.CheckBox(); + this.openOnLanBox = new System.Windows.Forms.CheckBox(); + this.enableLogBox = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); @@ -403,15 +404,26 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // - // OpenOnLanBox + // openOnLanBox // - this.OpenOnLanBox.AutoSize = true; - this.OpenOnLanBox.Location = new System.Drawing.Point(16, 260); - this.OpenOnLanBox.Name = "OpenOnLanBox"; - this.OpenOnLanBox.Size = new System.Drawing.Size(90, 17); - this.OpenOnLanBox.TabIndex = 7; - this.OpenOnLanBox.Text = "Open On Lan"; - this.OpenOnLanBox.UseVisualStyleBackColor = true; + this.openOnLanBox.AutoSize = true; + this.openOnLanBox.Location = new System.Drawing.Point(16, 260); + this.openOnLanBox.Name = "openOnLanBox"; + this.openOnLanBox.Size = new System.Drawing.Size(90, 17); + this.openOnLanBox.TabIndex = 7; + this.openOnLanBox.Text = "Open On Lan"; + this.openOnLanBox.UseVisualStyleBackColor = true; + // + // enableLogBox + // + this.enableLogBox.AutoSize = true; + this.enableLogBox.Location = new System.Drawing.Point(112, 260); + this.enableLogBox.Name = "enableLogBox"; + this.enableLogBox.Size = new System.Drawing.Size(80, 17); + this.enableLogBox.TabIndex = 7; + this.enableLogBox.Text = "Enable Log"; + this.enableLogBox.UseVisualStyleBackColor = true; + this.enableLogBox.CheckedChanged += new System.EventHandler(this.enableLogBox_CheckedChanged); // // ConfigForm // @@ -421,7 +433,8 @@ this.AutoSize = true; this.CancelButton = this.MyCancelButton; this.ClientSize = new System.Drawing.Size(489, 286); - this.Controls.Add(this.OpenOnLanBox); + this.Controls.Add(this.enableLogBox); + this.Controls.Add(this.openOnLanBox); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); @@ -484,7 +497,8 @@ private System.Windows.Forms.TextBox RemarksTextBox; private System.Windows.Forms.Label label6; private System.Windows.Forms.MenuItem QRCodeItem; - private System.Windows.Forms.CheckBox OpenOnLanBox; + private System.Windows.Forms.CheckBox openOnLanBox; + private System.Windows.Forms.CheckBox enableLogBox; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index e9d0fa49..65aa4b39 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -15,9 +15,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 Configuration _oldConfiguration; + private int _oldSelectedIndex = -1; + private bool _isFirstRun; public ConfigForm(ShadowsocksController controller) { @@ -57,11 +58,11 @@ namespace Shadowsocks.View IPTextBox.Focus(); } - private bool SaveOldSelectedServer() + private bool SaveOldSelectedServer(object sender) { try { - if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count) + if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) { return true; } @@ -75,7 +76,27 @@ namespace Shadowsocks.View remarks = RemarksTextBox.Text }; Configuration.CheckServer(server); - modifiedConfiguration.configs[oldSelectedIndex] = server; + + _modifiedConfiguration.configs[_oldSelectedIndex] = server; + if (sender.Equals(OKButton)) + { + if (_oldConfiguration.configs[_oldConfiguration.index].server == + _modifiedConfiguration.configs[_oldSelectedIndex].server && + _oldConfiguration.configs[_oldConfiguration.index].server_port == + _modifiedConfiguration.configs[_oldSelectedIndex].server_port && + _oldConfiguration.configs[_oldConfiguration.index].password == + _modifiedConfiguration.configs[_oldSelectedIndex].password && + _oldConfiguration.configs[_oldConfiguration.index].local_port == + _modifiedConfiguration.configs[_oldSelectedIndex].local_port) + { + _modifiedConfiguration.noChange = true; + } + else + { + _modifiedConfiguration.noChange = false; + _oldConfiguration = _modifiedConfiguration; + } + } return true; } catch (FormatException) @@ -91,15 +112,15 @@ namespace Shadowsocks.View 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(); @@ -113,7 +134,7 @@ namespace Shadowsocks.View 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 + ")"); } @@ -121,15 +142,16 @@ namespace Shadowsocks.View private void LoadCurrentConfiguration() { - modifiedConfiguration = controller.GetConfiguration(); - LoadConfiguration(modifiedConfiguration); - oldSelectedIndex = modifiedConfiguration.index; - ServersListBox.SelectedIndex = modifiedConfiguration.index; + _modifiedConfiguration = controller.GetConfiguration(); + _oldConfiguration = _modifiedConfiguration; + LoadConfiguration(_modifiedConfiguration); + _oldSelectedIndex = _modifiedConfiguration.index; + ServersListBox.SelectedIndex = _modifiedConfiguration.index; LoadSelectedServer(); UpdateServersMenu(); - enableItem.Checked = modifiedConfiguration.enabled; - OpenOnLanBox.Checked = modifiedConfiguration.openOnLan; + enableItem.Checked = _modifiedConfiguration.enabled; + openOnLanBox.Checked = _modifiedConfiguration.openOnLan; } private void UpdateServersMenu() @@ -168,55 +190,55 @@ namespace Shadowsocks.View } else { - isFirstRun = true; + _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(sender)) { // why this won't cause stack overflow? - ServersListBox.SelectedIndex = oldSelectedIndex; + ServersListBox.SelectedIndex = _oldSelectedIndex; return; } LoadSelectedServer(); - oldSelectedIndex = ServersListBox.SelectedIndex; + _oldSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!SaveOldSelectedServer(sender)) { 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; + ServersListBox.SelectedIndex = _oldSelectedIndex; + LoadConfiguration(_modifiedConfiguration); + ServersListBox.SelectedIndex = _oldSelectedIndex; LoadSelectedServer(); } @@ -232,28 +254,28 @@ namespace Shadowsocks.View 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(sender)) { return; } - if (modifiedConfiguration.configs.Count == 0) + if (_modifiedConfiguration.configs.Count == 0) { MessageBox.Show("Please add at least one server"); return; } - modifiedConfiguration.openOnLan = OpenOnLanBox.Checked; - controller.SaveConfig(modifiedConfiguration); + _modifiedConfiguration.openOnLan = openOnLanBox.Checked; + controller.SaveConfig(_modifiedConfiguration); this.Hide(); ShowFirstTimeBalloon(); } @@ -311,5 +333,10 @@ namespace Shadowsocks.View qrCodeForm.Icon = this.Icon; qrCodeForm.Show(); } + + private void enableLogBox_CheckedChanged(object sender, EventArgs e) + { + MessageBox.Show("This option only works on next startup."); + } } } From 159a77a6311567802cc39d7e92de0b019449014d Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 06:06:26 +0800 Subject: [PATCH 03/13] small bug Fix a small bug --- shadowsocks-csharp/View/ConfigForm.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 65aa4b39..a7550998 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -152,6 +152,7 @@ namespace Shadowsocks.View UpdateServersMenu(); enableItem.Checked = _modifiedConfiguration.enabled; openOnLanBox.Checked = _modifiedConfiguration.openOnLan; + enableLogBox.Checked = _modifiedConfiguration.enableLog; } private void UpdateServersMenu() @@ -275,6 +276,7 @@ namespace Shadowsocks.View return; } _modifiedConfiguration.openOnLan = openOnLanBox.Checked; + _modifiedConfiguration.enableLog = enableLogBox.Checked; controller.SaveConfig(_modifiedConfiguration); this.Hide(); ShowFirstTimeBalloon(); @@ -336,7 +338,10 @@ namespace Shadowsocks.View private void enableLogBox_CheckedChanged(object sender, EventArgs e) { - MessageBox.Show("This option only works on next startup."); + if (this.Visible) + { + MessageBox.Show("This option only works on next startup."); + } } } } From 7d539d3281cf53a3cb14da26b1d293900ff52e32 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 15:01:36 +0800 Subject: [PATCH 04/13] add modify detector Don't reload server if config not change. --- shadowsocks-csharp/Controller/PACServer.cs | 17 ++---------- .../Controller/ShadowsocksController.cs | 8 +++++- shadowsocks-csharp/View/ConfigForm.Designer.cs | 30 ++-------------------- shadowsocks-csharp/View/ConfigForm.cs | 10 ++++---- shadowsocks-csharp/View/QRCodeForm.cs | 2 +- 5 files changed, 17 insertions(+), 50 deletions(-) diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index 4cd95eab..9ad74abf 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -23,7 +23,7 @@ namespace Shadowsocks.Controller public event EventHandler PACFileChanged; public void Start(Configuration configuration) - { + { try { // Create a TCP/IP socket. @@ -58,20 +58,7 @@ namespace Shadowsocks.Controller public void Stop() { _listener.Close(); - _listener = null; - } - - public void Stop() - { - try - { - _listener.Close(); - } - catch (Exception) - { - - throw; - } + _listener = null; } public string TouchPACFile() diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 887854f9..ff52daf6 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -71,9 +71,10 @@ namespace Shadowsocks.Controller return Configuration.Load(); } - public void SaveServers(List servers) + public void SaveServers(List servers, bool noChange) { _config.configs = servers; + _config.noChange = noChange; SaveConfig(_config); } @@ -91,6 +92,7 @@ namespace Shadowsocks.Controller public void ToggleShareOverLAN(bool enabled) { _config.shareOverLan = enabled; + _config.noChange = false; SaveConfig(_config); if (ShareOverLANStatusChanged != null) { @@ -140,6 +142,10 @@ namespace Shadowsocks.Controller protected void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); + if (newConfig.noChange) + { + return; + } // some logic in configuration updated the config when saving, we need to read it again _config = Configuration.Load(); diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 20c3a870..cf874286 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -66,8 +66,6 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); - this.openOnLanBox = new System.Windows.Forms.CheckBox(); - this.enableLogBox = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); @@ -420,27 +418,6 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // - // openOnLanBox - // - this.openOnLanBox.AutoSize = true; - this.openOnLanBox.Location = new System.Drawing.Point(16, 260); - this.openOnLanBox.Name = "openOnLanBox"; - this.openOnLanBox.Size = new System.Drawing.Size(90, 17); - this.openOnLanBox.TabIndex = 7; - this.openOnLanBox.Text = "Open On Lan"; - this.openOnLanBox.UseVisualStyleBackColor = true; - // - // enableLogBox - // - this.enableLogBox.AutoSize = true; - this.enableLogBox.Location = new System.Drawing.Point(112, 260); - this.enableLogBox.Name = "enableLogBox"; - this.enableLogBox.Size = new System.Drawing.Size(80, 17); - this.enableLogBox.TabIndex = 7; - this.enableLogBox.Text = "Enable Log"; - this.enableLogBox.UseVisualStyleBackColor = true; - this.enableLogBox.CheckedChanged += new System.EventHandler(this.enableLogBox_CheckedChanged); - // // ConfigForm // this.AcceptButton = this.OKButton; @@ -449,8 +426,6 @@ this.AutoSize = true; this.CancelButton = this.MyCancelButton; this.ClientSize = new System.Drawing.Size(489, 286); - this.Controls.Add(this.enableLogBox); - this.Controls.Add(this.openOnLanBox); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); @@ -459,7 +434,6 @@ this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; - this.MinimizeBox = false; this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Edit Servers"; @@ -512,9 +486,9 @@ private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.TextBox RemarksTextBox; private System.Windows.Forms.Label label6; - private System.Windows.Forms.MenuItem QRCodeItem; + private System.Windows.Forms.MenuItem QRCodeItem; private System.Windows.Forms.MenuItem ShowLogItem; - private System.Windows.Forms.MenuItem ShareOverLANItem; + private System.Windows.Forms.MenuItem ShareOverLANItem; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index b9c3cb03..0502411a 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -155,9 +155,9 @@ namespace Shadowsocks.View ServersListBox.SelectedIndex = _modifiedConfiguration.index; LoadSelectedServer(); - UpdateServersMenu(); - enableItem.Checked = modifiedConfiguration.enabled; - ShareOverLANItem.Checked = modifiedConfiguration.shareOverLan; + UpdateServersMenu(); + enableItem.Checked = _modifiedConfiguration.enabled; + ShareOverLANItem.Checked = _modifiedConfiguration.shareOverLan; } private void UpdateServersMenu() @@ -279,8 +279,8 @@ namespace Shadowsocks.View { MessageBox.Show("Please add at least one server"); return; - } - controller.SaveServers(modifiedConfiguration.configs); + } + controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.noChange); this.Hide(); ShowFirstTimeBalloon(); } diff --git a/shadowsocks-csharp/View/QRCodeForm.cs b/shadowsocks-csharp/View/QRCodeForm.cs index 0a0ec840..c8ae0df8 100755 --- a/shadowsocks-csharp/View/QRCodeForm.cs +++ b/shadowsocks-csharp/View/QRCodeForm.cs @@ -25,7 +25,7 @@ namespace Shadowsocks.View private void GenQR(string ssconfig) { string qrText = ssconfig; - QRCode4CS.QRCode qrCoded = new QRCode4CS.QRCode(6, QRErrorCorrectLevel.H); + QRCode4CS.QRCode qrCoded = new QRCode4CS.QRCode(6, QRErrorCorrectLevel.L); qrCoded.AddData(qrText); qrCoded.Make(); int blockSize = 5; From 8af7a799b5869c8e5c1ea81314d4a50dc1b75adf Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 15:27:50 +0800 Subject: [PATCH 05/13] add about form Add an about form instead of just open link. --- shadowsocks-csharp/View/AboutForm.Designer.cs | 104 ++++++++++++++++++++++ shadowsocks-csharp/View/AboutForm.cs | 24 ++++++ shadowsocks-csharp/View/AboutForm.resx | 120 ++++++++++++++++++++++++++ shadowsocks-csharp/View/ConfigForm.cs | 5 +- shadowsocks-csharp/shadowsocks-csharp.csproj | 10 +++ 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 shadowsocks-csharp/View/AboutForm.Designer.cs create mode 100644 shadowsocks-csharp/View/AboutForm.cs create mode 100644 shadowsocks-csharp/View/AboutForm.resx diff --git a/shadowsocks-csharp/View/AboutForm.Designer.cs b/shadowsocks-csharp/View/AboutForm.Designer.cs new file mode 100644 index 00000000..498d31d4 --- /dev/null +++ b/shadowsocks-csharp/View/AboutForm.Designer.cs @@ -0,0 +1,104 @@ +namespace Shadowsocks.View +{ + partial class AboutForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.titleLabel = new System.Windows.Forms.Label(); + this.versionLabel = new System.Windows.Forms.Label(); + this.authorLabel = new System.Windows.Forms.Label(); + this.githubLabel = new System.Windows.Forms.LinkLabel(); + this.SuspendLayout(); + // + // titleLabel + // + this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.titleLabel.Location = new System.Drawing.Point(12, 40); + this.titleLabel.Name = "titleLabel"; + this.titleLabel.Size = new System.Drawing.Size(260, 23); + this.titleLabel.TabIndex = 0; + this.titleLabel.Text = "Shadowsocks for Windows"; + this.titleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // versionLabel + // + this.versionLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.versionLabel.Location = new System.Drawing.Point(12, 84); + this.versionLabel.Name = "versionLabel"; + this.versionLabel.Size = new System.Drawing.Size(260, 23); + this.versionLabel.TabIndex = 0; + this.versionLabel.Text = "Version: 2.0.4.0"; + this.versionLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // authorLabel + // + this.authorLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.authorLabel.Location = new System.Drawing.Point(12, 139); + this.authorLabel.Name = "authorLabel"; + this.authorLabel.Size = new System.Drawing.Size(260, 23); + this.authorLabel.TabIndex = 0; + this.authorLabel.Text = "By: clowwindy"; + this.authorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // githubLabel + // + this.githubLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.githubLabel.Location = new System.Drawing.Point(13, 201); + this.githubLabel.Name = "githubLabel"; + this.githubLabel.Size = new System.Drawing.Size(259, 23); + this.githubLabel.TabIndex = 1; + this.githubLabel.TabStop = true; + this.githubLabel.Text = "GitHub"; + this.githubLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.githubLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.githubLabel_LinkClicked); + // + // AboutForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 261); + this.Controls.Add(this.githubLabel); + this.Controls.Add(this.authorLabel); + this.Controls.Add(this.versionLabel); + this.Controls.Add(this.titleLabel); + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "AboutForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "About"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Label titleLabel; + private System.Windows.Forms.Label versionLabel; + private System.Windows.Forms.Label authorLabel; + private System.Windows.Forms.LinkLabel githubLabel; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/View/AboutForm.cs b/shadowsocks-csharp/View/AboutForm.cs new file mode 100644 index 00000000..0b182cb5 --- /dev/null +++ b/shadowsocks-csharp/View/AboutForm.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Text; +using System.Windows.Forms; + +namespace Shadowsocks.View +{ + public partial class AboutForm : Form + { + public AboutForm() + { + InitializeComponent(); + } + + private void githubLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + { + string url = "https://github.com/clowwindy/shadowsocks-csharp"; + System.Diagnostics.Process.Start(url); + } + } +} diff --git a/shadowsocks-csharp/View/AboutForm.resx b/shadowsocks-csharp/View/AboutForm.resx new file mode 100644 index 00000000..7080a7d1 --- /dev/null +++ b/shadowsocks-csharp/View/AboutForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 0502411a..b44f65ea 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -13,6 +13,7 @@ namespace Shadowsocks.View public partial class ConfigForm : Form { private ShadowsocksController controller; + private AboutForm aboutForm; // this is a copy of configuration that we are working on private Configuration _modifiedConfiguration; @@ -299,7 +300,9 @@ namespace Shadowsocks.View private void AboutItem_Click(object sender, EventArgs e) { - Process.Start("https://github.com/clowwindy/shadowsocks-csharp"); + aboutForm = new AboutForm(); + aboutForm.Show(); + //Process.Start("https://github.com/clowwindy/shadowsocks-csharp"); } private void notifyIcon1_DoubleClick(object sender, EventArgs e) diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index ab31982a..5f486f5c 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -63,6 +63,7 @@ + @@ -78,6 +79,12 @@ + + Form + + + AboutForm.cs + Form @@ -96,6 +103,9 @@ QRCodeForm.cs + + AboutForm.cs + ConfigForm.cs Designer From 257390d202fa7c72e8a7a4bf815a74311ce94e20 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 04:39:57 +0800 Subject: [PATCH 06/13] add open on lan option Add an option to choose whether to open on lan or not. --- shadowsocks-csharp.sln | 2 +- shadowsocks-csharp/Controller/Local.cs | 36 +++++++++++----------- shadowsocks-csharp/Controller/PACServer.cs | 1 + shadowsocks-csharp/Controller/PolipoRunner.cs | 5 +-- .../Controller/ShadowsocksController.cs | 1 + shadowsocks-csharp/Model/Configuration.cs | 1 + shadowsocks-csharp/View/ConfigForm.Designer.cs | 1 + 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/shadowsocks-csharp.sln b/shadowsocks-csharp.sln index 46bde1a8..03faec06 100755 --- a/shadowsocks-csharp.sln +++ b/shadowsocks-csharp.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Express 2012 for Windows Desktop +# Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shadowsocks-csharp", "shadowsocks-csharp\shadowsocks-csharp.csproj", "{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}" EndProject Global diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index baf535c3..bf2f95ad 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -10,9 +10,9 @@ namespace Shadowsocks.Controller { class Local - { + { private Server _server; - private bool _shareOverLAN; + private bool _shareOverLAN; //private Encryptor encryptor; Socket _listener; public Local(Configuration config) @@ -26,7 +26,7 @@ namespace Shadowsocks.Controller { try { - // Create a TCP/IP socket. + // Create a TCP/IP socket. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPEndPoint localEndPoint = null; @@ -36,7 +36,7 @@ namespace Shadowsocks.Controller } else { - localEndPoint = new IPEndPoint(IPAddress.Loopback, _server.local_port); + localEndPoint = new IPEndPoint(IPAddress.Loopback, _server.local_port); } // Bind the socket to the local endpoint and listen for incoming connections. @@ -221,7 +221,7 @@ namespace Shadowsocks.Controller // reject socks 4 response = new byte[]{ 0, 91 }; } - connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(handshakeSendCallback), null); + connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null); } else { @@ -235,7 +235,7 @@ namespace Shadowsocks.Controller } } - private void handshakeSendCallback(IAsyncResult ar) + private void HandshakeSendCallback(IAsyncResult ar) { try { @@ -267,7 +267,7 @@ namespace Shadowsocks.Controller if (bytesRead > 0) { byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 }; - connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(startPipe), null); + connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(StartPipe), null); } else { @@ -282,15 +282,15 @@ namespace Shadowsocks.Controller } - private void startPipe(IAsyncResult ar) + private void StartPipe(IAsyncResult ar) { try { connection.EndReceive(ar); remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeRemoteReceiveCallback), null); + new AsyncCallback(PipeRemoteReceiveCallback), null); connection.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeConnectionReceiveCallback), null); + new AsyncCallback(PipeConnectionReceiveCallback), null); } catch (Exception e) { @@ -299,7 +299,7 @@ namespace Shadowsocks.Controller } } - private void pipeRemoteReceiveCallback(IAsyncResult ar) + private void PipeRemoteReceiveCallback(IAsyncResult ar) { try @@ -310,7 +310,7 @@ namespace Shadowsocks.Controller { int bytesToSend; encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend); - connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(pipeConnectionSendCallback), null); + connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null); } else { @@ -325,7 +325,7 @@ namespace Shadowsocks.Controller } } - private void pipeConnectionReceiveCallback(IAsyncResult ar) + private void PipeConnectionReceiveCallback(IAsyncResult ar) { try @@ -336,7 +336,7 @@ namespace Shadowsocks.Controller { int bytesToSend; encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend); - remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(pipeRemoteSendCallback), null); + remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null); } else { @@ -350,13 +350,13 @@ namespace Shadowsocks.Controller } } - private void pipeRemoteSendCallback(IAsyncResult ar) + private void PipeRemoteSendCallback(IAsyncResult ar) { try { remote.EndSend(ar); connection.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeConnectionReceiveCallback), null); + new AsyncCallback(PipeConnectionReceiveCallback), null); } catch (Exception e) { @@ -365,13 +365,13 @@ namespace Shadowsocks.Controller } } - private void pipeConnectionSendCallback(IAsyncResult ar) + private void PipeConnectionSendCallback(IAsyncResult ar) { try { connection.EndSend(ar); remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0, - new AsyncCallback(pipeRemoteReceiveCallback), null); + new AsyncCallback(PipeRemoteReceiveCallback), null); } catch (Exception e) { diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index 422089dc..9ad74abf 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -15,6 +15,7 @@ namespace Shadowsocks.Controller { private static int PORT = 8090; private static string PAC_FILE = "pac.txt"; + public bool openOnLan; Socket _listener; FileSystemWatcher watcher; diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index de1c3c3a..a82333f5 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -12,6 +12,7 @@ namespace Shadowsocks.Controller class PolipoRunner { private Process _process; + public bool openOnLan; public void Start(Configuration configuration) { @@ -31,10 +32,10 @@ namespace Shadowsocks.Controller Console.WriteLine(e.ToString()); } } - string temppath = Path.GetTempPath(); + string temppath = Path.GetTempPath(); string polipoConfig = Resources.polipo_config; polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", server.local_port.ToString()); - polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1"); + polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1"); FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index e6e80d05..4c1d34a8 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -34,6 +34,7 @@ namespace Shadowsocks.Controller public ShadowsocksController() { _config = Configuration.Load(); + openOnLan = _config.openOnLan; polipoRunner = new PolipoRunner(); polipoRunner.Start(_config); local = new Local(_config); diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 65b0a3b6..e89983b0 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -17,6 +17,7 @@ namespace Shadowsocks.Model public bool enabled; public bool shareOverLan; public bool isDefault; + public bool openOnLan; private static string CONFIG_FILE = "gui-config.json"; diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index cf874286..c1187c04 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -434,6 +434,7 @@ this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; + this.MinimizeBox = false; this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Edit Servers"; From 0ef96e19f2c180eb1dae0206b0e2d1d5d4a09de2 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 05:46:08 +0800 Subject: [PATCH 07/13] add log switch and modify detector Add an option to select whether to open log or not. Add a detector to check whether the server setting changed. --- .../Controller/ShadowsocksController.cs | 28 +++++- shadowsocks-csharp/Model/Configuration.cs | 2 + shadowsocks-csharp/Program.cs | 5 +- shadowsocks-csharp/View/ConfigForm.cs | 109 +++++++++++++-------- 4 files changed, 97 insertions(+), 47 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 4c1d34a8..53ce3753 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -1,4 +1,5 @@ -using Shadowsocks.Model; +using System.IO; +using Shadowsocks.Model; using System; using System.Collections.Generic; using System.Text; @@ -34,7 +35,11 @@ namespace Shadowsocks.Controller public ShadowsocksController() { _config = Configuration.Load(); - openOnLan = _config.openOnLan; + if (_config.enableLog) + { + SetLog(); + } + polipoRunner = new PolipoRunner(); polipoRunner.Start(_config); local = new Local(_config); @@ -52,7 +57,7 @@ namespace Shadowsocks.Controller UpdateSystemProxy(); } - + public Server GetCurrentServer() { return _config.GetCurrentServer(); @@ -174,5 +179,22 @@ namespace Shadowsocks.Controller UpdateSystemProxy(); } + private void SetLog() + { + try + { + FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); + TextWriter tmp = Console.Out; + StreamWriter sw = new StreamWriter(fs); + sw.AutoFlush = true; + Console.SetOut(sw); + Console.SetError(sw); + } + catch (IOException e) + { + Console.WriteLine(e.ToString()); + } + } + } } diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index e89983b0..4869e2c8 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -18,6 +18,8 @@ namespace Shadowsocks.Model public bool shareOverLan; public bool isDefault; public bool openOnLan; + public bool enableLog; + public bool noChange; private static string CONFIG_FILE = "gui-config.json"; diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index a42a97ae..251ba424 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -46,15 +46,14 @@ namespace Shadowsocks Console.WriteLine(e.ToString()); } LoadLibrary(dllPath); - - Logging.OpenLogFile(); + + Logging.OpenLogFile(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ShadowsocksController controller = new ShadowsocksController(); // TODO run without a main form to save RAM Application.Run(new ConfigForm(controller)); - } } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 2a7e5db2..894b7f27 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -15,9 +15,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 Configuration _oldConfiguration; + private int _oldSelectedIndex = -1; + private bool _isFirstRun; public ConfigForm(ShadowsocksController controller) { @@ -63,11 +64,11 @@ namespace Shadowsocks.View IPTextBox.Focus(); } - private bool SaveOldSelectedServer() + private bool SaveOldSelectedServer(object sender) { try { - if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count) + if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) { return true; } @@ -81,7 +82,27 @@ namespace Shadowsocks.View remarks = RemarksTextBox.Text }; Configuration.CheckServer(server); - modifiedConfiguration.configs[oldSelectedIndex] = server; + + _modifiedConfiguration.configs[_oldSelectedIndex] = server; + if (sender.Equals(OKButton)) + { + if (_oldConfiguration.configs[_oldConfiguration.index].server == + _modifiedConfiguration.configs[_oldSelectedIndex].server && + _oldConfiguration.configs[_oldConfiguration.index].server_port == + _modifiedConfiguration.configs[_oldSelectedIndex].server_port && + _oldConfiguration.configs[_oldConfiguration.index].password == + _modifiedConfiguration.configs[_oldSelectedIndex].password && + _oldConfiguration.configs[_oldConfiguration.index].local_port == + _modifiedConfiguration.configs[_oldSelectedIndex].local_port) + { + _modifiedConfiguration.noChange = true; + } + else + { + _modifiedConfiguration.noChange = false; + _oldConfiguration = _modifiedConfiguration; + } + } return true; } catch (FormatException) @@ -97,15 +118,15 @@ namespace Shadowsocks.View 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(); @@ -119,7 +140,7 @@ namespace Shadowsocks.View 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 + ")"); } @@ -127,15 +148,16 @@ namespace Shadowsocks.View private void LoadCurrentConfiguration() { - modifiedConfiguration = controller.GetConfiguration(); - LoadConfiguration(modifiedConfiguration); - oldSelectedIndex = modifiedConfiguration.index; - ServersListBox.SelectedIndex = modifiedConfiguration.index; + _modifiedConfiguration = controller.GetConfiguration(); + _oldConfiguration = _modifiedConfiguration; + LoadConfiguration(_modifiedConfiguration); + _oldSelectedIndex = _modifiedConfiguration.index; + ServersListBox.SelectedIndex = _modifiedConfiguration.index; LoadSelectedServer(); - UpdateServersMenu(); - enableItem.Checked = modifiedConfiguration.enabled; - ShareOverLANItem.Checked = modifiedConfiguration.shareOverLan; + UpdateServersMenu(); + enableItem.Checked = _modifiedConfiguration.enabled; + ShareOverLANItem.Checked = _modifiedConfiguration.shareOverLan; } private void UpdateServersMenu() @@ -174,55 +196,55 @@ namespace Shadowsocks.View } else { - isFirstRun = true; + _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(sender)) { // why this won't cause stack overflow? - ServersListBox.SelectedIndex = oldSelectedIndex; + ServersListBox.SelectedIndex = _oldSelectedIndex; return; } LoadSelectedServer(); - oldSelectedIndex = ServersListBox.SelectedIndex; + _oldSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!SaveOldSelectedServer(sender)) { 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; + ServersListBox.SelectedIndex = _oldSelectedIndex; + LoadConfiguration(_modifiedConfiguration); + ServersListBox.SelectedIndex = _oldSelectedIndex; LoadSelectedServer(); } @@ -238,27 +260,27 @@ namespace Shadowsocks.View 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(sender)) { return; } - if (modifiedConfiguration.configs.Count == 0) + if (_modifiedConfiguration.configs.Count == 0) { MessageBox.Show("Please add at least one server"); return; - } - controller.SaveServers(modifiedConfiguration.configs); + } + controller.SaveServers(_modifiedConfiguration.configs); this.Hide(); ShowFirstTimeBalloon(); } @@ -327,5 +349,10 @@ namespace Shadowsocks.View qrCodeForm.Icon = this.Icon; qrCodeForm.Show(); } + + private void enableLogBox_CheckedChanged(object sender, EventArgs e) + { + MessageBox.Show("This option only works on next startup."); + } } } From fa5f7adad85d129ee3d067e031eb1d02e9c28816 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 06:06:26 +0800 Subject: [PATCH 08/13] small bug Fix a small bug --- shadowsocks-csharp/View/ConfigForm.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 894b7f27..eb2a3d8f 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -155,9 +155,9 @@ namespace Shadowsocks.View ServersListBox.SelectedIndex = _modifiedConfiguration.index; LoadSelectedServer(); - UpdateServersMenu(); + UpdateServersMenu(); enableItem.Checked = _modifiedConfiguration.enabled; - ShareOverLANItem.Checked = _modifiedConfiguration.shareOverLan; + ShareOverLANItem.Checked = _modifiedConfiguration.shareOverLan; } private void UpdateServersMenu() @@ -279,8 +279,8 @@ namespace Shadowsocks.View { MessageBox.Show("Please add at least one server"); return; - } - controller.SaveServers(_modifiedConfiguration.configs); + } + controller.SaveServers(_modifiedConfiguration.configs); this.Hide(); ShowFirstTimeBalloon(); } @@ -352,7 +352,10 @@ namespace Shadowsocks.View private void enableLogBox_CheckedChanged(object sender, EventArgs e) { - MessageBox.Show("This option only works on next startup."); + if (this.Visible) + { + MessageBox.Show("This option only works on next startup."); + } } } } From 8b0d7604ed7badb5f5229edcd87fef1886552185 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 15:01:36 +0800 Subject: [PATCH 09/13] add modify detector Don't reload server if config not change. --- shadowsocks-csharp/Controller/ShadowsocksController.cs | 10 ++++++++-- shadowsocks-csharp/View/ConfigForm.Designer.cs | 1 - shadowsocks-csharp/View/ConfigForm.cs | 4 ++-- shadowsocks-csharp/View/QRCodeForm.cs | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 53ce3753..5bb1b836 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -57,7 +57,7 @@ namespace Shadowsocks.Controller UpdateSystemProxy(); } - + public Server GetCurrentServer() { return _config.GetCurrentServer(); @@ -69,9 +69,10 @@ namespace Shadowsocks.Controller return Configuration.Load(); } - public void SaveServers(List servers) + public void SaveServers(List servers, bool noChange) { _config.configs = servers; + _config.noChange = noChange; SaveConfig(_config); } @@ -89,6 +90,7 @@ namespace Shadowsocks.Controller public void ToggleShareOverLAN(bool enabled) { _config.shareOverLan = enabled; + _config.noChange = false; SaveConfig(_config); if (ShareOverLANStatusChanged != null) { @@ -138,6 +140,10 @@ namespace Shadowsocks.Controller protected void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); + if (newConfig.noChange) + { + return; + } // some logic in configuration updated the config when saving, we need to read it again _config = Configuration.Load(); diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index c1187c04..cf874286 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -434,7 +434,6 @@ this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; - this.MinimizeBox = false; this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Edit Servers"; diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index eb2a3d8f..92b49c2b 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -279,8 +279,8 @@ namespace Shadowsocks.View { MessageBox.Show("Please add at least one server"); return; - } - controller.SaveServers(_modifiedConfiguration.configs); + } + controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.noChange); this.Hide(); ShowFirstTimeBalloon(); } diff --git a/shadowsocks-csharp/View/QRCodeForm.cs b/shadowsocks-csharp/View/QRCodeForm.cs index 12c5b6fd..87bcef50 100755 --- a/shadowsocks-csharp/View/QRCodeForm.cs +++ b/shadowsocks-csharp/View/QRCodeForm.cs @@ -24,12 +24,12 @@ namespace Shadowsocks.View private void GenQR(string ssconfig) { - string qrText = ssconfig; + string qrText = ssconfig; QRCode4CS.Options options = new QRCode4CS.Options(); options.Text = qrText; QRCode4CS.QRCode qrCoded = null; bool success = false; - foreach (var level in new QRErrorCorrectLevel[]{QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L}) + foreach (var level in new QRErrorCorrectLevel[]{QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L}) { for (int i = 3; i < 10; i++) { From cd5135e066b093faf5caa9dacb9f7f1ec919e5ce Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 15:27:50 +0800 Subject: [PATCH 10/13] add about form Add an about form instead of just open link. --- shadowsocks-csharp/View/AboutForm.Designer.cs | 104 ++++++++++++++++++++++ shadowsocks-csharp/View/AboutForm.cs | 24 ++++++ shadowsocks-csharp/View/AboutForm.resx | 120 ++++++++++++++++++++++++++ shadowsocks-csharp/View/ConfigForm.cs | 5 +- shadowsocks-csharp/shadowsocks-csharp.csproj | 10 +++ 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 shadowsocks-csharp/View/AboutForm.Designer.cs create mode 100644 shadowsocks-csharp/View/AboutForm.cs create mode 100644 shadowsocks-csharp/View/AboutForm.resx diff --git a/shadowsocks-csharp/View/AboutForm.Designer.cs b/shadowsocks-csharp/View/AboutForm.Designer.cs new file mode 100644 index 00000000..498d31d4 --- /dev/null +++ b/shadowsocks-csharp/View/AboutForm.Designer.cs @@ -0,0 +1,104 @@ +namespace Shadowsocks.View +{ + partial class AboutForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.titleLabel = new System.Windows.Forms.Label(); + this.versionLabel = new System.Windows.Forms.Label(); + this.authorLabel = new System.Windows.Forms.Label(); + this.githubLabel = new System.Windows.Forms.LinkLabel(); + this.SuspendLayout(); + // + // titleLabel + // + this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.titleLabel.Location = new System.Drawing.Point(12, 40); + this.titleLabel.Name = "titleLabel"; + this.titleLabel.Size = new System.Drawing.Size(260, 23); + this.titleLabel.TabIndex = 0; + this.titleLabel.Text = "Shadowsocks for Windows"; + this.titleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // versionLabel + // + this.versionLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.versionLabel.Location = new System.Drawing.Point(12, 84); + this.versionLabel.Name = "versionLabel"; + this.versionLabel.Size = new System.Drawing.Size(260, 23); + this.versionLabel.TabIndex = 0; + this.versionLabel.Text = "Version: 2.0.4.0"; + this.versionLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // authorLabel + // + this.authorLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.authorLabel.Location = new System.Drawing.Point(12, 139); + this.authorLabel.Name = "authorLabel"; + this.authorLabel.Size = new System.Drawing.Size(260, 23); + this.authorLabel.TabIndex = 0; + this.authorLabel.Text = "By: clowwindy"; + this.authorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // githubLabel + // + this.githubLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.githubLabel.Location = new System.Drawing.Point(13, 201); + this.githubLabel.Name = "githubLabel"; + this.githubLabel.Size = new System.Drawing.Size(259, 23); + this.githubLabel.TabIndex = 1; + this.githubLabel.TabStop = true; + this.githubLabel.Text = "GitHub"; + this.githubLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.githubLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.githubLabel_LinkClicked); + // + // AboutForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 261); + this.Controls.Add(this.githubLabel); + this.Controls.Add(this.authorLabel); + this.Controls.Add(this.versionLabel); + this.Controls.Add(this.titleLabel); + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "AboutForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "About"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Label titleLabel; + private System.Windows.Forms.Label versionLabel; + private System.Windows.Forms.Label authorLabel; + private System.Windows.Forms.LinkLabel githubLabel; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/View/AboutForm.cs b/shadowsocks-csharp/View/AboutForm.cs new file mode 100644 index 00000000..0b182cb5 --- /dev/null +++ b/shadowsocks-csharp/View/AboutForm.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Text; +using System.Windows.Forms; + +namespace Shadowsocks.View +{ + public partial class AboutForm : Form + { + public AboutForm() + { + InitializeComponent(); + } + + private void githubLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + { + string url = "https://github.com/clowwindy/shadowsocks-csharp"; + System.Diagnostics.Process.Start(url); + } + } +} diff --git a/shadowsocks-csharp/View/AboutForm.resx b/shadowsocks-csharp/View/AboutForm.resx new file mode 100644 index 00000000..7080a7d1 --- /dev/null +++ b/shadowsocks-csharp/View/AboutForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 92b49c2b..852d12f7 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -13,6 +13,7 @@ namespace Shadowsocks.View public partial class ConfigForm : Form { private ShadowsocksController controller; + private AboutForm aboutForm; // this is a copy of configuration that we are working on private Configuration _modifiedConfiguration; @@ -299,7 +300,9 @@ namespace Shadowsocks.View private void AboutItem_Click(object sender, EventArgs e) { - Process.Start("https://github.com/clowwindy/shadowsocks-csharp"); + aboutForm = new AboutForm(); + aboutForm.Show(); + //Process.Start("https://github.com/clowwindy/shadowsocks-csharp"); } private void notifyIcon1_DoubleClick(object sender, EventArgs e) diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index ab31982a..5f486f5c 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -63,6 +63,7 @@ + @@ -78,6 +79,12 @@ + + Form + + + AboutForm.cs + Form @@ -96,6 +103,9 @@ QRCodeForm.cs + + AboutForm.cs + ConfigForm.cs Designer From 82ce787da2da8a7306243f9212664da5cd5333a3 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 9 Nov 2014 15:40:12 +0800 Subject: [PATCH 11/13] update CHANGES --- CHANGES | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index b7838460..0a52a4a2 100644 --- a/CHANGES +++ b/CHANGES @@ -1,12 +1,17 @@ -2.0.4 +2.0.5 2014-11-09 +- Fix QRCode size +- Share over LAN option +- Log to temp path instead + +2.0.4 2014-11-09 - Try to fix data corruption - Remove all configuration except x86 -2.0.3 +2.0.3 2014-11-08 - Support QRCode generation - Fix compatibility issues with some Chrome version -2.0.2 +2.0.2 2014-11-08 - Add remarks - Fix error when polipo is killed From 7807b2407f8678a9bba4fd8eedaef132f84ff672 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 9 Nov 2014 15:42:03 +0800 Subject: [PATCH 12/13] bump --- shadowsocks-csharp/Properties/AssemblyInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shadowsocks-csharp/Properties/AssemblyInfo.cs b/shadowsocks-csharp/Properties/AssemblyInfo.cs index 02c3eba7..7351ffd1 100755 --- a/shadowsocks-csharp/Properties/AssemblyInfo.cs +++ b/shadowsocks-csharp/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.0.4")] +[assembly: AssemblyVersion("2.0.5")] // [assembly: AssemblyFileVersion("2.0.0")] From f5c9b10855f2c6ef868837d1e2aa723b7a33fa05 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 15:57:49 +0800 Subject: [PATCH 13/13] add about form Version now change with AssemblyVersion --- shadowsocks-csharp/View/AboutForm.Designer.cs | 1 + shadowsocks-csharp/View/AboutForm.cs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/shadowsocks-csharp/View/AboutForm.Designer.cs b/shadowsocks-csharp/View/AboutForm.Designer.cs index 498d31d4..11568121 100644 --- a/shadowsocks-csharp/View/AboutForm.Designer.cs +++ b/shadowsocks-csharp/View/AboutForm.Designer.cs @@ -90,6 +90,7 @@ this.Name = "AboutForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "About"; + this.Load += new System.EventHandler(this.AboutForm_Load); this.ResumeLayout(false); } diff --git a/shadowsocks-csharp/View/AboutForm.cs b/shadowsocks-csharp/View/AboutForm.cs index 0b182cb5..e2a91e5d 100644 --- a/shadowsocks-csharp/View/AboutForm.cs +++ b/shadowsocks-csharp/View/AboutForm.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; +using System.Diagnostics; using System.Drawing; +using System.Reflection; using System.Text; using System.Windows.Forms; @@ -20,5 +22,13 @@ namespace Shadowsocks.View string url = "https://github.com/clowwindy/shadowsocks-csharp"; System.Diagnostics.Process.Start(url); } + + private void AboutForm_Load(object sender, EventArgs e) + { + Assembly assembly = Assembly.GetExecutingAssembly(); + FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); + string version = fvi.FileVersion; + versionLabel.Text = "Version: " + version; + } } }