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