diff --git a/shadowsocks-csharp/Controller/PolipoRunner.cs b/shadowsocks-csharp/Controller/PolipoRunner.cs index 1b91783b..d24ad367 100755 --- a/shadowsocks-csharp/Controller/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/PolipoRunner.cs @@ -6,6 +6,8 @@ using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Text; +using System.Net.NetworkInformation; +using System.Net; namespace Shadowsocks.Controller { @@ -47,6 +49,7 @@ namespace Shadowsocks.Controller } string polipoConfig = Resources.polipo_config; polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString()); + polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", this.GetFreePort().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)); @@ -79,5 +82,25 @@ namespace Shadowsocks.Controller _process = null; } } + + private int GetFreePort() + { + IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); + + List usedPorts = new List(); + foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()) + { + usedPorts.Add(endPoint.Port); + } + for (int port = 8123; port < 65535; port++) + { + if (!usedPorts.Contains(port)) + { + return port; + } + } + throw new Exception("No free port found."); + } } } diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index a313d424..2c0ace5c 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(8123)); + services.Add(new PortForwarder(_config.localPort)); _listener = new Listener(services); _listener.Start(_config); } diff --git a/shadowsocks-csharp/Data/polipo_config.txt b/shadowsocks-csharp/Data/polipo_config.txt index 5a18557f..fabeab66 100755 --- a/shadowsocks-csharp/Data/polipo_config.txt +++ b/shadowsocks-csharp/Data/polipo_config.txt @@ -1,4 +1,5 @@ -proxyAddress = "__POLIPO_BIND_IP__" +proxyAddress = "__POLIPO_BIND_IP__" +proxyPort = 8123 socksParentProxy = "127.0.0.1:__SOCKS_PORT__" socksProxyType = socks5