no hard code 8123

This commit is contained in:
clowwindy
2015-01-18 17:30:42 +08:00
parent 8c8c947356
commit 5463b2859a
3 changed files with 26 additions and 2 deletions
@@ -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<int> usedPorts = new List<int>();
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.");
}
}
}
@@ -222,7 +222,7 @@ namespace Shadowsocks.Controller
List<Listener.Service> services = new List<Listener.Service>();
services.Add(local);
services.Add(_pacServer);
services.Add(new PortForwarder(8123));
services.Add(new PortForwarder(_config.localPort));
_listener = new Listener(services);
_listener.Start(_config);
}
+2 -1
View File
@@ -1,4 +1,5 @@
proxyAddress = "__POLIPO_BIND_IP__"
proxyAddress = "__POLIPO_BIND_IP__"
proxyPort = 8123
socksParentProxy = "127.0.0.1:__SOCKS_PORT__"
socksProxyType = socks5