mirror of
https://github.com/snltty/linker.git
synced 2025-10-07 01:52:51 +08:00
整理代码,解耦和可测试性
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
using linker.libs;
|
||||
using linker.tunnel.transport;
|
||||
using Mono.Nat;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
|
||||
namespace linker.tunnel
|
||||
{
|
||||
@@ -17,13 +15,8 @@ namespace linker.tunnel
|
||||
|
||||
public MapInfo PortMap => MapInfo1 ?? MapInfo;
|
||||
|
||||
private readonly TransportTcpPortMap transportTcpPortMap;
|
||||
private readonly TransportUdpPortMap transportUdpPortMap;
|
||||
public TunnelUpnpTransfer(TransportTcpPortMap transportTcpPortMap, TransportUdpPortMap transportUdpPortMap)
|
||||
public TunnelUpnpTransfer()
|
||||
{
|
||||
this.transportTcpPortMap = transportTcpPortMap;
|
||||
this.transportUdpPortMap = transportUdpPortMap;
|
||||
|
||||
NatUtility.DeviceFound += DeviceFound;
|
||||
NatUtility.StartDiscovery();
|
||||
LoopDiscovery();
|
||||
@@ -101,20 +94,14 @@ namespace linker.tunnel
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetMap(IPAddress lanIP, int privatePort)
|
||||
public void SetMap(int privatePort)
|
||||
{
|
||||
int ip = lanIP.GetAddressBytes()[3];
|
||||
MapInfo = new MapInfo { PrivatePort = privatePort, PublicPort = privatePort + ip };
|
||||
MapInfo = new MapInfo { PrivatePort = privatePort, PublicPort = privatePort };
|
||||
AddMap();
|
||||
|
||||
_ = transportTcpPortMap.Listen(privatePort);
|
||||
_ = transportUdpPortMap.Listen(privatePort);
|
||||
}
|
||||
public void SetMap(int privatePort, int publicPort)
|
||||
{
|
||||
MapInfo1 = new MapInfo { PrivatePort = privatePort, PublicPort = publicPort };
|
||||
_ = transportTcpPortMap.Listen(privatePort);
|
||||
_ = transportUdpPortMap.Listen(privatePort);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -55,8 +55,14 @@ namespace linker.tunnel.transport
|
||||
|
||||
|
||||
Socket socket;
|
||||
SemaphoreSlim slim = new SemaphoreSlim(1);
|
||||
public async Task Listen(int localPort)
|
||||
{
|
||||
await slim.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (localPort == 0) return;
|
||||
|
||||
if (socket != null && (socket.LocalEndPoint as IPEndPoint).Port == localPort)
|
||||
{
|
||||
if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG)
|
||||
@@ -66,17 +72,15 @@ namespace linker.tunnel.transport
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
socket?.SafeClose();
|
||||
if (localPort == 0) return;
|
||||
|
||||
IPAddress localIP = IPAddress.Any;
|
||||
|
||||
socket = new Socket(localIP.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
|
||||
socket.IPv6Only(localIP.AddressFamily, false);
|
||||
socket.ReuseBind(new IPEndPoint(localIP, localPort));
|
||||
socket.Listen(int.MaxValue);
|
||||
Socket _socket = new Socket(localIP.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
|
||||
_socket.IPv6Only(localIP.AddressFamily, false);
|
||||
_socket.ReuseBind(new IPEndPoint(localIP, localPort));
|
||||
_socket.Listen(int.MaxValue);
|
||||
socket = _socket;
|
||||
|
||||
if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG)
|
||||
{
|
||||
@@ -126,6 +130,10 @@ namespace linker.tunnel.transport
|
||||
LoggerHelper.Instance.Error(ex);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
slim.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ITunnelConnection> ConnectAsync(TunnelTransportInfo tunnelTransportInfo)
|
||||
|
@@ -11,7 +11,6 @@ namespace linker.plugins.sforward
|
||||
public string Name => "sforward";
|
||||
public VersionManager SyncVersion { get; } = new VersionManager();
|
||||
public VersionManager DataVersion { get; } = new VersionManager();
|
||||
|
||||
public ConcurrentDictionary<string, int> CountDic { get; } = new ConcurrentDictionary<string, int>();
|
||||
|
||||
private readonly ClientConfigTransfer clientConfigTransfer;
|
||||
@@ -22,6 +21,11 @@ namespace linker.plugins.sforward
|
||||
this.sForwardTransfer = sForwardTransfer;
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
SyncVersion.Add();
|
||||
}
|
||||
|
||||
public Memory<byte> GetData()
|
||||
{
|
||||
CountInfo info = new CountInfo { MachineId = clientConfigTransfer.Id, Count = sForwardTransfer.Count };
|
||||
@@ -44,10 +48,6 @@ namespace linker.plugins.sforward
|
||||
}
|
||||
DataVersion.Add();
|
||||
}
|
||||
public void Refresh()
|
||||
{
|
||||
SyncVersion.Add();
|
||||
}
|
||||
}
|
||||
|
||||
[MemoryPackable]
|
||||
|
@@ -24,6 +24,10 @@ namespace linker.plugins.tunnel
|
||||
private readonly TunnelUpnpTransfer tunnelUpnpTransfer;
|
||||
private readonly TunnelTransfer tunnelTransfer;
|
||||
|
||||
|
||||
private readonly TransportTcpPortMap transportTcpPortMap;
|
||||
private readonly TransportUdpPortMap transportUdpPortMap;
|
||||
|
||||
public TunnelAdapter(ClientSignInState clientSignInState, IMessengerSender messengerSender,
|
||||
TunnelExcludeIPTransfer excludeIPTransfer, ClientConfigTransfer clientConfigTransfer, TunnelConfigTransfer tunnelConfigTransfer,
|
||||
TunnelWanPortTransfer tunnelWanPortTransfer, TunnelUpnpTransfer tunnelUpnpTransfer, TunnelTransfer tunnelTransfer)
|
||||
@@ -54,12 +58,14 @@ namespace linker.plugins.tunnel
|
||||
tunnelTransfer.SendConnectBegin = SendConnectBegin;
|
||||
tunnelTransfer.SendConnectFail = SendConnectFail;
|
||||
tunnelTransfer.SendConnectSuccess = SendConnectSuccess;
|
||||
transportTcpPortMap = new TransportTcpPortMap();
|
||||
transportUdpPortMap = new TransportUdpPortMap();
|
||||
tunnelTransfer.LoadTransports(tunnelWanPortTransfer, tunnelUpnpTransfer, new List<ITunnelTransport> {
|
||||
new TunnelTransportTcpNutssb(),
|
||||
new TransportMsQuic(),
|
||||
new TransportTcpP2PNAT(),
|
||||
new TransportTcpPortMap(),
|
||||
new TransportUdpPortMap(),
|
||||
transportTcpPortMap,
|
||||
transportUdpPortMap,
|
||||
new TransportUdp(),
|
||||
});
|
||||
|
||||
@@ -149,11 +155,21 @@ namespace linker.plugins.tunnel
|
||||
if (tunnelConfigTransfer.PortMapLan > 0)
|
||||
{
|
||||
tunnelUpnpTransfer.SetMap(tunnelConfigTransfer.PortMapLan, tunnelConfigTransfer.PortMapWan);
|
||||
_ = transportTcpPortMap.Listen(tunnelConfigTransfer.PortMapLan);
|
||||
_ = transportUdpPortMap.Listen(tunnelConfigTransfer.PortMapLan);
|
||||
}
|
||||
else
|
||||
{
|
||||
tunnelUpnpTransfer.SetMap(clientSignInState.Connection.LocalAddress.Address, 18180);
|
||||
}
|
||||
if (clientSignInState.Connected)
|
||||
{
|
||||
int ip = clientSignInState.Connection.LocalAddress.Address.GetAddressBytes()[3];
|
||||
tunnelUpnpTransfer.SetMap(18180 + ip);
|
||||
|
||||
_ = transportTcpPortMap.Listen(18180 + ip);
|
||||
_ = transportUdpPortMap.Listen(18180 + ip);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,10 +19,6 @@ namespace linker.plugins.tunnel
|
||||
this.tunnelConfigTransfer = tunnelConfigTransfer;
|
||||
tunnelConfigTransfer.OnChanged += Refresh;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新关于隧道的配置信息,也就是获取自己的和别的客户端的,方便查看
|
||||
/// </summary>
|
||||
public void Refresh()
|
||||
{
|
||||
SyncVersion.Add();
|
||||
|
@@ -1,7 +1,6 @@
|
||||
using linker.libs;
|
||||
using linker.plugins.client;
|
||||
using linker.plugins.route;
|
||||
using linker.plugins.tunnel;
|
||||
using linker.plugins.tuntap.config;
|
||||
using linker.tun;
|
||||
using System.Net;
|
||||
|
@@ -14,66 +14,7 @@ namespace linker.startup
|
||||
/// <param name="assemblies"></param>
|
||||
public static void Init(FileConfig config)
|
||||
{
|
||||
List<IStartup> temps = GetSourceGeneratorInstances().OrderByDescending(c => c.Level).ToList();
|
||||
TestDependent(temps);
|
||||
LoadPlugins(config, temps);
|
||||
}
|
||||
/// <summary>
|
||||
/// 检查插件依赖
|
||||
/// </summary>
|
||||
/// <param name="temps"></param>
|
||||
private static void TestDependent(List<IStartup> temps)
|
||||
{
|
||||
IEnumerable<string> names = temps.Select(c => c.Name);
|
||||
foreach (IStartup item in temps.Where(c => c.Dependent.Length > 0))
|
||||
{
|
||||
IEnumerable<string> excepts = item.Dependent.Except(names);
|
||||
if (excepts.Any())
|
||||
{
|
||||
LoggerHelper.Instance.Error($"【{item.Name}】dependent by {string.Join(",", excepts)},but it not exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 加载插件
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="temps"></param>
|
||||
private static void LoadPlugins(FileConfig config, List<IStartup> temps)
|
||||
{
|
||||
//只要哪些
|
||||
if (config.Data.Common.IncludePlugins.Length > 0)
|
||||
{
|
||||
temps = temps.Where(c => c.Required || config.Data.Common.IncludePlugins.Contains(c.Name)).ToList();
|
||||
}
|
||||
//不要哪些
|
||||
else if (config.Data.Common.ExcludePlugins.Length > 0)
|
||||
{
|
||||
temps = temps.Where(c => c.Required || config.Data.Common.ExcludePlugins.Contains(c.Name) == false).ToList();
|
||||
}
|
||||
|
||||
LoadDependents(temps, temps.Select(c => c.Name));
|
||||
startups = startups.Distinct().ToList();
|
||||
|
||||
config.Data.Common.Plugins = startups.Select(c => c.Name).ToArray();
|
||||
|
||||
LoggerHelper.Instance.Info($"load startup : {string.Join(",", startups.Select(c => c.GetType().Name))}");
|
||||
}
|
||||
/// <summary>
|
||||
/// 加载插件依赖
|
||||
/// </summary>
|
||||
/// <param name="all"></param>
|
||||
/// <param name="sependents"></param>
|
||||
private static void LoadDependents(List<IStartup> all, IEnumerable<string> sependents)
|
||||
{
|
||||
if (sependents.Any() == false) return;
|
||||
|
||||
IEnumerable<IStartup> temps = all.Where(c => sependents.Contains(c.Name));
|
||||
IEnumerable<string> _sependents = temps.SelectMany(c => c.Dependent);
|
||||
|
||||
startups.AddRange(temps);
|
||||
|
||||
LoadDependents(all, _sependents);
|
||||
startups = GetSourceGeneratorInstances().OrderByDescending(c => c.Level).Distinct().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,7 +22,6 @@ namespace linker.startup
|
||||
/// </summary>
|
||||
/// <param name="serviceCollection"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="assemblies"></param>
|
||||
public static void Add(ServiceCollection serviceCollection, FileConfig config)
|
||||
{
|
||||
LoggerHelper.Instance.Info($"add startup : {string.Join(",", startups.Select(c => c.GetType().Name))}");
|
||||
|
@@ -1,5 +1,5 @@
|
||||
v1.6.3
|
||||
2024-12-13 15:52:27
|
||||
2024-12-13 17:19:41
|
||||
1. 优化UI,显示网络计算IP数
|
||||
2. 修复内网穿透不停止直接删除导致的无法再次添加的问题
|
||||
3. 优化网卡的端口转发
|
||||
|
Reference in New Issue
Block a user