From 5bb4bb31ba9dfa8e46df7c11ff4f5352da5347d7 Mon Sep 17 00:00:00 2001
From: snltty <1069410172@qq.com>
Date: Wed, 24 Jul 2024 16:26:33 +0800
Subject: [PATCH] test
---
 .github/workflows/dotnet.yml                  |  5 +-
 .github/workflows/nuget.yml                   |  4 +-
 linker.libs/linker.libs.csproj                |  6 +-
 linker.service/linker.service.csproj          |  6 +-
 linker.tunnel/TunnelTransfer.cs               | 18 ++--
 linker.tunnel/linker.tunnel.csproj            |  6 +-
 linker/linker.csproj                          |  6 +-
 linker/plugins/tunnel/TunnelAdapter.cs        | 12 +--
 linker/plugins/tunnel/TunnelApiController.cs  |  9 +-
 linker/plugins/tunnel/TunnelConfigTransfer.cs | 35 +-------
 linker/plugins/tunnel/TunnelStartup.cs        |  7 ++
 .../tunnel/excludeip/ExcludeIPTransfer.cs     | 85 +++++++++++++++++++
 linker/plugins/tunnel/excludeip/IExcludeIP.cs | 15 ++++
 linker/plugins/tuntap/ExcludeIP.cs            | 21 +++++
 linker/plugins/tuntap/TuntapStartup.cs        |  5 +-
 15 files changed, 174 insertions(+), 66 deletions(-)
 create mode 100644 linker/plugins/tunnel/excludeip/ExcludeIPTransfer.cs
 create mode 100644 linker/plugins/tunnel/excludeip/IExcludeIP.cs
 create mode 100644 linker/plugins/tuntap/ExcludeIP.cs
diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml
index 046431fa..2aa8f046 100644
--- a/.github/workflows/dotnet.yml
+++ b/.github/workflows/dotnet.yml
@@ -48,13 +48,14 @@ jobs:
       env:
           GITHUB_TOKEN: '${{ secrets.ACTIONS_TOKEN }}'
       with:
-        tag_name: v1.1.2.6
-        release_name: v1.1.2.6.${{ steps.date.outputs.today }}
+        tag_name: v1.1.3.1
+        release_name: v1.1.3.1.${{ steps.date.outputs.today }}
         draft: false
         prerelease: false
         body: |
           1. 托盘检查
           2. 停止服务清理网卡
+          3. 打洞错误
 
     - name: upload win x64
       id: upload-win-x64
diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml
index 6d2444c1..66b22ec9 100644
--- a/.github/workflows/nuget.yml
+++ b/.github/workflows/nuget.yml
@@ -36,5 +36,5 @@ jobs:
 
     - name: Push
       run: |
-        nuget push ./linker.tunnel/bin/release/linker.tunnel.1.1.2.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NUGET_KEY }} -NoSymbol
-        nuget push ./linker.libs/bin/release/linker.libs.1.1.2.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NUGET_KEY }} -NoSymbol
+        nuget push ./linker.tunnel/bin/release/linker.tunnel.1.1.3.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NUGET_KEY }} -NoSymbol
+        nuget push ./linker.libs/bin/release/linker.libs.1.1.3.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NUGET_KEY }} -NoSymbol
diff --git a/linker.libs/linker.libs.csproj b/linker.libs/linker.libs.csproj
index 21013f8f..436ec1bc 100644
--- a/linker.libs/linker.libs.csproj
+++ b/linker.libs/linker.libs.csproj
@@ -14,9 +14,9 @@
 		snltty
 		https://github.com/snltty/linker
 		https://github.com/snltty/linker
-		1.1.2
-		1.1.2.6
-		1.1.2.6
+		1.1.3
+		1.1.3.1
+		1.1.3.1
 	
 	
 		full
diff --git a/linker.service/linker.service.csproj b/linker.service/linker.service.csproj
index 81185c7b..ec4a4131 100644
--- a/linker.service/linker.service.csproj
+++ b/linker.service/linker.service.csproj
@@ -14,7 +14,7 @@
 		false
 		true
 		true
-		1.1.2
+		1.1.3
 		snltty
 		snltty
 		snltty
@@ -22,8 +22,8 @@
 		https://github.com/snltty/linker
 		https://github.com/snltty/linker
 		snltty service
-		1.1.2.6
-		1.1.2.6
+		1.1.3.1
+		1.1.3.1
 	
 
 	
diff --git a/linker.tunnel/TunnelTransfer.cs b/linker.tunnel/TunnelTransfer.cs
index 74443f9d..dbf06a1a 100644
--- a/linker.tunnel/TunnelTransfer.cs
+++ b/linker.tunnel/TunnelTransfer.cs
@@ -352,15 +352,17 @@ namespace linker.tunnel
         private void ParseRemoteEndPoint(TunnelTransportInfo tunnelTransportInfo)
         {
             //要连接哪些IP
-            IPAddress[] localIps = tunnelTransportInfo.Remote.LocalIps.Where(c => c.Equals(tunnelTransportInfo.Remote.Local.Address) == false).ToArray();
             List eps = new List();
 
             //先尝试内网ipv4
-            foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetwork))
+            //if (tunnelTransportInfo.Local.Remote.Address.Equals(tunnelTransportInfo.Remote.Remote.Address))
             {
-                eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
-                eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
-                eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
+                foreach (IPAddress item in tunnelTransportInfo.Remote.LocalIps.Where(c => c.AddressFamily == AddressFamily.InterNetwork))
+                {
+                    eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
+                    eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
+                    eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port + 1));
+                }
             }
             //在尝试外网
             eps.AddRange(new List{
@@ -368,7 +370,7 @@ namespace linker.tunnel
                 new IPEndPoint(tunnelTransportInfo.Remote.Remote.Address,tunnelTransportInfo.Remote.Remote.Port+1),
             });
             //再尝试IPV6
-            foreach (IPAddress item in localIps.Where(c => c.AddressFamily == AddressFamily.InterNetworkV6))
+            foreach (IPAddress item in tunnelTransportInfo.Remote.LocalIps.Where(c => c.AddressFamily == AddressFamily.InterNetworkV6))
             {
                 eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Local.Port));
                 eps.Add(new IPEndPoint(item, tunnelTransportInfo.Remote.Remote.Port));
@@ -398,7 +400,7 @@ namespace linker.tunnel
         /// 
         /// 
         /// 
-        public void StartBackground(string remoteMachineId, string transactionId,int times = 10)
+        public void StartBackground(string remoteMachineId, string transactionId, int times = 10)
         {
             if (AddBackground(remoteMachineId, transactionId) == false)
             {
@@ -432,7 +434,7 @@ namespace linker.tunnel
         }
         private bool AddBackground(string remoteMachineId, string transactionId)
         {
-           return backgroundDic.TryAdd(GetBackgroundKey(remoteMachineId, transactionId), true);
+            return backgroundDic.TryAdd(GetBackgroundKey(remoteMachineId, transactionId), true);
         }
         private void RemoveBackground(string remoteMachineId, string transactionId)
         {
diff --git a/linker.tunnel/linker.tunnel.csproj b/linker.tunnel/linker.tunnel.csproj
index 0156fa92..87e1bd01 100644
--- a/linker.tunnel/linker.tunnel.csproj
+++ b/linker.tunnel/linker.tunnel.csproj
@@ -9,7 +9,7 @@
 	  true
 	  true
 	  linker tunnel
-	  1.1.2
+	  1.1.3
 	  snltty
 	  snltty
 	  linker tunnel
@@ -17,8 +17,8 @@
 	  https://github.com/snltty/linker
 	  https://github.com/snltty/linker
 	  linker tunnel
-	  1.1.2.6
-	  1.1.2.6
+	  1.1.3.1
+	  1.1.3.1
   
 
 	
diff --git a/linker/linker.csproj b/linker/linker.csproj
index 03b1400a..167b91a4 100644
--- a/linker/linker.csproj
+++ b/linker/linker.csproj
@@ -17,7 +17,7 @@
 		true
 		false
 		linker
-		1.1.2
+		1.1.3
 		snltty
 		snltty
 		linker
@@ -25,8 +25,8 @@
 		https://github.com/snltty/linker
 		https://github.com/snltty/linker
 		linker
-		1.1.2.6
-		1.1.2.6
+		1.1.3.1
+		1.1.3.1
 	
 
 
diff --git a/linker/plugins/tunnel/TunnelAdapter.cs b/linker/plugins/tunnel/TunnelAdapter.cs
index 3784f1ae..62118815 100644
--- a/linker/plugins/tunnel/TunnelAdapter.cs
+++ b/linker/plugins/tunnel/TunnelAdapter.cs
@@ -11,6 +11,7 @@ using linker.tunnel.wanport;
 using System.Buffers.Binary;
 using linker.plugins.client;
 using linker.plugins.messenger;
+using linker.plugins.tunnel.excludeip;
 
 namespace linker.plugins.tunnel
 {
@@ -25,17 +26,19 @@ namespace linker.plugins.tunnel
         private readonly FileConfig config;
         private readonly RunningConfig running;
         private readonly RunningConfigTransfer runningConfigTransfer;
+        private readonly ExcludeIPTransfer excludeIPTransfer;
 
         private string wanPortConfigKey = "tunnelWanPortProtocols";
         private string transportConfigKey = "tunnelTransports";
 
-        public TunnelAdapter(ClientSignInState clientSignInState, MessengerSender messengerSender, FileConfig config, RunningConfig running, RunningConfigTransfer runningConfigTransfer)
+        public TunnelAdapter(ClientSignInState clientSignInState, MessengerSender messengerSender, FileConfig config, RunningConfig running, RunningConfigTransfer runningConfigTransfer, ExcludeIPTransfer excludeIPTransfer)
         {
             this.clientSignInState = clientSignInState;
             this.messengerSender = messengerSender;
             this.config = config;
             this.running = running;
             this.runningConfigTransfer = runningConfigTransfer;
+            this.excludeIPTransfer = excludeIPTransfer;
 
             string path = Path.GetFullPath(config.Data.Client.Certificate);
             if (File.Exists(path))
@@ -104,16 +107,15 @@ namespace linker.plugins.tunnel
 
         public NetworkInfo GetLocalConfig()
         {
+            var excludeips = excludeIPTransfer.Get();
             return new NetworkInfo
             {
-                LocalIps = config.Data.Client.Tunnel.LocalIPs
-                .Where(c => c.Equals(running.Data.Tuntap.IP) == false)
-                .Where(c =>
+                LocalIps = config.Data.Client.Tunnel.LocalIPs.Where(c =>
                 {
                     if (c.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                     {
                         uint ip = BinaryPrimitives.ReadUInt32BigEndian(c.GetAddressBytes());
-                        foreach (var item in running.Data.Tunnel.ExcludeIPs)
+                        foreach (var item in excludeips)
                         {
                             uint maskValue = NetworkHelper.MaskValue(item.Mask);
                             uint ip1 = BinaryPrimitives.ReadUInt32BigEndian(item.IPAddress.GetAddressBytes());
diff --git a/linker/plugins/tunnel/TunnelApiController.cs b/linker/plugins/tunnel/TunnelApiController.cs
index 1f13792c..ebd9d7a9 100644
--- a/linker/plugins/tunnel/TunnelApiController.cs
+++ b/linker/plugins/tunnel/TunnelApiController.cs
@@ -12,6 +12,7 @@ using linker.plugins.client;
 using linker.plugins.server;
 using linker.plugins.capi;
 using linker.plugins.messenger;
+using linker.plugins.tunnel.excludeip;
 
 namespace linker.plugins.tunnel
 {
@@ -26,8 +27,9 @@ namespace linker.plugins.tunnel
         private readonly MessengerSender messengerSender;
         private readonly TunnelConfigTransfer tunnelConfigTransfer;
         private readonly ITunnelAdapter tunnelMessengerAdapter;
+        private readonly ExcludeIPTransfer excludeIPTransfer;
 
-        public TunnelApiController(FileConfig config, TunnelWanPortTransfer compactTransfer, ClientSignInState clientSignInState, MessengerSender messengerSender, TunnelConfigTransfer tunnelConfigTransfer, ITunnelAdapter tunnelMessengerAdapter)
+        public TunnelApiController(FileConfig config, TunnelWanPortTransfer compactTransfer, ClientSignInState clientSignInState, MessengerSender messengerSender, TunnelConfigTransfer tunnelConfigTransfer, ITunnelAdapter tunnelMessengerAdapter, ExcludeIPTransfer excludeIPTransfer)
         {
             this.config = config;
             this.compactTransfer = compactTransfer;
@@ -35,6 +37,7 @@ namespace linker.plugins.tunnel
             this.messengerSender = messengerSender;
             this.tunnelConfigTransfer = tunnelConfigTransfer;
             this.tunnelMessengerAdapter = tunnelMessengerAdapter;
+            this.excludeIPTransfer = excludeIPTransfer;
         }
 
         /// 
@@ -134,12 +137,12 @@ namespace linker.plugins.tunnel
 
         public ExcludeIPItem[] GetExcludeIPs(ApiControllerParamsInfo param)
         {
-            return tunnelConfigTransfer.GetExcludeIPs();
+            return excludeIPTransfer.GetExcludeIPs();
         }
         public void SetExcludeIPs(ApiControllerParamsInfo param)
         {
             ExcludeIPItem[] info = param.Content.DeJson();
-            tunnelConfigTransfer.SettExcludeIPs(info);
+            excludeIPTransfer.SettExcludeIPs(info);
         }
 
         public sealed class TunnelListInfo
diff --git a/linker/plugins/tunnel/TunnelConfigTransfer.cs b/linker/plugins/tunnel/TunnelConfigTransfer.cs
index 6aaaa7e4..22469cd0 100644
--- a/linker/plugins/tunnel/TunnelConfigTransfer.cs
+++ b/linker/plugins/tunnel/TunnelConfigTransfer.cs
@@ -21,7 +21,7 @@ namespace linker.plugins.tunnel
         private readonly RunningConfigTransfer runningConfigTransfer;
         private readonly ITunnelAdapter tunnelAdapter;
 
-        private string exipConfigKey = "excludeIPConfig";
+       
 
         private uint version = 0;
         public uint ConfigVersion => version;
@@ -39,7 +39,7 @@ namespace linker.plugins.tunnel
 
 
             InitRouteLevel();
-            InitExcludeIP();
+           
             InitConfig();
 
             TestQuic();
@@ -147,37 +147,6 @@ namespace linker.plugins.tunnel
         }
 
 
-        private void InitExcludeIP()
-        {
-            clientSignInState.NetworkFirstEnabledHandle += () =>
-            {
-                SyncExcludeIP();
-            };
-            runningConfigTransfer.Setter(exipConfigKey, SettExcludeIPs);
-            runningConfigTransfer.Getter(exipConfigKey, () => MemoryPackSerializer.Serialize(GetExcludeIPs()));
-        }
-        private void SyncExcludeIP()
-        {
-            runningConfigTransfer.Sync(exipConfigKey, MemoryPackSerializer.Serialize(running.Data.Tunnel.ExcludeIPs));
-        }
-        public ExcludeIPItem[] GetExcludeIPs()
-        {
-            return running.Data.Tunnel.ExcludeIPs;
-        }
-        public void SettExcludeIPs(ExcludeIPItem[] ips)
-        {
-            running.Data.Tunnel.ExcludeIPs = ips;
-            running.Data.Update();
-            runningConfigTransfer.IncrementVersion(exipConfigKey);
-            SyncExcludeIP();
-        }
-        private void SettExcludeIPs(Memory data)
-        {
-            running.Data.Tunnel.ExcludeIPs = MemoryPackSerializer.Deserialize(data.Span);
-            running.Data.Update();
-        }
-
-
         bool reboot = false;
         private void TestQuic()
         {
diff --git a/linker/plugins/tunnel/TunnelStartup.cs b/linker/plugins/tunnel/TunnelStartup.cs
index 70bc6ed9..55f5ed2a 100644
--- a/linker/plugins/tunnel/TunnelStartup.cs
+++ b/linker/plugins/tunnel/TunnelStartup.cs
@@ -10,6 +10,7 @@ using Microsoft.Extensions.DependencyInjection;
 using System.Net;
 using System.Reflection;
 using linker.tunnel.wanport;
+using linker.plugins.tunnel.excludeip;
 
 namespace linker.plugins.tunnel
 {
@@ -54,6 +55,8 @@ namespace linker.plugins.tunnel
             serviceCollection.AddSingleton();
             serviceCollection.AddSingleton();
 
+            
+            serviceCollection.AddSingleton();
             serviceCollection.AddSingleton();
             serviceCollection.AddSingleton();
 
@@ -93,6 +96,10 @@ namespace linker.plugins.tunnel
             tunnel.Init(compack, tunnelAdapter, transports);
 
             TunnelConfigTransfer tunnelConfigTransfer = serviceProvider.GetService();
+
+
+            ExcludeIPTransfer excludeIPTransfer = serviceProvider.GetService();
+            excludeIPTransfer.Load(assemblies);
         }
 
         public void UseServer(ServiceProvider serviceProvider, FileConfig config, Assembly[] assemblies)
diff --git a/linker/plugins/tunnel/excludeip/ExcludeIPTransfer.cs b/linker/plugins/tunnel/excludeip/ExcludeIPTransfer.cs
new file mode 100644
index 00000000..0ed2a655
--- /dev/null
+++ b/linker/plugins/tunnel/excludeip/ExcludeIPTransfer.cs
@@ -0,0 +1,85 @@
+using linker.client.config;
+using linker.libs;
+using linker.plugins.client;
+using MemoryPack;
+using Microsoft.Extensions.DependencyInjection;
+using System.Reflection;
+
+namespace linker.plugins.tunnel.excludeip
+{
+    public sealed class ExcludeIPTransfer
+    {
+        private List excludeIPs;
+        private string exipConfigKey = "excludeIPConfig";
+
+        private readonly RunningConfig running;
+        private readonly ClientSignInState clientSignInState;
+        private readonly RunningConfigTransfer runningConfigTransfer;
+
+        private readonly ServiceProvider serviceProvider;
+        public ExcludeIPTransfer(RunningConfig running, ClientSignInState clientSignInState, RunningConfigTransfer runningConfigTransfer, ServiceProvider serviceProvider)
+        {
+            this.running = running;
+            this.clientSignInState = clientSignInState;
+            this.runningConfigTransfer = runningConfigTransfer;
+            this.serviceProvider = serviceProvider;
+            InitExcludeIP();
+        }
+
+        public void Load(Assembly[] assembs)
+        {
+            IEnumerable types = ReflectionHelper.GetInterfaceSchieves(assembs, typeof(IExcludeIP));
+            excludeIPs = types.Select(c => (IExcludeIP)serviceProvider.GetService(c)).Where(c => c != null).ToList();
+
+            LoggerHelper.Instance.Warning($"load tunnel excludeips :{string.Join(",", types.Select(c => c.Name))}");
+        }
+
+        public List Get()
+        {
+            List result = new List();
+            foreach (var item in excludeIPs)
+            {
+                var ips = item.Get();
+                if (ips != null && ips.Length > 0)
+                {
+                    result.AddRange(ips);
+                }
+            }
+            if (running.Data.Tunnel.ExcludeIPs.Length > 0)
+            {
+                result.AddRange(running.Data.Tunnel.ExcludeIPs);
+            }
+            return result;
+        }
+
+        private void InitExcludeIP()
+        {
+            clientSignInState.NetworkFirstEnabledHandle += () =>
+            {
+                SyncExcludeIP();
+            };
+            runningConfigTransfer.Setter(exipConfigKey, SettExcludeIPs);
+            runningConfigTransfer.Getter(exipConfigKey, () => MemoryPackSerializer.Serialize(GetExcludeIPs()));
+        }
+        private void SyncExcludeIP()
+        {
+            runningConfigTransfer.Sync(exipConfigKey, MemoryPackSerializer.Serialize(running.Data.Tunnel.ExcludeIPs));
+        }
+        public ExcludeIPItem[] GetExcludeIPs()
+        {
+            return running.Data.Tunnel.ExcludeIPs;
+        }
+        public void SettExcludeIPs(ExcludeIPItem[] ips)
+        {
+            running.Data.Tunnel.ExcludeIPs = ips;
+            running.Data.Update();
+            runningConfigTransfer.IncrementVersion(exipConfigKey);
+            SyncExcludeIP();
+        }
+        private void SettExcludeIPs(Memory data)
+        {
+            running.Data.Tunnel.ExcludeIPs = MemoryPackSerializer.Deserialize(data.Span);
+            running.Data.Update();
+        }
+    }
+}
diff --git a/linker/plugins/tunnel/excludeip/IExcludeIP.cs b/linker/plugins/tunnel/excludeip/IExcludeIP.cs
new file mode 100644
index 00000000..4a142cfb
--- /dev/null
+++ b/linker/plugins/tunnel/excludeip/IExcludeIP.cs
@@ -0,0 +1,15 @@
+using linker.client.config;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace linker.plugins.tunnel.excludeip
+{
+    public interface IExcludeIP
+    {
+        public ExcludeIPItem[] Get();
+    }
+}
diff --git a/linker/plugins/tuntap/ExcludeIP.cs b/linker/plugins/tuntap/ExcludeIP.cs
new file mode 100644
index 00000000..d1230668
--- /dev/null
+++ b/linker/plugins/tuntap/ExcludeIP.cs
@@ -0,0 +1,21 @@
+using linker.client.config;
+using linker.plugins.tunnel.excludeip;
+
+namespace linker.plugins.tuntap
+{
+    public sealed class ExcludeIP : IExcludeIP
+    {
+        private readonly RunningConfig runningConfig;
+        public ExcludeIP(RunningConfig runningConfig)
+        {
+            this.runningConfig = runningConfig;
+        }
+        public ExcludeIPItem[] Get()
+        {
+            //网卡IP,和局域网IP。不参与打洞
+            return new ExcludeIPItem[] { new ExcludeIPItem { IPAddress = runningConfig.Data.Tuntap.IP, Mask = 32 } }
+            .Concat(runningConfig.Data.Tuntap.LanIPs.Select((c, index) => new ExcludeIPItem { IPAddress = c, Mask = (byte)runningConfig.Data.Tuntap.Masks[index] }))
+            .ToArray();
+        }
+    }
+}
diff --git a/linker/plugins/tuntap/TuntapStartup.cs b/linker/plugins/tuntap/TuntapStartup.cs
index beb0e50c..ff16ede2 100644
--- a/linker/plugins/tuntap/TuntapStartup.cs
+++ b/linker/plugins/tuntap/TuntapStartup.cs
@@ -16,7 +16,7 @@ namespace linker.plugins.tuntap
         public StartupLevel Level => StartupLevel.Normal;
         public string Name => "tuntap";
         public bool Required => false;
-        public string[] Dependent => new string[] {"messenger", "relay", "tunnel", "signin", "serialize", "config" };
+        public string[] Dependent => new string[] { "messenger", "relay", "tunnel", "signin", "serialize", "config" };
 
         public StartupLoadType LoadType => StartupLoadType.Normal;
 
@@ -33,6 +33,9 @@ namespace linker.plugins.tuntap
             serviceCollection.AddSingleton();
 
             serviceCollection.AddSingleton();
+
+
+            serviceCollection.AddSingleton();
         }
 
         public void AddServer(ServiceCollection serviceCollection, FileConfig config, Assembly[] assemblies)