lontivero / Open.NAT

Lightweight and easy-to-use class library to allow port forwarding in NAT devices with UPNP and/or PMP
MIT License
420 stars 99 forks source link

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

Open JJakaJonas opened 6 years ago

JJakaJonas commented 6 years ago

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 commented 6 years ago

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

JJakaJonas commented 6 years ago

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

Wiilf commented 2 years ago

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.