lontivero / Open.NAT

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

Only one port open after Port renewel #82

Open JJakaJonas opened 6 years ago

JJakaJonas commented 6 years ago

Hello. I have tryed to build a console application to open specific ports on a router. I have at the moment port 3389 both as UPD and TCP.

At application start both ports opens as programmed. but after end lifetime and a renewel only one of the ports are open... This is my 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());
    }
}

} ` how do i fix this?

lontivero commented 6 years ago

Could you provide the steps to reproduce it? log files? error messages?

JJakaJonas commented 6 years ago

Made the code above in vs C#, ran it and checked the router. No error message .