2019-11-01 13:38:54 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
using System;
|
2020-04-06 00:05:47 -04:00
|
|
|
using System.Collections.Concurrent;
|
2014-08-21 11:55:35 -04:00
|
|
|
using System.Collections.Generic;
|
2015-04-29 14:48:34 -04:00
|
|
|
using System.Net;
|
2019-11-01 16:22:35 -04:00
|
|
|
using System.Text;
|
2019-01-13 14:20:41 -05:00
|
|
|
using System.Threading;
|
2017-08-09 15:56:38 -04:00
|
|
|
using System.Threading.Tasks;
|
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;
|
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
|
|
|
{
|
2019-11-01 16:25:37 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Server entrypoint handling external port forwarding.
|
|
|
|
/// </summary>
|
2014-02-25 23:38:21 -05:00
|
|
|
public class ExternalPortForwarding : IServerEntryPoint
|
|
|
|
{
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2016-09-11 03:33:53 -04:00
|
|
|
private readonly IDeviceDiscovery _deviceDiscovery;
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
private readonly ConcurrentDictionary<IPEndPoint, byte> _createdRules = new ConcurrentDictionary<IPEndPoint, byte>();
|
|
|
|
|
2019-11-01 16:25:37 -04:00
|
|
|
private Timer _timer;
|
2020-04-06 00:22:27 -04:00
|
|
|
private string _configIdentifier;
|
2019-11-01 16:22:35 -04:00
|
|
|
|
|
|
|
private bool _disposed = false;
|
2019-09-23 17:10:51 -04:00
|
|
|
|
2019-11-01 16:25:37 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ExternalPortForwarding"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="appHost">The application host.</param>
|
|
|
|
/// <param name="config">The configuration manager.</param>
|
|
|
|
/// <param name="deviceDiscovery">The device discovery.</param>
|
2019-11-01 16:22:35 -04:00
|
|
|
public ExternalPortForwarding(
|
|
|
|
ILogger<ExternalPortForwarding> logger,
|
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IServerConfigurationManager config,
|
|
|
|
IDeviceDiscovery deviceDiscovery)
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
_logger = logger;
|
2014-02-25 23:38:21 -05:00
|
|
|
_appHost = appHost;
|
|
|
|
_config = config;
|
2016-09-11 03:33:53 -04:00
|
|
|
_deviceDiscovery = deviceDiscovery;
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2016-04-04 15:07:43 -04:00
|
|
|
private string GetConfigIdentifier()
|
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
const char Separator = '|';
|
2016-04-04 15:07:43 -04:00
|
|
|
var config = _config.Configuration;
|
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
return new StringBuilder(32)
|
|
|
|
.Append(config.EnableUPnP).Append(Separator)
|
|
|
|
.Append(config.PublicPort).Append(Separator)
|
|
|
|
.Append(_appHost.HttpPort).Append(Separator)
|
|
|
|
.Append(_appHost.HttpsPort).Append(Separator)
|
|
|
|
.Append(_appHost.EnableHttps).Append(Separator)
|
|
|
|
.Append(config.EnableRemoteAccess).Append(Separator)
|
|
|
|
.ToString();
|
2015-01-18 23:29:57 -05:00
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
2019-11-02 05:31:14 -04:00
|
|
|
private void OnConfigurationUpdated(object sender, EventArgs e)
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2020-04-06 00:22:27 -04:00
|
|
|
var oldConfigIdentifier = _configIdentifier;
|
|
|
|
_configIdentifier = GetConfigIdentifier();
|
|
|
|
|
|
|
|
if (!string.Equals(_configIdentifier, oldConfigIdentifier, StringComparison.OrdinalIgnoreCase))
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
Stop();
|
2019-11-02 05:31:14 -04:00
|
|
|
Start();
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
2016-03-31 15:22:07 -04:00
|
|
|
}
|
2014-08-21 11:55:35 -04:00
|
|
|
|
2019-11-01 16:25:37 -04:00
|
|
|
/// <inheritdoc />
|
2019-01-27 09:40:37 -05:00
|
|
|
public Task RunAsync()
|
2016-03-31 15:22:07 -04:00
|
|
|
{
|
2019-11-02 05:31:14 -04:00
|
|
|
Start();
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
_config.ConfigurationUpdated += OnConfigurationUpdated;
|
2019-01-27 09:40:37 -05:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
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
|
|
|
{
|
2019-11-02 05:31:14 -04:00
|
|
|
if (!_config.Configuration.EnableUPnP || !_config.Configuration.EnableRemoteAccess)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:22:27 -04:00
|
|
|
_logger.LogInformation("Starting NAT discovery");
|
2019-11-01 16:22:35 -04:00
|
|
|
|
|
|
|
NatUtility.DeviceFound += OnNatUtilityDeviceFound;
|
|
|
|
NatUtility.StartDiscovery();
|
2015-04-23 12:50:54 -04:00
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
_timer = new Timer((_) => _createdRules.Clear(), null, TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10));
|
2016-04-04 15:07:43 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
_deviceDiscovery.DeviceDiscovered += OnDeviceDiscoveryDeviceDiscovered;
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
private void Stop()
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2020-04-06 00:22:27 -04:00
|
|
|
_logger.LogInformation("Stopping NAT discovery");
|
2016-09-11 03:33:53 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
NatUtility.StopDiscovery();
|
|
|
|
NatUtility.DeviceFound -= OnNatUtilityDeviceFound;
|
2016-10-27 21:07:40 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
_timer?.Dispose();
|
2016-10-14 12:22:04 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
_deviceDiscovery.DeviceDiscovered -= OnDeviceDiscoveryDeviceDiscovered;
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
private void OnDeviceDiscoveryDeviceDiscovered(object sender, GenericEventArgs<UpnpDeviceInfo> e)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
NatUtility.Search(e.Argument.LocalIpAddress, NatProtocol.Upnp);
|
|
|
|
}
|
2017-01-02 14:36:54 -05:00
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
private async void OnNatUtilityDeviceFound(object sender, DeviceEventArgs e)
|
2019-11-01 16:22:35 -04:00
|
|
|
{
|
2016-04-04 15:07:43 -04:00
|
|
|
try
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2020-04-06 00:05:47 -04:00
|
|
|
await CreateRules(e.Device).ConfigureAwait(false);
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
2019-11-01 16:22:35 -04:00
|
|
|
catch (Exception ex)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2019-11-01 16:22:35 -04: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
|
|
|
}
|
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
private Task 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
|
2020-04-06 00:05:47 -04:00
|
|
|
if (!_createdRules.TryAdd(device.DeviceEndpoint, 0))
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2020-04-06 00:05:47 -04:00
|
|
|
return Task.CompletedTask;
|
2017-02-22 15:58:02 -05:00
|
|
|
}
|
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
return Task.WhenAll(CreatePortMaps(device));
|
|
|
|
}
|
2016-10-27 21:07:40 -04:00
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
private IEnumerable<Task> CreatePortMaps(INatDevice device)
|
|
|
|
{
|
|
|
|
yield return CreatePortMap(device, _appHost.HttpPort, _config.Configuration.PublicPort);
|
|
|
|
|
|
|
|
if (_appHost.EnableHttps)
|
2016-10-27 21:07:40 -04:00
|
|
|
{
|
2020-04-06 00:05:47 -04:00
|
|
|
yield return CreatePortMap(device, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort);
|
2016-10-27 21:07:40 -04:00
|
|
|
}
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
private async Task CreatePortMap(INatDevice device, int privatePort, int publicPort)
|
2016-04-04 15:07:43 -04:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
_logger.LogDebug(
|
2020-04-06 00:05:47 -04:00
|
|
|
"Creating port map on local port {LocalPort} to public port {PublicPort} with device {DeviceEndpoint}",
|
2019-11-01 16:22:35 -04:00
|
|
|
privatePort,
|
|
|
|
publicPort,
|
|
|
|
device.DeviceEndpoint);
|
|
|
|
|
2020-04-06 00:05:47 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var mapping = new Mapping(Protocol.Tcp, privatePort, publicPort, 0, _appHost.Name);
|
|
|
|
await device.CreatePortMapAsync(mapping).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(
|
|
|
|
ex,
|
|
|
|
"Error creating port map on local port {LocalPort} to public port {PublicPort} with device {DeviceEndpoint}.",
|
|
|
|
privatePort,
|
|
|
|
publicPort,
|
|
|
|
device.DeviceEndpoint);
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
/// <inheritdoc />
|
2016-03-31 14:46:03 -04:00
|
|
|
public void Dispose()
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
2016-03-31 14:46:03 -04:00
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2016-03-31 14:46:03 -04:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
if (_disposed)
|
2014-02-25 23:38:21 -05:00
|
|
|
{
|
2019-11-01 16:22:35 -04:00
|
|
|
return;
|
2016-04-04 15:07:43 -04:00
|
|
|
}
|
|
|
|
|
2019-11-02 05:31:14 -04:00
|
|
|
_config.ConfigurationUpdated -= OnConfigurationUpdated;
|
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
Stop();
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
_timer = null;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2019-11-01 16:22:35 -04:00
|
|
|
_disposed = true;
|
2014-02-25 23:38:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-28 10:48:26 -05:00
|
|
|
}
|