diff --git a/shadowsocks-csharp/Config.cs b/shadowsocks-csharp/Config.cs index 3800e832..41afe134 100755 --- a/shadowsocks-csharp/Config.cs +++ b/shadowsocks-csharp/Config.cs @@ -25,14 +25,14 @@ namespace shadowsocks_csharp return config; } } - catch (IOException e) + catch (IOException) { return new Config { server = "127.0.0.1", server_port = 8388, - local_port = 1080, - password = "foobar!" + local_port = 1081, + password = "barfoo!" }; } } @@ -42,7 +42,7 @@ namespace shadowsocks_csharp DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config)); try { - using (FileStream fs = File.Open(@"config.json",FileMode.Create)) + using (FileStream fs = File.Open(@"config.json", FileMode.Create)) { ser.WriteObject(fs, config); } diff --git a/shadowsocks-csharp/Form1.Designer.cs b/shadowsocks-csharp/Form1.Designer.cs index 0d8ff863..497895da 100755 --- a/shadowsocks-csharp/Form1.Designer.cs +++ b/shadowsocks-csharp/Form1.Designer.cs @@ -45,8 +45,8 @@ this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); - this.Config = new System.Windows.Forms.ToolStripMenuItem(); - this.Quit = new System.Windows.Forms.ToolStripMenuItem(); + this.ConfigItem = new System.Windows.Forms.ToolStripMenuItem(); + this.QuitItem = new System.Windows.Forms.ToolStripMenuItem(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); @@ -178,6 +178,7 @@ this.button2.TabIndex = 1; this.button2.Text = "Cancel"; this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // @@ -188,6 +189,7 @@ this.button1.TabIndex = 0; this.button1.Text = "OK"; this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); // // tableLayoutPanel2 // @@ -217,26 +219,26 @@ // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.Config, - this.Quit}); + this.ConfigItem, + this.QuitItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System; this.contextMenuStrip1.ShowImageMargin = false; this.contextMenuStrip1.Size = new System.Drawing.Size(86, 48); // - // Config + // ConfigItem // - this.Config.Name = "Config"; - this.Config.Size = new System.Drawing.Size(127, 22); - this.Config.Text = "Config"; - this.Config.Click += new System.EventHandler(this.Config_Click); + this.ConfigItem.Name = "ConfigItem"; + this.ConfigItem.Size = new System.Drawing.Size(127, 22); + this.ConfigItem.Text = "Config"; + this.ConfigItem.Click += new System.EventHandler(this.Config_Click); // - // Quit + // QuitItem // - this.Quit.Name = "Quit"; - this.Quit.Size = new System.Drawing.Size(127, 22); - this.Quit.Text = "Quit"; - this.Quit.Click += new System.EventHandler(this.Quit_Click); + this.QuitItem.Name = "QuitItem"; + this.QuitItem.Size = new System.Drawing.Size(127, 22); + this.QuitItem.Text = "Quit"; + this.QuitItem.Click += new System.EventHandler(this.Quit_Click); // // Form1 // @@ -251,6 +253,7 @@ this.Controls.Add(this.tableLayoutPanel1); this.Name = "Form1"; this.Text = "Shadowsocks"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); this.panel1.ResumeLayout(false); @@ -279,8 +282,8 @@ private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; - private System.Windows.Forms.ToolStripMenuItem Config; - private System.Windows.Forms.ToolStripMenuItem Quit; + private System.Windows.Forms.ToolStripMenuItem ConfigItem; + private System.Windows.Forms.ToolStripMenuItem QuitItem; } } diff --git a/shadowsocks-csharp/Form1.cs b/shadowsocks-csharp/Form1.cs index 077299a4..587d0411 100755 --- a/shadowsocks-csharp/Form1.cs +++ b/shadowsocks-csharp/Form1.cs @@ -10,9 +10,29 @@ namespace shadowsocks_csharp { public partial class Form1 : Form { + Local local; + Config config; + public Form1() { + config = Config.Load(); + reload(config); InitializeComponent(); + textBox1.Text = config.server; + textBox2.Text = config.server_port.ToString(); + textBox3.Text = config.password; + textBox4.Text = config.local_port.ToString(); + } + + private void reload(Config config) + { + if (local != null) + { + local.Stop(); + } + local = new Local(config.local_port); + local.Start(); + } private void Config_Click(object sender, EventArgs e) @@ -24,5 +44,21 @@ namespace shadowsocks_csharp { } + + private void button1_Click(object sender, EventArgs e) + { + reload(Config.Load()); + } + + private void button2_Click(object sender, EventArgs e) + { + + } + + private void Form1_FormClosed(object sender, FormClosedEventArgs e) + { + local.Stop(); + } + } } diff --git a/shadowsocks-csharp/Local.cs b/shadowsocks-csharp/Local.cs index 66ad8184..12d2658a 100755 --- a/shadowsocks-csharp/Local.cs +++ b/shadowsocks-csharp/Local.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; -using System.Threading; namespace shadowsocks_csharp { @@ -49,23 +48,30 @@ namespace shadowsocks_csharp public void AcceptCallback(IAsyncResult ar) { + try + { - // Get the socket that handles the client request. - Socket listener = (Socket)ar.AsyncState; - listener.BeginAccept( - new AsyncCallback(AcceptCallback), - listener); + // Get the socket that handles the client request. + Socket listener = (Socket)ar.AsyncState; + listener.BeginAccept( + new AsyncCallback(AcceptCallback), + listener); - Socket conn = listener.EndAccept(ar); + Socket conn = listener.EndAccept(ar); - // Create the state object. - Handler handler = new Handler(); - handler.connection = conn; - handler.encryptor = encryptor; + // Create the state object. + Handler handler = new Handler(); + handler.connection = conn; + handler.encryptor = encryptor; - handler.Start(); - //handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, - // new AsyncCallback(ReadCallback), state); + handler.Start(); + //handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, + // new AsyncCallback(ReadCallback), state); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } } } @@ -110,7 +116,6 @@ namespace shadowsocks_csharp Console.WriteLine("Socket connected to {0}", remote.RemoteEndPoint.ToString()); - Console.WriteLine(Thread.CurrentThread.ManagedThreadId); handshakeReceive(); } diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 417442a6..9656d676 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -12,12 +12,9 @@ namespace shadowsocks_csharp [STAThread] static void Main() { - Local local = new Local(1081); - local.Start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); - local.Stop(); } } }