Skip to content

Commit

Permalink
add fallback for access denied
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Jan 18, 2015
1 parent 5463b28 commit b2b11bb
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions shadowsocks-csharp/Controller/PolipoRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,31 @@ public void Stop()

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++)
int defaultPort = 8123;
try
{
if (!usedPorts.Contains(port))
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 = defaultPort; port < 65535; port++)
{
return port;
if (!usedPorts.Contains(port))
{
return port;
}
}
}
catch (Exception e)
{
// in case access denied
Logging.LogUsefulException(e);
return defaultPort;
}
throw new Exception("No free port found.");
}
}
Expand Down

0 comments on commit b2b11bb

Please sign in to comment.