RFC3489ViewModel.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Dns.Net.Abstractions;
  2. using JetBrains.Annotations;
  3. using Microsoft;
  4. using NatTypeTester.Models;
  5. using ReactiveUI;
  6. using STUN;
  7. using STUN.Client;
  8. using STUN.Proxy;
  9. using STUN.StunResult;
  10. using System;
  11. using System.Net;
  12. using System.Reactive;
  13. using System.Reactive.Linq;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace NatTypeTester.ViewModels
  17. {
  18. [UsedImplicitly]
  19. public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
  20. {
  21. public string UrlPathSegment => @"RFC3489";
  22. public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
  23. private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
  24. private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
  25. public ClassicStunResult Result3489 { get; set; }
  26. public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
  27. public RFC3489ViewModel()
  28. {
  29. Result3489 = new ClassicStunResult { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
  30. TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeImpl);
  31. }
  32. private async Task TestClassicNatTypeImpl(CancellationToken token)
  33. {
  34. Verify.Operation(StunServer.TryParse(Config.StunServer, out var server), @"Wrong STUN Server!");
  35. using var proxy = ProxyFactory.CreateProxy(
  36. Config.ProxyType,
  37. Result3489.LocalEndPoint,
  38. IPEndPoint.Parse(Config.ProxyServer),
  39. Config.ProxyUser, Config.ProxyPassword
  40. );
  41. var ip = await DnsClient.QueryAsync(server.Hostname, token);
  42. using var client = new StunClient3489(ip, server.Port, Result3489.LocalEndPoint, proxy);
  43. Result3489 = client.Status;
  44. using (Observable.Interval(TimeSpan.FromSeconds(0.1))
  45. .ObserveOn(RxApp.MainThreadScheduler)
  46. .Subscribe(_ => this.RaisePropertyChanged(nameof(Result3489))))
  47. {
  48. await client.QueryAsync(token);
  49. }
  50. Result3489.LocalEndPoint = client.LocalEndPoint;
  51. this.RaisePropertyChanged(nameof(Result3489));
  52. }
  53. }
  54. }