diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-10-27 21:07:40 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-10-27 21:07:40 -0400 |
| commit | ce043225c4096b2082bdf351bd5d59848f4eb533 (patch) | |
| tree | 2978843a59e7df933451a438226c7a91497e9bce /MediaBrowser.Server.Implementations | |
| parent | 8e57296f6986f13578f37647640fcaf49c9981f1 (diff) | |
update port mapper
Diffstat (limited to 'MediaBrowser.Server.Implementations')
| -rw-r--r-- | MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs b/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs index 1430eb9cd..3d860a27b 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. |
