mirror of
https://github.com/snltty/linker.git
synced 2025-12-24 12:38:04 +08:00
sync
This commit is contained in:
@@ -6,7 +6,8 @@ using common.libs;
|
||||
using common.libs.extends;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
|
||||
namespace cmonitor.tunnel
|
||||
@@ -137,6 +138,7 @@ namespace cmonitor.tunnel
|
||||
SSL = transportItem.SSL
|
||||
};
|
||||
OnConnecting(tunnelTransportInfo);
|
||||
ParseRemoteEndPoint(tunnelTransportInfo);
|
||||
ITunnelConnection connection = await transport.ConnectAsync(tunnelTransportInfo);
|
||||
if (connection != null)
|
||||
{
|
||||
@@ -177,6 +179,7 @@ namespace cmonitor.tunnel
|
||||
if (_transports != null)
|
||||
{
|
||||
OnConnectBegin(tunnelTransportInfo);
|
||||
ParseRemoteEndPoint(tunnelTransportInfo);
|
||||
_transports.OnBegin(tunnelTransportInfo).ContinueWith((result) =>
|
||||
{
|
||||
connectingDic.TryRemove(tunnelTransportInfo.Remote.MachineId, out _);
|
||||
@@ -283,5 +286,46 @@ namespace cmonitor.tunnel
|
||||
{
|
||||
Logger.Instance.Error($"tunnel connect {tunnelTransportInfo.Remote.MachineId} fail->{tunnelTransportInfo.ToJsonFormat()}");
|
||||
}
|
||||
|
||||
private void ParseRemoteEndPoint(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//要连接哪些IP
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
|
||||
//先尝试内网ipv4
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetwork))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//在尝试外网
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//再尝试IPV6
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetworkV6))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
tunnelTransportInfo.RemoteEndPoints = eps;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ namespace cmonitor.tunnel.connection
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"TransactionId:{TransactionId},TransportName:{TransportName},ProtocolType:{ProtocolType},Type:{Type},Direction:{Direction},IPEndPoint:{IPEndPoint},RemoteMachineId:{RemoteMachineId},RemoteMachineName:{RemoteMachineName}";
|
||||
return this.ToJsonFormat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using cmonitor.tunnel.connection;
|
||||
using System.Net;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace cmonitor.tunnel.transport
|
||||
{
|
||||
@@ -133,8 +134,13 @@ namespace cmonitor.tunnel.transport
|
||||
/// 方向
|
||||
/// </summary>
|
||||
public TunnelDirection Direction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 需要加密
|
||||
/// </summary>
|
||||
public bool SSL { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public List<IPEndPoint> RemoteEndPoints { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -163,45 +163,9 @@ namespace cmonitor.tunnel.transport
|
||||
/// <returns></returns>
|
||||
private async Task<ITunnelConnection> ConnectForward(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//要连接哪些IP
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
|
||||
//先尝试内网ipv4
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetwork))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//在尝试外网
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//再尝试IPV6
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetworkV6))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
if (Logger.Instance.LoggerLevel <= LoggerTypes.DEBUG)
|
||||
{
|
||||
Logger.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {string.Join("\r\n", eps.Select(c => c.ToString()))}");
|
||||
Logger.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {string.Join("\r\n", tunnelTransportInfo.RemoteEndPoints.Select(c => c.ToString()))}");
|
||||
}
|
||||
|
||||
IPEndPoint local = new IPEndPoint(tunnelTransportInfo.Local.Local.Address, tunnelTransportInfo.Local.Local.Port);
|
||||
@@ -210,7 +174,7 @@ namespace cmonitor.tunnel.transport
|
||||
(UdpClient remoteUdp, UdpClient remoteUdp6) = BindListen(local, taskCompletionSource);
|
||||
|
||||
//给远端发送一些消息
|
||||
foreach (IPEndPoint ep in eps)
|
||||
foreach (IPEndPoint ep in tunnelTransportInfo.RemoteEndPoints)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -298,7 +262,10 @@ namespace cmonitor.tunnel.transport
|
||||
}
|
||||
try
|
||||
{
|
||||
remoteUdp.Close();
|
||||
remoteUdp?.Close();
|
||||
remoteUdp?.Close();
|
||||
remoteUdp6?.Close();
|
||||
remoteUdp6?.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -311,35 +278,8 @@ namespace cmonitor.tunnel.transport
|
||||
/// <param name="tunnelTransportInfo"></param>
|
||||
private void BindAndTTL(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//给对方发送TTL消息
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
foreach (IPAddress item in localIps)
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
IPEndPoint local = new IPEndPoint(tunnelTransportInfo.Local.Local.Address, tunnelTransportInfo.Local.Local.Port);
|
||||
foreach (var ip in eps)
|
||||
foreach (var ip in tunnelTransportInfo.RemoteEndPoints)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -666,7 +606,7 @@ namespace cmonitor.tunnel.transport
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 收到连接失败
|
||||
/// </summary>
|
||||
|
||||
@@ -140,47 +140,12 @@ namespace cmonitor.tunnel.transport
|
||||
|
||||
private async Task<ITunnelConnection> ConnectForward(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//要连接哪些IP
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
//先尝试内网ipv4
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetwork))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//在尝试外网
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//再尝试IPV6
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetworkV6))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
if (Logger.Instance.LoggerLevel <= LoggerTypes.DEBUG)
|
||||
{
|
||||
Logger.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {string.Join("\r\n", eps.Select(c => c.ToString()))}");
|
||||
Logger.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {string.Join("\r\n", tunnelTransportInfo.RemoteEndPoints.Select(c => c.ToString()))}");
|
||||
}
|
||||
|
||||
foreach (IPEndPoint ep in eps)
|
||||
foreach (IPEndPoint ep in tunnelTransportInfo.RemoteEndPoints)
|
||||
{
|
||||
Socket targetSocket = new(ep.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
|
||||
try
|
||||
@@ -234,34 +199,7 @@ namespace cmonitor.tunnel.transport
|
||||
}
|
||||
private void BindAndTTL(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//给对方发送TTL消息
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
foreach (IPAddress item in localIps)
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
//过滤掉不支持IPV6的情况
|
||||
IEnumerable<Socket> sockets = eps.Select(ip =>
|
||||
IEnumerable<Socket> sockets = tunnelTransportInfo.RemoteEndPoints.Select(ip =>
|
||||
{
|
||||
Socket targetSocket = new(ip.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
|
||||
try
|
||||
|
||||
@@ -95,45 +95,10 @@ namespace cmonitor.tunnel.transport
|
||||
|
||||
private async Task<ITunnelConnection> ConnectForward(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//要连接哪些IP
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
|
||||
//先尝试内网ipv4
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetwork))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//在尝试外网
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//再尝试IPV6
|
||||
foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetworkV6))
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
if (Logger.Instance.LoggerLevel <= LoggerTypes.DEBUG)
|
||||
{
|
||||
Logger.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {string.Join("\r\n", eps.Select(c => c.ToString()))}");
|
||||
Logger.Instance.Warning($"{Name} connect to {tunnelTransportInfo.Remote.MachineId}->{tunnelTransportInfo.Remote.MachineName} {string.Join("\r\n", tunnelTransportInfo.RemoteEndPoints.Select(c => c.ToString()))}");
|
||||
}
|
||||
|
||||
IPEndPoint local = new IPEndPoint(tunnelTransportInfo.Local.Local.Address, tunnelTransportInfo.Local.Local.Port);
|
||||
@@ -141,7 +106,7 @@ namespace cmonitor.tunnel.transport
|
||||
//接收远端数据,收到了就是成功了
|
||||
(UdpClient remoteUdp, UdpClient remoteUdp6) = BindListen(local, taskCompletionSource);
|
||||
|
||||
foreach (IPEndPoint ep in eps)
|
||||
foreach (IPEndPoint ep in tunnelTransportInfo.RemoteEndPoints)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -208,7 +173,10 @@ namespace cmonitor.tunnel.transport
|
||||
}
|
||||
try
|
||||
{
|
||||
remoteUdp.Close();
|
||||
remoteUdp?.Close();
|
||||
remoteUdp?.Close();
|
||||
remoteUdp6?.Close();
|
||||
remoteUdp6?.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -342,35 +310,8 @@ namespace cmonitor.tunnel.transport
|
||||
}
|
||||
private void BindAndTTL(TunnelTransportInfo tunnelTransportInfo)
|
||||
{
|
||||
//给对方发送TTL消息
|
||||
IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
|
||||
List<IPEndPoint> eps = new List<IPEndPoint>();
|
||||
foreach (IPAddress item in localIps)
|
||||
{
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
|
||||
eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
|
||||
}
|
||||
|
||||
eps.AddRange(new List<IPEndPoint>{
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port),
|
||||
new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
|
||||
});
|
||||
//本机有V6
|
||||
bool hasV6 = tunnelTransportInfo.Local.LocalIps.Any(c => c.AddressFamily == AddressFamily.InterNetworkV6);
|
||||
//本机的局域网ip和外网ip
|
||||
List<IPAddress> localLocalIps = tunnelTransportInfo.Local.LocalIps.Concat(new List<IPAddress> { tunnelTransportInfo.Local.Remote.Address }).ToList();
|
||||
eps = eps
|
||||
//对方是V6,本机也得有V6
|
||||
.Where(c => (c.AddressFamily == AddressFamily.InterNetworkV6 && hasV6) || c.AddressFamily == AddressFamily.InterNetwork)
|
||||
//端口和本机端口一样,那不应该是换回地址
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && c.Address.Equals(IPAddress.Loopback)) == false)
|
||||
//端口和本机端口一样。那不应该是本机的IP
|
||||
.Where(c => (c.Port == tunnelTransportInfo.Local.Local.Port && localLocalIps.Any(d => d.Equals(c.Address))) == false)
|
||||
.ToList();
|
||||
|
||||
IPEndPoint local = new IPEndPoint(tunnelTransportInfo.Local.Local.Address, tunnelTransportInfo.Local.Local.Port);
|
||||
foreach (var ip in eps)
|
||||
foreach (var ip in tunnelTransportInfo.RemoteEndPoints)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user