ExternalPortForwarding.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  33. public void Run()
  34. {
  35. //NatUtility.Logger = new LogWriter(_logger);
  36. if (_config.Configuration.EnableUPnP)
  37. {
  38. Discover();
  39. }
  40. }
  41. private async void Discover()
  42. {
  43. var discoverer = new NatDiscoverer();
  44. var cancellationTokenSource = new CancellationTokenSource(10000);
  45. _currentCancellationTokenSource = cancellationTokenSource;
  46. try
  47. {
  48. var device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cancellationTokenSource).ConfigureAwait(false);
  49. await CreateRules(device).ConfigureAwait(false);
  50. }
  51. catch (OperationCanceledException)
  52. {
  53. }
  54. catch (Exception ex)
  55. {
  56. _logger.ErrorException("Error discovering NAT devices", ex);
  57. }
  58. finally
  59. {
  60. _currentCancellationTokenSource = null;
  61. }
  62. if (_config.Configuration.EnableUPnP)
  63. {
  64. await Task.Delay(_interval).ConfigureAwait(false);
  65. Discover();
  66. }
  67. }
  68. private async Task CreateRules(NatDevice device)
  69. {
  70. // On some systems the device discovered event seems to fire repeatedly
  71. // This check will help ensure we're not trying to port map the same device over and over
  72. await CreatePortMap(device, _appHost.HttpPort, _config.Configuration.PublicPort).ConfigureAwait(false);
  73. await CreatePortMap(device, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort).ConfigureAwait(false);
  74. }
  75. private async Task CreatePortMap(NatDevice device, int privatePort, int publicPort)
  76. {
  77. _logger.Debug("Creating port map on port {0}", privatePort);
  78. try
  79. {
  80. await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, privatePort, publicPort, _appHost.Name)).ConfigureAwait(false);
  81. }
  82. catch (Exception ex)
  83. {
  84. _logger.ErrorException("Error creating port map", ex);
  85. }
  86. }
  87. public void Dispose()
  88. {
  89. DisposeNat();
  90. }
  91. private void DisposeNat()
  92. {
  93. if (_currentCancellationTokenSource != null)
  94. {
  95. try
  96. {
  97. _currentCancellationTokenSource.Cancel();
  98. }
  99. catch (Exception ex)
  100. {
  101. _logger.ErrorException("Error calling _currentCancellationTokenSource.Cancel", ex);
  102. }
  103. }
  104. }
  105. }
  106. }