NetUtils.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using STUN.Client;
  2. using STUN.StunResult;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.NetworkInformation;
  8. using System.Net.Sockets;
  9. using System.Threading.Tasks;
  10. namespace STUN.Utils
  11. {
  12. public static class NetUtils
  13. {
  14. public const string DefaultLocalEnd = @"0.0.0.0:0";
  15. public static IPEndPoint ParseEndpoint(string str)
  16. {
  17. var ipPort = str.Trim().Split(':');
  18. if (ipPort.Length < 2) return null;
  19. IPAddress ip = null;
  20. if (ipPort.Length == 2 && IPAddress.TryParse(ipPort[0], out ip))
  21. {
  22. if (!IPAddress.TryParse(ipPort[0], out ip))
  23. {
  24. return null;
  25. }
  26. }
  27. else if (ipPort.Length > 2)
  28. {
  29. var ipStr = string.Join(@":", ipPort, 0, ipPort.Length - 1);
  30. if (!ipStr.StartsWith(@"[") || !ipStr.EndsWith(@"]") || !IPAddress.TryParse(ipStr, out ip))
  31. {
  32. return null;
  33. }
  34. }
  35. if (ip != null && ushort.TryParse(ipPort.Last(), out var port))
  36. {
  37. return new IPEndPoint(ip, port);
  38. }
  39. return null;
  40. }
  41. public static async Task<StunResult5389> NatBehaviorDiscovery(string server, ushort port, IPEndPoint local)
  42. {
  43. // proxy is not supported yet
  44. using var client = new StunClient5389UDP(server, port, local);
  45. return await client.QueryAsync();
  46. }
  47. public static (byte[], IPEndPoint, IPAddress) UdpReceive(this UdpClient client, byte[] bytes, IPEndPoint remote, EndPoint receive)
  48. {
  49. var localEndPoint = (IPEndPoint)client.Client.LocalEndPoint;
  50. Debug.WriteLine($@"{localEndPoint} => {remote} {bytes.Length} 字节");
  51. client.Send(bytes, bytes.Length, remote);
  52. var res = new byte[ushort.MaxValue];
  53. var flag = SocketFlags.None;
  54. var length = client.Client.ReceiveMessageFrom(res, 0, res.Length, ref flag, ref receive, out var ipPacketInformation);
  55. var local = ipPacketInformation.Address;
  56. Debug.WriteLine($@"{(IPEndPoint)receive} => {local} {length} 字节");
  57. return (res.Take(length).ToArray(),
  58. (IPEndPoint)receive
  59. , local);
  60. }
  61. public static async Task<(byte[], IPEndPoint, IPAddress)> UdpReceiveAsync(this UdpClient client, byte[] bytes, IPEndPoint remote, EndPoint receive)
  62. {
  63. var localEndPoint = (IPEndPoint)client.Client.LocalEndPoint;
  64. Debug.WriteLine($@"{localEndPoint} => {remote} {bytes.Length} 字节");
  65. await client.SendAsync(bytes, bytes.Length, remote);
  66. var res = new byte[ushort.MaxValue];
  67. var flag = SocketFlags.None;
  68. var length = client.Client.ReceiveMessageFrom(res, 0, res.Length, ref flag, ref receive, out var ipPacketInformation);
  69. var local = ipPacketInformation.Address;
  70. Debug.WriteLine($@"{(IPEndPoint)receive} => {local} {length} 字节");
  71. return (res.Take(length).ToArray(),
  72. (IPEndPoint)receive
  73. , local);
  74. }
  75. public static TcpState GetState(this TcpClient tcpClient)
  76. {
  77. var foo = IPGlobalProperties.GetIPGlobalProperties()
  78. .GetActiveTcpConnections()
  79. .SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint));
  80. return foo != null ? foo.State : TcpState.Unknown;
  81. }
  82. }
  83. }