RFC3489ViewModel.cs 2.6 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;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Reactive;
  15. using System.Reactive.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace NatTypeTester.ViewModels
  19. {
  20. [UsedImplicitly]
  21. public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
  22. {
  23. public string UrlPathSegment => @"RFC3489";
  24. public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
  25. private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
  26. private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
  27. public ClassicStunResult Result3489 { get; set; }
  28. public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
  29. private static readonly IPEndPoint DefaultLocalEndpoint = new(IPAddress.Any, 0);
  30. public RFC3489ViewModel()
  31. {
  32. Result3489 = new ClassicStunResult
  33. {
  34. LocalEndPoint = DefaultLocalEndpoint
  35. };
  36. TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeAsync);
  37. }
  38. private async Task TestClassicNatTypeAsync(CancellationToken token)
  39. {
  40. Verify.Operation(StunServer.TryParse(Config.StunServer, out var server), @"Wrong STUN Server!");
  41. var proxyIpe = IPEndPoint.Parse(Config.ProxyServer);
  42. var socks5Option = new Socks5CreateOption
  43. {
  44. Address = proxyIpe.Address,
  45. Port = (ushort)proxyIpe.Port,
  46. UsernamePassword = new UsernamePassword
  47. {
  48. UserName = Config.ProxyUser,
  49. Password = Config.ProxyPassword
  50. }
  51. };
  52. Result3489.LocalEndPoint ??= DefaultLocalEndpoint;
  53. using var proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result3489.LocalEndPoint, socks5Option);
  54. var ip = await DnsClient.QueryAsync(server.Hostname, token);
  55. using var client = new StunClient3489(ip, server.Port, Result3489.LocalEndPoint, proxy);
  56. Result3489 = client.Status;
  57. using (Observable.Interval(TimeSpan.FromSeconds(0.1))
  58. .ObserveOn(RxApp.MainThreadScheduler)
  59. .Subscribe(_ => this.RaisePropertyChanged(nameof(Result3489))))
  60. {
  61. await client.ConnectProxyAsync(token);
  62. try
  63. {
  64. await client.QueryAsync(token);
  65. Result3489.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
  66. }
  67. finally
  68. {
  69. await client.CloseProxyAsync(token);
  70. }
  71. }
  72. Result3489 = new ClassicStunResult();
  73. Result3489.Clone(client.Status);
  74. this.RaisePropertyChanged(nameof(Result3489));
  75. }
  76. }
  77. }