ExternalPortForwarding.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Mono.Nat does never rise this event. The event is there however it is useless.
  48. // You could remove it with no risk.
  49. // NatUtility.DeviceLost += NatUtility_DeviceLost;
  50. // it is hard to say what one should do when an unhandled exception is raised
  51. // because there isn't anything one can do about it. Probably save a log or ignored it.
  52. NatUtility.UnhandledException += NatUtility_UnhandledException;
  53. NatUtility.StartDiscovery();
  54. _isStarted = true;
  55. }
  56. }
  57. void NatUtility_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  58. {
  59. //var ex = e.ExceptionObject as Exception;
  60. //if (ex == null)
  61. //{
  62. // _logger.Error("Unidentified error reported by Mono.Nat");
  63. //}
  64. //else
  65. //{
  66. // // Seeing some blank exceptions coming through here
  67. // _logger.ErrorException("Error reported by Mono.Nat: ", ex);
  68. //}
  69. }
  70. void NatUtility_DeviceFound(object sender, DeviceEventArgs e)
  71. {
  72. try
  73. {
  74. var device = e.Device;
  75. _logger.Debug("NAT device found: {0}", device.LocalAddress.ToString());
  76. CreateRules(device);
  77. }
  78. catch (Exception)
  79. {
  80. // I think it could be a good idea to log the exception because
  81. // you are using permanent portmapping here (never expire) and that means that next time
  82. // CreatePortMap is invoked it can fails with a 718-ConflictInMappingEntry or not. That depends
  83. // on the router's upnp implementation (specs says it should fail however some routers don't do it)
  84. // It also can fail with others like 727-ExternalPortOnlySupportsWildcard, 728-NoPortMapsAvailable
  85. // and those errors (upnp errors) could be useful for diagnosting.
  86. //_logger.ErrorException("Error creating port forwarding rules", ex);
  87. }
  88. }
  89. private void CreateRules(INatDevice device)
  90. {
  91. var info = _appHost.GetSystemInfo();
  92. CreatePortMap(device, info.HttpServerPortNumber);
  93. }
  94. private void CreatePortMap(INatDevice device, int port)
  95. {
  96. _logger.Debug("Creating port map on port {0}", port);
  97. device.CreatePortMap(new Mapping(Protocol.Tcp, port, port)
  98. {
  99. Description = "Media Browser Server"
  100. });
  101. }
  102. // As I said before, this method will be never invoked. You can remove it.
  103. //void NatUtility_DeviceLost(object sender, DeviceEventArgs e)
  104. //{
  105. // var device = e.Device;
  106. // _logger.Debug("NAT device lost: {0}", device.LocalAddress.ToString());
  107. //}
  108. public void Dispose()
  109. {
  110. DisposeNat();
  111. }
  112. private void DisposeNat()
  113. {
  114. _logger.Debug("Stopping NAT discovery");
  115. try
  116. {
  117. // This is not a significant improvement
  118. NatUtility.StopDiscovery();
  119. NatUtility.DeviceFound -= NatUtility_DeviceFound;
  120. //NatUtility.DeviceLost -= NatUtility_DeviceLost;
  121. NatUtility.UnhandledException -= NatUtility_UnhandledException;
  122. }
  123. // Statements in try-block will no fail because StopDiscovery is a one-line
  124. // method that was no chances to fail.
  125. // public static void StopDiscovery ()
  126. // {
  127. // searching.Reset();
  128. // }
  129. // IMO you could remove the catch-block
  130. catch (Exception ex)
  131. {
  132. _logger.ErrorException("Error stopping NAT Discovery", ex);
  133. }
  134. finally
  135. {
  136. _isStarted = false;
  137. }
  138. }
  139. private class LogWriter : TextWriter
  140. {
  141. private readonly ILogger _logger;
  142. public LogWriter(ILogger logger)
  143. {
  144. _logger = logger;
  145. }
  146. public override Encoding Encoding
  147. {
  148. get { return Encoding.UTF8; }
  149. }
  150. public override void WriteLine(string format, params object[] arg)
  151. {
  152. _logger.Debug(format, arg);
  153. }
  154. public override void WriteLine(string value)
  155. {
  156. _logger.Debug(value);
  157. }
  158. }
  159. }
  160. }