Program.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Dns.Net.Clients;
  2. using STUN;
  3. using STUN.Client;
  4. using STUN.StunResult;
  5. using System.Net;
  6. //stun.qq.com:3478 0.0.0.0:0
  7. string server = @"stun.syncthing.net";
  8. ushort port = 3478;
  9. IPEndPoint local = new(IPAddress.Any, 0);
  10. if (args.Length > 0 && StunServer.TryParse(args[0], out StunServer? stun))
  11. {
  12. server = stun.Hostname;
  13. port = stun.Port;
  14. }
  15. if (args.Length > 1)
  16. {
  17. if (IPEndPoint.TryParse(args[2], out IPEndPoint? ipEndPoint))
  18. {
  19. local = ipEndPoint;
  20. }
  21. }
  22. DefaultDnsClient dnsClient = new();
  23. IPAddress ip = await dnsClient.QueryAsync(server);
  24. using StunClient5389UDP client = new(new IPEndPoint(ip, port), local);
  25. using CancellationTokenSource cts = new();
  26. cts.CancelAfter(TimeSpan.FromSeconds(5));
  27. await client.QueryAsync(cts.Token);
  28. StunResult5389 res = client.State;
  29. Console.WriteLine($@"Other address is {res.OtherEndPoint}");
  30. Console.WriteLine($@"Binding test: {res.BindingTestResult}");
  31. Console.WriteLine($@"Local address: {res.LocalEndPoint}");
  32. Console.WriteLine($@"Mapped address: {res.PublicEndPoint}");
  33. Console.WriteLine($@"Nat mapping behavior: {res.MappingBehavior}");
  34. Console.WriteLine($@"Nat filtering behavior: {res.FilteringBehavior}");