RFC3489ViewModel.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using NatTypeTester.Models;
  2. using ReactiveUI;
  3. using ReactiveUI.Fody.Helpers;
  4. using STUN.Client;
  5. using STUN.Proxy;
  6. using STUN.StunResult;
  7. using STUN.Utils;
  8. using System;
  9. using System.Net;
  10. using System.Reactive;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace NatTypeTester.ViewModels
  14. {
  15. public class RFC3489ViewModel : ReactiveObject, IRoutableViewModel
  16. {
  17. public string UrlPathSegment { get; } = @"RFC3489";
  18. public IScreen HostScreen { get; }
  19. private readonly Config _config;
  20. [Reactive]
  21. public ClassicStunResult Result3489 { get; set; }
  22. public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
  23. public RFC3489ViewModel(IScreen hostScreen, Config config)
  24. {
  25. HostScreen = hostScreen;
  26. _config = config;
  27. Result3489 = new ClassicStunResult { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
  28. TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeImpl);
  29. }
  30. private async Task TestClassicNatTypeImpl(CancellationToken token)
  31. {
  32. var server = new StunServer();
  33. if (!server.Parse(_config.StunServer))
  34. {
  35. throw new Exception(@"Wrong STUN Server!");
  36. }
  37. using var proxy = ProxyFactory.CreateProxy(
  38. _config.ProxyType,
  39. Result3489.LocalEndPoint,
  40. NetUtils.ParseEndpoint(_config.ProxyServer),
  41. _config.ProxyUser, _config.ProxyPassword
  42. );
  43. using var client = new StunClient3489(server.Hostname, server.Port, Result3489.LocalEndPoint, proxy);
  44. Result3489 = client.Status;
  45. await client.Query3489Async();
  46. Result3489.LocalEndPoint = client.LocalEndPoint;
  47. }
  48. }
  49. }