Merge pull request #11286 from Mailaender/open.nat-2.0.16

Dump Mono.NAT in favor of Open.Nat 2.0.16
This commit is contained in:
reaperrr
2016-06-26 16:16:11 +02:00
committed by GitHub
15 changed files with 81 additions and 155 deletions

View File

@@ -18,6 +18,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using OpenRA.Chat;
using OpenRA.Graphics;
using OpenRA.Network;
@@ -51,6 +52,8 @@ namespace OpenRA
public static GlobalChat GlobalChat;
static Task discoverNat;
public static OrderManager JoinServer(string host, int port, string password, bool recordReplay = true)
{
var connection = new NetworkConnection(host, port);
@@ -247,16 +250,7 @@ namespace OpenRA
Log.AddChannel("graphics", "graphics.log");
Log.AddChannel("geoip", "geoip.log");
Log.AddChannel("irc", "irc.log");
if (Settings.Server.DiscoverNatDevices)
UPnP.TryNatDiscovery();
else
{
Settings.Server.NatDeviceAvailable = false;
Settings.Server.AllowPortForward = false;
}
GeoIP.Initialize();
Log.AddChannel("nat", "nat.log");
var renderers = new[] { Settings.Graphics.Renderer, "Default", null };
foreach (var r in renderers)
@@ -277,6 +271,16 @@ namespace OpenRA
}
}
GeoIP.Initialize();
if (!Game.Settings.Server.DiscoverNatDevices)
Game.Settings.Server.AllowPortForward = false;
else
{
discoverNat = UPnP.DiscoverNatDevices(Settings.Server.NatDiscoveryTimeout);
Game.Settings.Server.AllowPortForward = true;
}
Sound = new Sound(Settings.Sound.Engine);
GlobalChat = new GlobalChat();
@@ -286,9 +290,6 @@ namespace OpenRA
Console.WriteLine("\t{0}: {1} ({2})", mod.Key, mod.Value.Title, mod.Value.Version);
InitializeMod(Settings.Game.Mod, args);
if (Settings.Server.DiscoverNatDevices)
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, UPnP.StoppingNatDiscovery);
}
public static bool IsModInstalled(string modId)
@@ -392,6 +393,18 @@ namespace OpenRA
JoinLocal();
try
{
if (discoverNat != null)
discoverNat.Wait();
}
catch (Exception e)
{
Console.WriteLine("NAT discovery failed: {0}", e.Message);
Log.Write("nat", e.ToString());
Game.Settings.Server.AllowPortForward = false;
}
ModData.LoadScreen.StartGame(args);
}

View File

@@ -10,127 +10,67 @@
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using Mono.Nat;
using System.Threading;
using System.Threading.Tasks;
using Open.Nat;
namespace OpenRA.Network
{
public static class UPnP
public class UPnP
{
static INatDevice natDevice;
static NatDevice natDevice;
static Mapping mapping;
public static void TryNatDiscovery()
public static IPAddress ExternalIP { get; private set; }
public static async Task DiscoverNatDevices(int timeout)
{
NatDiscoverer.TraceSource.Switch.Level = SourceLevels.Verbose;
var logChannel = Log.Channel("nat");
NatDiscoverer.TraceSource.Listeners.Add(new TextWriterTraceListener(logChannel.Writer));
var natDiscoverer = new NatDiscoverer();
var token = new CancellationTokenSource(timeout);
natDevice = await natDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, token);
try
{
NatUtility.Logger = Log.Channel("server").Writer;
NatUtility.Verbose = Game.Settings.Server.VerboseNatDiscovery;
NatUtility.DeviceFound += DeviceFound;
Game.Settings.Server.NatDeviceAvailable = false;
NatUtility.StartDiscovery();
Log.Write("server", "NAT discovery started.");
ExternalIP = await natDevice.GetExternalIPAsync();
}
catch (Exception e)
{
Log.Write("server", "Can't discover UPnP-enabled device: {0}", e);
Game.Settings.Server.NatDeviceAvailable = false;
Game.Settings.Server.AllowPortForward = false;
Console.WriteLine("Getting the external IP from NAT device failed: {0}", e.Message);
Log.Write("nat", e.StackTrace);
}
}
public static void StoppingNatDiscovery()
public static async Task ForwardPort(int listen, int external)
{
Log.Write("server", "Stopping NAT discovery.");
NatUtility.StopDiscovery();
if (natDevice == null || natDevice.GetType() != typeof(Mono.Nat.Upnp.UpnpNatDevice))
{
Log.Write("server",
"No NAT devices with UPnP enabled found within {0} ms deadline. Disabling automatic port forwarding.".F(Game.Settings.Server.NatDiscoveryTimeout));
Game.Settings.Server.NatDeviceAvailable = false;
Game.Settings.Server.AllowPortForward = false;
}
}
public static void DeviceFound(object sender, DeviceEventArgs args)
{
Log.Write("server", "NAT device discovered.");
Game.Settings.Server.NatDeviceAvailable = true;
Game.Settings.Server.AllowPortForward = true;
mapping = new Mapping(Protocol.Tcp, listen, external, "OpenRA");
try
{
natDevice = args.Device;
Log.Write("server", "Type: {0}", natDevice.GetType());
Log.Write("server", "Your external IP is: {0}", natDevice.GetExternalIP());
foreach (var mp in natDevice.GetAllMappings())
Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}",
mp.Protocol, mp.PublicPort, mp.PrivatePort);
await natDevice.CreatePortMapAsync(mapping);
}
catch (Exception e)
{
Log.Write("server", "Can't fetch information from NAT device: {0}", e);
Game.Settings.Server.NatDeviceAvailable = false;
Game.Settings.Server.AllowPortForward = false;
Console.WriteLine("Port forwarding failed: {0}", e.Message);
Log.Write("nat", e.StackTrace);
}
}
public static void ForwardPort(int lifetime)
public static async Task RemovePortForward()
{
try
{
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort, lifetime);
natDevice.CreatePortMap(mapping);
Log.Write("server", "Create port mapping: protocol = {0}, public = {1}, private = {2}, lifetime = {3} s",
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Lifetime);
}
catch (MappingException e)
{
if (e.ErrorCode == 725 && lifetime != 0)
{
Log.Write("server", "NAT device answered with OnlyPermanentLeasesSupported. Retrying...");
ForwardPort(0);
}
else
{
Log.Write("server", "Can not forward ports via UPnP: {0}", e);
Game.Settings.Server.AllowPortForward = false;
}
}
}
public static void RemovePortforward()
{
try
{
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort);
natDevice.DeletePortMap(mapping);
Log.Write("server", "Remove port mapping: protocol = {0}, public = {1}, private = {2}, expiration = {3}",
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Expiration);
await natDevice.DeletePortMapAsync(mapping);
}
catch (Exception e)
{
Log.Write("server", "Can not remove UPnP portforwarding rules: {0}", e);
Game.Settings.Server.AllowPortForward = false;
}
}
public static IPAddress GetExternalIP()
{
if (natDevice == null)
return null;
try
{
return natDevice.GetExternalIP();
}
catch (Exception e)
{
Log.Write("server", "Failed to get the external IP from NAT device: {0}", e);
return null;
Console.WriteLine("Port removal failed: {0}", e.Message);
Log.Write("nat", e.StackTrace);
}
}
}

View File

@@ -63,11 +63,8 @@
<HintPath>..\thirdparty\download\SharpFont.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Nat">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
<Package>mono.nat</Package>
<HintPath>..\thirdparty\download\Mono.Nat.dll</HintPath>
<Reference Include="Open.Nat">
<HintPath>..\thirdparty\download\Open.Nat.dll</HintPath>
</Reference>
<Reference Include="Eluant">
<HintPath>..\thirdparty\download\Eluant.dll</HintPath>

View File

@@ -137,7 +137,7 @@ namespace OpenRA.Server
randomSeed = (int)DateTime.Now.ToBinary();
if (Settings.AllowPortForward)
UPnP.ForwardPort(3600);
UPnP.ForwardPort(Settings.ListenPort, Settings.ExternalPort).Wait();
foreach (var trait in modData.Manifest.ServerTraits)
serverTraits.Add(modData.ObjectCreator.CreateObject<ServerTrait>(trait));
@@ -202,7 +202,8 @@ namespace OpenRA.Server
if (State == ServerState.ShuttingDown)
{
EndGame();
if (Settings.AllowPortForward) UPnP.RemovePortforward();
if (Settings.AllowPortForward)
UPnP.RemovePortForward().Wait();
break;
}
}

View File

@@ -59,14 +59,9 @@ namespace OpenRA
[Desc("Set this to false to disable UPnP even if compatible devices are found.")]
public bool AllowPortForward = true;
public bool NatDeviceAvailable = false; // internal check if discovery succeeded
[Desc("Time in milliseconds to search for UPnP enabled NAT devices.")]
public int NatDiscoveryTimeout = 1000;
[Desc("Print very detailed logs for debugging issues with routers.")]
public bool VerboseNatDiscovery = false;
[Desc("Starts the game with a default map. Input as hash that can be obtained by the utility.")]
public string Map = null;