1
0

RFC3489ViewModel.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (!HostnameEndpoint.TryParse(Config.ProxyServer, out var proxyIpe))
  42. {
  43. throw new NotSupportedException(@"Unknown proxy address");
  44. }
  45. var socks5Option = new Socks5CreateOption
  46. {
  47. Address = await DnsClient.QueryAsync(proxyIpe.Hostname, token),
  48. Port = proxyIpe.Port,
  49. UsernamePassword = new UsernamePassword
  50. {
  51. UserName = Config.ProxyUser,
  52. Password = Config.ProxyPassword
  53. }
  54. };
  55. Result3489.LocalEndPoint ??= DefaultLocalEndpoint;
  56. using var proxy = ProxyFactory.CreateProxy(Config.ProxyType, Result3489.LocalEndPoint, socks5Option);
  57. var ip = await DnsClient.QueryAsync(server.Hostname, token);
  58. using var client = new StunClient3489(new IPEndPoint(ip, server.Port), Result3489.LocalEndPoint, proxy);
  59. Result3489 = client.State;
  60. using (Observable.Interval(TimeSpan.FromSeconds(0.1))
  61. .ObserveOn(RxApp.MainThreadScheduler)
  62. .Subscribe(_ => this.RaisePropertyChanged(nameof(Result3489))))
  63. {
  64. await client.ConnectProxyAsync(token);
  65. try
  66. {
  67. await client.QueryAsync(token);
  68. Result3489.LocalEndPoint = new IPEndPoint(client.LocalEndPoint.AddressFamily is AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, client.LocalEndPoint.Port);
  69. }
  70. finally
  71. {
  72. await client.CloseProxyAsync(token);
  73. }
  74. }
  75. Result3489 = new ClassicStunResult();
  76. Result3489.Clone(client.State);
  77. this.RaisePropertyChanged(nameof(Result3489));
  78. }
  79. }
  80. }