diff --git a/shadowsocks-csharp/Controller/FileManager.cs b/shadowsocks-csharp/Controller/FileManager.cs new file mode 100755 index 00000000..940899f0 --- /dev/null +++ b/shadowsocks-csharp/Controller/FileManager.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Text; + +namespace shadowsocks_csharp.Controller +{ + public class FileManager + { + public static bool ByteArrayToFile(string fileName, byte[] content) + { + try + { + System.IO.FileStream _FileStream = + new System.IO.FileStream(fileName, System.IO.FileMode.Create, + System.IO.FileAccess.Write); + _FileStream.Write(content, 0, content.Length); + _FileStream.Close(); + return true; + } + catch (Exception _Exception) + { + Console.WriteLine("Exception caught in process: {0}", + _Exception.ToString()); + } + return false; + } + + public static void UncompressFile(string fileName, byte[] content) + { + FileStream destinationFile = File.Create(fileName); + + // Because the uncompressed size of the file is unknown, + // we are using an arbitrary buffer size. + byte[] buffer = new byte[4096]; + int n; + + using (GZipStream input = new GZipStream(new MemoryStream(content), + CompressionMode.Decompress, false)) + { + while (true) + { + n = input.Read(buffer, 0, buffer.Length); + if (n == 0) + { + break; + } + destinationFile.Write(buffer, 0, n); + } + } + destinationFile.Close(); + } + } +} diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 9172fd29..693cc03a 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -4,11 +4,11 @@ using System.Text; using System.Net.Sockets; using System.Net; using shadowsocks_csharp.Encrypt; +using shadowsocks_csharp.Model; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.Controller { - class Local { private Config config; diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index 165491ba..92f420e0 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -7,7 +7,7 @@ using System.Net; using System.Net.Sockets; using System.Text; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.Controller { class PACServer { diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index c7bb1cdc..0263b6d0 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -1,4 +1,5 @@ -using shadowsocks_csharp.Properties; +using shadowsocks_csharp.Model; +using shadowsocks_csharp.Properties; using System; using System.Collections.Generic; using System.Diagnostics; @@ -6,54 +7,11 @@ using System.IO; using System.IO.Compression; using System.Text; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.Controller { class PolipoRunner { private Process process; - private bool ByteArrayToFile(string fileName, byte[] content) - { - try - { - System.IO.FileStream _FileStream = - new System.IO.FileStream(fileName, System.IO.FileMode.Create, - System.IO.FileAccess.Write); - _FileStream.Write(content, 0, content.Length); - _FileStream.Close(); - return true; - } - catch (Exception _Exception) - { - Console.WriteLine("Exception caught in process: {0}", - _Exception.ToString()); - } - return false; - } - - public static void UncompressFile(string fileName, byte[] content) - { - FileStream destinationFile = File.Create(fileName); - - // Because the uncompressed size of the file is unknown, - // we are using an arbitrary buffer size. - byte[] buffer = new byte[4096]; - int n; - - using (GZipStream input = new GZipStream(new MemoryStream(content), - CompressionMode.Decompress, false)) - { - while (true) - { - n = input.Read(buffer, 0, buffer.Length); - if (n == 0) - { - break; - } - destinationFile.Write(buffer, 0, n); - } - } - destinationFile.Close(); - } public void Start(Config config) { @@ -68,8 +26,8 @@ namespace shadowsocks_csharp string temppath = Path.GetTempPath(); string polipoConfig = Resources.polipo_config; polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", config.local_port.ToString()); - ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); - UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); + FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); + FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); process = new Process(); // Configure the process using the StartInfo properties. diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 2c42d3ac..fe89a619 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -1,8 +1,9 @@ -using System; +using shadowsocks_csharp.Model; +using System; using System.Collections.Generic; using System.Text; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.Controller { public class ShadowsocksController { @@ -17,8 +18,16 @@ namespace shadowsocks_csharp private PolipoRunner polipoRunner; private bool stopped = false; + public class PathEventArgs : EventArgs + { + public string Path; + } + public event EventHandler ConfigChanged; public event EventHandler EnableStatusChanged; + + // when user clicked Edit PAC, and PAC file has already created + public event EventHandler PACFileReadyToOpen; public ShadowsocksController() { @@ -37,6 +46,14 @@ namespace shadowsocks_csharp { Config.Save(newConfig); config = newConfig; + + local.Stop(); + polipoRunner.Stop(); + polipoRunner.Start(config); + + local = new Local(config); + local.Start(); + if (ConfigChanged != null) { ConfigChanged(this, new EventArgs()); @@ -74,6 +91,10 @@ namespace shadowsocks_csharp } } + public void TouchPACFile() + { + } + private void updateSystemProxy() { if (config.enabled) diff --git a/shadowsocks-csharp/Controller/SystemProxy.cs b/shadowsocks-csharp/Controller/SystemProxy.cs index c41bbf11..264406c8 100755 --- a/shadowsocks-csharp/Controller/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/SystemProxy.cs @@ -4,9 +4,9 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.Controller { - class SystemProxy + public class SystemProxy { [DllImport("wininet.dll")] diff --git a/shadowsocks-csharp/Model/Config.cs b/shadowsocks-csharp/Model/Config.cs index 50a3bcaf..edca2a16 100755 --- a/shadowsocks-csharp/Model/Config.cs +++ b/shadowsocks-csharp/Model/Config.cs @@ -5,7 +5,7 @@ using System.IO; using System.Diagnostics; using SimpleJson; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.Model { [Serializable] public class Config diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index b1e05c49..70a8ba93 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -1,4 +1,6 @@ -using shadowsocks_csharp.Properties; +using shadowsocks_csharp.Controller; +using shadowsocks_csharp.Properties; +using shadowsocks_csharp.View; using System; using System.Collections.Generic; using System.IO; @@ -22,7 +24,8 @@ namespace shadowsocks_csharp { string tempPath = Path.GetTempPath(); string dllPath = tempPath + "/polarssl.dll"; - PolipoRunner.UncompressFile(dllPath, Resources.polarssl_dll); + // TODO: PolipoRunner should not do this job + FileManager.UncompressFile(dllPath, Resources.polarssl_dll); LoadLibrary(dllPath); FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 38524b64..6795a1f3 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -1,4 +1,4 @@ -namespace shadowsocks_csharp +namespace shadowsocks_csharp.View { partial class ConfigForm { diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 22e86e86..7dbb85f0 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -5,8 +5,10 @@ using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; +using shadowsocks_csharp.Controller; +using shadowsocks_csharp.Model; -namespace shadowsocks_csharp +namespace shadowsocks_csharp.View { public partial class ConfigForm : Form { diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 1e1dd0bd..3d3dbaec 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -81,6 +81,7 @@ +