Program.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Dns.Net.Clients;
  2. using STUN.Client;
  3. using STUN.Utils;
  4. using System;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7. namespace NatTypeTester
  8. {
  9. internal static class Program
  10. {
  11. /// <summary>
  12. /// stun.qq.com 3478 0.0.0.0:0
  13. /// </summary>
  14. private static async Task Main(string[] args)
  15. {
  16. var server = @"stun.syncthing.net";
  17. ushort port = 3478;
  18. IPEndPoint? local = null;
  19. if (args.Length > 0 && (Uri.CheckHostName(args[0]) == UriHostNameType.Dns || IPAddress.TryParse(args[0], out _)))
  20. {
  21. server = args[0];
  22. }
  23. if (args.Length > 1)
  24. {
  25. ushort.TryParse(args[1], out port);
  26. }
  27. if (args.Length > 2)
  28. {
  29. local = NetUtils.ParseEndpoint(args[2]);
  30. }
  31. using var client = new StunClient5389UDP(new DefaultDnsClient(), server, port, local);
  32. await client.QueryAsync();
  33. var res = client.Status;
  34. Console.WriteLine($@"Other address is {res.OtherEndPoint}");
  35. Console.WriteLine($@"Binding test: {res.BindingTestResult}");
  36. Console.WriteLine($@"Local address: {res.LocalEndPoint}");
  37. Console.WriteLine($@"Mapped address: {res.PublicEndPoint}");
  38. Console.WriteLine($@"Nat mapping behavior: {res.MappingBehavior}");
  39. Console.WriteLine($@"Nat filtering behavior: {res.FilteringBehavior}");
  40. }
  41. }
  42. }