This commit is contained in:
snltty
2024-08-07 11:21:59 +08:00
parent afc14044d3
commit 38ef976e13
6 changed files with 32 additions and 20 deletions

View File

@@ -7,8 +7,9 @@ sidebar_position: 1
:::tip[说明] :::tip[说明]
1. 各个设备的`网卡IP`,不要一样,要同一网段,且不应该使用`1``255` 1. 各个设备的`网卡IP`,不要一样,要同一网段,且不应该使用`1``255`
2. `局域网IP`,是选填的,可以不填,不懂是啥时,不要填、不要填、不要填 2. 请使用 `10.0.0.0 - 10.255.255.255``172.16.0.0 - 172.31.255.255``192.168.0.0 - 192.168.255.255` 范围内的IP
3. 虽然支持UDP广播但是UDP广播不会去主动连接所有设备所以你可以先 ping 以下对方,让两端相互连接 3. `局域网IP`,是选填的,可以不填,不懂是啥时,不要填、不要填、不要填
4. 虽然支持UDP广播但是UDP广播不会去主动连接所有设备所以你可以先 ping 以下对方,让两端相互连接
::: :::

View File

@@ -76,7 +76,7 @@ namespace linker.libs
} }
else else
{ {
LoggerHelper.Instance.Warning(error); LoggerHelper.Instance.Warning($"file:{fileName},arg:{arg},commands:{string.Join(Environment.NewLine,commands)} -> {error}");
} }
proc.WaitForExit(); proc.WaitForExit();
proc.Close(); proc.Close();

View File

@@ -1,4 +1,5 @@
using System.Net; using System.Net;
using System.Net.Sockets;
namespace linker.tun namespace linker.tun
{ {
@@ -55,7 +56,7 @@ namespace linker.tun
/// <param name="ips"></param> /// <param name="ips"></param>
/// <param name="ip"></param> /// <param name="ip"></param>
/// <param name="gateway">是不是网关是网关将使用NAT转发不是网关将添加路由</param> /// <param name="gateway">是不是网关是网关将使用NAT转发不是网关将添加路由</param>
public void AddRoute(LinkerTunDeviceRouteItem[] ips, IPAddress ip,bool gateway); public void AddRoute(LinkerTunDeviceRouteItem[] ips, IPAddress ip, bool gateway);
/// <summary> /// <summary>
/// 删除路由 /// 删除路由
/// </summary> /// </summary>
@@ -114,6 +115,24 @@ namespace linker.tun
/// 原始IP包 /// 原始IP包
/// </summary> /// </summary>
public ReadOnlyMemory<byte> IPPacket; public ReadOnlyMemory<byte> IPPacket;
public void Unpacket(ReadOnlyMemory<byte> buffer)
{
Packet = buffer;
IPPacket = buffer.Slice(4);
Version = (byte)(IPPacket.Span[0] >> 4 & 0b1111);
if (Version == 4)
{
SourceIPAddress = IPPacket.Slice(12, 4);
DistIPAddress = IPPacket.Slice(16, 4);
}
else if (Version == 6)
{
SourceIPAddress = IPPacket.Slice(8, 16);
DistIPAddress = IPPacket.Slice(24, 16);
}
}
} }
/// <summary> /// <summary>

View File

@@ -191,21 +191,8 @@ namespace linker.tun
LinkerTunDevicPacket packet = new LinkerTunDevicPacket(); LinkerTunDevicPacket packet = new LinkerTunDevicPacket();
packet.Packet = buffer; packet.Unpacket(buffer);
packet.IPPacket = buffer.Slice(4);
packet.Version = (byte)(packet.IPPacket.Span[0] >> 4 & 0b1111);
if (packet.Version == 4)
{
packet.SourceIPAddress = packet.IPPacket.Slice(12, 4);
packet.DistIPAddress = packet.IPPacket.Slice(16, 4);
}
else if (packet.Version == 6)
{
packet.SourceIPAddress = packet.IPPacket.Slice(8, 16);
packet.DistIPAddress = packet.IPPacket.Slice(24, 16);
}
await linkerTunDeviceCallback.Callback(packet).ConfigureAwait(false); await linkerTunDeviceCallback.Callback(packet).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -156,6 +156,7 @@ namespace linker.plugins.relay.transport
{ {
LoggerHelper.Instance.Error(ex); LoggerHelper.Instance.Error(ex);
} }
callback(null);
} }
return false; return false;
} }

View File

@@ -10,6 +10,7 @@ using linker.plugins.tuntap.config;
using linker.tun; using linker.tun;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Net.Sockets; using System.Net.Sockets;
using System.Net;
namespace linker.plugins.tuntap.proxy namespace linker.plugins.tuntap.proxy
{ {
@@ -80,7 +81,7 @@ namespace linker.plugins.tuntap.proxy
{ {
if (connections.IsEmpty == false) if (connections.IsEmpty == false)
{ {
await Task.WhenAll(connections.Values.Select(c => c.SendAsync(packet.Packet))); await Task.WhenAll(connections.Values.Where(c => c != null && c.Connected).Select(c => c.SendAsync(packet.Packet)));
} }
} }
else else
@@ -97,13 +98,16 @@ namespace linker.plugins.tuntap.proxy
{ {
await connection.SendAsync(packet.Packet); await connection.SendAsync(packet.Packet);
} }
else
{
}
} }
} }
else if (packet.Version == 6 && (packet.DistIPAddress.Span[0] & 0xFF) == 0xFF) else if (packet.Version == 6 && (packet.DistIPAddress.Span[0] & 0xFF) == 0xFF)
{ {
if (connections.IsEmpty == false) if (connections.IsEmpty == false)
{ {
await Task.WhenAll(connections.Values.Select(c => c.SendAsync(packet.Packet))); await Task.WhenAll(connections.Values.Where(c => c != null && c.Connected).Select(c => c.SendAsync(packet.Packet)));
} }
} }
} }