Removed UPnP port mapping.

This commit is contained in:
Albin Corén 2018-01-08 09:16:49 +01:00
parent 9389c1c9c4
commit 3dfc999c38
6 changed files with 2 additions and 119 deletions

View File

@ -27,8 +27,6 @@ namespace MLAPI
//Should only be used for dedicated servers and will require the servers RSA keypair being hard coded into clients in order to exchange a AES key //Should only be used for dedicated servers and will require the servers RSA keypair being hard coded into clients in order to exchange a AES key
//TODO //TODO
public bool EncryptMessages = false; public bool EncryptMessages = false;
public bool UseUPnP = false;
public Action<bool, IPAddress> UPnPCompleteCallback = null;
//Cached config hash //Cached config hash
private byte[] ConfigHash = null; private byte[] ConfigHash = null;

View File

@ -1,87 +0,0 @@
using Open.Nat;
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace MLAPI.Helper
{
public static class UPnPHelper
{
//Needs to be threaded, Currently freezes the game for up to 10 sec
internal static void AttemptPortMap(int port, Action<bool, IPAddress> callback)
{
bool invoked = false;
NatDiscoverer nat = new NatDiscoverer();
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(10000);
NatDevice device = null;
StringBuilder sb = new StringBuilder();
Task<NatDevice> natTask = nat.DiscoverDeviceAsync(PortMapper.Upnp, cts);
Mapping tcpMapping = new Mapping(Protocol.Tcp, port, port, 0, Application.productName + " (TCP)");
Mapping udpMapping = new Mapping(Protocol.Udp, port, port, 0, Application.productName + " (UDP)");
IPAddress publicIPAddress = null;
natTask.ContinueWith(tt =>
{
device = tt.Result;
device.GetExternalIPAsync()
.ContinueWith(task =>
{
publicIPAddress = task.Result;
return device.CreatePortMapAsync(udpMapping);
})
.Unwrap()
.ContinueWith(task =>
{
return device.CreatePortMapAsync(udpMapping);
})
.Unwrap()
.ContinueWith(task =>
{
return device.GetAllMappingsAsync();
})
.Unwrap()
.ContinueWith(task =>
{
Mapping[] mappings = task.Result.ToArray();
if(mappings.Length == 0)
{
if (!invoked && callback != null)
callback(false, publicIPAddress);
invoked = true;
}
else
{
for (int i = 0; i < mappings.Length; i++)
{
if(mappings[i].PrivatePort == port)
{
if (!invoked && callback != null)
callback(true, publicIPAddress);
invoked = true;
}
}
}
});
}, TaskContinuationOptions.OnlyOnRanToCompletion);
try
{
natTask.Wait();
}
catch (AggregateException e)
{
if (e.InnerException is NatDeviceNotFoundException)
{
if (!invoked && callback != null)
callback(false, publicIPAddress);
invoked = true;
}
}
}
}
}

View File

@ -34,14 +34,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Open.Nat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f22a6a4582336c76, processorArchitecture=MSIL">
<HintPath>..\packages\Open.NAT.2.1.0.0\lib\net35\Open.Nat.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Open.NAT.2.1.0.0\lib\net35\System.Threading.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
@ -52,7 +46,6 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Helper\UPnPHelper.cs" />
<Compile Include="MonoBehaviours\Core\NetworkedBehaviour.cs" /> <Compile Include="MonoBehaviours\Core\NetworkedBehaviour.cs" />
<Compile Include="Data\NetworkedClient.cs" /> <Compile Include="Data\NetworkedClient.cs" />
<Compile Include="MonoBehaviours\Core\NetworkedObject.cs" /> <Compile Include="MonoBehaviours\Core\NetworkedObject.cs" />
@ -62,8 +55,5 @@
<Compile Include="NetworkingManagerComponents\SpawnManager.cs" /> <Compile Include="NetworkingManagerComponents\SpawnManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -1,5 +1,4 @@
using MLAPI.Helper; using MLAPI.NetworkingManagerComponents;
using MLAPI.NetworkingManagerComponents;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -124,10 +123,6 @@ namespace MLAPI
Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout"); Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout");
} }
} }
if (NetworkConfig.UseUPnP)
{
UPnPHelper.AttemptPortMap(NetworkConfig.Port, NetworkConfig.UPnPCompleteCallback);
}
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port); hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
isServer = true; isServer = true;
@ -192,10 +187,6 @@ namespace MLAPI
Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout"); Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout");
} }
} }
if(NetworkConfig.UseUPnP)
{
UPnPHelper.AttemptPortMap(NetworkConfig.Port, NetworkConfig.UPnPCompleteCallback);
}
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null); hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
isServer = true; isServer = true;

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Open.NAT" version="2.1.0.0" targetFramework="net35" />
</packages>

View File

@ -18,7 +18,6 @@ It's licenced under the MIT licence :D
## Done features ## Done features
* Host support (Client hosts the server) (done) * Host support (Client hosts the server) (done)
* Port forwarding using Open.NAT using the UPnP protcol (done)
* Object and player spawning (done) * Object and player spawning (done)
* Connection approval (done) * Connection approval (done)
* Message names (done) * Message names (done)
@ -33,8 +32,4 @@ That's all I can think of right now. But there is more to come, especially if pe
## Indepth ## Indepth
The project is not yet very tested. Examples will be created when it's more functional. The project is not yet very tested. Examples will be created when it's more functional.
## Dependencies
Open.NAT - https://github.com/lontivero/Open.NAT (MIT)