diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 693cc03a..bc6fa9fb 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -22,22 +22,29 @@ namespace shadowsocks_csharp.Controller public void Start() { + try + { + // Create a TCP/IP socket. + listener = new Socket(AddressFamily.InterNetwork, + SocketType.Stream, ProtocolType.Tcp); + IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port); - // Create a TCP/IP socket. - listener = new Socket(AddressFamily.InterNetwork, - SocketType.Stream, ProtocolType.Tcp); - IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port); - - // Bind the socket to the local endpoint and listen for incoming connections. - listener.Bind(localEndPoint); - listener.Listen(100); + // Bind the socket to the local endpoint and listen for incoming connections. + listener.Bind(localEndPoint); + listener.Listen(100); - // Start an asynchronous socket to listen for connections. - Console.WriteLine("Shadowsocks started"); - listener.BeginAccept( - new AsyncCallback(AcceptCallback), - listener); + // Start an asynchronous socket to listen for connections. + Console.WriteLine("Shadowsocks started"); + listener.BeginAccept( + new AsyncCallback(AcceptCallback), + listener); + } + catch(SocketException) + { + listener.Close(); + throw; + } } diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index 92f420e0..c5d8f7d3 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -11,6 +11,8 @@ namespace shadowsocks_csharp.Controller { class PACServer { + private static string PAC_FILE = "pac.txt"; + Socket listener; public void Start() { @@ -28,6 +30,19 @@ namespace shadowsocks_csharp.Controller listener); } + public string TouchPACFile() + { + if (File.Exists(PAC_FILE)) + { + return PAC_FILE; + } + else + { + FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt); + return PAC_FILE; + } + } + public void AcceptCallback(IAsyncResult ar) { @@ -51,21 +66,29 @@ namespace shadowsocks_csharp.Controller private string getPACContent() { // TODO try pac.txt in current directory - byte[] pacGZ = Resources.proxy_pac_txt; - - byte[] buffer = new byte[1024 * 1024]; // builtin pac gzip size: maximum 1M - int n; - - using (GZipStream input = new GZipStream(new MemoryStream(pacGZ), - CompressionMode.Decompress, false)) + + if (File.Exists(PAC_FILE)) { - n = input.Read(buffer, 0, buffer.Length); - if (n == 0) + return File.ReadAllText(PAC_FILE, Encoding.UTF8); + } + else + { + byte[] pacGZ = Resources.proxy_pac_txt; + byte[] buffer = new byte[1024 * 1024]; // builtin pac gzip size: maximum 1M + int n; + + using (GZipStream input = new GZipStream(new MemoryStream(pacGZ), + CompressionMode.Decompress, false)) { - throw new IOException("can not decompress pac"); + n = input.Read(buffer, 0, buffer.Length); + if (n == 0) + { + throw new IOException("can not decompress pac"); + } + return System.Text.Encoding.UTF8.GetString(buffer, 0, n); } - return System.Text.Encoding.UTF8.GetString(buffer, 0, n); } + } private void receiveCallback(IAsyncResult ar) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index fe89a619..32042d48 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -23,8 +23,15 @@ namespace shadowsocks_csharp.Controller public string Path; } + public class ErrorEventArgs : EventArgs + { + public string Error; + } + public event EventHandler ConfigChanged; public event EventHandler EnableStatusChanged; + + public event EventHandler LocalFailToStart; // when user clicked Edit PAC, and PAC file has already created public event EventHandler PACFileReadyToOpen; @@ -35,9 +42,16 @@ namespace shadowsocks_csharp.Controller polipoRunner = new PolipoRunner(); polipoRunner.Start(config); local = new Local(config); - local.Start(); - pacServer = new PACServer(); - pacServer.Start(); + try + { + local.Start(); + pacServer = new PACServer(); + pacServer.Start(); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } updateSystemProxy(); } @@ -93,6 +107,11 @@ namespace shadowsocks_csharp.Controller public void TouchPACFile() { + string pacFilename = pacServer.TouchPACFile(); + if (PACFileReadyToOpen != null) + { + PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename }); + } } private void updateSystemProxy() diff --git a/shadowsocks-csharp/Data/polarssl.dll.gz b/shadowsocks-csharp/Data/polarssl.dll.gz index 7963932b..1010242e 100755 Binary files a/shadowsocks-csharp/Data/polarssl.dll.gz and b/shadowsocks-csharp/Data/polarssl.dll.gz differ diff --git a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs index e14b26ab..1b76fe25 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs @@ -271,7 +271,7 @@ namespace shadowsocks_csharp.Encrypt PolarSSL.blowfish_free(_encryptCtx); break; case CIPHER_RC4: - PolarSSL.arc4_free(_encryptCtx); + //PolarSSL.arc4_free(_encryptCtx); break; } } @@ -286,7 +286,7 @@ namespace shadowsocks_csharp.Encrypt PolarSSL.blowfish_free(_decryptCtx); break; case CIPHER_RC4: - PolarSSL.arc4_free(_decryptCtx); + //PolarSSL.arc4_free(_decryptCtx); break; } } diff --git a/shadowsocks-csharp/Model/Config.cs b/shadowsocks-csharp/Model/Config.cs index edca2a16..0b962113 100755 --- a/shadowsocks-csharp/Model/Config.cs +++ b/shadowsocks-csharp/Model/Config.cs @@ -44,7 +44,10 @@ namespace shadowsocks_csharp.Model } catch (Exception e) { - Console.WriteLine(e); + if (!(e is FileNotFoundException)) + { + Console.WriteLine(e); + } return new Config { server = "127.0.0.1", diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index e4e34572..0913fda3 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -20,13 +20,20 @@ namespace shadowsocks_csharp [STAThread] static void Main() { + string tempPath = Path.GetTempPath(); + string dllPath = tempPath + "/polarssl.dll"; try { - string tempPath = Path.GetTempPath(); - string dllPath = tempPath + "/polarssl.dll"; FileManager.UncompressFile(dllPath, Resources.polarssl_dll); - LoadLibrary(dllPath); + } + catch (IOException e) + { + Console.WriteLine(e.ToString()); + } + LoadLibrary(dllPath); + try + { FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); TextWriter tmp = Console.Out; StreamWriter sw = new StreamWriter(fs); diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 6795a1f3..9caffefb 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -52,6 +52,7 @@ this.aboutItem = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.quitItem = new System.Windows.Forms.MenuItem(); + this.editPACFileItem = new System.Windows.Forms.MenuItem(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.SuspendLayout(); @@ -249,6 +250,7 @@ this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.enableItem, this.configItem, + this.editPACFileItem, this.aboutItem, this.menuItem3, this.quitItem}); @@ -267,21 +269,27 @@ // // aboutItem // - this.aboutItem.Index = 2; + this.aboutItem.Index = 3; this.aboutItem.Text = "About"; this.aboutItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); // // menuItem3 // - this.menuItem3.Index = 3; + this.menuItem3.Index = 4; this.menuItem3.Text = "-"; // // quitItem // - this.quitItem.Index = 4; + this.quitItem.Index = 5; this.quitItem.Text = "Quit"; this.quitItem.Click += new System.EventHandler(this.Quit_Click); // + // editPACFileItem + // + this.editPACFileItem.Index = 2; + this.editPACFileItem.Text = "Edit PAC File..."; + this.editPACFileItem.Click += new System.EventHandler(this.editPACFileItem_Click); + // // ConfigForm // this.AcceptButton = this.button1; @@ -332,6 +340,7 @@ private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem quitItem; private System.Windows.Forms.MenuItem configItem; + private System.Windows.Forms.MenuItem editPACFileItem; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 7dbb85f0..1ea370a4 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -22,6 +22,7 @@ namespace shadowsocks_csharp.View this.controller = controller; controller.EnableStatusChanged += controller_EnableStatusChanged; controller.ConfigChanged += controller_ConfigChanged; + controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; updateUI(); } @@ -35,6 +36,14 @@ namespace shadowsocks_csharp.View { updateUI(); } + + void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e) + { + string argument = @"/select, " + e.Path; + + System.Diagnostics.Process.Start("explorer.exe", argument); + } + private void showWindow() { @@ -131,5 +140,10 @@ namespace shadowsocks_csharp.View controller.ToggleEnable(enableItem.Checked); } + private void editPACFileItem_Click(object sender, EventArgs e) + { + controller.TouchPACFile(); + } + } }