Changed UPnP library to Mono.Nat

This commit is contained in:
MaxXor 2015-07-15 14:55:44 +02:00
parent 48ed28c92f
commit 5031a50f07
4 changed files with 40 additions and 76 deletions

View File

@ -1,8 +1,6 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Threading;
using NATUPNPLib;
using Mono.Nat;
namespace xServer.Core.Helper
{
@ -10,85 +8,58 @@ internal static class UPnP
{
public static bool IsPortForwarded { get; private set; }
public static ushort Port { get; private set; }
private static readonly HashSet<INatDevice> Devices = new HashSet<INatDevice>();
public static void ForwardPort(ushort port)
{
Port = port;
NatUtility.DeviceFound += DeviceFound;
NatUtility.DeviceLost += DeviceLost;
NatUtility.StartDiscovery();
new Thread(() =>
{
string ipAddr = string.Empty;
int retry = 0;
do
while (Devices.Count == 0) // wait until first device found
{
try
{
TcpClient c = null;
EndPoint endPoint;
try
{
c = new TcpClient();
c.Connect("www.google.com", 80);
endPoint = c.Client.LocalEndPoint;
}
finally
{
// Placed in here to make sure that a failed TcpClient will never linger!
if (c != null)
{
c.Close();
}
}
if (endPoint != null)
{
ipAddr = endPoint.ToString();
int index = ipAddr.IndexOf(":", StringComparison.Ordinal);
ipAddr = ipAddr.Remove(index);
// We got through successfully and with an endpoint and a parsed IP address. We may exit the loop.
break;
}
else
{
retry++;
}
}
catch
{
retry++;
}
} while (retry < 5);
if (string.IsNullOrEmpty(ipAddr)) // If we can't successfully connect
return;
Thread.Sleep(1000);
}
try
{
IStaticPortMappingCollection portMap = new UPnPNAT().StaticPortMappingCollection;
if (portMap != null)
portMap.Add(port, "TCP", port, ipAddr, true, "xRAT 2.0 UPnP");
foreach (var device in Devices)
{
device.CreatePortMap(new Mapping(Protocol.Tcp, Port, Port));
}
IsPortForwarded = true;
}
catch
catch (MappingException)
{
IsPortForwarded = false;
}
}).Start();
}
private static void DeviceFound(object sender, DeviceEventArgs args)
{
Devices.Add(args.Device);
}
private static void DeviceLost(object sender, DeviceEventArgs args)
{
Devices.Remove(args.Device);
}
public static void RemovePort()
{
try
{
IStaticPortMappingCollection portMap = new UPnPNAT().StaticPortMappingCollection;
if (portMap != null)
portMap.Remove(Port, "TCP");
IsPortForwarded = false;
}
catch
foreach (var device in Devices)
{
if (device.GetSpecificMapping(Protocol.Tcp, Port).PublicPort > 0) // if port map exists
{
device.DeletePortMap(new Mapping(Protocol.Tcp, Port, Port));
}
}
IsPortForwarded = false;
NatUtility.StopDiscovery();
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.Windows.Forms;
using xServer.Core.Helper;
using xServer.Core.Misc;
using xServer.Core.Networking;
using xServer.Settings;
@ -47,8 +48,8 @@ private void btnListen_Click(object sender, EventArgs e)
{
try
{
if (chkUseUpnp.Checked && !Core.Helper.UPnP.IsPortForwarded)
Core.Helper.UPnP.ForwardPort(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture)));
if (chkUseUpnp.Checked && !UPnP.IsPortForwarded)
UPnP.ForwardPort(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture)));
if(chkNoIPIntegration.Checked)
NoIpUpdater.Start();
_listenServer.Listen(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture)));
@ -65,8 +66,8 @@ private void btnListen_Click(object sender, EventArgs e)
try
{
_listenServer.Disconnect();
if (Core.Helper.UPnP.IsPortForwarded)
Core.Helper.UPnP.RemovePort();
if (UPnP.IsPortForwarded)
UPnP.RemovePort();
}
finally
{

View File

@ -51,6 +51,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Nat">
<HintPath>lib\Mono.Nat.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
@ -436,17 +439,6 @@
<ItemGroup>
<Content Include="xRAT-64x64.ico" />
</ItemGroup>
<ItemGroup>
<COMReference Include="NATUPNPLib">
<Guid>{1C565858-F302-471E-B409-F180AA4ABEC6}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Binary file not shown.