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