From ce043225c4096b2082bdf351bd5d59848f4eb533 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 27 Oct 2016 21:07:40 -0400 Subject: update port mapper --- .../EntryPoints/ExternalPortForwarding.cs | 37 ++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'MediaBrowser.Server.Implementations/EntryPoints') diff --git a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs index 1430eb9cd6..3d860a27bd 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Net; +using MediaBrowser.Common.Net; using MediaBrowser.Model.Events; using MediaBrowser.Server.Implementations.Threading; @@ -17,18 +18,20 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { private readonly IServerApplicationHost _appHost; private readonly ILogger _logger; + private readonly IHttpClient _httpClient; private readonly IServerConfigurationManager _config; private readonly IDeviceDiscovery _deviceDiscovery; private PeriodicTimer _timer; private bool _isStarted; - public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost appHost, IServerConfigurationManager config, IDeviceDiscovery deviceDiscovery) + public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost appHost, IServerConfigurationManager config, IDeviceDiscovery deviceDiscovery, IHttpClient httpClient) { _logger = logmanager.GetLogger("PortMapper"); _appHost = appHost; _config = config; _deviceDiscovery = deviceDiscovery; + _httpClient = httpClient; } private string _lastConfigIdentifier; @@ -63,6 +66,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints public void Run() { NatUtility.Logger = _logger; + NatUtility.HttpClient = _httpClient; if (_config.Configuration.EnableUPnP) { @@ -136,7 +140,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints _usnsHandled.Add(identifier); } - _logger.Debug("Calling Nat.Handle on " + identifier); + _logger.Debug("Found NAT device: " + identifier); IPAddress address; if (IPAddress.TryParse(info.Location.Host, out address)) @@ -150,16 +154,23 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { var localAddressString = await _appHost.GetLocalApiUrl().ConfigureAwait(false); - if (!IPAddress.TryParse(localAddressString, out localAddress)) + Uri uri; + if (Uri.TryCreate(localAddressString, UriKind.Absolute, out uri)) { - return; + localAddressString = uri.Host; + + if (!IPAddress.TryParse(localAddressString, out localAddress)) + { + return; + } } } - catch + catch (Exception ex) { return; } + _logger.Debug("Calling Nat.Handle on " + identifier); NatUtility.Handle(localAddress, info, endpoint, NatProtocol.Upnp); } } @@ -229,13 +240,21 @@ namespace MediaBrowser.Server.Implementations.EntryPoints } } - private void CreatePortMap(INatDevice device, int privatePort, int publicPort) + private async void CreatePortMap(INatDevice device, int privatePort, int publicPort) { _logger.Debug("Creating port map on port {0}", privatePort); - device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) + + try + { + await device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) + { + Description = _appHost.Name + }).ConfigureAwait(false); + } + catch (Exception ex) { - Description = _appHost.Name - }); + _logger.ErrorException("Error creating port map", ex); + } } // As I said before, this method will be never invoked. You can remove it. -- cgit v1.2.3