Skip to content

Commit 5463b28

Browse files
author
clowwindy
committed
no hard code 8123
1 parent 8c8c947 commit 5463b28

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

shadowsocks-csharp/Controller/PolipoRunner.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.IO;
77
using System.IO.Compression;
88
using System.Text;
9+
using System.Net.NetworkInformation;
10+
using System.Net;
911

1012
namespace Shadowsocks.Controller
1113
{
@@ -47,6 +49,7 @@ public void Start(Configuration configuration)
4749
}
4850
string polipoConfig = Resources.polipo_config;
4951
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
52+
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", this.GetFreePort().ToString());
5053
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
5154
FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));
5255

@@ -79,5 +82,25 @@ public void Stop()
7982
_process = null;
8083
}
8184
}
85+
86+
private int GetFreePort()
87+
{
88+
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
89+
IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();
90+
91+
List<int> usedPorts = new List<int>();
92+
foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners())
93+
{
94+
usedPorts.Add(endPoint.Port);
95+
}
96+
for (int port = 8123; port < 65535; port++)
97+
{
98+
if (!usedPorts.Contains(port))
99+
{
100+
return port;
101+
}
102+
}
103+
throw new Exception("No free port found.");
104+
}
82105
}
83106
}

shadowsocks-csharp/Controller/ShadowsocksController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected void Reload()
222222
List<Listener.Service> services = new List<Listener.Service>();
223223
services.Add(local);
224224
services.Add(_pacServer);
225-
services.Add(new PortForwarder(8123));
225+
services.Add(new PortForwarder(_config.localPort));
226226
_listener = new Listener(services);
227227
_listener.Start(_config);
228228
}

shadowsocks-csharp/Data/polipo_config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
proxyAddress = "__POLIPO_BIND_IP__"
1+
proxyAddress = "__POLIPO_BIND_IP__"
2+
proxyPort = 8123
23

34
socksParentProxy = "127.0.0.1:__SOCKS_PORT__"
45
socksProxyType = socks5

0 commit comments

Comments
 (0)