From 0537febeeb56ea3e9de137f718bb47c8e1de3e62 Mon Sep 17 00:00:00 2001 From: snltty <1069410172@qq.com> Date: Tue, 17 Sep 2024 16:31:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0=E7=BC=9D=E5=88=87=E6=8D=A2=E9=9A=A7?= =?UTF-8?q?=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dotnet.yml | 2 +- linker/linker.csproj | 5 +-- .../plugins/forward/proxy/ForwardProxyTcp.cs | 22 ++----------- .../forward/proxy/ForwardProxyTunnel.cs | 32 ++++++++++++++----- linker/plugins/tuntap/proxy/TuntapProxy.cs | 9 +++++- version.txt | 7 ++-- 6 files changed, 43 insertions(+), 34 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index af824eb7..75a608fb 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -37,7 +37,7 @@ jobs: release_name: v1.3.8.${{ steps.date.outputs.today }} draft: false prerelease: false - body: "1. 优化隧道连接逻辑,先中继,成功则后台打洞,失败则立即打洞\r\n2. 测试中,请勿更新" + body: "1. 优化隧道连接逻辑,先中继,成功则后台打洞,后台打洞成功则切换隧道,中继失败则立即打洞\r\n2. 虚拟网卡,可能会丢失一两个包,TCP不会断开连接\r\n3. 端口转发,在发送数据包失败后,会尝试切换隧道再次发送,无缝切换隧道" - name: upload-win-x86-oss id: upload-win-x86-oss uses: tvrcgo/oss-action@v0.1.1 diff --git a/linker/linker.csproj b/linker/linker.csproj index d42cefa1..fcb4ba8e 100644 --- a/linker/linker.csproj +++ b/linker/linker.csproj @@ -19,8 +19,9 @@ linker snltty snltty - 1. 优化隧道连接逻辑,先中继,成功则后台打洞,失败则立即打洞 -2. 测试中,请勿更新 + 1. 优化隧道连接逻辑,先中继,成功则后台打洞,后台打洞成功则切换隧道,中继失败则立即打洞 +2. 虚拟网卡,可能会丢失一两个包,TCP不会断开连接 +3. 端口转发,在发送数据包失败后,会尝试切换隧道再次发送,无缝切换隧道 snltty https://github.com/snltty/linker https://github.com/snltty/linker diff --git a/linker/plugins/forward/proxy/ForwardProxyTcp.cs b/linker/plugins/forward/proxy/ForwardProxyTcp.cs index 52ee8952..db3e773e 100644 --- a/linker/plugins/forward/proxy/ForwardProxyTcp.cs +++ b/linker/plugins/forward/proxy/ForwardProxyTcp.cs @@ -183,7 +183,9 @@ namespace linker.plugins.forward.proxy await ConnectTunnelConnection(token).ConfigureAwait(false); res = await token.Connection.SendAsync(connectData.AsMemory(0, length)).ConfigureAwait(false); if (res == false) + { CloseClientSocket(token, 5); + } } } catch (Exception) @@ -232,6 +234,7 @@ namespace linker.plugins.forward.proxy Connection = state.Connection, Socket = state.Socket, Buffer = new byte[(1 << state.BufferSize) * 1024], + Proxy = new ProxyInfo { ConnectId = state.ConnectId, @@ -345,25 +348,6 @@ namespace linker.plugins.forward.proxy } token.Clear(); } - private void CloseClientSocketTcp(ITunnelConnection connection) - { - int hashcode1 = connection.RemoteMachineId.GetHashCode(); - int hashcode2 = connection.TransactionId.GetHashCode(); - var tokens = tcpConnections.Where(c => c.Key.hashcode1 == hashcode1 && c.Key.hashcode2 == hashcode2).ToList(); - foreach (var item in tokens) - { - try - { - if (tcpConnections.TryRemove(item.Key, out AsyncUserToken token)) - { - token.Clear(); - } - } - catch (Exception) - { - } - } - } private void StopTcp() { diff --git a/linker/plugins/forward/proxy/ForwardProxyTunnel.cs b/linker/plugins/forward/proxy/ForwardProxyTunnel.cs index 69e0becb..b6185a3b 100644 --- a/linker/plugins/forward/proxy/ForwardProxyTunnel.cs +++ b/linker/plugins/forward/proxy/ForwardProxyTunnel.cs @@ -89,12 +89,20 @@ namespace linker.plugins.forward.proxy /// private async ValueTask ConnectTunnelConnection(AsyncUserToken token) { - if (caches.TryGetValue(token.ListenPort, out ForwardProxyCacheInfo cache)) + if (token.ListenPort > 0) { - token.Proxy.TargetEP = cache.TargetEP; - cache.Connection = await ConnectTunnel(cache.MachineId, TunnelProtocolType.Udp).ConfigureAwait(false); - token.Connection = cache.Connection; + if (caches.TryGetValue(token.ListenPort, out ForwardProxyCacheInfo cache)) + { + token.Proxy.TargetEP = cache.TargetEP; + cache.Connection = await ConnectTunnel(cache.MachineId, TunnelProtocolType.Udp).ConfigureAwait(false); + token.Connection = cache.Connection; + } } + else if (token.Connection != null) + { + token.Connection = await ConnectTunnel(token.Connection.RemoteMachineId, TunnelProtocolType.Udp).ConfigureAwait(false); + } + return true; } /// @@ -104,12 +112,20 @@ namespace linker.plugins.forward.proxy /// private async ValueTask ConnectTunnelConnection(AsyncUserUdpToken token) { - if (caches.TryGetValue(token.ListenPort, out ForwardProxyCacheInfo cache)) + if (token.ListenPort > 0) { - token.Proxy.TargetEP = cache.TargetEP; - cache.Connection = await ConnectTunnel(cache.MachineId, TunnelProtocolType.Udp).ConfigureAwait(false); - token.Connection = cache.Connection; + if (caches.TryGetValue(token.ListenPort, out ForwardProxyCacheInfo cache)) + { + token.Proxy.TargetEP = cache.TargetEP; + cache.Connection = await ConnectTunnel(cache.MachineId, TunnelProtocolType.Udp).ConfigureAwait(false); + token.Connection = cache.Connection; + } } + else if (token.Connection != null) + { + token.Connection = await ConnectTunnel(token.Connection.RemoteMachineId, TunnelProtocolType.Udp).ConfigureAwait(false); + } + } /// diff --git a/linker/plugins/tuntap/proxy/TuntapProxy.cs b/linker/plugins/tuntap/proxy/TuntapProxy.cs index 93c76f93..b4d694f3 100644 --- a/linker/plugins/tuntap/proxy/TuntapProxy.cs +++ b/linker/plugins/tuntap/proxy/TuntapProxy.cs @@ -32,7 +32,14 @@ namespace linker.plugins.tuntap.proxy protected override void Connected(ITunnelConnection connection) { connection.BeginReceive(this, null); - ipConnections.Clear(); + + //有哪些目标IP用了相同目标隧道,更新一下 + List keys = ipConnections.Where(c => c.Value.RemoteMachineId == connection.RemoteMachineId).Select(c => c.Key).ToList(); + foreach (uint ip in keys) + { + ipConnections.AddOrUpdate(ip, connection, (a, b) => connection); + }; + //ipConnections.Clear(); } /// /// 收到隧道数据 diff --git a/version.txt b/version.txt index a9a1d55d..5fdfd6a2 100644 --- a/version.txt +++ b/version.txt @@ -1,4 +1,5 @@ v1.3.8 -2024-09-17 14:57:51 -1. 优化隧道连接逻辑,先中继,成功则后台打洞,失败则立即打洞 -2. 测试中,请勿更新 \ No newline at end of file +2024-09-17 16:31:53 +1. 优化隧道连接逻辑,先中继,成功则后台打洞,后台打洞成功则切换隧道,中继失败则立即打洞 +2. 虚拟网卡,可能会丢失一两个包,TCP不会断开连接 +3. 端口转发,在发送数据包失败后,会尝试切换隧道再次发送,无缝切换隧道 \ No newline at end of file