public static IAsyncResult BeginGetExternalIP(this INatDevice device, AsyncCallback callback, object asyncState) { var result = new TaskAsyncResult(device.GetExternalIPAsync(), callback, asyncState); result.Task.ContinueWith(t => result.Complete(), TaskScheduler.Default); return(result); }
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); } }
private async Task SetNatDevice(INatDevice device) { if (natDevice == device) { return; } // Remove mappings from old device before switching. if (natDevice != null) { await RemoveMappingsWithUI(); } bool hasDevice = device != null; if (hasDevice) { targetGatewayInfo.Text = device.DeviceEndpoint.ToString(); IPAddress externalIp = null; try { externalIp = await device.GetExternalIPAsync(); } catch (Exception e) { Debug.WriteLine("Failed to get external ip: {0}", e); } externalIpInfo.Text = externalIp != null?externalIp.ToString() : "Unknown"; } else { targetGatewayInfo.Text = ""; externalIpInfo.Text = "Unknown"; } natDevice = device; settingsGroup.Enabled = hasDevice; createMappingButton.Enabled = hasDevice; removeMappingButton.Enabled = false; }
public static IPAddress GetExternalIP(this INatDevice device) { return(device.GetExternalIPAsync().GetAwaiter().GetResult()); }
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(); } }