time limit the port forwarding if possible

This commit is contained in:
Matthias Mailänder
2014-06-16 16:42:06 +02:00
parent a8c029f130
commit 1f7b0ad5d4
2 changed files with 20 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -76,20 +76,29 @@ namespace OpenRA.Network
} }
} }
public static void ForwardPort() public static void ForwardPort(int lifetime)
{ {
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, lifetime);
NatDevice.CreatePortMap(mapping); NatDevice.CreatePortMap(mapping);
Log.Write("server", "Create port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); Log.Write("server", "Create port mapping: protocol = {0}, public = {1}, private = {2}, lifetime = {3} s",
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Lifetime);
} }
catch (Exception e) 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); Log.Write("server", "Can not forward ports via UPnP: {0}", e);
Game.Settings.Server.AllowPortForward = false; Game.Settings.Server.AllowPortForward = false;
} }
} }
}
public static void RemovePortforward() public static void RemovePortforward()
{ {
@@ -97,7 +106,8 @@ namespace OpenRA.Network
{ {
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}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); Log.Write("server", "Remove port mapping: protocol = {0}, public = {1}, private = {2}, expiration = {3}",
mapping.Protocol, mapping.PublicPort, mapping.PrivatePort, mapping.Expiration);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -110,7 +110,7 @@ namespace OpenRA.Server
randomSeed = (int)DateTime.Now.ToBinary(); randomSeed = (int)DateTime.Now.ToBinary();
if (Settings.AllowPortForward) if (Settings.AllowPortForward)
UPnP.ForwardPort(); UPnP.ForwardPort(3600);
foreach (var trait in modData.Manifest.ServerTraits) foreach (var trait in modData.Manifest.ServerTraits)
serverTraits.Add(modData.ObjectCreator.CreateObject<ServerTrait>(trait)); serverTraits.Add(modData.ObjectCreator.CreateObject<ServerTrait>(trait));