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

177 lines
4.9 KiB
C#
Raw Normal View History

2014-02-26 04:38:21 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Logging;
using Mono.Nat;
2014-03-06 05:17:13 +00:00
using Mono.Nat.Enums;
using Mono.Nat.EventArgs;
2014-02-26 04:38:21 +00:00
using System;
using System.IO;
using System.Text;
namespace MediaBrowser.Server.Implementations.EntryPoints
{
public class ExternalPortForwarding : IServerEntryPoint
{
private readonly IServerApplicationHost _appHost;
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
private bool _isStarted;
public ExternalPortForwarding(ILogger logger, IServerApplicationHost appHost, IServerConfigurationManager config)
{
_logger = logger;
_appHost = appHost;
_config = config;
_config.ConfigurationUpdated += _config_ConfigurationUpdated;
}
void _config_ConfigurationUpdated(object sender, EventArgs e)
{
var enable = _config.Configuration.EnableUPnP;
if (enable && !_isStarted)
{
Reload();
}
else if (!enable && _isStarted)
{
DisposeNat();
}
}
public void Run()
{
2014-03-01 22:34:27 +00:00
//NatUtility.Logger = new LogWriter(_logger);
2014-02-26 04:38:21 +00:00
Reload();
}
private void Reload()
{
if (_config.Configuration.EnableUPnP)
{
_logger.Debug("Starting NAT discovery");
NatUtility.DeviceFound += NatUtility_DeviceFound;
NatUtility.DeviceLost += NatUtility_DeviceLost;
NatUtility.UnhandledException += NatUtility_UnhandledException;
NatUtility.StartDiscovery();
_isStarted = true;
}
}
void NatUtility_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
2014-03-01 22:34:27 +00:00
//var ex = e.ExceptionObject as Exception;
//if (ex == null)
//{
// _logger.Error("Unidentified error reported by Mono.Nat");
//}
//else
//{
// // Seeing some blank exceptions coming through here
// _logger.ErrorException("Error reported by Mono.Nat: ", ex);
//}
2014-02-26 04:38:21 +00:00
}
void NatUtility_DeviceFound(object sender, DeviceEventArgs e)
{
try
{
var device = e.Device;
_logger.Debug("NAT device found: {0}", device.LocalAddress.ToString());
CreateRules(device);
}
catch (Exception ex)
{
2014-03-01 22:34:27 +00:00
//_logger.ErrorException("Error creating port forwarding rules", ex);
2014-02-26 04:38:21 +00:00
}
}
private void CreateRules(INatDevice device)
{
var info = _appHost.GetSystemInfo();
CreatePortMap(device, info.HttpServerPortNumber);
if (info.WebSocketPortNumber != info.HttpServerPortNumber)
{
CreatePortMap(device, info.WebSocketPortNumber);
}
}
private void CreatePortMap(INatDevice device, int port)
{
2014-03-01 22:34:27 +00:00
_logger.Debug("Creating port map on port {0}", port);
2014-02-26 04:38:21 +00:00
device.CreatePortMap(new Mapping(Protocol.Tcp, port, port)
{
Description = "Media Browser Server"
});
}
void NatUtility_DeviceLost(object sender, DeviceEventArgs e)
{
var device = e.Device;
_logger.Debug("NAT device lost: {0}", device.LocalAddress.ToString());
}
public void Dispose()
{
DisposeNat();
}
private void DisposeNat()
{
_logger.Debug("Stopping NAT discovery");
try
{
NatUtility.DeviceFound -= NatUtility_DeviceFound;
NatUtility.DeviceLost -= NatUtility_DeviceLost;
NatUtility.UnhandledException -= NatUtility_UnhandledException;
NatUtility.StopDiscovery();
}
catch (Exception ex)
{
_logger.ErrorException("Error stopping NAT Discovery", ex);
}
finally
{
_isStarted = false;
}
}
private class LogWriter : TextWriter
{
private readonly ILogger _logger;
public LogWriter(ILogger logger)
{
_logger = logger;
}
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
public override void WriteLine(string format, params object[] arg)
{
_logger.Debug(format, arg);
}
public override void WriteLine(string value)
{
_logger.Debug(value);
}
}
}
}