diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index 3f84f933..b0ccf4d3 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -37,7 +37,7 @@ namespace Shadowsocks.Controller { string request = Encoding.UTF8.GetString(firstPacket, 0, length); string[] lines = request.Split('\r', '\n'); - bool hostMatch = false, pathMatch = false; + bool hostMatch = false, pathMatch = false, useSocks = false; foreach (string line in lines) { string[] kv = line.Split(new char[]{':'}, 2); @@ -50,6 +50,13 @@ namespace Shadowsocks.Controller hostMatch = true; } } + else if (kv[0] == "User-Agent") + { + if (kv[1].IndexOf("Chrome") >= 0) + { + useSocks = true; + } + } } else if (kv.Length == 1) { @@ -61,7 +68,7 @@ namespace Shadowsocks.Controller } if (hostMatch && pathMatch) { - SendResponse(firstPacket, length, socket); + SendResponse(firstPacket, length, socket, useSocks); return true; } return false; @@ -98,7 +105,7 @@ namespace Shadowsocks.Controller } } - public void SendResponse(byte[] firstPacket, int length, Socket socket) + public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks) { try { @@ -106,7 +113,7 @@ namespace Shadowsocks.Controller IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint; - string proxy = GetPACAddress(firstPacket, length, localEndPoint); + string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks); pac = pac.Replace("__PROXY__", proxy); @@ -163,9 +170,22 @@ Connection: Close } } - private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint) + private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks) { - return "PROXY " + localEndPoint.Address + ":" + this._config.localPort + ";"; + //try + //{ + // string requestString = Encoding.UTF8.GetString(requestBuf); + // if (requestString.IndexOf("AppleWebKit") >= 0) + // { + // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port; + // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";"; + // } + //} + //catch (Exception e) + //{ + // Console.WriteLine(e); + //} + return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";"; } } } diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index d5cc2792..ae0b46f1 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -15,6 +15,7 @@ namespace Shadowsocks.Controller { private Process _process; private static string temppath; + private int _runningPort; static PolipoRunner() { @@ -29,6 +30,14 @@ namespace Shadowsocks.Controller } } + public int RunningPort + { + get + { + return _runningPort; + } + } + public void Start(Configuration configuration) { Server server = configuration.GetCurrentServer(); @@ -48,8 +57,9 @@ namespace Shadowsocks.Controller } } string polipoConfig = Resources.polipo_config; + _runningPort = this.GetFreePort(); polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString()); - polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", this.GetFreePort().ToString()); + polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString()); polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1"); FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 2c0ace5c..4e343c96 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -222,7 +222,7 @@ namespace Shadowsocks.Controller List services = new List(); services.Add(local); services.Add(_pacServer); - services.Add(new PortForwarder(_config.localPort)); + services.Add(new PortForwarder(polipoRunner.RunningPort)); _listener = new Listener(services); _listener.Start(_config); }