ExternalPortForwarding.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Dlna;
  4. using MediaBrowser.Controller.Plugins;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Net;
  11. using System.Text;
  12. using MediaBrowser.Common.Threading;
  13. using Open.Nat;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Server.Implementations.EntryPoints
  17. {
  18. public class ExternalPortForwarding : IServerEntryPoint
  19. {
  20. private readonly IServerApplicationHost _appHost;
  21. private readonly ILogger _logger;
  22. private readonly IServerConfigurationManager _config;
  23. private readonly ISsdpHandler _ssdp;
  24. private CancellationTokenSource _currentCancellationTokenSource;
  25. private TimeSpan _interval = TimeSpan.FromHours(1);
  26. public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost appHost, IServerConfigurationManager config, ISsdpHandler ssdp)
  27. {
  28. _logger = logmanager.GetLogger("PortMapper");
  29. _appHost = appHost;
  30. _config = config;
  31. _ssdp = ssdp;
  32. _config.ConfigurationUpdated += _config_ConfigurationUpdated;
  33. }
  34. private void _config_ConfigurationUpdated(object sender, EventArgs e)
  35. {
  36. }
  37. public void Run()
  38. {
  39. Discover();
  40. }
  41. private async void Discover()
  42. {
  43. if (!_config.Configuration.EnableUPnP)
  44. {
  45. return;
  46. }
  47. var discoverer = new NatDiscoverer();
  48. var cancellationTokenSource = new CancellationTokenSource(10000);
  49. _currentCancellationTokenSource = cancellationTokenSource;
  50. try
  51. {
  52. var device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cancellationTokenSource).ConfigureAwait(false);
  53. await CreateRules(device).ConfigureAwait(false);
  54. }
  55. catch (OperationCanceledException)
  56. {
  57. }
  58. catch (Exception ex)
  59. {
  60. _logger.ErrorException("Error discovering NAT devices", ex);
  61. }
  62. finally
  63. {
  64. _currentCancellationTokenSource = null;
  65. }
  66. if (_config.Configuration.EnableUPnP)
  67. {
  68. await Task.Delay(_interval).ConfigureAwait(false);
  69. Discover();
  70. }
  71. }
  72. private async Task CreateRules(NatDevice device)
  73. {
  74. // On some systems the device discovered event seems to fire repeatedly
  75. // This check will help ensure we're not trying to port map the same device over and over
  76. await CreatePortMap(device, _appHost.HttpPort, _config.Configuration.PublicPort).ConfigureAwait(false);
  77. await CreatePortMap(device, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort).ConfigureAwait(false);
  78. }
  79. private async Task CreatePortMap(NatDevice device, int privatePort, int publicPort)
  80. {
  81. _logger.Debug("Creating port map on port {0}", privatePort);
  82. try
  83. {
  84. await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, privatePort, publicPort, _appHost.Name)).ConfigureAwait(false);
  85. }
  86. catch (Exception ex)
  87. {
  88. _logger.ErrorException("Error creating port map", ex);
  89. }
  90. }
  91. public void Dispose()
  92. {
  93. DisposeNat();
  94. }
  95. private void DisposeNat()
  96. {
  97. if (_currentCancellationTokenSource != null)
  98. {
  99. try
  100. {
  101. _currentCancellationTokenSource.Cancel();
  102. }
  103. catch (Exception ex)
  104. {
  105. _logger.ErrorException("Error calling _currentCancellationTokenSource.Cancel", ex);
  106. }
  107. }
  108. }
  109. }
  110. }