diff --git a/shadowsocks-csharp/Form1.cs b/shadowsocks-csharp/Form1.cs index 460c42f4..e0294d71 100755 --- a/shadowsocks-csharp/Form1.cs +++ b/shadowsocks-csharp/Form1.cs @@ -13,6 +13,7 @@ namespace shadowsocks_csharp Local local; PACServer pacServer; Config config; + PolipoRunner polipoRunner; public Form1() { @@ -55,9 +56,18 @@ namespace shadowsocks_csharp if (local != null) { local.Stop(); + if (polipoRunner != null) + { + polipoRunner.Stop(); + } } local = new Local(config); local.Start(); + if (polipoRunner == null) + { + polipoRunner = new PolipoRunner(); + } + polipoRunner.Start(config); } @@ -108,6 +118,7 @@ namespace shadowsocks_csharp private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (local != null) local.Stop(); + if (polipoRunner != null) polipoRunner.Stop(); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/shadowsocks-csharp/PolipoRunner.cs b/shadowsocks-csharp/PolipoRunner.cs new file mode 100755 index 00000000..1b356927 --- /dev/null +++ b/shadowsocks-csharp/PolipoRunner.cs @@ -0,0 +1,65 @@ +using shadowsocks_csharp.Properties; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; + +namespace shadowsocks_csharp +{ + 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 void Start(Config config) + { + if (process == null) + { + 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)); + ByteArrayToFile(temppath + "/polipo.exe", Resources.polipo); + + process = new Process(); + // Configure the process using the StartInfo properties. + process.StartInfo.FileName = temppath + "/polipo.exe"; + process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\""; + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + //process.StandardOutput + process.Start(); + } + } + + public void Stop() + { + if (process != null) + { + process.Kill(); + process.WaitForExit(); + process = null; + } + } + } +} diff --git a/shadowsocks-csharp/SystemProxy.cs b/shadowsocks-csharp/SystemProxy.cs new file mode 100755 index 00000000..e62f84b3 --- /dev/null +++ b/shadowsocks-csharp/SystemProxy.cs @@ -0,0 +1,42 @@ +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace shadowsocks_csharp +{ + class SystemProxy + { + + [DllImport("wininet.dll")] + public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); + public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; + public const int INTERNET_OPTION_REFRESH = 37; + static bool settingsReturn, refreshReturn; + + public void Enable() + { + RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); + registry.SetValue("ProxyEnable", 1); + registry.SetValue("ProxyServer", "127.0.0.1:8123"); + + // These lines implement the Interface in the beginning of program + // They cause the OS to refresh the settings, causing IP to realy update + settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); + refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); + } + + public void Disable() + { + RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); + registry.SetValue("ProxyEnable", 0); + registry.SetValue("ProxyServer", ""); + + // These lines implement the Interface in the beginning of program + // They cause the OS to refresh the settings, causing IP to realy update + settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); + refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); + } + } +} diff --git a/shadowsocks-csharp/config.txt b/shadowsocks-csharp/config.txt index 318b2cd9..449e7736 100755 --- a/shadowsocks-csharp/config.txt +++ b/shadowsocks-csharp/config.txt @@ -38,7 +38,7 @@ # Uncomment this if you want to use a parent SOCKS proxy: -socksParentProxy = "127.0.0.1:1080" +socksParentProxy = "127.0.0.1:__SOCKS_PORT__" socksProxyType = socks5 # Uncomment this if you want to scrub private information from the log: diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index a5802311..48a46982 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -96,6 +96,7 @@ Form1.cs +