Browse Source

Fix privoxy listening port conflict (#920)

Let TCP stack find a free port for us

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
tags/3.3.6
Syrone Wong Noisyfox 8 years ago
parent
commit
1e7a8de34a
1 changed files with 9 additions and 24 deletions
  1. +9
    -24
      shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs

+ 9
- 24
shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs View File

@@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
@@ -41,17 +42,10 @@ namespace Shadowsocks.Controller
}
}
public int RunningPort
{
get
{
return _runningPort;
}
}
public int RunningPort => _runningPort;
public void Start(Configuration configuration)
{
Server server = configuration.GetCurrentServer();
if (_process == null)
{
Process[] existingPrivoxy = Process.GetProcessesByName("ss_privoxy");
@@ -90,6 +84,7 @@ namespace Shadowsocks.Controller
if (_process != null)
{
KillProcess(_process);
_process.Dispose();
_process = null;
}
RefreshTrayArea();
@@ -160,21 +155,12 @@ namespace Shadowsocks.Controller
int defaultPort = 8123;
try
{
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++)
{
if (!usedPorts.Contains(port))
{
return port;
}
}
// TCP stack please do me a favor
TcpListener l = new TcpListener(IPAddress.Loopback, 0);
l.Start();
var port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
return port;
}
catch (Exception e)
{
@@ -182,7 +168,6 @@ namespace Shadowsocks.Controller
Logging.LogUsefulException(e);
return defaultPort;
}
throw new Exception("No free port found.");
}
[StructLayout(LayoutKind.Sequential)]


Loading…
Cancel
Save