关闭端口

This commit is contained in:
snltty
2026-02-11 15:21:13 +08:00
parent 4dbef66652
commit 620ae43e99
6 changed files with 15 additions and 14 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ is_docker_running () {
DOCKER_NAME="linker" DOCKER_NAME="linker"
if [ -f "$FILE_PATH" ]; then if [ -f "$FILE_PATH" ]; then
DOCKER_NAME=$(cat $FILE_PATH | grep "linker" | awk -F ':' '{print $2}' | xargs) DOCKER_NAME=$(cat $FILE_PATH | grep "container_name" | awk -F ':' '{print $2}' | xargs)
echo "DOCKER_NAME is set to: $DOCKER_NAME" echo "DOCKER_NAME is set to: $DOCKER_NAME"
fi fi
+1 -1
View File
@@ -1,5 +1,5 @@
v1.9.9 v1.9.9
2026-02-03 22:40:58 2026-02-11 15:21:13
1. 一些累计更新,一些BUG修复 1. 一些累计更新,一些BUG修复
2. 国家级别的连接限制 2. 国家级别的连接限制
3. 修复一些地方未能正确释放端口的问题 3. 修复一些地方未能正确释放端口的问题
@@ -267,8 +267,8 @@ namespace linker.tunnel.transport
} }
try try
{ {
remoteUdp?.Close(); remoteUdp?.SafeClose();
remoteUdp?.Close(); remoteUdp?.SafeClose();
} }
catch (Exception) catch (Exception)
{ {
@@ -166,13 +166,13 @@ namespace linker.tunnel.transport
targetSocket.KeepAlive(); targetSocket.KeepAlive();
targetSocket.IPv6Only(ep.AddressFamily, false); targetSocket.IPv6Only(ep.AddressFamily, false);
targetSocket.ReuseBind(new IPEndPoint(ep.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, tunnelTransportInfo.Local.Local.Port)); targetSocket.ReuseBind(new IPEndPoint(ep.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, tunnelTransportInfo.Local.Local.Port));
if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG) if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG)
{ {
LoggerHelper.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {ep}"); LoggerHelper.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {ep}");
} }
await targetSocket.ConnectAsync(ep,cts.Token).ConfigureAwait(false); await targetSocket.ConnectAsync(ep, cts.Token).ConfigureAwait(false);
//需要ssl //需要ssl
SslStream sslStream = null; SslStream sslStream = null;
@@ -180,7 +180,8 @@ namespace linker.tunnel.transport
{ {
sslStream = new SslStream(new NetworkStream(targetSocket, false), false, ValidateServerCertificate, null); sslStream = new SslStream(new NetworkStream(targetSocket, false), false, ValidateServerCertificate, null);
#pragma warning disable SYSLIB0039 // 类型或成员已过时 #pragma warning disable SYSLIB0039 // 类型或成员已过时
await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions { await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
{
EnabledSslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, EnabledSslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls,
CertificateRevocationCheckMode = X509RevocationMode.NoCheck, CertificateRevocationCheckMode = X509RevocationMode.NoCheck,
ClientCertificates = new X509CertificateCollection { certificate } ClientCertificates = new X509CertificateCollection { certificate }
@@ -236,7 +237,7 @@ namespace linker.tunnel.transport
} }
return null; return null;
}); });
foreach (Socket item in sockets.Where(c => c != null && c.Connected == false)) foreach (Socket item in sockets.Where(c => c != null))
{ {
item.SafeClose(); item.SafeClose();
} }
@@ -282,7 +283,7 @@ namespace linker.tunnel.transport
return; return;
} }
sslStream = new SslStream(new NetworkStream(socket, false), false, ValidateServerCertificate,null); sslStream = new SslStream(new NetworkStream(socket, false), false, ValidateServerCertificate, null);
#pragma warning disable SYSLIB0039 // 类型或成员已过时 #pragma warning disable SYSLIB0039 // 类型或成员已过时
await sslStream.AuthenticateAsServerAsync(certificate, OperatingSystem.IsAndroid(), SslProtocols.Tls13 | SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, false).ConfigureAwait(false); await sslStream.AuthenticateAsServerAsync(certificate, OperatingSystem.IsAndroid(), SslProtocols.Tls13 | SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, false).ConfigureAwait(false);
#pragma warning restore SYSLIB0039 // 类型或成员已过时 #pragma warning restore SYSLIB0039 // 类型或成员已过时
@@ -145,6 +145,7 @@ namespace linker.tunnel.transport
} }
catch (Exception ex) catch (Exception ex)
{ {
socket.SafeClose();
if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG) if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG)
{ {
LoggerHelper.Instance.Error(ex); LoggerHelper.Instance.Error(ex);
@@ -113,18 +113,16 @@ namespace linker.tunnel.wanport
{ {
byte[] buffer = ArrayPool<byte>.Shared.Rent(1024); byte[] buffer = ArrayPool<byte>.Shared.Rent(1024);
using CancellationTokenSource cts = new CancellationTokenSource(5000); using CancellationTokenSource cts = new CancellationTokenSource(5000);
Socket socket = new Socket(server.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
socket.ReuseBind(new IPEndPoint(IPAddress.Any, 0));
try try
{ {
Socket socket = new Socket(server.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
socket.ReuseBind(new IPEndPoint(IPAddress.Any, 0));
await socket.ConnectAsync(server, cts.Token).ConfigureAwait(false); await socket.ConnectAsync(server, cts.Token).ConfigureAwait(false);
await socket.SendAsync(BuildSendData(buffer, (byte)new Random().Next(0, 255))).ConfigureAwait(false); await socket.SendAsync(BuildSendData(buffer, (byte)new Random().Next(0, 255))).ConfigureAwait(false);
int length = await socket.ReceiveAsync(buffer.AsMemory(), SocketFlags.None, cts.Token).ConfigureAwait(false); int length = await socket.ReceiveAsync(buffer.AsMemory(), SocketFlags.None, cts.Token).ConfigureAwait(false);
IPEndPoint localEP = socket.LocalEndPoint as IPEndPoint; IPEndPoint localEP = socket.LocalEndPoint as IPEndPoint;
socket.Close();
return new TunnelWanPortEndPoint { Local = localEP, Remote = UnpackRecvData(buffer, length) }; return new TunnelWanPortEndPoint { Local = localEP, Remote = UnpackRecvData(buffer, length) };
} }
catch (Exception ex) catch (Exception ex)
@@ -134,6 +132,7 @@ namespace linker.tunnel.wanport
} }
finally finally
{ {
socket.SafeClose();
ArrayPool<byte>.Shared.Return(buffer); ArrayPool<byte>.Shared.Return(buffer);
} }