ExternalPortForwarding.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Plugins;
  4. using MediaBrowser.Model.Logging;
  5. using Mono.Nat;
  6. using System;
  7. using System.IO;
  8. using System.Text;
  9. namespace MediaBrowser.Server.Implementations.EntryPoints
  10. {
  11. public class ExternalPortForwarding : IServerEntryPoint
  12. {
  13. private readonly IServerApplicationHost _appHost;
  14. private readonly ILogger _logger;
  15. private readonly IServerConfigurationManager _config;
  16. private bool _isStarted;
  17. public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost appHost, IServerConfigurationManager config)
  18. {
  19. _logger = logmanager.GetLogger("PortMapper");
  20. _appHost = appHost;
  21. _config = config;
  22. _config.ConfigurationUpdated += _config_ConfigurationUpdated;
  23. }
  24. void _config_ConfigurationUpdated(object sender, EventArgs e)
  25. {
  26. var enable = _config.Configuration.EnableUPnP;
  27. if (enable && !_isStarted)
  28. {
  29. Reload();
  30. }
  31. else if (!enable && _isStarted)
  32. {
  33. DisposeNat();
  34. }
  35. }
  36. public void Run()
  37. {
  38. //NatUtility.Logger = new LogWriter(_logger);
  39. Reload();
  40. }
  41. private void Reload()
  42. {
  43. if (_config.Configuration.EnableUPnP)
  44. {
  45. _logger.Debug("Starting NAT discovery");
  46. NatUtility.DeviceFound += NatUtility_DeviceFound;
  47. NatUtility.DeviceLost += NatUtility_DeviceLost;
  48. NatUtility.UnhandledException += NatUtility_UnhandledException;
  49. NatUtility.StartDiscovery();
  50. _isStarted = true;
  51. }
  52. }
  53. void NatUtility_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  54. {
  55. //var ex = e.ExceptionObject as Exception;
  56. //if (ex == null)
  57. //{
  58. // _logger.Error("Unidentified error reported by Mono.Nat");
  59. //}
  60. //else
  61. //{
  62. // // Seeing some blank exceptions coming through here
  63. // _logger.ErrorException("Error reported by Mono.Nat: ", ex);
  64. //}
  65. }
  66. void NatUtility_DeviceFound(object sender, DeviceEventArgs e)
  67. {
  68. try
  69. {
  70. var device = e.Device;
  71. _logger.Debug("NAT device found: {0}", device.LocalAddress.ToString());
  72. CreateRules(device);
  73. }
  74. catch (Exception)
  75. {
  76. //_logger.ErrorException("Error creating port forwarding rules", ex);
  77. }
  78. }
  79. private void CreateRules(INatDevice device)
  80. {
  81. var info = _appHost.GetSystemInfo();
  82. CreatePortMap(device, info.HttpServerPortNumber);
  83. }
  84. private void CreatePortMap(INatDevice device, int port)
  85. {
  86. _logger.Debug("Creating port map on port {0}", port);
  87. device.CreatePortMap(new Mapping(Protocol.Tcp, port, port)
  88. {
  89. Description = "Media Browser Server"
  90. });
  91. }
  92. void NatUtility_DeviceLost(object sender, DeviceEventArgs e)
  93. {
  94. var device = e.Device;
  95. _logger.Debug("NAT device lost: {0}", device.LocalAddress.ToString());
  96. }
  97. public void Dispose()
  98. {
  99. DisposeNat();
  100. }
  101. private void DisposeNat()
  102. {
  103. _logger.Debug("Stopping NAT discovery");
  104. try
  105. {
  106. NatUtility.DeviceFound -= NatUtility_DeviceFound;
  107. NatUtility.DeviceLost -= NatUtility_DeviceLost;
  108. NatUtility.UnhandledException -= NatUtility_UnhandledException;
  109. NatUtility.StopDiscovery();
  110. }
  111. catch (Exception ex)
  112. {
  113. _logger.ErrorException("Error stopping NAT Discovery", ex);
  114. }
  115. finally
  116. {
  117. _isStarted = false;
  118. }
  119. }
  120. private class LogWriter : TextWriter
  121. {
  122. private readonly ILogger _logger;
  123. public LogWriter(ILogger logger)
  124. {
  125. _logger = logger;
  126. }
  127. public override Encoding Encoding
  128. {
  129. get { return Encoding.UTF8; }
  130. }
  131. public override void WriteLine(string format, params object[] arg)
  132. {
  133. _logger.Debug(format, arg);
  134. }
  135. public override void WriteLine(string value)
  136. {
  137. _logger.Debug(value);
  138. }
  139. }
  140. }
  141. }