Browse Source

add polipo runner

tags/3.2
clowwindy 10 years ago
parent
commit
28f09ead5e
5 changed files with 120 additions and 1 deletions
  1. +11
    -0
      shadowsocks-csharp/Form1.cs
  2. +65
    -0
      shadowsocks-csharp/PolipoRunner.cs
  3. +42
    -0
      shadowsocks-csharp/SystemProxy.cs
  4. +1
    -1
      shadowsocks-csharp/config.txt
  5. +1
    -0
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 11
- 0
shadowsocks-csharp/Form1.cs View File

@@ -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)


+ 65
- 0
shadowsocks-csharp/PolipoRunner.cs View File

@@ -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;
}
}
}
}

+ 42
- 0
shadowsocks-csharp/SystemProxy.cs View File

@@ -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);
}
}
}

+ 1
- 1
shadowsocks-csharp/config.txt View File

@@ -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:


+ 1
- 0
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -96,6 +96,7 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Local.cs" />
<Compile Include="PolipoRunner.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">


Loading…
Cancel
Save