From 16e0ff7d14f1e1e3deb8d2978bca0b393fa46d40 Mon Sep 17 00:00:00 2001 From: snltty <1069410172@qq.com> Date: Sun, 4 Aug 2024 20:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=8D=A1=E8=87=AA=E5=8A=A8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linker.tun.test/Program.cs | 2 +- linker.tun/ILinkerTunDevice.cs | 2 +- linker.tun/LinkerLinuxTunDevice.cs | 2 +- linker.tun/LinkerOsxTunDevice.cs | 2 +- linker.tun/LinkerTunDeviceAdapter.cs | 10 +- linker.tun/LinkerWinTunDevice.cs | 2 +- linker/plugins/tuntap/TuntapApiController.cs | 10 +- linker/plugins/tuntap/TuntapTransfer.cs | 96 ++++++++----------- .../tuntap/messenger/TuntapMessenger.cs | 8 +- 9 files changed, 60 insertions(+), 74 deletions(-) diff --git a/linker.tun.test/Program.cs b/linker.tun.test/Program.cs index b71eeceb..d5265cc0 100644 --- a/linker.tun.test/Program.cs +++ b/linker.tun.test/Program.cs @@ -11,7 +11,7 @@ namespace linker.tun.test { linkerTunDeviceAdapter = new LinkerTunDeviceAdapter(); linkerTunDeviceAdapter.SetReadCallback(new LinkerTunDeviceCallback()); - linkerTunDeviceAdapter.SetUp("linker111" + linkerTunDeviceAdapter.Setup("linker111" , IPAddress.Parse("192.168.55.2"), 24); linkerTunDeviceAdapter.SetMtu(1420); diff --git a/linker.tun/ILinkerTunDevice.cs b/linker.tun/ILinkerTunDevice.cs index a11bc27c..a1215ad1 100644 --- a/linker.tun/ILinkerTunDevice.cs +++ b/linker.tun/ILinkerTunDevice.cs @@ -29,7 +29,7 @@ namespace linker.tun /// /// /// - public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error); + public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error); /// /// 关闭 /// diff --git a/linker.tun/LinkerLinuxTunDevice.cs b/linker.tun/LinkerLinuxTunDevice.cs index 18e4d48a..671d8086 100644 --- a/linker.tun/LinkerLinuxTunDevice.cs +++ b/linker.tun/LinkerLinuxTunDevice.cs @@ -24,7 +24,7 @@ namespace linker.tun this.name = name; } - public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error) + public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error) { error = string.Empty; this.address = address; diff --git a/linker.tun/LinkerOsxTunDevice.cs b/linker.tun/LinkerOsxTunDevice.cs index f520fe28..3cee6f3d 100644 --- a/linker.tun/LinkerOsxTunDevice.cs +++ b/linker.tun/LinkerOsxTunDevice.cs @@ -23,7 +23,7 @@ namespace linker.tun this.name = name; } - public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error) + public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error) { error = string.Empty; diff --git a/linker.tun/LinkerTunDeviceAdapter.cs b/linker.tun/LinkerTunDeviceAdapter.cs index 2d8777b4..4fda10f8 100644 --- a/linker.tun/LinkerTunDeviceAdapter.cs +++ b/linker.tun/LinkerTunDeviceAdapter.cs @@ -57,7 +57,7 @@ namespace linker.tun /// 网卡名,如果是osx,需要utunX的命名,X是一个数字 /// 网卡IP /// 掩码。一般24即可 - public bool SetUp(string name, IPAddress address, byte prefixLength) + public bool Setup(string name, IPAddress address, byte prefixLength) { if (Interlocked.CompareExchange(ref operating, 1, 0) == 1) { @@ -72,7 +72,7 @@ namespace linker.tun error = $"{System.Runtime.InteropServices.RuntimeInformation.OSDescription} not support"; return false; } - linkerTunDevice.SetUp(address, NetworkHelper.ToGatewayIP(address, prefixLength), prefixLength, out error); + linkerTunDevice.Setup(address, NetworkHelper.ToGatewayIP(address, prefixLength), prefixLength, out error); if (string.IsNullOrWhiteSpace(error) == false) { return false; @@ -114,7 +114,7 @@ namespace linker.tun /// /// 关闭网卡 /// - public bool Shutdown(int index) + public bool Shutdown() { if (Interlocked.CompareExchange(ref operating, 1, 0) == 1) { @@ -187,7 +187,7 @@ namespace linker.tun ReadOnlyMemory buffer = linkerTunDevice.Read(); if (buffer.Length == 0) { - Shutdown(4); + Shutdown(); break; } @@ -213,7 +213,7 @@ namespace linker.tun catch (Exception ex) { error = ex.Message; - Shutdown(5); + Shutdown(); break; } } diff --git a/linker.tun/LinkerWinTunDevice.cs b/linker.tun/LinkerWinTunDevice.cs index ff8dbba4..5ac0b664 100644 --- a/linker.tun/LinkerWinTunDevice.cs +++ b/linker.tun/LinkerWinTunDevice.cs @@ -28,7 +28,7 @@ namespace linker.tun this.guid = guid; } - public bool SetUp(IPAddress address, IPAddress gateway, byte prefixLength, out string error) + public bool Setup(IPAddress address, IPAddress gateway, byte prefixLength, out string error) { this.address = address; this.prefixLength = prefixLength; diff --git a/linker/plugins/tuntap/TuntapApiController.cs b/linker/plugins/tuntap/TuntapApiController.cs index 8af97b18..530cae46 100644 --- a/linker/plugins/tuntap/TuntapApiController.cs +++ b/linker/plugins/tuntap/TuntapApiController.cs @@ -64,7 +64,7 @@ namespace linker.plugins.tuntap /// public void Refresh(ApiControllerParamsInfo param) { - tuntapTransfer.Refresh(); + tuntapTransfer.RefreshConfig(); } /// @@ -77,8 +77,8 @@ namespace linker.plugins.tuntap //运行自己的 if (param.Content == config.Data.Client.Id) { - tuntapTransfer.Stop(); - tuntapTransfer.Run(); + tuntapTransfer.Shutdown(); + tuntapTransfer.Setup(); } else { @@ -102,7 +102,7 @@ namespace linker.plugins.tuntap //停止自己的 if (param.Content == config.Data.Client.Id) { - tuntapTransfer.Stop(); + tuntapTransfer.Shutdown(); } else { @@ -124,7 +124,7 @@ namespace linker.plugins.tuntap //更新自己的 if (info.MachineId == config.Data.Client.Id) { - tuntapTransfer.OnUpdate(info); + tuntapTransfer.UpdateConfig(info); } else { diff --git a/linker/plugins/tuntap/TuntapTransfer.cs b/linker/plugins/tuntap/TuntapTransfer.cs index 976b98f7..34850fdb 100644 --- a/linker/plugins/tuntap/TuntapTransfer.cs +++ b/linker/plugins/tuntap/TuntapTransfer.cs @@ -44,45 +44,32 @@ namespace linker.plugins.tuntap this.tuntapProxy = tuntapProxy; this.runningConfig = runningConfig; - Shutdown(); + linkerTunDeviceAdapter.Shutdown(); linkerTunDeviceAdapter.Clear(); linkerTunDeviceAdapter.SetReadCallback(tuntapProxy); + AppDomain.CurrentDomain.ProcessExit += (s, e) => linkerTunDeviceAdapter.Shutdown(); + Console.CancelKeyPress += (s, e) => linkerTunDeviceAdapter.Shutdown(); clientSignInState.NetworkEnabledHandle += (times) => { - OnChange(); + NotifyConfig(); }; clientSignInState.NetworkFirstEnabledHandle += () => { - OnChange(); + NotifyConfig(); + CheckTuntapStatusTask(); if (runningConfig.Data.Tuntap.Running) { - Stop(); Run(); - _ = CheckVeaStatusTask(); + Setup(); } }; - - AppDomain.CurrentDomain.ProcessExit += (s, e) => Shutdown(); - Console.CancelKeyPress += (s, e) => Shutdown(); - GetRouteIps(); } - /// - /// 程序关闭 - /// - private void Shutdown() - { - bool running = runningConfig.Data.Tuntap.Running; - Stop(); - runningConfig.Data.Tuntap.Running = running; - runningConfig.Data.Update(); - } - /// /// 运行网卡 /// - public void Run() + public void Setup() { if (Interlocked.CompareExchange(ref operating, 1, 0) == 1) { @@ -90,14 +77,14 @@ namespace linker.plugins.tuntap } Task.Run(() => { - OnChange(); + NotifyConfig(); try { if (runningConfig.Data.Tuntap.IP.Equals(IPAddress.Any)) { return; } - linkerTunDeviceAdapter.SetUp(interfaceName, runningConfig.Data.Tuntap.IP, 24); + linkerTunDeviceAdapter.Setup(interfaceName, runningConfig.Data.Tuntap.IP, 24); if (string.IsNullOrWhiteSpace(linkerTunDeviceAdapter.Error)) { linkerTunDeviceAdapter.SetMtu(1416); @@ -117,14 +104,14 @@ namespace linker.plugins.tuntap finally { Interlocked.Exchange(ref operating, 0); - OnChange(); + NotifyConfig(); } }); } /// /// 停止网卡 /// - public void Stop() + public void Shutdown() { if (Interlocked.CompareExchange(ref operating, 1, 0) == 1) { @@ -132,8 +119,8 @@ namespace linker.plugins.tuntap } try { - OnChange(); - linkerTunDeviceAdapter.Shutdown(3); + NotifyConfig(); + linkerTunDeviceAdapter.Shutdown(); runningConfig.Data.Tuntap.Running = Status == TuntapStatus.Running; runningConfig.Data.Update(); @@ -148,23 +135,22 @@ namespace linker.plugins.tuntap finally { Interlocked.Exchange(ref operating, 0); - OnChange(); + NotifyConfig(); } } /// /// 刷新信息,把自己的网卡配置发给别人,顺便把别人的网卡信息带回来 /// - public void Refresh() + public void RefreshConfig() { - OnChange(); + NotifyConfig(); } - /// /// 更新本机网卡信息 /// /// - public void OnUpdate(TuntapInfo info) + public void UpdateConfig(TuntapInfo info) { Task.Run(() => { @@ -175,12 +161,12 @@ namespace linker.plugins.tuntap runningConfig.Data.Update(); if (Status == TuntapStatus.Running) { - Stop(); - Run(); + Shutdown(); + Setup(); } else { - OnChange(); + NotifyConfig(); } }); } @@ -201,17 +187,16 @@ namespace linker.plugins.tuntap return GetLocalInfo(); } - /// /// 信息有变化,刷新信息,把自己的网卡配置发给别人,顺便把别人的网卡信息带回来 /// - private void OnChange() + private void NotifyConfig() { GetRemoteInfo().ContinueWith((result) => { if (result.Result == null) { - OnChange(); + NotifyConfig(); } else { @@ -303,6 +288,8 @@ namespace linker.plugins.tuntap } } + + List routeIps = new List(); private List ParseIPs(List infos) { uint[] localIps = NetworkHelper.GetIPV4() @@ -349,32 +336,31 @@ namespace linker.plugins.tuntap Broadcast = ipInt | (~maskValue), OriginIPAddress = ip, }; - } - - - List routeIps = new List(); + } private void GetRouteIps() { NetworkHelper.GetRouteLevel(out routeIps); } - private async Task CheckVeaStatusTask() + private void CheckTuntapStatusTask() { - while (true) + Task.Run(async () => { - try + while (true) { - if (runningConfig.Data.Tuntap.Running && OperatingSystem.IsWindows()) + await Task.Delay(15000).ConfigureAwait(false); + try + { + if (runningConfig.Data.Tuntap.Running && OperatingSystem.IsWindows()) + { + await CheckInterface().ConfigureAwait(false); + } + } + catch (Exception) { - await CheckInterface().ConfigureAwait(false); } } - catch (Exception) - { - } - - await Task.Delay(15000).ConfigureAwait(false); - } + }); } private async Task CheckInterface() { @@ -383,13 +369,13 @@ namespace linker.plugins.tuntap if (networkInterface == null || networkInterface.OperationalStatus != OperationalStatus.Up) { LoggerHelper.Instance.Error($"tuntap inerface {interfaceName} is {networkInterface?.OperationalStatus ?? OperationalStatus.Unknown}, restarting"); - Stop(); + Shutdown(); await Task.Delay(5000).ConfigureAwait(false); networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(c => c.Name == interfaceName); if (networkInterface == null || networkInterface.OperationalStatus != OperationalStatus.Up) { - Run(); + Setup(); } } } diff --git a/linker/plugins/tuntap/messenger/TuntapMessenger.cs b/linker/plugins/tuntap/messenger/TuntapMessenger.cs index e9695d58..f6ad4463 100644 --- a/linker/plugins/tuntap/messenger/TuntapMessenger.cs +++ b/linker/plugins/tuntap/messenger/TuntapMessenger.cs @@ -20,8 +20,8 @@ namespace linker.plugins.tuntap.messenger [MessengerId((ushort)TuntapMessengerIds.Run)] public void Run(IConnection connection) { - tuntapTransfer.Stop(); - tuntapTransfer.Run(); + tuntapTransfer.Shutdown(); + tuntapTransfer.Setup(); } /// @@ -31,7 +31,7 @@ namespace linker.plugins.tuntap.messenger [MessengerId((ushort)TuntapMessengerIds.Stop)] public void Stop(IConnection connection) { - tuntapTransfer.Stop(); + tuntapTransfer.Shutdown(); } /// @@ -42,7 +42,7 @@ namespace linker.plugins.tuntap.messenger public void Update(IConnection connection) { TuntapInfo info = MemoryPackSerializer.Deserialize(connection.ReceiveRequestWrap.Payload.Span); - tuntapTransfer.OnUpdate(info); + tuntapTransfer.UpdateConfig(info); } ///