Skip to content

Commit

Permalink
fix the logic of port occupy testing in linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoonlight committed Oct 2, 2019
1 parent 70c170a commit 4a830f9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
16 changes: 11 additions & 5 deletions src/NSmartProxy.Infrastructure/NetworkUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ public static int[] FindAvailableTCPPorts(int startPort, int PortCount)
continue;
}
foreach (IPEndPoint endPoint in endPoints)
{
if (endPoint.Port != port) continue;
isAvailable = false;
break;
{//判断规则 :0000或者三冒号打头,ip占用,才算占用,否则pass
//因为linux下 出现192.168.0.106:8787的记录,也会被误判为端口被占用
if (endPoint.Address.ToString() == "0.0.0.0" || endPoint.Address.ToString() == ":::")
{
if (endPoint.Port == port)
{
isAvailable = false;
break;
}
}
}

} while (!isAvailable && port < IPEndPoint.MaxPort);
Expand Down Expand Up @@ -127,7 +133,7 @@ public static List<int> FindUnAvailableTCPPorts(List<int> ports)
ipGlobalProperties.GetActiveTcpListeners();
for (int i = ports.Count - 1; i > -1; i--)
{
if (ports[i] > 65535 || ports[i] < 1|| _usedPorts.Contains(ports[i]))
if (ports[i] > 65535 || ports[i] < 1 || _usedPorts.Contains(ports[i]))
{
usedPortList.Add(ports[i]);
ports.Remove(ports[i]);
Expand Down
11 changes: 8 additions & 3 deletions src/NSmartProxy/Authorize/NSPServerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ public void CloseAllSourceByClient(int clientId, bool addToBanlist = false)
if (nspAppGroup.IsAllClosed())
{
Server.Logger.Info($"端口{port}所有app退出,侦听终止");
//如果port内所有的app全都移除,才关闭listener
nspAppGroup.Listener.Server.Close();
nspAppGroup.Listener.Stop();
if (nspAppGroup.Listener != null)
{
//如果port内所有的app全都移除,才关闭listener
nspAppGroup.Listener.Server.NoDelay = true;
nspAppGroup.Listener.Server.Close();
nspAppGroup.Listener.Stop();
}

PortAppMap.Remove(port);
}
}
Expand Down
33 changes: 26 additions & 7 deletions src/NSmartProxy/NSPApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,34 @@ public int Close()
{
if (!_closed)
{
int ClosedCount = 0;
int closedCount = 0;
try
{
Tunnels.ForEach((t) =>
foreach (var t in Tunnels)
{
t.ClientServerClient?.Close();
t.ConsumerClient?.Close();
ClosedCount++;
});
//TODO 3调试用
//Console.WriteLine("XXX");
////Console.WriteLine(t.ConsumerClient?.Client.LocalEndPoint);
//Console.WriteLine("XXX");
if (t.ClientServerClient != null && t.ClientServerClient.Connected)
{
t.ClientServerClient.Close();
}


if (t.ConsumerClient != null && t.ConsumerClient.Connected)
{
//关闭会直接出timewat
t.ConsumerClient.LingerState.Enabled = true;
t.ConsumerClient.LingerState.LingerTime = 0;
t.ConsumerClient.NoDelay = true;
//t.ConsumerClient.Client.Shutdown(SocketShutdown.Both);
t.ConsumerClient.Close();
}

closedCount++;
}

//关闭循环和当前的侦听
CancelListenSource?.Cancel();
//Listener?.Stop();//TODO 3 逻辑错误!这个侦听可能还共享给了其他的app
Expand All @@ -89,7 +108,7 @@ public int Close()
TcpClientBlocks.Receive().Close();
}
_closed = true;
return ClosedCount;
return closedCount;
}
catch (Exception ex)
{
Expand Down
21 changes: 18 additions & 3 deletions src/NSmartProxy/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
{
Server.Logger.Debug("AppTcpClientMapConfigConnected");
int port = 0;
Protocol protocol;
//Protocol protocol;
//string host = "";
//TODO 如果有host 则分配到相同的group中
foreach (var kv in ServerContext.PortAppMap)
Expand All @@ -217,7 +217,7 @@ private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
kv.Value.ActivateApp.ClientId == e.App.ClientId)
{
port = kv.Value.ActivateApp.ConsumePort;
protocol = kv.Value.ActivateApp.AppProtocol;
//protocol = kv.Value.ActivateApp.AppProtocol;
break;
}

Expand Down Expand Up @@ -261,7 +261,11 @@ async Task ListenConsumeAsync(int consumerPort)
}
catch (ObjectDisposedException ode)
{
Logger.Debug($"外网端口{consumerPort}侦听时被外部终止" + ode);
_ = ode;
Logger.Debug($"外网端口{consumerPort}侦听时被外部终止");
//#if DEBUG
// Logger.Debug("详细信息:" + ode);
//#endif
}
catch (Exception ex)
{
Expand Down Expand Up @@ -350,6 +354,17 @@ private async Task ProcessConsumeRequestAsync(int consumerPort, string clientApp
}
finally
{
//if (consumerClient.Client.Connected)
//{
// consumerClient.Client.LingerState = new LingerOption(false, 0);
//}

//if (s2pClient.Client.Connected)
//{
// s2pClient.Client.LingerState = new LingerOption(false, 0);
//}


consumerClient.Close();
s2pClient.Close();
transfering.Cancel();
Expand Down

0 comments on commit 4a830f9

Please sign in to comment.