Merge pull request #2403 from Mailaender/mono-nat
Use Mono.Nat for UPnP port forwarding.
This commit is contained in:
@@ -21,6 +21,10 @@ using OpenRA.Network;
|
||||
using OpenRA.Support;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
using Mono.Nat;
|
||||
using Mono.Nat.Pmp;
|
||||
using Mono.Nat.Upnp;
|
||||
|
||||
using XRandom = OpenRA.Thirdparty.Random;
|
||||
|
||||
namespace OpenRA
|
||||
@@ -34,6 +38,8 @@ namespace OpenRA
|
||||
public static ModData modData;
|
||||
static WorldRenderer worldRenderer;
|
||||
|
||||
public static INatDevice natDevice;
|
||||
|
||||
public static Viewport viewport;
|
||||
public static Settings Settings;
|
||||
|
||||
@@ -257,6 +263,24 @@ namespace OpenRA
|
||||
Log.AddChannel("perf", "perf.log");
|
||||
Log.AddChannel("debug", "debug.log");
|
||||
Log.AddChannel("sync", "syncreport.log");
|
||||
Log.AddChannel("server", "server.log");
|
||||
|
||||
try
|
||||
{
|
||||
NatUtility.Logger = Log.Channels["server"].Writer;
|
||||
NatUtility.Verbose = Settings.Server.VerboseNatDiscovery;
|
||||
NatUtility.DeviceFound += DeviceFound;
|
||||
NatUtility.DeviceLost += DeviceLost;
|
||||
Settings.Server.NatDeviceAvailable = false;
|
||||
NatUtility.StartDiscovery();
|
||||
Log.Write("server", "NAT discovery started.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Write("server", "Can't discover UPnP-enabled device: {0}", e);
|
||||
Settings.Server.NatDeviceAvailable = false;
|
||||
Settings.Server.AllowUPnP = false;
|
||||
}
|
||||
|
||||
FileSystem.Mount("."); // Needed to access shaders
|
||||
Renderer.Initialize( Game.Settings.Graphics.Mode );
|
||||
@@ -268,6 +292,44 @@ namespace OpenRA
|
||||
|
||||
Sound.Create(Settings.Sound.Engine);
|
||||
InitializeWithMods(Settings.Game.Mods);
|
||||
|
||||
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, () =>
|
||||
{
|
||||
NatUtility.StopDiscovery();
|
||||
Log.Write("server", "NAT discovery stopped.");
|
||||
if (natDevice == null)
|
||||
{
|
||||
Log.Write("server", "No NAT devices with UPnP enabled found within {0} ms deadline. Disabling automatic port forwarding.".F(Settings.Server.NatDiscoveryTimeout));
|
||||
Settings.Server.NatDeviceAvailable = false;
|
||||
Settings.Server.AllowUPnP = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void DeviceFound(object sender, DeviceEventArgs args)
|
||||
{
|
||||
natDevice = args.Device;
|
||||
|
||||
Log.Write("server", "NAT device discovered.");
|
||||
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);
|
||||
|
||||
Settings.Server.NatDeviceAvailable = true;
|
||||
Settings.Server.AllowUPnP = true;
|
||||
}
|
||||
|
||||
public static void DeviceLost(object sender, DeviceEventArgs args)
|
||||
{
|
||||
natDevice = args.Device;
|
||||
|
||||
Log.Write("server", "NAT device lost.");
|
||||
Log.Write("server", "Type: {0}", natDevice.GetType());
|
||||
|
||||
Settings.Server.NatDeviceAvailable = false;
|
||||
Settings.Server.AllowUPnP = false;
|
||||
}
|
||||
|
||||
public static void InitializeWithMods(string[] mods)
|
||||
@@ -416,7 +478,7 @@ namespace OpenRA
|
||||
public static void CreateServer(ServerSettings settings)
|
||||
{
|
||||
server = new Server.Server(new IPEndPoint(IPAddress.Any, settings.ListenPort),
|
||||
Game.Settings.Game.Mods, settings, modData);
|
||||
Game.Settings.Game.Mods, settings, modData, natDevice);
|
||||
}
|
||||
|
||||
public static int CreateLocalServer(string map)
|
||||
@@ -433,7 +495,7 @@ namespace OpenRA
|
||||
settings.AllowUPnP = false;
|
||||
|
||||
server = new Server.Server(new IPEndPoint(IPAddress.Loopback, 0),
|
||||
Game.Settings.Game.Mods, settings, modData);
|
||||
Game.Settings.Game.Mods, settings, modData, natDevice);
|
||||
|
||||
return server.Port;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user