Exemplo n.º 1
0
        public static IAsyncResult BeginGetAllMappings(this INatDevice device, AsyncCallback callback, object asyncState)
        {
            var result = new TaskAsyncResult(device.GetAllMappingsAsync(), callback, asyncState);

            result.Task.ContinueWith(t => result.Complete(), TaskScheduler.Default);
            return(result);
        }
Exemplo n.º 2
0
        private async void DeviceFound(object sender, DeviceEventArgs args)
        {
            try
            {
                INatDevice device = args.Device;

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Device found");
                Console.ResetColor();
                Console.WriteLine("Type: {0}", device.GetType().Name);

                Console.WriteLine("IP: {0}", await device.GetExternalIPAsync());
                await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 1500, 1500));

                Console.WriteLine("---");

                return;

                Mapping mapping = new Mapping(Protocol.Tcp, 6001, 6001);
                await device.CreatePortMapAsync(mapping);

                Console.WriteLine("Create Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);

                try
                {
                    Mapping m = await device.GetSpecificMappingAsync(Protocol.Tcp, 6001);

                    Console.WriteLine("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort);
                }
                catch
                {
                    Console.WriteLine("Couldnt get specific mapping");
                }
                foreach (Mapping mp in await device.GetAllMappingsAsync())
                {
                    Console.WriteLine("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
                    await device.DeletePortMapAsync(mp);
                }

                Console.WriteLine("External IP: {0}", await device.GetExternalIPAsync());
                Console.WriteLine("Done...");
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
Exemplo n.º 3
0
 public static Mapping [] GetAllMappings(this INatDevice device)
 {
     return(device.GetAllMappingsAsync().GetAwaiter().GetResult());
 }
Exemplo n.º 4
0
        private async void DeviceFound(object sender, DeviceEventArgs args)
        {
            await locker.WaitAsync();

            try {
                INatDevice device = args.Device;

                // Only interact with one device at a time. Some devices support both
                // upnp and nat-pmp.

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Device found: {0}", device.NatProtocol);
                Console.ResetColor();
                Console.WriteLine("Type: {0}", device.GetType().Name);

                Console.WriteLine("IP: {0}", await device.GetExternalIPAsync());

                Console.WriteLine("---");

                //return;

                /******************************************/
                /*         Advanced test suite.           */
                /******************************************/

                // Try to create a new port map:
                var mapping = new Mapping(Protocol.Tcp, 6001, 6011);
                await device.CreatePortMapAsync(mapping);

                Console.WriteLine("Create Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort,
                                  mapping.PrivatePort);

                // Try to retrieve confirmation on the port map we just created:
                try {
                    Mapping m = await device.GetSpecificMappingAsync(Protocol.Tcp, mapping.PublicPort);

                    Console.WriteLine("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort,
                                      m.PrivatePort);
                } catch {
                    Console.WriteLine("Couldn't get specific mapping");
                }

                // Try retrieving all port maps:
                try {
                    var mappings = await device.GetAllMappingsAsync();

                    if (mappings.Length == 0)
                    {
                        Console.WriteLine("No existing uPnP mappings found.");
                    }
                    foreach (Mapping mp in mappings)
                    {
                        Console.WriteLine("Existing Mappings: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
                    }
                } catch {
                    Console.WriteLine("Couldn't get all mappings");
                }

                // Try deleting the port we opened before:
                try {
                    await device.DeletePortMapAsync(mapping);

                    Console.WriteLine("Deleting Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
                } catch {
                    Console.WriteLine("Couldn't delete specific mapping");
                }

                // Try retrieving all port maps:
                try {
                    var mappings = await device.GetAllMappingsAsync();

                    if (mappings.Length == 0)
                    {
                        Console.WriteLine("No existing uPnP mappings found.");
                    }
                    foreach (Mapping mp in mappings)
                    {
                        Console.WriteLine("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
                    }
                } catch {
                    Console.WriteLine("Couldn't get all mappings");
                }

                Console.WriteLine("External IP: {0}", await device.GetExternalIPAsync());
                Console.WriteLine("Done...");
            } finally {
                locker.Release();
            }
        }