RFC5780ViewModel.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (!HostnameEndpoint.TryParse(Config.ProxyServer, out var proxyIpe))
  39. {
  40. throw new NotSupportedException(@"Unknown proxy address");
  41. }
  42. var socks5Option = new Socks5CreateOption
  43. {
  44. Address = await DnsClient.QueryAsync(proxyIpe.Hostname, token),
  45. Port = proxyIpe.Port,
  46. UsernamePassword = new UsernamePassword
  47. {
  48. UserName = Config.ProxyUser,
  49. Password = Config.ProxyPassword
  50. }
  51. };
  52. Result5389.LocalEndPoint ??= DefaultLocalEndpoint;
  53. using var proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result5389.LocalEndPoint, socks5Option);
  54. var ip = await DnsClient.QueryAsync(server.Hostname, token);
  55. using var client = new StunClient5389UDP(new IPEndPoint(ip, server.Port), Result5389.LocalEndPoint, proxy);
  56. Result5389 = client.State;
  57. using (Observable.Interval(TimeSpan.FromSeconds(0.1))
  58. .ObserveOn(RxApp.MainThreadScheduler)
  59. .Subscribe(_ => this.RaisePropertyChanged(nameof(Result5389))))
  60. {
  61. await client.ConnectProxyAsync(token);
  62. try
  63. {
  64. await client.QueryAsync(token);
  65. Result5389.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. Result5389 = new StunResult5389();
  73. Result5389.Clone(client.State);
  74. this.RaisePropertyChanged(nameof(Result5389));
  75. }
  76. }
  77. }