2015-04-19 11:54:20 -04:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-04-29 14:48:34 -04:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
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;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
using System.IO;
|
2015-04-29 14:48:34 -04:00
|
|
|
|
using System.Net;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
using System.Text;
|
2016-01-28 22:40:21 -05:00
|
|
|
|
using MediaBrowser.Common.Threading;
|
2016-03-31 14:46:03 -04:00
|
|
|
|
using Open.Nat;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2014-02-25 23:38:21 -05:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.EntryPoints
|
|
|
|
|
{
|
|
|
|
|
public class ExternalPortForwarding : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2015-04-29 14:48:34 -04:00
|
|
|
|
private readonly ISsdpHandler _ssdp;
|
2016-03-31 14:46:03 -04:00
|
|
|
|
private CancellationTokenSource _currentCancellationTokenSource;
|
|
|
|
|
private TimeSpan _interval = TimeSpan.FromHours(1);
|
2014-08-21 11:55:35 -04:00
|
|
|
|
|
2015-04-29 14:48:34 -04:00
|
|
|
|
public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost appHost, IServerConfigurationManager config, ISsdpHandler ssdp)
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
2014-05-01 22:54:33 -04:00
|
|
|
|
_logger = logmanager.GetLogger("PortMapper");
|
2014-02-25 23:38:21 -05:00
|
|
|
|
_appHost = appHost;
|
|
|
|
|
_config = config;
|
2015-04-29 14:48:34 -04:00
|
|
|
|
_ssdp = ssdp;
|
2015-01-18 23:29:57 -05:00
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
2014-03-01 17:34:27 -05:00
|
|
|
|
//NatUtility.Logger = new LogWriter(_logger);
|
2014-08-21 11:55:35 -04:00
|
|
|
|
|
2015-01-18 23:29:57 -05:00
|
|
|
|
if (_config.Configuration.EnableUPnP)
|
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
Discover();
|
2015-01-18 23:29:57 -05:00
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
private async void Discover()
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
var discoverer = new NatDiscoverer();
|
2015-04-29 14:48:34 -04:00
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource(10000);
|
|
|
|
|
_currentCancellationTokenSource = cancellationTokenSource;
|
2015-01-18 23:29:57 -05:00
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
try
|
2015-04-29 14:48:34 -04:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
var device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cancellationTokenSource).ConfigureAwait(false);
|
2015-04-23 12:50:54 -04:00
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
await CreateRules(device).ConfigureAwait(false);
|
2015-04-23 12:50:54 -04:00
|
|
|
|
}
|
2016-03-31 14:46:03 -04:00
|
|
|
|
catch (OperationCanceledException)
|
2015-04-23 12:50:54 -04:00
|
|
|
|
{
|
2014-02-25 23:38:21 -05:00
|
|
|
|
|
|
|
|
|
}
|
2015-04-13 12:21:10 -04:00
|
|
|
|
catch (Exception ex)
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
_logger.ErrorException("Error discovering NAT devices", ex);
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
2016-03-31 14:46:03 -04:00
|
|
|
|
finally
|
2014-08-21 11:55:35 -04:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
_currentCancellationTokenSource = null;
|
2014-08-21 11:55:35 -04:00
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
if (_config.Configuration.EnableUPnP)
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
await Task.Delay(_interval).ConfigureAwait(false);
|
|
|
|
|
Discover();
|
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
private async Task CreateRules(NatDevice device)
|
2014-08-27 21:27:46 -04: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
|
2014-02-25 23:38:21 -05:00
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
await CreatePortMap(device, _appHost.HttpPort, _config.Configuration.PublicPort).ConfigureAwait(false);
|
|
|
|
|
await CreatePortMap(device, _appHost.HttpsPort, _config.Configuration.PublicHttpsPort).ConfigureAwait(false);
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
private async Task CreatePortMap(NatDevice device, int privatePort, int publicPort)
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
_logger.Debug("Creating port map on port {0}", privatePort);
|
2015-04-29 14:48:34 -04:00
|
|
|
|
|
2014-02-25 23:38:21 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, privatePort, publicPort, _appHost.Name)).ConfigureAwait(false);
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
_logger.ErrorException("Error creating port map", ex);
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 14:46:03 -04:00
|
|
|
|
public void Dispose()
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
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()
|
|
|
|
|
{
|
|
|
|
|
if (_currentCancellationTokenSource != null)
|
2014-02-25 23:38:21 -05:00
|
|
|
|
{
|
2016-03-31 14:46:03 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_currentCancellationTokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error calling _currentCancellationTokenSource.Cancel", ex);
|
|
|
|
|
}
|
2014-02-25 23:38:21 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|