RFC3489ViewModel.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using JetBrains.Annotations;
  2. using NatTypeTester.Models;
  3. using ReactiveUI;
  4. using ReactiveUI.Fody.Helpers;
  5. using STUN.Client;
  6. using STUN.Proxy;
  7. using STUN.StunResult;
  8. using STUN.Utils;
  9. using System;
  10. using System.Net;
  11. using System.Reactive;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace NatTypeTester.ViewModels
  15. {
  16. [UsedImplicitly]
  17. public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
  18. {
  19. public string UrlPathSegment => @"RFC3489";
  20. public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
  21. private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
  22. [Reactive]
  23. public ClassicStunResult Result3489 { get; set; }
  24. public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
  25. public RFC3489ViewModel()
  26. {
  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. }