Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Can't open uPnP ports on my router. #83

Open
JJakaJonas opened this issue Jul 16, 2018 · 3 comments
Open

Can't open uPnP ports on my router. #83

JJakaJonas opened this issue Jul 16, 2018 · 3 comments

Comments

@JJakaJonas
Copy link

JJakaJonas commented Jul 16, 2018

Hello.
I have an huawei HA35 router.
It's a hybrid thing. It have a DSL and a LTE connection that it bonding together to one connection.
But the thing is that this code
`
namespace Auto_open_UPnP
{
class Program
{
public static void Main(string[] args)
{
Open.Nat.NatDiscoverer.TraceSource.Switch.Level = SourceLevels.Verbose;
Open.Nat.NatDiscoverer.TraceSource.Listeners.Add(new ColorConsoleTraceListener());
Test().Wait();
var endPoint = new IPEndPoint(IPAddress.Any, 1602);
var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
socket.Bind(endPoint);
socket.Listen(4);

    Console.ReadKey();
    socket.Close();
    
}

private async static Task Test()
{

    var nat = new Open.Nat.NatDiscoverer();
    var cts = new CancellationTokenSource(5000);
    var device = await nat.DiscoverDeviceAsync(Open.Nat.PortMapper.Upnp, cts);
    
    foreach (var mapping in await device.GetAllMappingsAsync())
    {
        if (mapping.Description.Contains("RDP"))
        {
            Console.WriteLine("Deleting {0}",  mapping);
            await device.DeletePortMapAsync(mapping);
            Console.WriteLine("Port mapping Deleted");
        }
        if (mapping.Description.Contains("uTorrent"))
        {
            Console.WriteLine("Deleting {0}", mapping);
            await device.DeletePortMapAsync(mapping);
            Console.WriteLine("Port mapping Deleted");
        }
    }
    
    var sb = new StringBuilder();
    var ip = await device.GetExternalIPAsync();

    sb.AppendFormat("\nYour IP: {0}", ip);
    await device.CreatePortMapAsync(new Open.Nat.Mapping(Open.Nat.Protocol.Udp, 3389, 3389, "RDP UDP"));
    await device.CreatePortMapAsync(new Open.Nat.Mapping(Open.Nat.Protocol.Tcp, 3389, 3389, "RDP TCP"));
    sb.AppendFormat("\n+------+----------------------+-------------------------+-------------+--------+");
    sb.AppendFormat("\n| PROT | PUBLIC (Reacheable)  | PRIVATE (Your computer) | Descriptopn |        |");
    sb.AppendFormat("\n+------+-------------+--------+-------------------------+------+------+--------+");
    sb.AppendFormat("\n|      | IP Address  |  Port  |  IP Address    | Port   |      | Expires       |");
    sb.AppendFormat("\n+------+-------------+--------+----------------+--------+------+---------------+");
    foreach (var mapping in await device.GetAllMappingsAsync())
    {
        sb.AppendFormat("\n| {5}  |{0}| {1} | {2} |{3} | {4} |{6}|",
            ip, mapping.PublicPort, mapping.PrivateIP, mapping.PrivatePort, mapping.Description, mapping.Protocol == Open.Nat.Protocol.Udp ? "TCP" : "UDP", mapping.Expiration.ToLocalTime());

    }
    sb.AppendFormat("\n+------+-------------+--------+----------------+--------+------+---------------+");
    sb.AppendFormat("\n[Done]");


    Console.WriteLine(sb.ToString());
}

}

}
`
The application breaks already at "Test().Wait();" but if i use my regular xDSL router it works.
I don't know what's the reson for the break but if you need more info to help me fix this just tell me what and how and then i will do my best to give you the info.

@lontivero
Copy link
Owner

This issue doesn't provide any kind of information. I need tt least the log file.

@JJakaJonas
Copy link
Author

Was not able to make one and I do not have the application any more.

@Wiilf
Copy link

Wiilf commented Sep 18, 2022

I'm not seeing what the issue is here, but in my case I added a Thread.Sleep(2000) call on Form_Closing, which properly waits for the entry to delete from router table and close application. If you don't wait on it, it won't have time to remove ports (in my case, on a brand new Aeris DOCSIS 3.1 modem).

Just some insight.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants