RFC5780ViewModel.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 RFC5780ViewModel : ViewModelBase, IRoutableViewModel
  22. {
  23. public string UrlPathSegment => @"RFC5780";
  24. public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
  25. private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
  26. private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
  27. public StunResult5389 Result5389 { get; set; }
  28. public ReactiveCommand<Unit, Unit> DiscoveryNatType { get; }
  29. private static readonly IPEndPoint DefaultLocalEndpoint = new(IPAddress.Any, 0);
  30. public RFC5780ViewModel()
  31. {
  32. Result5389 = new StunResult5389 { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
  33. DiscoveryNatType = ReactiveCommand.CreateFromTask(DiscoveryNatTypeAsync);
  34. }
  35. private async Task DiscoveryNatTypeAsync(CancellationToken token)
  36. {
  37. Verify.Operation(StunServer.TryParse(Config.StunServer, out var server), @"Wrong STUN Server!");
  38. var proxyIpe = IPEndPoint.Parse(Config.ProxyServer);
  39. var socks5Option = new Socks5CreateOption
  40. {
  41. Address = proxyIpe.Address,
  42. Port = (ushort)proxyIpe.Port,
  43. UsernamePassword = new UsernamePassword
  44. {
  45. UserName = Config.ProxyUser,
  46. Password = Config.ProxyPassword
  47. }
  48. };
  49. Result5389.LocalEndPoint ??= DefaultLocalEndpoint;
  50. using var proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result5389.LocalEndPoint, socks5Option);
  51. var ip = await DnsClient.QueryAsync(server.Hostname, token);
  52. using var client = new StunClient5389UDP(ip, server.Port, Result5389.LocalEndPoint, proxy);
  53. Result5389 = client.Status;
  54. using (Observable.Interval(TimeSpan.FromSeconds(0.1))
  55. .ObserveOn(RxApp.MainThreadScheduler)
  56. .Subscribe(_ => this.RaisePropertyChanged(nameof(Result5389))))
  57. {
  58. await client.ConnectProxyAsync(token);
  59. try
  60. {
  61. await client.QueryAsync(token);
  62. Result5389.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
  63. }
  64. finally
  65. {
  66. await client.CloseProxyAsync(token);
  67. }
  68. }
  69. Result5389 = new StunResult5389();
  70. Result5389.Clone(client.Status);
  71. this.RaisePropertyChanged(nameof(Result5389));
  72. }
  73. }
  74. }