diff --git a/shells/version.txt b/shells/version.txt index 4de2c18e..6f8aa719 100644 --- a/shells/version.txt +++ b/shells/version.txt @@ -1,5 +1,5 @@ v1.9.6 -2025-11-15 19:45:45 +2025-11-16 20:01:56 1. 一些累计更新,一些BUG修复 2. 优化客户端数据同步,减少服务器流量 3. 去除cdkey,改为发电解锁中继速度 diff --git a/src/linker.libs/OperatingManager.cs b/src/linker.libs/OperatingManager.cs index f931bdf0..73ad095d 100644 --- a/src/linker.libs/OperatingManager.cs +++ b/src/linker.libs/OperatingManager.cs @@ -22,24 +22,30 @@ namespace linker.libs { public ConcurrentDictionary StringKeyValue=> dicOperating; + public VersionManager DataVersion { get; } = new VersionManager(); + private readonly ConcurrentDictionary dicOperating = new ConcurrentDictionary(); private readonly ConcurrentDictionary dicOperating1 = new ConcurrentDictionary(); public bool StartOperation(string key) { + DataVersion.Increment(); return dicOperating.TryAdd(key, true); } public void StopOperation(string key) { + DataVersion.Increment(); dicOperating.TryRemove(key, out _); } public bool StartOperation(uint key) { + DataVersion.Increment(); return dicOperating1.TryAdd(key, true); } public void StopOperation(uint key) { + DataVersion.Increment(); dicOperating1.TryRemove(key, out _); } } diff --git a/src/linker.messenger.channel/Channel.cs b/src/linker.messenger.channel/Channel.cs index 939ecfeb..db60271f 100644 --- a/src/linker.messenger.channel/Channel.cs +++ b/src/linker.messenger.channel/Channel.cs @@ -1,37 +1,93 @@ using linker.libs; using linker.libs.extends; +using linker.libs.timer; +using linker.messenger.pcp; +using linker.messenger.relay.client; +using linker.messenger.signin; using linker.tunnel; using linker.tunnel.connection; using System.Collections.Concurrent; -using linker.messenger.relay.client; -using linker.messenger.pcp; -using linker.messenger.signin; -using linker.libs.timer; -using System.Net; namespace linker.messenger.channel { - public class Channel + public sealed class ChannelConnectionCaching { public VersionManager Version { get; } = new VersionManager(); + public ConcurrentDictionary> Connections { get; } = new(); + + public ConcurrentDictionary this[string transactionId] + { + get + { + if (Connections.TryGetValue(transactionId, out ConcurrentDictionary _connections) == false) + { + _connections = new ConcurrentDictionary(); + Connections.TryAdd(transactionId, _connections); + } + return _connections; + } + } + + public bool TryGetValue(string machineId, string transactionId, out ITunnelConnection connection) + { + connection = null; + if (Connections.TryGetValue(transactionId, out ConcurrentDictionary _connections)) + { + return _connections.TryGetValue(machineId, out connection); + } + return false; + } + public void Add(ITunnelConnection connection) + { + if (Connections.TryGetValue(connection.TransactionId, out ConcurrentDictionary _connections) == false) + { + _connections = new ConcurrentDictionary(); + Connections.TryAdd(connection.TransactionId, _connections); + } + _connections.AddOrUpdate(connection.RemoteMachineId, connection, (a, b) => connection); + Version.Increment(); + } + public void Remove(string machineId, string transactionId) + { + if (Connections.TryGetValue(transactionId, out ConcurrentDictionary _connections)) + { + if (_connections.TryRemove(machineId, out ITunnelConnection _connection)) + { + try + { + _connection.Dispose(); + } + catch (Exception) + { + } + Version.Increment(); + } + } + } + } + public class Channel + { + public VersionManager Version => channelConnectionCaching.Version; + public ConcurrentDictionary Connections => channelConnectionCaching[TransactionId]; + protected virtual string TransactionId { get; } - protected readonly ConcurrentDictionary connections = new ConcurrentDictionary(); private readonly TunnelTransfer tunnelTransfer; private readonly RelayClientTransfer relayTransfer; private readonly PcpTransfer pcpTransfer; private readonly SignInClientTransfer signInClientTransfer; private readonly ISignInClientStore signInClientStore; - private readonly IRelayClientStore relayClientStore; + private readonly ChannelConnectionCaching channelConnectionCaching; - public Channel(TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, SignInClientTransfer signInClientTransfer, ISignInClientStore signInClientStore, IRelayClientStore relayClientStore) + public Channel(TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, + SignInClientTransfer signInClientTransfer, ISignInClientStore signInClientStore, ChannelConnectionCaching channelConnectionCaching) { this.tunnelTransfer = tunnelTransfer; this.relayTransfer = relayTransfer; this.pcpTransfer = pcpTransfer; this.signInClientTransfer = signInClientTransfer; this.signInClientStore = signInClientStore; - this.relayClientStore = relayClientStore; + this.channelConnectionCaching = channelConnectionCaching; //监听打洞成功 tunnelTransfer.SetConnectedCallback(TransactionId, OnConnected); @@ -54,15 +110,11 @@ namespace linker.messenger.channel if (LoggerHelper.Instance.LoggerLevel <= LoggerTypes.DEBUG) LoggerHelper.Instance.Warning($"{TransactionId} add connection {connection.GetHashCode()} {connection.ToJson()}"); - if (connections.TryGetValue(connection.RemoteMachineId, out ITunnelConnection connectionOld) && connection.Equals(connectionOld) == false) + if (channelConnectionCaching.TryGetValue(connection.RemoteMachineId, TransactionId, out ITunnelConnection connectionOld) && connection.Equals(connectionOld) == false) { - connections.AddOrUpdate(connection.RemoteMachineId, connection, (a, b) => connection); TimerHelper.SetTimeout(connectionOld.Dispose, 5000); } - else - { - connections.AddOrUpdate(connection.RemoteMachineId, connection, (a, b) => connection); - } + channelConnectionCaching.Add(connection); Version.Increment(); Connected(connection); @@ -86,7 +138,7 @@ namespace linker.messenger.channel return null; } //之前这个客户端已经连接过 - if (connections.TryGetValue(machineId, out ITunnelConnection connection) && connection.Connected) + if (channelConnectionCaching.TryGetValue(machineId, TransactionId, out ITunnelConnection connection) && connection.Connected) { return connection; } @@ -99,7 +151,7 @@ namespace linker.messenger.channel } //获得锁再次看看之前有没有连接成功 - if (connections.TryGetValue(machineId, out connection) && connection.Connected) + if (channelConnectionCaching.TryGetValue(machineId, TransactionId, out connection) && connection.Connected) { return connection; } @@ -113,7 +165,7 @@ namespace linker.messenger.channel if (connection != null) { - connections.AddOrUpdate(machineId, connection, (a, b) => connection); + channelConnectionCaching.Add(connection); } } @@ -148,7 +200,7 @@ namespace linker.messenger.channel //后台打洞 tunnelTransfer.StartBackground(machineId, TransactionId, denyProtocols, () => { - return connections.TryGetValue(machineId, out ITunnelConnection connection) && connection.Connected && connection.Type == TunnelType.P2P; + return channelConnectionCaching.TryGetValue(machineId, TransactionId, out ITunnelConnection connection) && connection.Connected && connection.Type == TunnelType.P2P; }, async (_connection) => { //后台打洞失败,pcp @@ -181,31 +233,5 @@ namespace linker.messenger.channel return connection; } - /// - /// 获取隧道 - /// - /// - public ConcurrentDictionary GetConnections() - { - return connections; - } - /// - /// 删除隧道 - /// - /// - public void RemoveConnection(string machineId) - { - if (connections.TryRemove(machineId, out ITunnelConnection _connection)) - { - try - { - _connection.Dispose(); - } - catch (Exception) - { - } - Version.Increment(); - } - } } } diff --git a/src/linker.messenger.channel/ChannelApiController.cs b/src/linker.messenger.channel/ChannelApiController.cs new file mode 100644 index 00000000..d3e308a5 --- /dev/null +++ b/src/linker.messenger.channel/ChannelApiController.cs @@ -0,0 +1,53 @@ +using linker.libs.extends; +using System.Collections.Concurrent; +using linker.tunnel.connection; +using linker.messenger.api; +using linker.libs.web; + +namespace linker.messenger.channel +{ + public sealed class ChannelApiController : IApiController + { + private readonly ChannelConnectionCaching channelConnectionCaching; + public ChannelApiController(ChannelConnectionCaching channelConnectionCaching) + { + this.channelConnectionCaching = channelConnectionCaching; + } + + public ConnectionListInfo Get(ApiControllerParamsInfo param) + { + ulong hashCode = ulong.Parse(param.Content); + if (channelConnectionCaching.Version.Eq(hashCode, out ulong version) == false) + { + return new ConnectionListInfo + { + List = channelConnectionCaching.Connections, + HashCode = version + }; + } + return new ConnectionListInfo { HashCode = version }; + } + + [Access(AccessValue.TunnelRemove)] + public void Remove(ApiControllerParamsInfo param) + { + RemoveInfo info = param.Content.DeJson(); + channelConnectionCaching.Remove(info.MachineId, info.TransactionId); + } + + + } + + public sealed class RemoveInfo + { + public string MachineId { get; set; } + public string TransactionId { get; set; } + } + + public sealed class ConnectionListInfo + { + public ConcurrentDictionary> List { get; set; } + public ulong HashCode { get; set; } + } + +} diff --git a/src/linker.messenger.channel/Entry.cs b/src/linker.messenger.channel/Entry.cs new file mode 100644 index 00000000..541dcb08 --- /dev/null +++ b/src/linker.messenger.channel/Entry.cs @@ -0,0 +1,23 @@ +using linker.libs.web; +using Microsoft.Extensions.DependencyInjection; +using System.Text.Json; +namespace linker.messenger.channel +{ + public static class Entry + { + public static ServiceCollection AddChannelClient(this ServiceCollection serviceCollection) + { + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + + return serviceCollection; + } + public static ServiceProvider UseChannelClient(this ServiceProvider serviceProvider, JsonDocument json = default) + { + linker.messenger.api.IWebServer apiServer = serviceProvider.GetService(); + apiServer.AddPlugins(new List { serviceProvider.GetService() }); + + return serviceProvider; + } + } +} diff --git a/src/linker.messenger.entry/LinkerMessengerEntry.cs b/src/linker.messenger.entry/LinkerMessengerEntry.cs index c577786d..8cbba62f 100644 --- a/src/linker.messenger.entry/LinkerMessengerEntry.cs +++ b/src/linker.messenger.entry/LinkerMessengerEntry.cs @@ -23,6 +23,7 @@ using System.Text.Json; using linker.messenger.firewall; using linker.messenger.wakeup; using linker.messenger.wlist; +using linker.messenger.channel; namespace linker.messenger.entry { @@ -68,7 +69,7 @@ namespace linker.messenger.entry .AddPcpClient().AddPcpServer() //中继 .AddRelayClient().AddRelayServer() - + //服务器穿透 .AddSForwardClient().AddSForwardServer() //登录 @@ -104,7 +105,9 @@ namespace linker.messenger.entry .AddWakeupClient().AddWakeupServer() //白名单 - .AddWhiteListClient().AddWhiteListServer(); + .AddWhiteListClient().AddWhiteListServer() + + .AddChannelClient(); } /// @@ -160,8 +163,6 @@ namespace linker.messenger.entry ICommonStore commonStore = serviceProvider.GetService(); - - serviceProvider.UseMessenger(); if ((modules & ExcludeModule.StoreFile) != ExcludeModule.StoreFile) @@ -228,6 +229,8 @@ namespace linker.messenger.entry serviceProvider.UseSignInClient(); serviceProvider.UsePlanClient(); + + serviceProvider.UseChannelClient(); } } diff --git a/src/linker.messenger.flow/FlowForward.cs b/src/linker.messenger.flow/FlowForward.cs index d19fa58e..2aaf5f7d 100644 --- a/src/linker.messenger.flow/FlowForward.cs +++ b/src/linker.messenger.flow/FlowForward.cs @@ -1,6 +1,7 @@ using linker.libs; using linker.libs.extends; using linker.libs.timer; +using linker.messenger.channel; using linker.messenger.forward.proxy; using linker.messenger.pcp; using linker.messenger.relay.client; @@ -18,7 +19,8 @@ namespace linker.messenger.flow private readonly FlowForward forwardFlow; private readonly FlowTunnel flowTunnel; public FlowForwardProxy(FlowForward forwardFlow, FlowTunnel flowTunnel, ISignInClientStore signInClientStore, TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, - SignInClientTransfer signInClientTransfer, IRelayClientStore relayClientStore) : base(signInClientStore, tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, relayClientStore) + SignInClientTransfer signInClientTransfer, ChannelConnectionCaching channelConnectionCaching) + : base(signInClientStore, tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, channelConnectionCaching) { this.forwardFlow = forwardFlow; this.flowTunnel = flowTunnel; diff --git a/src/linker.messenger.flow/FlowSocks5.cs b/src/linker.messenger.flow/FlowSocks5.cs index 1c155f0c..69b10882 100644 --- a/src/linker.messenger.flow/FlowSocks5.cs +++ b/src/linker.messenger.flow/FlowSocks5.cs @@ -1,6 +1,7 @@ using linker.libs; using linker.libs.extends; using linker.libs.timer; +using linker.messenger.channel; using linker.messenger.pcp; using linker.messenger.relay.client; using linker.messenger.signin; @@ -19,7 +20,8 @@ namespace linker.messenger.flow private readonly FlowTunnel flowTunnel; public FlowSocks5Proxy(FlowSocks5 flowSocks5, FlowTunnel flowTunnel, ISignInClientStore signInClientStore, TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, - SignInClientTransfer signInClientTransfer, IRelayClientStore relayClientStore, Socks5CidrDecenterManager socks5CidrDecenterManager) : base(signInClientStore, tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, relayClientStore, socks5CidrDecenterManager) + SignInClientTransfer signInClientTransfer, Socks5CidrDecenterManager socks5CidrDecenterManager, ChannelConnectionCaching channelConnectionCaching) + : base(signInClientStore, tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, socks5CidrDecenterManager, channelConnectionCaching) { this.flowSocks5 = flowSocks5; this.flowTunnel = flowTunnel; diff --git a/src/linker.messenger.flow/FlowTunnel.cs b/src/linker.messenger.flow/FlowTunnel.cs index 394afc2e..137343c4 100644 --- a/src/linker.messenger.flow/FlowTunnel.cs +++ b/src/linker.messenger.flow/FlowTunnel.cs @@ -1,5 +1,6 @@ using linker.libs; using linker.libs.extends; +using linker.messenger.channel; using linker.messenger.pcp; using linker.messenger.relay.client; using linker.messenger.signin; @@ -18,11 +19,11 @@ namespace linker.messenger.flow private readonly FlowTunnel flowTunnel; public FlowTuntapProxy(FlowTunnel flowTunnel, ISignInClientStore signInClientStore, TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, - SignInClientTransfer signInClientTransfer, IRelayClientStore relayClientStore, TuntapConfigTransfer tuntapConfigTransfer, + SignInClientTransfer signInClientTransfer, TuntapConfigTransfer tuntapConfigTransfer, TuntapCidrConnectionManager tuntapCidrConnectionManager, TuntapCidrDecenterManager tuntapCidrDecenterManager, - TuntapCidrMapfileManager tuntapCidrMapfileManager,TuntapDecenter tuntapDecenter) - : base(signInClientStore, tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, relayClientStore, - tuntapConfigTransfer, tuntapCidrConnectionManager, tuntapCidrDecenterManager, tuntapCidrMapfileManager, tuntapDecenter) + TuntapCidrMapfileManager tuntapCidrMapfileManager,TuntapDecenter tuntapDecenter, ChannelConnectionCaching channelConnectionCaching) + : base(signInClientStore, tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, + tuntapConfigTransfer, tuntapCidrConnectionManager, tuntapCidrDecenterManager, tuntapCidrMapfileManager, tuntapDecenter, channelConnectionCaching) { this.flowTunnel = flowTunnel; } diff --git a/src/linker.messenger.forward/ForwardApiController.cs b/src/linker.messenger.forward/ForwardApiController.cs index b8aed927..573082ad 100644 --- a/src/linker.messenger.forward/ForwardApiController.cs +++ b/src/linker.messenger.forward/ForwardApiController.cs @@ -31,35 +31,7 @@ namespace linker.messenger.forward this.serializer = serializer; } - /// - /// 获取隧道连接 - /// - /// - /// - public ConnectionListInfo Connections(ApiControllerParamsInfo param) - { - ulong hashCode = ulong.Parse(param.Content); - if (forwardProxy.Version.Eq(hashCode, out ulong version) == false) - { - return new ConnectionListInfo - { - List = forwardProxy.GetConnections(), - HashCode = version - }; - } - return new ConnectionListInfo { HashCode = version }; - } - - /// - /// 移除隧道连接 - /// - /// - [Access(AccessValue.TunnelRemove)] - public void RemoveConnection(ApiControllerParamsInfo param) - { - forwardProxy.RemoveConnection(param.Content); - } - + /// /// 获取绑定IP列表 /// @@ -165,14 +137,4 @@ namespace linker.messenger.forward } } - public sealed class ForwardListInfo - { - public ConcurrentDictionary List { get; set; } - public ulong HashCode { get; set; } - } - public sealed class ConnectionListInfo - { - public ConcurrentDictionary List { get; set; } - public ulong HashCode { get; set; } - } } diff --git a/src/linker.messenger.forward/proxy/ForwardProxyChan.cs b/src/linker.messenger.forward/proxy/ForwardProxyChan.cs index 5b59186d..6cebf605 100644 --- a/src/linker.messenger.forward/proxy/ForwardProxyChan.cs +++ b/src/linker.messenger.forward/proxy/ForwardProxyChan.cs @@ -23,8 +23,8 @@ namespace linker.messenger.forward.proxy protected override string TransactionId => "forward"; public ForwardProxy(ISignInClientStore signInClientStore, TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, - SignInClientTransfer signInClientTransfer, IRelayClientStore relayClientStore) - : base(tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, signInClientStore, relayClientStore) + SignInClientTransfer signInClientTransfer, ChannelConnectionCaching channelConnectionCaching) + : base(tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, signInClientStore, channelConnectionCaching) { TaskUdp(); } diff --git a/src/linker.messenger.relay/client/RelayApiController.cs b/src/linker.messenger.relay/client/RelayApiController.cs index 9a620769..101a5e66 100644 --- a/src/linker.messenger.relay/client/RelayApiController.cs +++ b/src/linker.messenger.relay/client/RelayApiController.cs @@ -7,6 +7,7 @@ using linker.messenger.relay.messenger; using linker.messenger.relay.server; using linker.messenger.signin; using linker.messenger.sync; +using linker.tunnel; using linker.tunnel.connection; using System.Collections.Concurrent; @@ -72,10 +73,20 @@ namespace linker.messenger.relay.client /// /// /// - public ConcurrentDictionary Operating(ApiControllerParamsInfo param) + public RelayOperatingInfo Operating(ApiControllerParamsInfo param) { - return relayTransfer.Operating; + ulong hashCode = ulong.Parse(param.Content); + if (relayTransfer.OperatingVersion.Eq(hashCode, out ulong version) == false) + { + return new RelayOperatingInfo + { + List = relayTransfer.Operating, + HashCode = version + }; + } + return new RelayOperatingInfo { HashCode = version }; } + /// /// 连接 /// @@ -146,6 +157,14 @@ namespace linker.messenger.relay.client return resp.Code == MessageResponeCodes.OK && resp.Data.Span.SequenceEqual(Helper.TrueArray); } } + + + public sealed class RelayOperatingInfo + { + public ConcurrentDictionary List { get; set; } + public ulong HashCode { get; set; } + } + public sealed class UpdateInfo { public string Key { get; set; } diff --git a/src/linker.messenger.relay/client/RelayClientTransfer.cs b/src/linker.messenger.relay/client/RelayClientTransfer.cs index 3a6b524b..f4b7519b 100644 --- a/src/linker.messenger.relay/client/RelayClientTransfer.cs +++ b/src/linker.messenger.relay/client/RelayClientTransfer.cs @@ -14,9 +14,9 @@ namespace linker.messenger.relay.client { public List Transports { get; private set; } - + public VersionManager OperatingVersion => operating.DataVersion; public ConcurrentDictionary Operating => operating.StringKeyValue; - private OperatingMultipleManager operating = new OperatingMultipleManager(); + private readonly OperatingMultipleManager operating = new OperatingMultipleManager(); private Dictionary>> OnConnected { get; } = new Dictionary>>(); @@ -77,7 +77,6 @@ namespace linker.messenger.relay.client if (string.IsNullOrWhiteSpace(nodeId)) nodeId = relayClientStore.DefaultNodeId; if(protocol == TunnelProtocolType.None) protocol = relayClientStore.DefaultProtocol; - if (operating.StartOperation(BuildKey(remoteMachineId, transactionId)) == false) { return null; diff --git a/src/linker.messenger.socks5/Socks5ApiController.cs b/src/linker.messenger.socks5/Socks5ApiController.cs index e59c7bea..3f8f6604 100644 --- a/src/linker.messenger.socks5/Socks5ApiController.cs +++ b/src/linker.messenger.socks5/Socks5ApiController.cs @@ -30,26 +30,6 @@ namespace linker.messenger.socks5 this.accessStore = accessStore; } - public ConnectionListInfo Connections(ApiControllerParamsInfo param) - { - ulong hashCode = ulong.Parse(param.Content); - if (tunnelProxy.Version.Eq(hashCode, out ulong version) == false) - { - return new ConnectionListInfo - { - List = tunnelProxy.GetConnections(), - HashCode = version - }; - } - return new ConnectionListInfo { HashCode = version }; - } - - [Access(AccessValue.TunnelRemove)] - public void RemoveConnection(ApiControllerParamsInfo param) - { - tunnelProxy.RemoveConnection(param.Content); - } - /// /// 获取所有客户端的信息 /// @@ -164,9 +144,4 @@ namespace linker.messenger.socks5 public ConcurrentDictionary List { get; set; } public ulong HashCode { get; set; } } - public sealed class ConnectionListInfo - { - public ConcurrentDictionary List { get; set; } - public ulong HashCode { get; set; } - } } diff --git a/src/linker.messenger.socks5/proxy/Socks5Proxy.cs b/src/linker.messenger.socks5/proxy/Socks5Proxy.cs index 79c64aac..2cb1138c 100644 --- a/src/linker.messenger.socks5/proxy/Socks5Proxy.cs +++ b/src/linker.messenger.socks5/proxy/Socks5Proxy.cs @@ -22,8 +22,8 @@ namespace linker.messenger.socks5 private readonly Socks5CidrDecenterManager socks5CidrDecenterManager; public Socks5Proxy(ISignInClientStore signInClientStore, TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, - SignInClientTransfer signInClientTransfer, IRelayClientStore relayClientStore, Socks5CidrDecenterManager socks5CidrDecenterManager) - : base(tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, signInClientStore, relayClientStore) + SignInClientTransfer signInClientTransfer, Socks5CidrDecenterManager socks5CidrDecenterManager, ChannelConnectionCaching channelConnectionCaching) + : base(tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, signInClientStore, channelConnectionCaching) { this.socks5CidrDecenterManager = socks5CidrDecenterManager; TaskUdp(); diff --git a/src/linker.messenger.tunnel/TunnelApiController.cs b/src/linker.messenger.tunnel/TunnelApiController.cs index ab4fe85f..a448e9b0 100644 --- a/src/linker.messenger.tunnel/TunnelApiController.cs +++ b/src/linker.messenger.tunnel/TunnelApiController.cs @@ -72,9 +72,18 @@ namespace linker.messenger.tunnel /// /// /// - public ConcurrentDictionary Operating(ApiControllerParamsInfo param) + public TunnelOperatingInfo Operating(ApiControllerParamsInfo param) { - return tunnelTransfer.Operating; + ulong hashCode = ulong.Parse(param.Content); + if (tunnelTransfer.OperatingVersion.Eq(hashCode, out ulong version) == false) + { + return new TunnelOperatingInfo + { + List = tunnelTransfer.Operating, + HashCode = version + }; + } + return new TunnelOperatingInfo { HashCode = version }; } /// /// 连接 @@ -187,6 +196,11 @@ namespace linker.messenger.tunnel return new TunnelLocalNetworkInfo(); } + public sealed class TunnelOperatingInfo + { + public ConcurrentDictionary List { get; set; } + public ulong HashCode { get; set; } + } public sealed class TunnelListInfo { public ConcurrentDictionary List { get; set; } diff --git a/src/linker.messenger.tuntap/TuntapApiController.cs b/src/linker.messenger.tuntap/TuntapApiController.cs index 4c81d04d..2eae3599 100644 --- a/src/linker.messenger.tuntap/TuntapApiController.cs +++ b/src/linker.messenger.tuntap/TuntapApiController.cs @@ -46,36 +46,6 @@ namespace linker.messenger.tuntap this.tuntapProxy = tuntapProxy; } - /// - /// 连接 - /// - /// - /// - public ConnectionListInfo Connections(ApiControllerParamsInfo param) - { - ulong hashCode = ulong.Parse(param.Content); - if (tuntapProxy.Version.Eq(hashCode, out ulong version) == false) - { - return new ConnectionListInfo - { - List = tuntapProxy.GetConnections(), - HashCode = version - }; - } - return new ConnectionListInfo { HashCode = version }; - } - - /// - /// 删除连接 - /// - /// - [Access(AccessValue.TunnelRemove)] - public void RemoveConnection(ApiControllerParamsInfo param) - { - tuntapProxy.RemoveConnection(param.Content); - } - - /// /// 路由表 /// @@ -363,12 +333,6 @@ namespace linker.messenger.tuntap public ConcurrentDictionary List { get; set; } public ulong HashCode { get; set; } } - public sealed class ConnectionListInfo - { - public ConcurrentDictionary List { get; set; } - public ulong HashCode { get; set; } - } - public sealed class NetworkParamInfo { public IPAddress IP { get; set; } diff --git a/src/linker.messenger.tuntap/TuntapPingTransfer.cs b/src/linker.messenger.tuntap/TuntapPingTransfer.cs index 6125ab55..15b1c19f 100644 --- a/src/linker.messenger.tuntap/TuntapPingTransfer.cs +++ b/src/linker.messenger.tuntap/TuntapPingTransfer.cs @@ -50,7 +50,7 @@ namespace linker.messenger.tuntap var items = tuntapDecenter.Infos.Values.Where(c => c.IP != null && c.IP.Equals(IPAddress.Any) == false && (c.Status & TuntapStatus.Running) == TuntapStatus.Running); if ((tuntapConfigTransfer.Info.Switch & TuntapSwitch.AutoConnect) != TuntapSwitch.AutoConnect) { - var connections = tuntapProxy.GetConnections(); + var connections = tuntapProxy.Connections; items = items.Where(c => connections.TryGetValue(c.MachineId, out ITunnelConnection connection) && connection.Connected || c.MachineId == signInClientStore.Id); } diff --git a/src/linker.messenger.tuntap/TuntapProxy.cs b/src/linker.messenger.tuntap/TuntapProxy.cs index eccba898..ad61ce2b 100644 --- a/src/linker.messenger.tuntap/TuntapProxy.cs +++ b/src/linker.messenger.tuntap/TuntapProxy.cs @@ -1,13 +1,14 @@ -using linker.tunnel; -using linker.tunnel.connection; -using linker.libs; -using System.Buffers.Binary; +using linker.libs; +using linker.messenger.channel; +using linker.messenger.pcp; using linker.messenger.relay.client; using linker.messenger.signin; -using linker.messenger.pcp; using linker.messenger.tuntap.cidr; using linker.nat; using linker.tun.device; +using linker.tunnel; +using linker.tunnel.connection; +using System.Buffers.Binary; namespace linker.messenger.tuntap { @@ -31,8 +32,10 @@ namespace linker.messenger.tuntap public TuntapProxy(ISignInClientStore signInClientStore, TunnelTransfer tunnelTransfer, RelayClientTransfer relayTransfer, PcpTransfer pcpTransfer, - SignInClientTransfer signInClientTransfer, IRelayClientStore relayClientStore, TuntapConfigTransfer tuntapConfigTransfer, TuntapCidrConnectionManager tuntapCidrConnectionManager, TuntapCidrDecenterManager tuntapCidrDecenterManager, TuntapCidrMapfileManager tuntapCidrMapfileManager, TuntapDecenter tuntapDecenter) - : base(tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, signInClientStore, relayClientStore) + SignInClientTransfer signInClientTransfer, TuntapConfigTransfer tuntapConfigTransfer, + TuntapCidrConnectionManager tuntapCidrConnectionManager, TuntapCidrDecenterManager tuntapCidrDecenterManager, + TuntapCidrMapfileManager tuntapCidrMapfileManager, TuntapDecenter tuntapDecenter, ChannelConnectionCaching channelConnectionCaching) + : base(tunnelTransfer, relayTransfer, pcpTransfer, signInClientTransfer, signInClientStore, channelConnectionCaching) { this.tuntapConfigTransfer = tuntapConfigTransfer; this.tuntapCidrConnectionManager = tuntapCidrConnectionManager; @@ -85,9 +88,9 @@ namespace linker.messenger.tuntap public async Task InputPacket(LinkerTunDevicPacket packet) { //IPV4广播组播、IPV6 多播 - if ((packet.IPV4Broadcast || packet.IPV6Multicast) && tuntapConfigTransfer.Info.Multicast == false && connections.IsEmpty == false) + if ((packet.IPV4Broadcast || packet.IPV6Multicast) && tuntapConfigTransfer.Info.Multicast == false && Connections.IsEmpty == false) { - await Task.WhenAll(connections.Values.Where(c => c != null && c.Connected).Select(c => c.SendAsync(packet.Buffer, packet.Offset, packet.Length))); + await Task.WhenAll(Connections.Values.Where(c => c != null && c.Connected).Select(c => c.SendAsync(packet.Buffer, packet.Offset, packet.Length))).ConfigureAwait(false); return; } diff --git a/src/linker.tunnel/TunnelTransfer.cs b/src/linker.tunnel/TunnelTransfer.cs index 1b20c95f..ab8e2c64 100644 --- a/src/linker.tunnel/TunnelTransfer.cs +++ b/src/linker.tunnel/TunnelTransfer.cs @@ -19,6 +19,7 @@ namespace linker.tunnel private readonly TunnelWanPortTransfer tunnelWanPortTransfer; private readonly TunnelUpnpTransfer tunnelUpnpTransfer; + public VersionManager OperatingVersion => operating.DataVersion; public ConcurrentDictionary Operating => operating.StringKeyValue; private readonly OperatingMultipleManager operating = new OperatingMultipleManager(); private uint flowid = 1; diff --git a/src/linker.web/src/apis/forward.js b/src/linker.web/src/apis/forward.js index 7f76e1a0..d664d788 100644 --- a/src/linker.web/src/apis/forward.js +++ b/src/linker.web/src/apis/forward.js @@ -1,11 +1,5 @@ import { sendWebsocketMsg } from './request' -export const getForwardConnections = (hashcode = '0') => { - return sendWebsocketMsg('forward/connections', hashcode); -} -export const removeForwardConnection = (id) => { - return sendWebsocketMsg('forward/removeconnection', id); -} export const getForwardInfo = (hashcode = '0') => { return sendWebsocketMsg('forward/get', hashcode); } diff --git a/src/linker.web/src/apis/relay.js b/src/linker.web/src/apis/relay.js index ff398d4a..76922c56 100644 --- a/src/linker.web/src/apis/relay.js +++ b/src/linker.web/src/apis/relay.js @@ -12,8 +12,8 @@ export const setRelayServers = (servers) => { export const setRelaySubscribe = () => { return sendWebsocketMsg('relay/Subscribe'); } -export const relayOperating = () => { - return sendWebsocketMsg('relay/Operating'); +export const relayOperating = (data) => { + return sendWebsocketMsg('relay/Operating',data); } export const relayConnect = (data) => { return sendWebsocketMsg('relay/Connect', data); diff --git a/src/linker.web/src/apis/socks5.js b/src/linker.web/src/apis/socks5.js index 59dde033..9d987626 100644 --- a/src/linker.web/src/apis/socks5.js +++ b/src/linker.web/src/apis/socks5.js @@ -1,13 +1,5 @@ import { sendWebsocketMsg } from './request' - -export const getSocks5Connections = (hashcode = '0') => { - return sendWebsocketMsg('socks5/connections', hashcode); -} -export const removeSocks5Connection = (id) => { - return sendWebsocketMsg('socks5/removeconnection', id); -} - export const getSocks5Info = (hashcode = '0') => { return sendWebsocketMsg('socks5/get', hashcode); } diff --git a/src/linker.web/src/apis/tunnel.js b/src/linker.web/src/apis/tunnel.js index e625bd07..a15afd3f 100644 --- a/src/linker.web/src/apis/tunnel.js +++ b/src/linker.web/src/apis/tunnel.js @@ -6,8 +6,8 @@ export const getTunnelInfo = (hashcode = '0') => { export const tunnelRefresh = () => { return sendWebsocketMsg('tunnel/refresh'); } -export const tunnelOperating = () => { - return sendWebsocketMsg('tunnel/Operating'); +export const tunnelOperating = (data) => { + return sendWebsocketMsg('tunnel/Operating',data); } export const tunnelConnect = (data) => { return sendWebsocketMsg('tunnel/connect',data); @@ -29,4 +29,11 @@ export const getTunnelRecords = () => { } export const getTunnelNetwork = (data) => { return sendWebsocketMsg('tunnel/GetNetwork',data); +} + +export const getTunnelConnections = (hashcode) => { + return sendWebsocketMsg('channel/get',hashcode); +} +export const removeTunnelConnection = (machineid,transactionId) => { + return sendWebsocketMsg('channel/remove',{machineid,transactionId}); } \ No newline at end of file diff --git a/src/linker.web/src/apis/tuntap.js b/src/linker.web/src/apis/tuntap.js index c51f2bc7..f461e486 100644 --- a/src/linker.web/src/apis/tuntap.js +++ b/src/linker.web/src/apis/tuntap.js @@ -1,13 +1,6 @@ import { sendWebsocketMsg } from './request' -export const getTuntapConnections = (hashcode = '0') => { - return sendWebsocketMsg('tuntap/connections', hashcode); -} -export const removeTuntapConnection = (id) => { - return sendWebsocketMsg('tuntap/removeconnection', id); -} - export const getTuntapRoutes = (machineid) => { return sendWebsocketMsg('tuntap/routes', machineid); } diff --git a/src/linker.web/src/views/components/accesss/access.js b/src/linker.web/src/views/components/accesss/access.js index 67e8c2eb..d537adf8 100644 --- a/src/linker.web/src/views/components/accesss/access.js +++ b/src/linker.web/src/views/components/accesss/access.js @@ -28,8 +28,8 @@ export const provideAccess = () => { }); }); } - const accessProcessFn = (device) => { - Object.assign(device,{ + const accessProcessFn = (device,json) => { + Object.assign(json,{ hook_accesss: access.value.list[device.MachineId] || '' }) } diff --git a/src/linker.web/src/views/components/connection/connections.js b/src/linker.web/src/views/components/connection/connections.js deleted file mode 100644 index f45a1238..00000000 --- a/src/linker.web/src/views/components/connection/connections.js +++ /dev/null @@ -1,156 +0,0 @@ -import { getForwardConnections, removeForwardConnection } from "@/apis/forward"; -import { getTuntapConnections, removeTuntapConnection } from "@/apis/tuntap"; -import { getSocks5Connections, removeSocks5Connection } from "@/apis/socks5"; -import { inject, provide, ref } from "vue"; - -const connectionsSymbol = Symbol(); -const forwardConnectionsSymbol = Symbol(); -const tuntapConnectionsSymbol = Symbol(); -const socks5ConnectionsSymbol = Symbol(); -export const provideConnections = () => { - const connections = ref({ - showEdit: false, - speedCache: {}, - current: '', - currentName: '', - hashcode: 0, - hashcode1: 0, - - _updateRealTime: false, - updateRealTime: (value) => { - forwardConnections.value.hashcode = 0; - tuntapConnections.value.hashcode = 0; - socks5Connections.value.hashcode = 0; - connections.value._updateRealTime = value; - } - }); - provide(connectionsSymbol, connections); - - const forwardConnections = ref({ - timer: 0, - list: {}, - hashcode: 0, - }); - provide(forwardConnectionsSymbol, forwardConnections); - const _getForwardConnections = () => { - clearTimeout(forwardConnections.value.timer) - getForwardConnections(forwardConnections.value.hashcode.toString()).then((res) => { - if (forwardConnections.value._updateRealTime == false) - forwardConnections.value.hashcode = res.HashCode; - if (res.List) { - parseConnections(res.List, removeForwardConnection); - forwardConnections.value.list = res.List; - } - - forwardConnections.value.timer = setTimeout(_getForwardConnections, 1000); - }).catch((e) => { - forwardConnections.value.timer = setTimeout(_getForwardConnections, 1000); - }) - } - - - const tuntapConnections = ref({ - timer: 0, - list: {}, - hashcode: 0, - }); - provide(tuntapConnectionsSymbol, tuntapConnections); - const _getTuntapConnections = () => { - clearTimeout(tuntapConnections.value.timer) - getTuntapConnections(tuntapConnections.value.hashcode.toString()).then((res) => { - if (connections.value._updateRealTime == false) - tuntapConnections.value.hashcode = res.HashCode; - if (res.List) { - parseConnections(res.List, removeTuntapConnection); - tuntapConnections.value.list = res.List; - } - - tuntapConnections.value.timer = setTimeout(_getTuntapConnections, 1000); - }).catch((e) => { - tuntapConnections.value.timer = setTimeout(_getTuntapConnections, 1000); - }) - } - - const socks5Connections = ref({ - timer: 0, - list: {}, - hashcode: 0, - }); - provide(socks5ConnectionsSymbol, socks5Connections); - const _getSocks5Connections = () => { - clearTimeout(socks5Connections.value.timer) - getSocks5Connections(socks5Connections.value.hashcode.toString()).then((res) => { - if (connections.value._updateRealTime == false) - socks5Connections.value.hashcode = res.HashCode; - if (res.List) { - parseConnections(res.List, removeSocks5Connection); - socks5Connections.value.list = res.List; - } - - socks5Connections.value.timer = setTimeout(_getSocks5Connections, 1000); - }).catch((e) => { - socks5Connections.value.timer = setTimeout(_getSocks5Connections, 1000); - }) - } - - - const parseConnections = (_connections, removeFunc) => { - const caches = connections.value.speedCache; - for (let machineId in _connections) { - const connection = _connections[machineId]; - connection.removeFunc = removeFunc; - - const key = `${connection.RemoteMachineId}-${connection.TransactionId}`; - const cache = caches[key] || { SendBytes: 0, ReceiveBytes: 0 }; - - connection.SendBytesText = parseSpeed(connection.SendBytes - cache.SendBytes,'/s'); - connection.ReceiveBytesText = parseSpeed(connection.ReceiveBytes - cache.ReceiveBytes,'/s'); - connection.SendBufferRemainingText = parseSpeed(connection.SendBufferRemaining,''); - connection.RecvBufferRemainingText = parseSpeed(connection.RecvBufferRemaining || 0,''); - - cache.SendBytes = connection.SendBytes; - cache.ReceiveBytes = connection.ReceiveBytes; - caches[key] = cache; - } - } - const parseSpeed = (num,subfix = '') => { - let index = 0; - while (num >= 1024) { - num /= 1024; - index++; - } - return `${num.toFixed(2)}${['B', 'KB', 'MB', 'GB', 'TB'][index]}${subfix}`; - } - - const handleTunnelConnections = (device) => { - connections.value.current = device.MachineId; - connections.value.currentName = device.MachineName; - connections.value.showEdit = true; - } - - - const clearConnectionsTimeout = () => { - clearTimeout(forwardConnections.value.timer); - clearTimeout(tuntapConnections.value.timer); - clearTimeout(socks5Connections.value.timer); - } - return { - connections, - forwardConnections, _getForwardConnections, - tuntapConnections, _getTuntapConnections, - socks5Connections, _getSocks5Connections, - handleTunnelConnections, clearConnectionsTimeout - } -} -export const useConnections = () => { - return inject(connectionsSymbol); -} -export const useForwardConnections = () => { - return inject(forwardConnectionsSymbol); -} -export const useTuntapConnections = () => { - return inject(tuntapConnectionsSymbol); -} -export const useSocks5Connections = () => { - return inject(socks5ConnectionsSymbol); -} \ No newline at end of file diff --git a/src/linker.web/src/views/components/decenter/decenter.js b/src/linker.web/src/views/components/decenter/decenter.js index 0811910d..7c061adf 100644 --- a/src/linker.web/src/views/components/decenter/decenter.js +++ b/src/linker.web/src/views/components/decenter/decenter.js @@ -25,10 +25,14 @@ export const provideDecenter = () => { }); } - const counterProcessFn = (device) => { - Object.assign(device,{ - hook_counter: decenter.value.list[device.MachineId] || '' - }) + const counterProcessFn = (device,json) => { + const _json = {}; + for (const key in decenter.value.list) { + _json[key] = decenter.value.list[key][device.MachineId] || 0; + } + Object.assign(json,{ + hook_counter: _json + }); } const counterRefreshFn = () => { refreshCounter(); diff --git a/src/linker.web/src/views/components/device/Device.vue b/src/linker.web/src/views/components/device/Device.vue index 000d2932..9eda16be 100644 --- a/src/linker.web/src/views/components/device/Device.vue +++ b/src/linker.web/src/views/components/device/Device.vue @@ -14,9 +14,9 @@