private void AddMap(ref INatDevice device, Protocol ptype, int localport, int publicport, string description) { try { Mapping mapper = null; mapper = new Mapping(Protocol.Tcp, localport, publicport); mapper.Description = description; device.CreatePortMap(mapper); } catch (MappingException) { MessageBox.Show("Sorry, something went wrong.\n" + "Could not add the port due to an error.\n" + "This error may occur because the port is already taken\n" + "or the port lies within a not allowed range.\n\n" + "Try a different port, for example above 1024.", "Error..."); } catch (Exception e) { MessageBox.Show("An unknown error occurred. The content of the error is:\n\n" + e.ToString(), "Error..."); } }
public static void OpenPort(int port, string descript) { bool alredy_opened = false; if (UPNP == true) { for (int i = 0; i < opened_ports.Count(); i++) { if (port == opened_ports[i]) { alredy_opened = true; //MessageBox.Show("THIS PORT IS ALREDY OPENED"); } } if (alredy_opened == false) { Mapping TCP = new Mapping(Protocol.Tcp, port, port); Mapping UDP = new Mapping(Protocol.Udp, port, port); TCP.Description = descript + "TCP"; UDP.Description = descript + "UDP"; device.CreatePortMap(TCP); device.CreatePortMap(UDP); opened_ports.Add(port); } } }
private void DeviceFound(object sender, DeviceEventArgs args) { if (btnOpenWasClicked == true) { INatDevice device = args.Device; Mapping minecraftTCP = new Mapping(Protocol.Tcp, 25565, 25565); Mapping minecraftUDP = new Mapping(Protocol.Udp, 25565, 25565); minecraftTCP.Description = "MinecraftTCP"; minecraftUDP.Description = "MinecraftUDP"; device.CreatePortMap(minecraftTCP); device.CreatePortMap(minecraftUDP); foreach (Mapping portMap in device.GetAllMappings()) { Debug.Print(portMap.ToString()); } MessageBox.Show("Port 25565 has been opened."); MessageBoxResult diag = MessageBox.Show("This is the IP you will give to your friends: " + device.GetExternalIP().ToString() + ":25565" + " Do you wanna copy the IP? ", "Success", MessageBoxButton.YesNo, MessageBoxImage.Information); if (diag == MessageBoxResult.Yes) { Thread thread = new Thread(() => Clipboard.SetText(device.GetExternalIP() + ":25565")); thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA thread.Start(); thread.Join(); //Wait for the thread to end } } }
private void DeviceFound(object sender, DeviceEventArgs args) { Mapping minecraftTCP = new Mapping(Protocol.Tcp, 25565, 25565); Mapping minecraftUDP = new Mapping(Protocol.Udp, 25565, 25565); minecraftTCP.Description = "MinecraftTCP"; minecraftUDP.Description = "MinecraftUDP"; device = args.Device; try { device.DeletePortMap(minecraftTCP); device.DeletePortMap(minecraftUDP); } catch { } device.CreatePortMap(minecraftTCP); device.CreatePortMap(minecraftUDP); device.CreatePortMap(new Mapping(Protocol.Udp, 2, 2)); // on device found code //cport.Text = "okay"; }
private void DeviceFound(object sender, DeviceEventArgs args) { INatDevice device = args.Device; device.CreatePortMap(new Mapping(Protocol.Udp, MessagePort, MessagePort)); device.CreatePortMap(new Mapping(Protocol.Udp, CallPort, CallPort)); device.CreatePortMap(new Mapping(Protocol.Udp, CallReceivePort, CallReceivePort)); }
private 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("Service Type: {0}", (device as UpnpNatDevice).ServiceType); Console.WriteLine("IP: {0}", device.GetExternalIP()); device.CreatePortMap(new Mapping(Protocol.Tcp, 15001, 15001)); Console.WriteLine("---"); //return; /******************************************/ /* Advanced test suite. */ /******************************************/ // Try to create a new port map: var mapping = new Mapping(Protocol.Tcp, 6001, 6001); device.CreatePortMap(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 = device.GetSpecificMapping(Protocol.Tcp, 6001); 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 deleting the port we opened before: try { device.DeletePortMap(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: foreach (Mapping mp in device.GetAllMappings()) { Console.WriteLine("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); } Console.WriteLine("External IP: {0}", device.GetExternalIP()); Console.WriteLine("Done..."); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
// Open ports if UPNP device found private void DeviceFound(object sender, DeviceEventArgs args) { INatDevice device = args.Device; // Free TCP and UDP slots device.DeletePortMap(new Mapping(Protocol.Tcp, port, port)); device.DeletePortMap(new Mapping(Protocol.Udp, port, port)); // Open the slots device.CreatePortMap(new Mapping(Protocol.Tcp, port, port)); device.CreatePortMap(new Mapping(Protocol.Udp, port, port)); log.Print(level.System, "UPNP device found, mapping port " + port + "."); }
private static void DeviceFound(object sender, DeviceEventArgs args) { if (args.Device.GetSpecificMapping(Protocol.Tcp, 27015).PublicPort == -1) { Debug.Log("UPnP device found trying to automatically port forward! " + args.Device.GetExternalIP()); INatDevice device = args.Device; globalDevice.Add(args.Device.GetExternalIP(), device); device.CreatePortMap(new Mapping(Protocol.Tcp, 27015, 27015)); device.CreatePortMap(new Mapping(Protocol.Udp, 27015, 27015)); } else { Debug.Log("Device already port forwarded! " + args.Device.GetExternalIP()); } }
private void DeviceFound(object sender, DeviceEventArgs args) { INatDevice device = device = args.Device; device.CreatePortMap(new Mapping(Protocol.Tcp, Epicoin.getport, Epicoin.getport)); device.CreatePortMap(new Mapping(Protocol.Tcp, Epicoin.mineport, Epicoin.mineport)); device.CreatePortMap(new Mapping(Protocol.Tcp, Epicoin.peerport, Epicoin.peerport)); device.CreatePortMap(new Mapping(Protocol.Tcp, Epicoin.transport, Epicoin.transport)); foreach (Mapping portMap in device.GetAllMappings()) { Console.WriteLine(portMap.ToString()); } Console.WriteLine(device.GetExternalIP().ToString()); }
public static bool CreatePortMap(int port, out int externalPort) { if (!_discoveryComplete) { externalPort = -1; return(false); } try { Mapping mapping = new Mapping(Protocol.Tcp, port, port); for (int i = 0; i < 3; i++) { _device.CreatePortMap(mapping); } if (_mappings.ContainsKey(mapping.PrivatePort)) { _mappings[mapping.PrivatePort] = mapping; } else { _mappings.Add(mapping.PrivatePort, mapping); } externalPort = mapping.PublicPort; return(true); } catch (MappingException) { externalPort = -1; return(false); } }
private void CreatePortMap(INatDevice device, List <Mapping> currentMappings, int privatePort, int publicPort) { var hasMapping = false; if (currentMappings != null) { hasMapping = currentMappings.Any(i => i.PublicPort == publicPort && i.PrivatePort == privatePort); } else { try { var mapping = device.GetSpecificMapping(Protocol.Tcp, publicPort); hasMapping = mapping != null; } catch (NotSupportedException) { } } if (!hasMapping) { _logger.Debug("Creating port map on port {0}", privatePort); device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) { Description = _appHost.Name }); } }
/// <summary> /// Deal with a found NAT device. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NatUtility_DeviceFound(object sender, DeviceEventArgs e) { // This is the upnp enabled router INatDevice device = e.Device; if (upnpEnabled.Checked) { // Create a mapping to forward the external port device.CreatePortMap(new Mapping(Protocol.Tcp, Int32.Parse(txtHostPort.Text), Int32.Parse(txtHostPort.Text))); } else { //foreach (Mapping mp in device.GetAllMappings()) // device.DeletePortMap(mp); } /* * // Retrieve the details for the port map for external port 3000 * Mapping m = device.GetSpecificMapping(Protocol.Tcp, Int32.Parse(txtHostPort.Text)); * * // Get all the port mappings on the device and delete them * foreach (Mapping mp in device.GetAllMappings()) * device.DeletePortMap(mp); * * // Get the external IP address * IPAddress externalIP = device.GetExternalIP(); */ }
public bool ForwardPort(MappingProtocol protocol, int port) { List <Mapping> mappingsToForward = new List <Mapping>(2); if (protocol.HasFlag(MappingProtocol.Tcp)) { mappingsToForward.Add(new Mapping(Protocol.Tcp, port, port)); } if (protocol.HasFlag(MappingProtocol.Udp)) { mappingsToForward.Add(new Mapping(Protocol.Udp, port, port)); } try { foreach (Mapping mapping in mappingsToForward) { _natDevice.CreatePortMap(mapping); } return(true); } catch { return(false); } }
private void DeviceFound(object sender, DeviceEventArgs args) { try { INatDevice device = args.Device; //remove existing port maps that exist on the ports we want foreach (Mapping portMap in device.GetAllMappings()) { if (externalPorts.Contains(portMap.PublicPort)) { Console.WriteLine("Deleting " + portMap.PublicPort.ToString()); device.DeletePortMap(portMap); } } //add the ports we want for our IP for (int i = 0; i <= 2; i++) { Console.WriteLine("Creating IP: " + device.LocalAddress.ToString() + " port: " + i.ToString()); device.CreatePortMap(new Mapping(Protocol.Tcp, internalPorts[i], externalPorts[i])); } } catch (Exception e) { } }
void MapDevice() { if (natdevice == null) { return; } ExternalIP = natdevice.GetExternalIP(); try { var mapping = new Mapping(Protocol.Udp, Port, Port); natdevice.CreatePortMap(mapping); if (!mappedPort) { Invoke(delegate { var message = string.Format("Succesfully mapped port {0} from external IP address {1} to this machine", Port, ExternalIP); Client.OnMessage(new ClientMessageArgs(message, null)); }); mappedPort = true; } } catch (Exception ex) { Invoke(delegate { Client.OnMessage(new ClientMessageArgs(ex.Message, null)); }); } }
public void Map(Protocol protocol, int port) { if (router == null) { Start(); Thread.Sleep(5000); } if (router == null) { return; } try { router.CreatePortMap(new Mapping(protocol, port, port)); } catch (MappingException) { if (port != 11789) { MessageBox.Show(String.Format("Network autoconfiguration failed while trying to automatically forward port {0} ({1}). If you have manually forwarded this port, you can ignore this error.", port, (protocol == Protocol.Tcp ? "TCP" : "UDP"))); } } }
private void DeviceFound(object sender, DeviceEventArgs args) { try { INatDevice device = args.Device; logger.Fatal("UPNP Enabled Device found"); logger.Info("Type: {0}", device.GetType().Name); logger.Info("External IP: {0}", device.GetExternalIP()); Mapping mapping = new Mapping(Protocol.Udp, Convert.ToInt32(uxServerPortUdp.Text), Convert.ToInt32(uxServerPortUdp.Text)); device.CreatePortMap(mapping); logger.Info("Create Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); try { Mapping m = device.GetSpecificMapping(Protocol.Udp, Convert.ToInt32(uxServerPortUdp.Text)); logger.Info("Testing port Mapping passed: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); // Se il portfoward funziona interrompiamo il discovery // NOTA: rileviamo solo il primo router della lista NatUtility.StopDiscovery(); } catch { logger.Fatal("Could not get specific mapping"); } } catch (Exception ex) { logger.Fatal(ex.Message); logger.Fatal(ex.StackTrace); } }
private static void DeviceFound(object sender, DeviceEventArgs args) { try { INatDevice device = args.Device; fLogger.WriteInfo("Device found"); fLogger.WriteInfo("Type: {0}", device.GetType().Name); fLogger.WriteInfo("External IP: {0}", device.GetExternalIP()); try { Mapping m = device.GetSpecificMapping(Mono.Nat.Protocol.Tcp, ProtocolHelper.PublicTCPPort); if (m != null) { fLogger.WriteInfo("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); } else { m = new Mapping(Mono.Nat.Protocol.Tcp, ProtocolHelper.PublicTCPPort, ProtocolHelper.PublicTCPPort); device.CreatePortMap(m); fLogger.WriteInfo("Create Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); } m = device.GetSpecificMapping(Mono.Nat.Protocol.Udp, DHTClient.PublicDHTPort); if (m != null) { fLogger.WriteInfo("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); } else { m = new Mapping(Mono.Nat.Protocol.Udp, DHTClient.PublicDHTPort, DHTClient.PublicDHTPort); device.CreatePortMap(m); fLogger.WriteInfo("Create Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); } } catch { fLogger.WriteInfo("Couldnt get specific mapping"); } foreach (Mapping mp in device.GetAllMappings()) { fLogger.WriteInfo("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); } fLogger.WriteInfo("Done..."); } catch (Exception ex) { fLogger.WriteError("NATMapper.DeviceFound()", ex); } }
void DeviceFound(object sender, DeviceEventArgs args) { // This is the upnp enabled router INatDevice device = args.Device; // Create a mapping to forward external port 3000 to local port 1500 device.CreatePortMap(new Mono.Nat.Mapping(Mono.Nat.Protocol.Tcp, Properties.Settings.Default.listenPort, Properties.Settings.Default.listenPort)); }
private void Setup(INatDevice mainNatDevice) { _mainNatDevice = mainNatDevice; ExternalIp = mainNatDevice.GetExternalIP(); mainNatDevice.CreatePortMap(PortMap); }
public void CreatePortMap(Protocol protocol, int number) { if (!EnabledUpnp || natDevice == null) { return; } natDevice.CreatePortMap(new Mapping(protocol, number, number)); }
private void CreatePortMap(INatDevice device, int privatePort, int publicPort) { _logger.Debug("Creating port map on port {0}", privatePort); device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) { Description = _appHost.Name }); }
//Implements the port forwarding procedure void DeviceFound(object sender, DeviceEventArgs args) { INatDevice device = args.Device; if (device.GetSpecificMapping(Protocol.Udp, MasterServerPort).PublicPort == -1) { device.CreatePortMap(new Mapping(Protocol.Udp, MasterServerPort, MasterServerPort)); } }
private void CreatePortMap(INatDevice device, int privatePort, int publicPort) { _logger.Debug("Creating port map on port {0}", privatePort); device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) { Description = "Media Browser Server" }); }
private Task CreatePortMap(INatDevice device, int privatePort, int publicPort) { _logger.LogDebug("Creating port map on local port {0} to public port {1} with device {2}", privatePort, publicPort, device.LocalAddress.ToString()); return(device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) { Description = _appHost.Name })); }
private 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}", device.GetExternalIP()); device.CreatePortMap(new Mapping(Protocol.Tcp, 1500, 1500)); Console.WriteLine("---"); return; Mapping mapping = new Mapping(Protocol.Tcp, 6001, 6001); device.CreatePortMap(mapping); Console.WriteLine("Create Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); try { Mapping m = device.GetSpecificMapping(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 device.GetAllMappings()) { Console.WriteLine("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); device.DeletePortMap(mp); } Console.WriteLine("External IP: {0}", device.GetExternalIP()); Console.WriteLine("Done..."); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
/* * When device is found, forward the port set up in App.config. */ public static void OnDeviceFound(object sender, DeviceEventArgs args) { Console.WriteLine("Port Forwarding Device Found"); device = args.Device; device.CreatePortMap(new Mapping(Protocol.Tcp, PORT, PORT)); // Forward Port Clog("Port - " + PORT.ToString() + " - Forwarded Successfully"); foreach (Mapping portMap in device.GetAllMappings()) // Log all portmaps { Clog("Port Map - " + portMap.ToString()); } }
private static void AddPortMapping(INatDevice device, Mapping m) { Console.WriteLine("Ajout du port " + m.ToString()); if (GetMappingFromDevice(device, m) != null) { Console.WriteLine("Le port " + m.ToString() + "existe déjà"); } else { device.CreatePortMap(m); } }
private void DeviceFound(object sender, DeviceEventArgs args) { device = args.Device; // on device found code Console.WriteLine(device.GetExternalIP().ToString()); device.CreatePortMap(new Mapping(Protocol.Tcp, obj.Client_Port, obj.Client_Port, 100000)); device.CreatePortMap(new Mapping(Protocol.Udp, obj.Client_Port, obj.Client_Port, 100000)); obj.Public_IP = device.GetExternalIP(); Console.WriteLine(device.GetExternalIP().ToString()); foreach (Mapping portMap in device.GetAllMappings()) { Console.WriteLine(portMap.ToString()); } }
private void DeviceFound(object sender, DeviceEventArgs args) { INatDevice device = device = args.Device; device.CreatePortMap(new Mapping(Mono.Nat.Protocol.Tcp, this.port, this.port)); foreach (Mapping portMap in device.GetAllMappings()) { Console.WriteLine(portMap.ToString()); } Console.WriteLine(device.GetExternalIP().ToString()); }
/// <summary> /// Sets up the port forwarding in the background. /// </summary> private void SetupPortForwardingAsync() { NatUtility.DeviceFound += (sender, router) => { NatUtility.StopDiscovery(); _uPnpRouter = router.Device; Logger.InfoFormat("{0} is creating UPnP external port {1} forwarding on the discovered router.", DeviceDisplayNameInternal, _portNumber); _uPnpRouter.CreatePortMap(new Mapping(Protocol.Tcp, _portNumber, _portNumber)); }; NatUtility.StartDiscovery(); }
private void DeviceFound(object sender, DeviceEventArgs args) { logger.IfInfo("Device Found"); this.Status = NatStatus.DeviceFound; // This is the upnp enabled router this.Device = args.Device; // Create a mapping to forward external port to local port try { Device.CreatePortMap(new Mapping(Protocol.Tcp, Injection.Kernel.Get<IServerSettings>().Port, Injection.Kernel.Get<IServerSettings>().Port)); this.Status = NatStatus.PortForwardedSuccessfully; } catch (Exception e) { this.Status = NatStatus.PortForwardingFailed; logger.Error("Port mapping failed", e); } }
private void MapDevice(INatDevice device) { bool exists; try { exists = device.GetSpecificMapping(Protocol.Udp, Port).PublicPort != -1; } catch (MappingException) { exists = false; } if (exists) { Relay.Instance.MessageLog.AddMessage("Unable to create UPnP port map: port has already been mapped.\nIs a server already running on your network?"); return; } device.CreatePortMap(new Mapping(Protocol.Udp, Port, Port)); Relay.Instance.MessageLog.AddMessage("Created UPnP port mapping."); }
private void CreatePortMap(INatDevice device, int port) { _logger.Debug("Creating port map on port {0}", port); device.CreatePortMap(new Mapping(Protocol.Tcp, port, port) { Description = "Media Browser Server" }); }
private static void InternalMapPort(INatDevice device, int port) { for (int i = 0; i < 2; i++) { var proto = Protocol.Tcp; if (i > 0) proto = Protocol.Udp; Mapping mapping = device.GetSpecificMapping(proto, port); if (mapping == null || mapping.IsExpired() || mapping.PrivatePort < 0 || mapping.PublicPort < 0) device.CreatePortMap(new Mapping(proto, port, port)); } }