From 835265b1cd4a6c1dfca1d2db5215c467ff3b6264 Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 9 Nov 2014 05:46:08 +0800 Subject: [PATCH] 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."); + } } }