1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-18 16:16:35 +09:00

Allow separate internal and external ports on port forwarding

Rename property on delete forwarding function
This commit is contained in:
Simon Hartfield
2018-10-04 21:08:08 +01:00
parent d0931dcf9f
commit 0e9da42ce0

View File

@@ -165,7 +165,10 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Add a forwarding rule to the router using UPnP /// Add a forwarding rule to the router using UPnP
/// </summary> /// </summary>
public bool ForwardPort(int port, string description) /// <param name="externalPort">The external, WAN facing, port</param>
/// <param name="description">A description for the port forwarding rule</param>
/// <param name="internalPort">The port on the client machine to send traffic to</param>
public bool ForwardPort(int externalPort, string description, int internalPort = 0)
{ {
if (!CheckAvailability()) if (!CheckAvailability())
return false; return false;
@@ -175,14 +178,17 @@ namespace Lidgren.Network
if (client == null) if (client == null)
return false; return false;
if (internalPort == 0)
internalPort = externalPort;
try try
{ {
SOAPRequest(m_serviceUrl, SOAPRequest(m_serviceUrl,
"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" + "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" +
"<NewRemoteHost></NewRemoteHost>" + "<NewRemoteHost></NewRemoteHost>" +
"<NewExternalPort>" + port.ToString() + "</NewExternalPort>" + "<NewExternalPort>" + externalPort.ToString() + "</NewExternalPort>" +
"<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" + "<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
"<NewInternalPort>" + port.ToString() + "</NewInternalPort>" + "<NewInternalPort>" + internalPort.ToString() + "</NewInternalPort>" +
"<NewInternalClient>" + client.ToString() + "</NewInternalClient>" + "<NewInternalClient>" + client.ToString() + "</NewInternalClient>" +
"<NewEnabled>1</NewEnabled>" + "<NewEnabled>1</NewEnabled>" +
"<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" + "<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
@@ -204,7 +210,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Delete a forwarding rule from the router using UPnP /// Delete a forwarding rule from the router using UPnP
/// </summary> /// </summary>
public bool DeleteForwardingRule(int port) /// <param name="externalPort">The external, 'internet facing', port</param>
public bool DeleteForwardingRule(int externalPort)
{ {
if (!CheckAvailability()) if (!CheckAvailability())
return false; return false;
@@ -215,7 +222,7 @@ namespace Lidgren.Network
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" + "<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" +
"<NewRemoteHost>" + "<NewRemoteHost>" +
"</NewRemoteHost>" + "</NewRemoteHost>" +
"<NewExternalPort>" + port + "</NewExternalPort>" + "<NewExternalPort>" + externalPort + "</NewExternalPort>" +
"<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" + "<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
"</u:DeletePortMapping>", "DeletePortMapping"); "</u:DeletePortMapping>", "DeletePortMapping");
return true; return true;