jellyfin/MediaBrowser.Server.Implementations/EntryPoints/ExternalPortForwarding.cs

123 lines
3.8 KiB
C#
Raw Normal View History

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