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

Only one port open after Port renewel #82

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

Only one port open after Port renewel #82

JJakaJonas opened this issue Jul 16, 2018 · 2 comments

Comments

@JJakaJonas
Copy link

JJakaJonas commented Jul 16, 2018

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
Copy link
Owner

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

@JJakaJonas
Copy link
Author

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

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

2 participants