Program.cs 1.2 KB

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