diff --git a/shadowsocks-csharp/ConfigForm.cs b/shadowsocks-csharp/ConfigForm.cs index 5ef69c3b..b6e6139c 100755 --- a/shadowsocks-csharp/ConfigForm.cs +++ b/shadowsocks-csharp/ConfigForm.cs @@ -10,18 +10,28 @@ namespace shadowsocks_csharp { public partial class ConfigForm : Form { - Local local; - PACServer pacServer; - Config config; - PolipoRunner polipoRunner; + ShadowsocksController controller; - public ConfigForm() + public ConfigForm(ShadowsocksController controller) { - config = Config.Load(); InitializeComponent(); notifyIcon1.ContextMenu = contextMenu1; - enableItem.Checked = config.enabled; - configToTextBox(); + + this.controller = controller; + controller.EnableStatusChanged += controller_EnableStatusChanged; + controller.ConfigChanged += controller_ConfigChanged; + + updateUI(); + } + + private void controller_ConfigChanged(object sender, EventArgs e) + { + updateUI(); + } + + private void controller_EnableStatusChanged(object sender, EventArgs e) + { + updateUI(); } private void showWindow() @@ -30,49 +40,29 @@ namespace shadowsocks_csharp this.Show(); } - private void configToTextBox() + private void updateUI() { + Config config = controller.GetConfig(); + textBox1.Text = config.server; textBox2.Text = config.server_port.ToString(); textBox3.Text = config.password; textBox4.Text = config.local_port.ToString(); - comboBox1.Text = config.method == null ? "table" : config.method; + comboBox1.Text = config.method == null ? "aes-256-cfb" : config.method; + + enableItem.Checked = config.enabled; } private void Form1_Load(object sender, EventArgs e) { - if (!config.isDefault) + if (!controller.GetConfig().isDefault) { this.Opacity = 0; - reload(config); BeginInvoke(new MethodInvoker(delegate + BeginInvoke(new MethodInvoker(delegate { this.Hide(); })); } - pacServer = new PACServer(); - pacServer.Start(); - updateSystemProxy(); - } - - private void reload(Config config) - { - if (local != null) - { - local.Stop(); - if (polipoRunner != null) - { - polipoRunner.Stop(); - } - } - if (polipoRunner == null) - { - polipoRunner = new PolipoRunner(); - } - polipoRunner.Start(config); - - local = new Local(config); - local.Start(); - } private void Config_Click(object sender, EventArgs e) @@ -98,9 +88,7 @@ namespace shadowsocks_csharp method = comboBox1.Text, isDefault = false }; - Config.Save(config); - this.config = config; - reload(config); + controller.SaveConfig(config); this.Hide(); } catch (FormatException) @@ -116,17 +104,12 @@ namespace shadowsocks_csharp private void cancelButton_Click(object sender, EventArgs e) { this.Hide(); - configToTextBox(); + updateUI(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { - if (local != null) local.Stop(); - if (polipoRunner != null) polipoRunner.Stop(); - if (config.enabled) - { - SystemProxy.Disable(); - } + controller.Stop(); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) @@ -139,24 +122,11 @@ namespace shadowsocks_csharp showWindow(); } - private void updateSystemProxy() - { - if (config.enabled) - { - SystemProxy.Enable(); - } - else - { - SystemProxy.Disable(); - } - } private void EnableItem_Click(object sender, EventArgs e) { enableItem.Checked = !enableItem.Checked; - config.enabled = enableItem.Checked; - Config.Save(config); - updateSystemProxy(); + controller.ToggleEnable(enableItem.Checked); } } diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 41100677..f0f7c5ba 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -38,7 +38,8 @@ namespace shadowsocks_csharp } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new ConfigForm()); + ShadowsocksController controller = new ShadowsocksController(); + Application.Run(new ConfigForm(controller)); } diff --git a/shadowsocks-csharp/ShadowsocksController.cs b/shadowsocks-csharp/ShadowsocksController.cs new file mode 100755 index 00000000..2c42d3ac --- /dev/null +++ b/shadowsocks-csharp/ShadowsocksController.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace shadowsocks_csharp +{ + public class ShadowsocksController + { + // controller: + // handle user actions + // manipulates UI + // interacts with low level logic + + private Local local; + private PACServer pacServer; + private Config config; + private PolipoRunner polipoRunner; + private bool stopped = false; + + public event EventHandler ConfigChanged; + public event EventHandler EnableStatusChanged; + + public ShadowsocksController() + { + config = Config.Load(); + polipoRunner = new PolipoRunner(); + polipoRunner.Start(config); + local = new Local(config); + local.Start(); + pacServer = new PACServer(); + pacServer.Start(); + + updateSystemProxy(); + } + + public void SaveConfig(Config newConfig) + { + Config.Save(newConfig); + config = newConfig; + if (ConfigChanged != null) + { + ConfigChanged(this, new EventArgs()); + } + } + + public Config GetConfig() + { + return config; + } + + public void ToggleEnable(bool enabled) + { + config.enabled = enabled; + updateSystemProxy(); + SaveConfig(config); + if (EnableStatusChanged != null) + { + EnableStatusChanged(this, new EventArgs()); + } + } + + public void Stop() + { + if (stopped) + { + return; + } + stopped = true; + local.Stop(); + polipoRunner.Stop(); + if (config.enabled) + { + SystemProxy.Disable(); + } + } + + private void updateSystemProxy() + { + if (config.enabled) + { + SystemProxy.Enable(); + } + else + { + SystemProxy.Disable(); + } + } + } +} diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 29e914df..635a5486 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -99,6 +99,7 @@ + ConfigForm.cs