2019-01-06 15:50:43 -05:00
|
|
|
using System;
|
2014-08-21 11:55:35 -04:00
|
|
|
using System.Collections.Generic;
|
2015-01-18 23:29:57 -05:00
|
|
|
using System.Globalization;
|
2015-04-29 14:48:34 -04:00
|
|
|
using System.Net;
|
2019-01-13 14:20:41 -05:00
|
|
|
using System.Threading;
|
2017-08-09 15:56:38 -04:00
|
|
|
using System.Threading.Tasks;
|
2016-10-27 21:07:40 -04:00
|
|
|
using MediaBrowser.Common.Net;
|
2016-11-10 23:25:21 -05:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2016-10-29 14:46:56 -04:00
|
|
|
using MediaBrowser.Model.Dlna;
|
2016-09-11 03:33:53 -04:00
|
|
|
using MediaBrowser.Model.Events;
|
2016-11-03 18:53:02 -04:00
|
|
|
using MediaBrowser.Model.Threading;
|
2019-01-13 14:20:41 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-10 23:25:21 -05:00
|
|
|
using Mono.Nat;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
2017-08-09 15:56:38 -04:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
|
|
|
public class ExternalPortForwarding : IServerEntryPoint
|
|
|
|
{
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
private readonly ILogger _logger;
|
2016-10-27 21:07:40 -04:00
|
|
|
private readonly IHttpClient _httpClient;
|
2014-02-25 23:38:21 -05:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2016-09-11 03:33:53 -04:00
|
|
|
private readonly IDeviceDiscovery _deviceDiscovery;
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2016-11-03 18:53:02 -04:00
|
|
|
private ITimer _timer;
|
|
|
|
private readonly ITimerFactory _timerFactory;
|
2014-08-21 11:55:35 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private NatManager _natManager;
|
|
|
|
|
2018-12-13 08:18:25 -05:00
|
|
|
public ExternalPortForwarding(ILoggerFactory loggerFactory, IServerApplicationHost appHost, IServerConfigurationManager config, IDeviceDiscovery deviceDiscovery, IHttpClient httpClient, ITimerFactory timerFactory)
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger = loggerFactory.CreateLogger("PortMapper");
|
2014-02-25 23:38:21 -05:00
|
|
|
_appHost = appHost;
|
|
|
|
_config = config;
|
2016-09-11 03:33:53 -04:00
|
|
|
_deviceDiscovery = deviceDiscovery;
|
2016-10-27 21:07:40 -04:00
|
|
|
_httpClient = httpClient;
|
2016-11-03 18:53:02 -04:00
|
|
|
_timerFactory = timerFactory;
|
2018-09-12 13:26:21 -04:00
|
|
|
_config.ConfigurationUpdated += _config_ConfigurationUpdated1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void _config_ConfigurationUpdated1(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
_config_ConfigurationUpdated(sender, e);
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
2016-03-31 15:22:07 -04:00
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
private string _lastConfigIdentifier;
|
|
|
|
private string GetConfigIdentifier()
|
|
|
|
{
|
|
|
|
var values = new List<string>();
|
|
|
|
var config = _config.Configuration;
|
|
|
|
|
|
|
|
values.Add(config.EnableUPnP.ToString());
|
|
|
|
values.Add(config.PublicPort.ToString(CultureInfo.InvariantCulture));
|
|
|
|
values.Add(_appHost.HttpPort.ToString(CultureInfo.InvariantCulture));
|
|
|
|
values.Add(_appHost.HttpsPort.ToString(CultureInfo.InvariantCulture));
|
|
|
|
values.Add(_appHost.EnableHttps.ToString());
|
2018-09-12 13:26:21 -04:00
|
|
|
values.Add((config.EnableRemoteAccess).ToString());
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2018-12-28 10:48:26 -05:00
|
|
|
return string.Join("|", values.ToArray());
|
2015-01-18 23:29:57 -05:00
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
void _config_ConfigurationUpdated(object sender, EventArgs e)
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2016-04-04 15:07:43 -04:00
|
|
|
if (!string.Equals(_lastConfigIdentifier, GetConfigIdentifier(), StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
DisposeNat();
|
2016-04-04 15:07:43 -04:00
|
|
|
|
|
|
|
Run();
|
|
|
|
}
|
2016-03-31 15:22:07 -04:00
|
|
|
}
|
2014-08-21 11:55:35 -04:00
|
|
|
|
2016-03-31 15:22:07 -04:00
|
|
|
public void Run()
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
if (_config.Configuration.EnableUPnP && _config.Configuration.EnableRemoteAccess)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
_config.ConfigurationUpdated -= _config_ConfigurationUpdated;
|
|
|
|
_config.ConfigurationUpdated += _config_ConfigurationUpdated;
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
private void Start()
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Starting NAT discovery");
|
2018-09-12 13:26:21 -04:00
|
|
|
if (_natManager == null)
|
2016-03-31 15:22:07 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
_natManager = new NatManager(_logger, _httpClient);
|
|
|
|
_natManager.DeviceFound += NatUtility_DeviceFound;
|
|
|
|
_natManager.StartDiscovery();
|
|
|
|
}
|
2015-04-23 12:50:54 -04:00
|
|
|
|
2016-11-19 00:52:49 -05:00
|
|
|
_timer = _timerFactory.Create(ClearCreatedRules, null, TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10));
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2016-09-11 03:33:53 -04:00
|
|
|
_deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered;
|
2016-04-04 15:07:43 -04:00
|
|
|
|
|
|
|
_lastConfigIdentifier = GetConfigIdentifier();
|
|
|
|
}
|
|
|
|
|
2016-09-11 03:33:53 -04:00
|
|
|
private async void _deviceDiscovery_DeviceDiscovered(object sender, GenericEventArgs<UpnpDeviceInfo> e)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2017-01-02 14:36:54 -05:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-11 03:33:53 -04:00
|
|
|
var info = e.Argument;
|
2016-06-11 11:56:15 -04:00
|
|
|
|
2019-01-17 12:47:41 -05:00
|
|
|
if (!info.Headers.TryGetValue("USN", out string usn)) usn = string.Empty;
|
2016-06-11 11:56:15 -04:00
|
|
|
|
2019-01-17 12:47:41 -05:00
|
|
|
if (!info.Headers.TryGetValue("NT", out string nt)) nt = string.Empty;
|
2016-06-11 11:56:15 -04:00
|
|
|
|
|
|
|
// Filter device type
|
|
|
|
if (usn.IndexOf("WANIPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
|
|
|
nt.IndexOf("WANIPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
|
|
|
usn.IndexOf("WANPPPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
|
|
|
nt.IndexOf("WANPPPConnection:", StringComparison.OrdinalIgnoreCase) == -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var identifier = string.IsNullOrWhiteSpace(usn) ? nt : usn;
|
|
|
|
|
2016-10-14 12:22:04 -04:00
|
|
|
if (info.Location == null)
|
2016-06-11 11:56:15 -04:00
|
|
|
{
|
2016-10-14 12:22:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_usnsHandled)
|
|
|
|
{
|
|
|
|
if (_usnsHandled.Contains(identifier))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-06-11 11:56:15 -04:00
|
|
|
_usnsHandled.Add(identifier);
|
2016-10-14 12:22:04 -04:00
|
|
|
}
|
2016-06-11 11:56:15 -04:00
|
|
|
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Found NAT device: " + identifier);
|
2016-09-11 03:33:53 -04:00
|
|
|
|
2019-01-13 15:46:33 -05:00
|
|
|
if (IPAddress.TryParse(info.Location.Host, out var address))
|
2016-10-14 12:22:04 -04:00
|
|
|
{
|
|
|
|
// The Handle method doesn't need the port
|
|
|
|
var endpoint = new IPEndPoint(address, info.Location.Port);
|
2016-09-11 03:33:53 -04:00
|
|
|
|
2016-10-14 12:22:04 -04:00
|
|
|
IPAddress localAddress = null;
|
2016-09-11 03:33:53 -04:00
|
|
|
|
2016-10-14 12:22:04 -04:00
|
|
|
try
|
|
|
|
{
|
2017-11-23 10:46:16 -05:00
|
|
|
var localAddressString = await _appHost.GetLocalApiUrl(CancellationToken.None).ConfigureAwait(false);
|
2016-09-11 03:33:53 -04:00
|
|
|
|
2019-01-13 15:46:33 -05:00
|
|
|
if (Uri.TryCreate(localAddressString, UriKind.Absolute, out var uri))
|
2016-09-11 03:33:53 -04:00
|
|
|
{
|
2016-10-27 21:07:40 -04:00
|
|
|
localAddressString = uri.Host;
|
|
|
|
|
|
|
|
if (!IPAddress.TryParse(localAddressString, out localAddress))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-09-11 03:33:53 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 21:07:40 -04:00
|
|
|
catch (Exception ex)
|
2016-10-14 12:22:04 -04:00
|
|
|
{
|
2018-12-20 07:39:58 -05:00
|
|
|
_logger.LogError(ex, "Error");
|
2016-10-14 12:22:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:36:54 -05:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
// This should never happen, but the Handle method will throw ArgumentNullException if it does
|
|
|
|
if (localAddress == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var natManager = _natManager;
|
|
|
|
if (natManager != null)
|
|
|
|
{
|
|
|
|
await natManager.Handle(localAddress, info, endpoint, NatProtocol.Upnp).ConfigureAwait(false);
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
|
|
|
|
2016-09-11 03:33:53 -04:00
|
|
|
private void ClearCreatedRules(object state)
|
|
|
|
{
|
2017-02-22 15:58:02 -05:00
|
|
|
lock (_createdRules)
|
|
|
|
{
|
|
|
|
_createdRules.Clear();
|
|
|
|
}
|
2016-10-14 12:22:04 -04:00
|
|
|
lock (_usnsHandled)
|
|
|
|
{
|
|
|
|
_usnsHandled.Clear();
|
|
|
|
}
|
2016-09-11 03:33:53 -04:00
|
|
|
}
|
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
void NatUtility_DeviceFound(object sender, DeviceEventArgs e)
|
|
|
|
{
|
2017-01-02 14:36:54 -05:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
try
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2016-04-04 15:07:43 -04:00
|
|
|
var device = e.Device;
|
|
|
|
|
|
|
|
CreateRules(device);
|
|
|
|
}
|
2016-08-05 17:15:48 -04:00
|
|
|
catch
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
|
|
|
// Commenting out because users are reporting problems out of our control
|
2018-12-20 07:11:26 -05:00
|
|
|
//_logger.LogError(ex, "Error creating port forwarding rules");
|
2016-03-31 14:46:03 -04:00
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
private List<string> _createdRules = new List<string>();
|
2016-06-11 11:56:15 -04:00
|
|
|
private List<string> _usnsHandled = new List<string>();
|
2017-01-24 14:54:18 -05:00
|
|
|
private async void CreateRules(INatDevice device)
|
2014-08-27 21:27:46 -04:00
|
|
|
{
|
2017-01-02 14:36:54 -05:00
|
|
|
if (_disposed)
|
|
|
|
{
|
2019-01-06 15:50:43 -05:00
|
|
|
throw new ObjectDisposedException(GetType().Name);
|
2017-01-02 14:36:54 -05:00
|
|
|
}
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
// 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
|
2018-09-12 13:26:21 -04:00
|
|
|
var address = device.LocalAddress;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
var addressString = address.ToString();
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2017-02-22 15:58:02 -05:00
|
|
|
lock (_createdRules)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!_createdRules.Contains(addressString))
|
2017-01-24 14:54:18 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
_createdRules.Add(addressString);
|
2017-01-24 14:54:18 -05:00
|
|
|
}
|
2017-02-22 15:58:02 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
try
|
2017-02-22 15:58:02 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
await CreatePortMap(device, _appHost.HttpPort, _config.Configuration.PublicPort).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
_logger.LogError(ex, "Error creating http port map");
|
2018-09-12 13:26:21 -04:00
|
|
|
return;
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
2016-10-27 21:07:40 -04:00
|
|
|
|
|
|
|
try
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
await CreatePortMap(device, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort).ConfigureAwait(false);
|
2016-10-27 21:07:40 -04:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
_logger.LogError(ex, "Error creating https port map");
|
2016-10-27 21:07:40 -04:00
|
|
|
}
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private Task CreatePortMap(INatDevice device, int privatePort, int publicPort)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Creating port map on local port {0} to public port {1} with device {2}", privatePort, publicPort, device.LocalAddress.ToString());
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
return device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort)
|
|
|
|
{
|
|
|
|
Description = _appHost.Name
|
|
|
|
});
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:36:54 -05:00
|
|
|
private bool _disposed = false;
|
2016-03-31 14:46:03 -04:00
|
|
|
public void Dispose()
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2017-01-02 14:36:54 -05:00
|
|
|
_disposed = true;
|
2016-03-31 14:46:03 -04:00
|
|
|
DisposeNat();
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
private void DisposeNat()
|
|
|
|
{
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Stopping NAT discovery");
|
2016-04-04 15:07:43 -04:00
|
|
|
|
|
|
|
if (_timer != null)
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2016-04-04 15:07:43 -04:00
|
|
|
_timer.Dispose();
|
|
|
|
_timer = null;
|
|
|
|
}
|
|
|
|
|
2016-09-11 03:33:53 -04:00
|
|
|
_deviceDiscovery.DeviceDiscovered -= _deviceDiscovery_DeviceDiscovered;
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
var natManager = _natManager;
|
|
|
|
|
|
|
|
if (natManager != null)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
_natManager = null;
|
|
|
|
|
|
|
|
using (natManager)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
natManager.StopDiscovery();
|
|
|
|
natManager.DeviceFound -= NatUtility_DeviceFound;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
_logger.LogError(ex, "Error stopping NAT Discovery");
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 10:48:26 -05:00
|
|
|
}
|