首页 > 其他分享 >.NET借助虚拟网卡实现一个简单异地组网工具

.NET借助虚拟网卡实现一个简单异地组网工具

时间:2024-06-09 21:14:06浏览次数:14  
标签:const udpClient int bytes 网卡 var NET public 组网

由于工作需要,经常需要远程客户的服务器,但是并不是所有服务器都能开外网端口,使用向日葵等软件终究还是不太方便,于是找了很多工具,包括zerotier 等,但是由于服务器在国外等有时候还不同,

于是开始自己想办法研究一个属于自己的组网工具,最后找到snltty大佬的 https://github.com/snltty/p2p-tunnel ,学习后发现是基于tun2socks实现的,

tun2socks 的优点是 把虚拟网卡的数据都打包到socket代理了,但是过滤了ping (ICmp)的包,他自行返回了 成功,这不是我要的效果

于是看了一下tun2socks 的实现,是基于tun/tap实现的,于是研究了一下,手动基于tun/tap实现了一个简易的

核心代码

  1  [SupportedOSPlatform("windows")]
  2  public class WinTunDriveHostedService : TunDriveHostedService
  3  {
  4      private readonly static string DriverPath = AppDomain.CurrentDomain.BaseDirectory + "Drivers";
  5      private const string AdapterKey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
  6      private const string ConnectionKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
  7 
  8 
  9      public const int TAP_WIN_IOCTL_GET_MAC = 1;
 10      public const int TAP_WIN_IOCTL_GET_VERSION = 2;
 11      public const int TAP_WIN_IOCTL_GET_MTU = 3;
 12      public const int TAP_WIN_IOCTL_GET_INFO = 4;
 13      public const int TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT = 5;
 14      public const int TAP_WIN_IOCTL_SET_MEDIA_STATUS = 6;
 15      public const int TAP_WIN_IOCTL_CONFIG_DHCP_MASQ = 7;
 16      public const int TAP_WIN_IOCTL_GET_LOG_LINE = 8;
 17      public const int TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT = 9;
 18      public const int TAP_WIN_IOCTL_CONFIG_TUN = 10;
 19 
 20      public const uint FILE_ATTRIBUTE_SYSTEM = 0x4;
 21      public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
 22      public const uint METHOD_BUFFERED = 0;
 23      public const uint FILE_ANY_ACCESS = 0;
 24      public const uint FILE_DEVICE_UNKNOWN = 0x22;
 25      public WinTunDriveHostedService(IOptions<TunDriveConfig> tunDriveConfigOptions, ILogger<WinTunDriveHostedService> logger) : base(tunDriveConfigOptions, logger)
 26      {
 27      }
 28      [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 29      public static extern bool DeviceIoControl(SafeHandle device, uint IoControlCode, IntPtr InBuffer, uint InBufferSize, IntPtr OutBuffer, uint OutBufferSize, ref uint BytesReturned, IntPtr Overlapped);
 30 
 31 
 32      protected override FileStream OpenDrive()
 33      {
 34          var className = InstallOrGetClassNameDrive();
 35          var safeFileHandle = System.IO.File.OpenHandle($@"\\.\\Global\\{className}.tap", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, FileOptions.Asynchronous);
 36          return new FileStream(safeFileHandle, FileAccess.ReadWrite, 1500);
 37      }
 38      protected virtual string InstallOrGetClassNameDrive()
 39      {
 40          using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(ConnectionKey))
 41          {
 42              var names = registryKey.GetSubKeyNames();
 43              foreach (var name in names)
 44              {
 45                  using (var connectionRegistryKey = registryKey.OpenSubKey(name).OpenSubKey("Connection"))
 46                  {
 47                      if (connectionRegistryKey != null && connectionRegistryKey.GetValue("Name").ToString() == TunDriveConfig.TunDriveName)
 48                      {
 49                          return name;
 50                      }
 51                  }
 52              }
 53 
 54              Directory.CreateDirectory(DriverPath);
 55              ZipArchive zipArchive = new ZipArchive(typeof(WinTunDriveHostedService).Assembly.GetManifestResourceStream($"RemoteNetwork.{(Environment.Is64BitOperatingSystem ? "amd64" : "i386")}.zip"), ZipArchiveMode.Read);
 56              foreach (ZipArchiveEntry entry in zipArchive.Entries)
 57              {
 58                  entry.ExtractToFile(Path.Combine(DriverPath, entry.FullName), overwrite: true);
 59              }
 60              StartProcess(Path.Combine(DriverPath, "tapinstall.exe"), $"install OemVista.inf TAP0901", "runas", DriverPath);
 61              foreach (var name in registryKey.GetSubKeyNames())
 62              {
 63                  if (!names.Contains(name))
 64                  {
 65                      using (var connectionRegistryKey = registryKey.OpenSubKey(name).OpenSubKey("Connection"))
 66                      {
 67                          if (connectionRegistryKey != null)
 68                          {
 69                              StartProcess("netsh", @$"interface set interface name=""{connectionRegistryKey.GetValue("Name")}"" newname=""{TunDriveConfig.TunDriveName}""");
 70                              return name;
 71                          }
 72                      }
 73                  }
 74              }
 75              return string.Empty;
 76          }
 77      }
 78      private static int ParseIP(string address)
 79      {
 80          byte[] addressBytes = address.Split('.').Select(s => byte.Parse(s)).ToArray();
 81          return addressBytes[0] | (addressBytes[1] << 8) | (addressBytes[2] << 16) | (addressBytes[3] << 24);
 82      }
 83      protected override void ConfigIP(string ip, string netmask)
 84      {
 85          StartProcess("netsh", $"interface ip set address name=\"{TunDriveConfig.TunDriveName}\" source=static addr={ip} mask={netmask} gateway=none");
 86          IntPtr intPtr = Marshal.AllocHGlobal(12);
 87          Marshal.WriteInt32(intPtr, 0, ParseIP(ip));
 88          Marshal.WriteInt32(intPtr, 4, 0);
 89          Marshal.WriteInt32(intPtr, 8,0);
 90          uint lpBytesReturned = 0;
 91          bool result = DeviceIoControl(TunStream.SafeFileHandle, 2228264, intPtr, 12u, intPtr, 12u, ref lpBytesReturned, IntPtr.Zero);
 92          Marshal.FreeHGlobal(intPtr);
 93      }
 94      private static uint CTL_CODE(uint iDeviceType, uint iFunction, uint iMethod, uint iAccess)
 95      {
 96          return ((iDeviceType << 16) | (iAccess << 14) | (iFunction << 2) | iMethod);
 97      }
 98      public override bool ConnectionState(bool connection)
 99      {
100          uint Length = 0;
101          IntPtr cconfig = Marshal.AllocHGlobal(4);
102          Marshal.WriteInt32(cconfig, connection ? 1 : 0);
103 
104          var b = DeviceIoControl(TunStream.SafeFileHandle, CTL_CODE(FILE_DEVICE_UNKNOWN, TAP_WIN_IOCTL_SET_MEDIA_STATUS, METHOD_BUFFERED, FILE_ANY_ACCESS), cconfig, 4, cconfig, 4, ref Length, IntPtr.Zero);
105          StartProcess("netsh", $"netsh interface ipv4 set subinterface \"{TunDriveConfig.TunDriveName}\" mtu=\"1400\" store=persistent");
106          return b;
107      }
108  }

 

liunx 核心代码

 1 public class TunNetWorkFrameHostedService : BackgroundService
 2 {
 3     private  readonly string exchangeHostName = "";
 4     private readonly int P2PPort = 61000;
 5     protected readonly ILogger<TunNetWorkFrameHostedService> _logger;
 6     public static TunNetWorkFrameHostedService Instance { get; private set; }
 7     private readonly UdpClient udpClient;
 8     private readonly System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(0, 0);
 9     public TunNetWorkFrameHostedService(ILogger<TunNetWorkFrameHostedService> logger, IOptions<TunDriveConfig> tunDriveConfigOptions)
10     {
11         exchangeHostName = tunDriveConfigOptions.Value.DataExchangeHostName;
12         _logger = logger;
13         Instance = this;
14         udpClient = new UdpClient(0); if (Environment.OSVersion.Platform == PlatformID.Win32NT)
15         {
16             const int SIP_UDP_CONNRESET = -1744830452;
17             udpClient.Client.IOControl(SIP_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null);
18         }
19     }
20 
21 
22     protected override async Task ExecuteAsync(CancellationToken stoppingToken)
23     {
24         udpClient.BeginReceive(ReceiveCallback, udpClient);
25         while (!stoppingToken.IsCancellationRequested)
26         {
27             await udpClient.SendAsync(TunDriveHostedService.Instance.Id, exchangeHostName, P2PPort, stoppingToken).ConfigureAwait(false);
28             await Task.Delay(1000*30, stoppingToken).ConfigureAwait(false);
29         }
30     }
31     void ReceiveCallback(IAsyncResult ar)
32     {
33         System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(0, 0);
34         byte[] bytes = null;
35         try
36         {
37 
38             bytes = udpClient.EndReceive(ar, ref remoteEndPoint);
39 
40         }
41         finally
42         {
43             udpClient.BeginReceive(ReceiveCallback, udpClient);
44         }
45         if (bytes.Length == 4)
46         {
47             return;
48         }
49         if (bytes.Length == 5)
50         {
51             if (bytes[0] == 2)
52             {
53                 P2PUDPSocketHostedService.Instance.TestP2P(bytes.Skip(1).ToArray(),false);
54             }
55             return;
56         }
57        
58         TunDriveHostedService.Instance.WriteFrameBuffer(bytes);
59     }
60     public virtual async Task WriteFrameBufferAsync(Memory<byte> buffer, CancellationToken stoppingToken)
61     { 
62         var destId = BitConverter.ToInt32(buffer.Slice(16, 4).ToArray(), 0);
63 
64        var tunNetWorkFrameSend= P2PUDPSocketHostedService.Instance.GetP2PClient(buffer.Slice(16, 4).ToArray());
65         if (tunNetWorkFrameSend != null)
66         {
67             await tunNetWorkFrameSend.SendAsync(buffer, stoppingToken).ConfigureAwait(false);
68             return;
69         }
70         var bytes = new byte[buffer.Length + 8];
71         buffer.Slice(12, 8).CopyTo(bytes);
72         Array.Copy(buffer.ToArray(), 0,bytes,8,buffer.Length);
73         await udpClient.SendAsync(bytes, exchangeHostName, P2PPort, stoppingToken).ConfigureAwait(false);
74         //var destId = BitConverter.ToInt32(buffer.Slice(16, 4).ToArray(), 0);// string.Join(".", buffer.Slice(16, 4).ToArray());// span[16] << 24 | span[17] << 16 | span[18] << 8 | span[19];
75         //var sourceId = BitConverter.ToInt32(buffer.Slice(12, 4).ToArray(), 0);
76         //_logger.LogInformation($"{sourceId} 发送到{destId}");
77     }
78     /// <summary>
79     /// 发送打洞请求
80     /// </summary>
81     /// <param name="destId"></param>
82     /// <param name="stoppingToken"></param>
83     /// <returns></returns>
84     public virtual async Task SendP2PRequestAsync(byte[] destId, CancellationToken stoppingToken)
85     {
86         using (MemoryStream memoryStream = new MemoryStream()) {
87             memoryStream.Write(TunDriveHostedService.Instance.Id);
88             memoryStream.Write(destId);
89             memoryStream.WriteByte(2);
90             memoryStream.Write(TunDriveHostedService.Instance.Id);
91             await udpClient.SendAsync(memoryStream.ToArray(), exchangeHostName, P2PPort, stoppingToken).ConfigureAwait(false);
92         }
93         
94     }
95 }
View Code

 

以下是远程桌面的效果

客户端运行

 打洞成功

 测速

 

代码地址

https://github.com/hn-lyf/RemoteNetwork

测试客户端

 

 https://files.cnblogs.com/files/dotnet-org-cn/linux-x64.zip?t=1717937932&download=true

https://files.cnblogs.com/files/dotnet-org-cn/win-x64.zip?t=1717937932&download=true

https://files.cnblogs.com/files/dotnet-org-cn/win-x86.zip?t=1717937932&download=true

标签:const,udpClient,int,bytes,网卡,var,NET,public,组网
From: https://www.cnblogs.com/dotnet-org-cn/p/18239999

相关文章

  • 西门子PROFINET信息安全介绍
    PROFINET信息安全●  自始至终的,经过验证的全面安全概念●  用于PROFINET的额外保护措施,以满足日益增长的需求●  可扩展的架构以适应不同的应用概述PROFINET信息安全-简单实用的安全概念   第一个PROFINET规范发布的时候,PI就同期发布了全面的安全理念......
  • ASP.NET Core的开发效率利器HotReload(带例子)
    ASP.NETCoreHotReload示例在ASP.NETCore中,HotReload功能使开发者能够在不重新启动应用程序的情况下,动态地应用代码更改。这有助于提高开发效率,因为你可以立即看到代码更改的效果。以下是一个使用HotReload的示例:1.确保环境配置正确确保你已经安装了最新版本的......
  • C#/.NET/.NET Core优秀项目和框架
    C#/.NET/.NETCore优秀项目和框架2024年5月简报 前言公众号每月定期推广和分享的C#/.NET/.NETCore优秀项目和框架(每周至少会推荐两个优秀的项目和框架当然节假日除外),公众号推文中有项目和框架的介绍、功能特点、使用方式以及部分功能截图等(打不开或者打开GitHub很慢的同学......
  • 探索Linux网络利器:netstat命令
    探索Linux网络利器:netstat命令在Linux系统管理中,了解系统的网络状态是至关重要的。而netstat命令就是这样一个强大的工具,它可以帮助我们查看网络连接、路由表、接口统计等与网络相关的信息。下面,我们将详细探讨如何使用netstat命令以及它的各种选项。1.netstat命令简介n......
  • VMware ESXi 8.0U2c macOS Unlocker & OEM BIOS 集成网卡驱动 Marvell AQC 网卡定制版
    VMwareESXi8.0U2cmacOSUnlocker&OEMBIOS集成网卡驱动MarvellAQC网卡定制版VMwareESXi8.0U2cmacOSUnlocker&OEMBIOS集成网卡驱动和NVMe驱动(集成驱动版)发布ESXi8.0U2集成驱动版,在个人电脑上运行企业级工作负载请访问原文链接:VMwareESXi8.0U2cmacOS......
  • Fortinet FortiOS 6.4.15 (FortiGate For ESXi, Hyper-V, KVM, Xen) 下载
    FortinetFortiOS6.4.15(FortiGateForESXi,Hyper-V,KVM,Xen)下载下一代防火墙(NGFW)、虚拟化NGFW和云原生防火墙请访问原文链接:https://sysin.org/blog/fortios-6/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgFortinetFortiGateNGFWFortiGate:下一......
  • 通过 Docker 启动的 .NET Core 解决方案无法找到本地 Microsoft SQL Server
    我遇到的问题是,当我使用Docker启动.NETCore项目时,无法找到在本地(而不是作为容器)运行的MicrosoftSQLServer。SwaggerUI的响应如下所示。TypeError:NetworkErrorwhenattemptingtofetchresource.这是我的连接。这是我的连接字符串......
  • ASP.NET第五章 Application、Session和Cookie对象
    第五章Global全局的1.包含所有应用程序的配置设置2.存储所有事件的事件处理程序3.存储在应用程序的根目录下4.它的位置定义应用程序的限界Global.asax六大事件:1.Application_Start调用当前应用程序目录(或其子目录)下的第一个ASP.NET页面时触发2.ApplicationEnd......
  • Linux命令 (network statistics -all numeric programs | Global Regular Expression P
    文章目录1、第一种解释2、第二种解释3、第三种解释4、第四种解释5、第五种解释6、netstat--help在Windows中,杀死端口占用的博客链接在Linux中,grep的英文全称是GlobalRegularExpressionPrint全局正则表达式打印。它用于在文本中搜索与指定模式匹配的行,并将这......
  • .net core使用PageOffice时提示POBrowser is not defined
    页面控制台提示: 说明PageOffice.js未引用,页面增加<scripttype="text/javascript"src="~/pageoffice.js"></script>如果还是访问不到这个js,检查一下Startup.cs,注册2个中间件即可。//Thismethodgetscalledbytheruntime.UsethismethodtoconfiguretheHT......