RFC3489ViewModel.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Dns.Net.Abstractions;
  2. using JetBrains.Annotations;
  3. using Microsoft;
  4. using NatTypeTester.Models;
  5. using ReactiveUI;
  6. using Socks5.Models;
  7. using STUN;
  8. using STUN.Client;
  9. using STUN.Proxy;
  10. using STUN.StunResult;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Reactive;
  14. using System.Reactive.Linq;
  15. namespace NatTypeTester.ViewModels;
  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. private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
  23. public ClassicStunResult Result3489 { get; set; }
  24. public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
  25. private static readonly IPEndPoint DefaultLocalEndpoint = new(IPAddress.Any, 0);
  26. public RFC3489ViewModel()
  27. {
  28. Result3489 = new ClassicStunResult
  29. {
  30. LocalEndPoint = DefaultLocalEndpoint
  31. };
  32. TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeAsync);
  33. }
  34. private async Task TestClassicNatTypeAsync(CancellationToken token)
  35. {
  36. Verify.Operation(StunServer.TryParse(Config.StunServer, out StunServer? server), @"Wrong STUN Server!");
  37. if (!HostnameEndpoint.TryParse(Config.ProxyServer, out HostnameEndpoint? proxyIpe))
  38. {
  39. throw new NotSupportedException(@"Unknown proxy address");
  40. }
  41. Socks5CreateOption socks5Option = new()
  42. {
  43. Address = await DnsClient.QueryAsync(proxyIpe.Hostname, token),
  44. Port = proxyIpe.Port,
  45. UsernamePassword = new UsernamePassword
  46. {
  47. UserName = Config.ProxyUser,
  48. Password = Config.ProxyPassword
  49. }
  50. };
  51. Result3489.LocalEndPoint ??= DefaultLocalEndpoint;
  52. using IUdpProxy proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result3489.LocalEndPoint, socks5Option);
  53. IPAddress ip = await DnsClient.QueryAsync(server.Hostname, token);
  54. using StunClient3489 client = new(new IPEndPoint(ip, server.Port), Result3489.LocalEndPoint, proxy);
  55. Result3489 = client.State;
  56. using (Observable.Interval(TimeSpan.FromSeconds(0.1))
  57. .ObserveOn(RxApp.MainThreadScheduler)
  58. .Subscribe(_ => this.RaisePropertyChanged(nameof(Result3489))))
  59. {
  60. await client.ConnectProxyAsync(token);
  61. try
  62. {
  63. await client.QueryAsync(token);
  64. Result3489.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
  65. }
  66. finally
  67. {
  68. await client.CloseProxyAsync(token);
  69. }
  70. }
  71. Result3489 = new ClassicStunResult();
  72. Result3489.Clone(client.State);
  73. this.RaisePropertyChanged(nameof(Result3489));
  74. }
  75. }