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