You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-17 07:36:32 +09:00
Merge pull request #108 from SimonHartfield/Feature/separate-portforwarding-internal-external-ports
Allow different internal and external ports on port forwarding
This commit is contained in:
@@ -160,77 +160,84 @@ namespace Lidgren.Network
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <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>
|
||||||
if (!CheckAvailability())
|
/// <param name="internalPort">The port on the client machine to send traffic to</param>
|
||||||
return false;
|
public bool ForwardPort(int externalPort, string description, int internalPort = 0)
|
||||||
|
{
|
||||||
|
if (!CheckAvailability())
|
||||||
|
return false;
|
||||||
|
|
||||||
IPAddress mask;
|
IPAddress mask;
|
||||||
var client = NetUtility.GetMyAddress(out mask);
|
var client = NetUtility.GetMyAddress(out mask);
|
||||||
if (client == null)
|
if (client == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
try
|
if (internalPort == 0)
|
||||||
{
|
internalPort = externalPort;
|
||||||
SOAPRequest(m_serviceUrl,
|
|
||||||
"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" +
|
|
||||||
"<NewRemoteHost></NewRemoteHost>" +
|
|
||||||
"<NewExternalPort>" + port.ToString() + "</NewExternalPort>" +
|
|
||||||
"<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
|
|
||||||
"<NewInternalPort>" + port.ToString() + "</NewInternalPort>" +
|
|
||||||
"<NewInternalClient>" + client.ToString() + "</NewInternalClient>" +
|
|
||||||
"<NewEnabled>1</NewEnabled>" +
|
|
||||||
"<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
|
|
||||||
"<NewLeaseDuration>0</NewLeaseDuration>" +
|
|
||||||
"</u:AddPortMapping>",
|
|
||||||
"AddPortMapping");
|
|
||||||
|
|
||||||
m_peer.LogDebug("Sent UPnP port forward request");
|
try
|
||||||
NetUtility.Sleep(50);
|
{
|
||||||
}
|
SOAPRequest(m_serviceUrl,
|
||||||
catch (Exception ex)
|
"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" +
|
||||||
{
|
"<NewRemoteHost></NewRemoteHost>" +
|
||||||
m_peer.LogWarning("UPnP port forward failed: " + ex.Message);
|
"<NewExternalPort>" + externalPort.ToString() + "</NewExternalPort>" +
|
||||||
return false;
|
"<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
|
||||||
}
|
"<NewInternalPort>" + internalPort.ToString() + "</NewInternalPort>" +
|
||||||
return true;
|
"<NewInternalClient>" + client.ToString() + "</NewInternalClient>" +
|
||||||
}
|
"<NewEnabled>1</NewEnabled>" +
|
||||||
|
"<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
|
||||||
|
"<NewLeaseDuration>0</NewLeaseDuration>" +
|
||||||
|
"</u:AddPortMapping>",
|
||||||
|
"AddPortMapping");
|
||||||
|
|
||||||
/// <summary>
|
m_peer.LogDebug("Sent UPnP port forward request");
|
||||||
/// Delete a forwarding rule from the router using UPnP
|
NetUtility.Sleep(50);
|
||||||
/// </summary>
|
}
|
||||||
public bool DeleteForwardingRule(int port)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
if (!CheckAvailability())
|
m_peer.LogWarning("UPnP port forward failed: " + ex.Message);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
/// <summary>
|
||||||
{
|
/// Delete a forwarding rule from the router using UPnP
|
||||||
SOAPRequest(m_serviceUrl,
|
/// </summary>
|
||||||
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" +
|
/// <param name="externalPort">The external, 'internet facing', port</param>
|
||||||
"<NewRemoteHost>" +
|
public bool DeleteForwardingRule(int externalPort)
|
||||||
"</NewRemoteHost>" +
|
{
|
||||||
"<NewExternalPort>" + port + "</NewExternalPort>" +
|
if (!CheckAvailability())
|
||||||
"<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
|
return false;
|
||||||
"</u:DeletePortMapping>", "DeletePortMapping");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
m_peer.LogWarning("UPnP delete forwarding rule failed: " + ex.Message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
try
|
||||||
/// Retrieve the extern ip using UPnP
|
{
|
||||||
/// </summary>
|
SOAPRequest(m_serviceUrl,
|
||||||
public IPAddress GetExternalIP()
|
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + m_serviceName + ":1\">" +
|
||||||
|
"<NewRemoteHost>" +
|
||||||
|
"</NewRemoteHost>" +
|
||||||
|
"<NewExternalPort>" + externalPort + "</NewExternalPort>" +
|
||||||
|
"<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
|
||||||
|
"</u:DeletePortMapping>", "DeletePortMapping");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
m_peer.LogWarning("UPnP delete forwarding rule failed: " + ex.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve the extern ip using UPnP
|
||||||
|
/// </summary>
|
||||||
|
public IPAddress GetExternalIP()
|
||||||
{
|
{
|
||||||
if (!CheckAvailability())
|
if (!CheckAvailability())
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user