Merge pull request #10068 from Mailaender/catch-natdevice-externalip
Fixed crashes when NatDevice.GetExternalIP() fails
This commit is contained in:
@@ -9,14 +9,14 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Net;
|
||||||
using Mono.Nat;
|
using Mono.Nat;
|
||||||
|
|
||||||
namespace OpenRA.Network
|
namespace OpenRA.Network
|
||||||
{
|
{
|
||||||
public static class UPnP
|
public static class UPnP
|
||||||
{
|
{
|
||||||
public static INatDevice NatDevice;
|
static INatDevice natDevice;
|
||||||
|
|
||||||
public static void TryNatDiscovery()
|
public static void TryNatDiscovery()
|
||||||
{
|
{
|
||||||
@@ -42,7 +42,7 @@ namespace OpenRA.Network
|
|||||||
Log.Write("server", "Stopping NAT discovery.");
|
Log.Write("server", "Stopping NAT discovery.");
|
||||||
NatUtility.StopDiscovery();
|
NatUtility.StopDiscovery();
|
||||||
|
|
||||||
if (NatDevice == null || NatDevice.GetType() != typeof(Mono.Nat.Upnp.UpnpNatDevice))
|
if (natDevice == null || natDevice.GetType() != typeof(Mono.Nat.Upnp.UpnpNatDevice))
|
||||||
{
|
{
|
||||||
Log.Write("server",
|
Log.Write("server",
|
||||||
"No NAT devices with UPnP enabled found within {0} ms deadline. Disabling automatic port forwarding.".F(Game.Settings.Server.NatDiscoveryTimeout));
|
"No NAT devices with UPnP enabled found within {0} ms deadline. Disabling automatic port forwarding.".F(Game.Settings.Server.NatDiscoveryTimeout));
|
||||||
@@ -60,11 +60,11 @@ namespace OpenRA.Network
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
NatDevice = args.Device;
|
natDevice = args.Device;
|
||||||
Log.Write("server", "Type: {0}", NatDevice.GetType());
|
Log.Write("server", "Type: {0}", natDevice.GetType());
|
||||||
Log.Write("server", "Your external IP is: {0}", NatDevice.GetExternalIP());
|
Log.Write("server", "Your external IP is: {0}", natDevice.GetExternalIP());
|
||||||
|
|
||||||
foreach (var mp in NatDevice.GetAllMappings())
|
foreach (var mp in natDevice.GetAllMappings())
|
||||||
Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}",
|
Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}",
|
||||||
mp.Protocol, mp.PublicPort, mp.PrivatePort);
|
mp.Protocol, mp.PublicPort, mp.PrivatePort);
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ namespace OpenRA.Network
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort, lifetime);
|
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort, lifetime);
|
||||||
NatDevice.CreatePortMap(mapping);
|
natDevice.CreatePortMap(mapping);
|
||||||
Log.Write("server", "Create port mapping: protocol = {0}, public = {1}, private = {2}, lifetime = {3} s",
|
Log.Write("server", "Create port mapping: protocol = {0}, public = {1}, private = {2}, lifetime = {3} s",
|
||||||
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Lifetime);
|
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Lifetime);
|
||||||
}
|
}
|
||||||
@@ -106,7 +106,7 @@ namespace OpenRA.Network
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort);
|
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort);
|
||||||
NatDevice.DeletePortMap(mapping);
|
natDevice.DeletePortMap(mapping);
|
||||||
Log.Write("server", "Remove port mapping: protocol = {0}, public = {1}, private = {2}, expiration = {3}",
|
Log.Write("server", "Remove port mapping: protocol = {0}, public = {1}, private = {2}, expiration = {3}",
|
||||||
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Expiration);
|
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Expiration);
|
||||||
}
|
}
|
||||||
@@ -116,5 +116,21 @@ namespace OpenRA.Network
|
|||||||
Game.Settings.Server.AllowPortForward = false;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -497,9 +497,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
public static string GetExternalIP(int clientIndex, OrderManager orderManager)
|
public static string GetExternalIP(int clientIndex, OrderManager orderManager)
|
||||||
{
|
{
|
||||||
var address = orderManager.LobbyInfo.ClientWithIndex(clientIndex).IpAddress;
|
var address = orderManager.LobbyInfo.ClientWithIndex(clientIndex).IpAddress;
|
||||||
if (clientIndex == orderManager.LocalClient.Index && address == IPAddress.Loopback.ToString() && UPnP.NatDevice != null)
|
if (clientIndex == orderManager.LocalClient.Index && address == IPAddress.Loopback.ToString())
|
||||||
{
|
{
|
||||||
var externalIP = UPnP.NatDevice.GetExternalIP();
|
var externalIP = UPnP.GetExternalIP();
|
||||||
if (externalIP != null)
|
if (externalIP != null)
|
||||||
address = externalIP.ToString();
|
address = externalIP.ToString();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user