Skip to content

Commit 4a830f9

Browse files
committed
fix the logic of port occupy testing in linux.
1 parent 70c170a commit 4a830f9

4 files changed

Lines changed: 63 additions & 18 deletions

File tree

src/NSmartProxy.Infrastructure/NetworkUtil.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ public static int[] FindAvailableTCPPorts(int startPort, int PortCount)
8787
continue;
8888
}
8989
foreach (IPEndPoint endPoint in endPoints)
90-
{
91-
if (endPoint.Port != port) continue;
92-
isAvailable = false;
93-
break;
90+
{//判断规则 :0000或者三冒号打头,ip占用,才算占用,否则pass
91+
//因为linux下 出现192.168.0.106:8787的记录,也会被误判为端口被占用
92+
if (endPoint.Address.ToString() == "0.0.0.0" || endPoint.Address.ToString() == ":::")
93+
{
94+
if (endPoint.Port == port)
95+
{
96+
isAvailable = false;
97+
break;
98+
}
99+
}
94100
}
95101

96102
} while (!isAvailable && port < IPEndPoint.MaxPort);
@@ -127,7 +133,7 @@ public static List<int> FindUnAvailableTCPPorts(List<int> ports)
127133
ipGlobalProperties.GetActiveTcpListeners();
128134
for (int i = ports.Count - 1; i > -1; i--)
129135
{
130-
if (ports[i] > 65535 || ports[i] < 1|| _usedPorts.Contains(ports[i]))
136+
if (ports[i] > 65535 || ports[i] < 1 || _usedPorts.Contains(ports[i]))
131137
{
132138
usedPortList.Add(ports[i]);
133139
ports.Remove(ports[i]);

src/NSmartProxy/Authorize/NSPServerContext.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ public void CloseAllSourceByClient(int clientId, bool addToBanlist = false)
106106
if (nspAppGroup.IsAllClosed())
107107
{
108108
Server.Logger.Info($"端口{port}所有app退出,侦听终止");
109-
//如果port内所有的app全都移除,才关闭listener
110-
nspAppGroup.Listener.Server.Close();
111-
nspAppGroup.Listener.Stop();
109+
if (nspAppGroup.Listener != null)
110+
{
111+
//如果port内所有的app全都移除,才关闭listener
112+
nspAppGroup.Listener.Server.NoDelay = true;
113+
nspAppGroup.Listener.Server.Close();
114+
nspAppGroup.Listener.Stop();
115+
}
116+
112117
PortAppMap.Remove(port);
113118
}
114119
}

src/NSmartProxy/NSPApp.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,34 @@ public int Close()
7171
{
7272
if (!_closed)
7373
{
74-
int ClosedCount = 0;
74+
int closedCount = 0;
7575
try
7676
{
77-
Tunnels.ForEach((t) =>
77+
foreach (var t in Tunnels)
7878
{
79-
t.ClientServerClient?.Close();
80-
t.ConsumerClient?.Close();
81-
ClosedCount++;
82-
});
79+
//TODO 3调试用
80+
//Console.WriteLine("XXX");
81+
////Console.WriteLine(t.ConsumerClient?.Client.LocalEndPoint);
82+
//Console.WriteLine("XXX");
83+
if (t.ClientServerClient != null && t.ClientServerClient.Connected)
84+
{
85+
t.ClientServerClient.Close();
86+
}
87+
88+
89+
if (t.ConsumerClient != null && t.ConsumerClient.Connected)
90+
{
91+
//关闭会直接出timewat
92+
t.ConsumerClient.LingerState.Enabled = true;
93+
t.ConsumerClient.LingerState.LingerTime = 0;
94+
t.ConsumerClient.NoDelay = true;
95+
//t.ConsumerClient.Client.Shutdown(SocketShutdown.Both);
96+
t.ConsumerClient.Close();
97+
}
98+
99+
closedCount++;
100+
}
101+
83102
//关闭循环和当前的侦听
84103
CancelListenSource?.Cancel();
85104
//Listener?.Stop();//TODO 3 逻辑错误!这个侦听可能还共享给了其他的app
@@ -89,7 +108,7 @@ public int Close()
89108
TcpClientBlocks.Receive().Close();
90109
}
91110
_closed = true;
92-
return ClosedCount;
111+
return closedCount;
93112
}
94113
catch (Exception ex)
95114
{

src/NSmartProxy/Server.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
208208
{
209209
Server.Logger.Debug("AppTcpClientMapConfigConnected");
210210
int port = 0;
211-
Protocol protocol;
211+
//Protocol protocol;
212212
//string host = "";
213213
//TODO 如果有host 则分配到相同的group中
214214
foreach (var kv in ServerContext.PortAppMap)
@@ -217,7 +217,7 @@ private void ConnectionManager_AppAdded(object sender, AppChangedEventArgs e)
217217
kv.Value.ActivateApp.ClientId == e.App.ClientId)
218218
{
219219
port = kv.Value.ActivateApp.ConsumePort;
220-
protocol = kv.Value.ActivateApp.AppProtocol;
220+
//protocol = kv.Value.ActivateApp.AppProtocol;
221221
break;
222222
}
223223

@@ -261,7 +261,11 @@ async Task ListenConsumeAsync(int consumerPort)
261261
}
262262
catch (ObjectDisposedException ode)
263263
{
264-
Logger.Debug($"外网端口{consumerPort}侦听时被外部终止" + ode);
264+
_ = ode;
265+
Logger.Debug($"外网端口{consumerPort}侦听时被外部终止");
266+
//#if DEBUG
267+
// Logger.Debug("详细信息:" + ode);
268+
//#endif
265269
}
266270
catch (Exception ex)
267271
{
@@ -350,6 +354,17 @@ private async Task ProcessConsumeRequestAsync(int consumerPort, string clientApp
350354
}
351355
finally
352356
{
357+
//if (consumerClient.Client.Connected)
358+
//{
359+
// consumerClient.Client.LingerState = new LingerOption(false, 0);
360+
//}
361+
362+
//if (s2pClient.Client.Connected)
363+
//{
364+
// s2pClient.Client.LingerState = new LingerOption(false, 0);
365+
//}
366+
367+
353368
consumerClient.Close();
354369
s2pClient.Close();
355370
transfering.Cancel();

0 commit comments

Comments
 (0)