diff options
| author | Michalis Adamidis <gsnerf@gsnerf.de> | 2014-08-24 19:53:53 +0200 |
|---|---|---|
| committer | Michalis Adamidis <gsnerf@gsnerf.de> | 2014-08-24 19:53:53 +0200 |
| commit | 5740a4c22d676d0050e875b0bd5455f5a303f5bd (patch) | |
| tree | 8cbdc9a742be677fd96b1be32bb74a294d8722c4 /MediaBrowser.Server.Implementations/EntryPoints | |
| parent | 7b8050ed382e1e39399c6d1b06637cd07ac6b8d5 (diff) | |
| parent | 58a38d0d1ddb89439b763e7bc50e8b84105f68fe (diff) | |
Merge branch 'master' of https://github.com/MediaBrowser/MediaBrowser
Conflicts:
MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
Diffstat (limited to 'MediaBrowser.Server.Implementations/EntryPoints')
3 files changed, 94 insertions, 28 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs index 6ad8b0d94..42191a270 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs @@ -4,8 +4,10 @@ using MediaBrowser.Controller.Plugins; using MediaBrowser.Model.Logging; using Mono.Nat; using System; +using System.Collections.Generic; using System.IO; using System.Text; +using System.Threading; namespace MediaBrowser.Server.Implementations.EntryPoints { @@ -17,6 +19,8 @@ namespace MediaBrowser.Server.Implementations.EntryPoints private bool _isStarted; + private Timer _timer; + public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost appHost, IServerConfigurationManager config) { _logger = logmanager.GetLogger("PortMapper"); @@ -43,7 +47,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints public void Run() { //NatUtility.Logger = new LogWriter(_logger); - + Reload(); } @@ -52,20 +56,22 @@ namespace MediaBrowser.Server.Implementations.EntryPoints if (_config.Configuration.EnableUPnP) { _logger.Debug("Starting NAT discovery"); - + NatUtility.DeviceFound += NatUtility_DeviceFound; - + // Mono.Nat does never rise this event. The event is there however it is useless. // You could remove it with no risk. // NatUtility.DeviceLost += NatUtility_DeviceLost; - - + + // it is hard to say what one should do when an unhandled exception is raised // because there isn't anything one can do about it. Probably save a log or ignored it. NatUtility.UnhandledException += NatUtility_UnhandledException; NatUtility.StartDiscovery(); _isStarted = true; + + _timer = new Timer(s => _createdRules = new List<string>(), null, TimeSpan.FromHours(6), TimeSpan.FromHours(6)); } } @@ -101,16 +107,27 @@ namespace MediaBrowser.Server.Implementations.EntryPoints // on the router's upnp implementation (specs says it should fail however some routers don't do it) // It also can fail with others like 727-ExternalPortOnlySupportsWildcard, 728-NoPortMapsAvailable // and those errors (upnp errors) could be useful for diagnosting. - + //_logger.ErrorException("Error creating port forwarding rules", ex); } } + private List<string> _createdRules = new List<string>(); private void CreateRules(INatDevice device) { - var info = _appHost.GetSystemInfo(); + // On some systems the device discovered event seems to fire repeatedly + // This check will help ensure we're not trying to port map the same device over and over + + var address = device.LocalAddress.ToString(); + + if (!_createdRules.Contains(address)) + { + _createdRules.Add(address); + + var info = _appHost.GetSystemInfo(); - CreatePortMap(device, info.HttpServerPortNumber); + CreatePortMap(device, info.HttpServerPortNumber); + } } private void CreatePortMap(INatDevice device, int port) @@ -139,6 +156,12 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { _logger.Debug("Stopping NAT discovery"); + if (_timer != null) + { + _timer.Dispose(); + _timer = null; + } + try { // This is not a significant improvement @@ -150,10 +173,10 @@ namespace MediaBrowser.Server.Implementations.EntryPoints // Statements in try-block will no fail because StopDiscovery is a one-line // method that was no chances to fail. // public static void StopDiscovery () - // { + // { // searching.Reset(); - // } - // IMO you could remove the catch-block + // } + // IMO you could remove the catch-block catch (Exception ex) { _logger.ErrorException("Error stopping NAT Discovery", ex); diff --git a/MediaBrowser.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs b/MediaBrowser.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs index 3b4379e37..386c16513 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs @@ -1,7 +1,5 @@ using MediaBrowser.Common.Net; using MediaBrowser.Controller; -using MediaBrowser.Controller.Configuration; -using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Plugins; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; @@ -29,32 +27,22 @@ namespace MediaBrowser.Server.Implementations.EntryPoints /// The _network manager /// </summary> private readonly INetworkManager _networkManager; - /// <summary> - /// The _server configuration manager - /// </summary> - private readonly IServerConfigurationManager _serverConfigurationManager; - /// <summary> - /// The _HTTP server - /// </summary> - private readonly IHttpServer _httpServer; private readonly IServerApplicationHost _appHost; private readonly IJsonSerializer _json; public const int PortNumber = 7359; /// <summary> - /// Initializes a new instance of the <see cref="UdpServerEntryPoint"/> class. + /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class. /// </summary> /// <param name="logger">The logger.</param> /// <param name="networkManager">The network manager.</param> - /// <param name="serverConfigurationManager">The server configuration manager.</param> - /// <param name="httpServer">The HTTP server.</param> - public UdpServerEntryPoint(ILogger logger, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager, IHttpServer httpServer, IServerApplicationHost appHost, IJsonSerializer json) + /// <param name="appHost">The application host.</param> + /// <param name="json">The json.</param> + public UdpServerEntryPoint(ILogger logger, INetworkManager networkManager, IServerApplicationHost appHost, IJsonSerializer json) { _logger = logger; _networkManager = networkManager; - _serverConfigurationManager = serverConfigurationManager; - _httpServer = httpServer; _appHost = appHost; _json = json; } @@ -64,7 +52,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints /// </summary> public void Run() { - var udpServer = new UdpServer(_logger, _networkManager, _serverConfigurationManager, _httpServer, _appHost, _json); + var udpServer = new UdpServer(_logger, _networkManager, _appHost, _json); try { diff --git a/MediaBrowser.Server.Implementations/EntryPoints/WanAddressEntryPoint.cs b/MediaBrowser.Server.Implementations/EntryPoints/WanAddressEntryPoint.cs new file mode 100644 index 000000000..2f6643588 --- /dev/null +++ b/MediaBrowser.Server.Implementations/EntryPoints/WanAddressEntryPoint.cs @@ -0,0 +1,55 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Plugins; +using System; +using System.IO; +using System.Threading; + +namespace MediaBrowser.Server.Implementations.EntryPoints +{ + public class WanAddressEntryPoint : IServerEntryPoint + { + public static string WanAddress; + private Timer _timer; + private readonly IHttpClient _httpClient; + + public WanAddressEntryPoint(IHttpClient httpClient) + { + _httpClient = httpClient; + } + + public void Run() + { + _timer = new Timer(TimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24)); + } + + private async void TimerCallback(object state) + { + try + { + using (var stream = await _httpClient.Get(new HttpRequestOptions + { + Url = "http://bot.whatismyipaddress.com/" + + }).ConfigureAwait(false)) + { + using (var reader = new StreamReader(stream)) + { + WanAddress = await reader.ReadToEndAsync().ConfigureAwait(false); + } + } + } + catch + { + } + } + + public void Dispose() + { + if (_timer != null) + { + _timer.Dispose(); + _timer = null; + } + } + } +} |
