コード例 #1
0
ファイル: MyNetManager.cs プロジェクト: Reaper9806/Battlegon
    //Port forwarding procedure
    void DeviceFound(object sender, DeviceEventArgs args)
    {
        Debug.Log("Hello");
        INatDevice device = args.Device;

        if (device.GetSpecificMapping(Protocol.Udp, this.networkPort).PublicPort == -1)
        {
            Debug.Log("Forwarding" + this.networkPort);
            device.CreatePortMap(new Mapping(Protocol.Udp, this.networkPort, this.networkPort));
        }
        else
        {
            Debug.Log("Map is " + device.GetSpecificMapping(Protocol.Udp, this.networkPort).PublicPort);
        }
    }
コード例 #2
0
        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
                });
            }
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
 public bool ExistingPortMap(Protocol protocol, int number)
 {
     if (!EnabledUpnp || natDevice == null)
     {
         return(false);
     }
     return(natDevice.GetSpecificMapping(protocol, number).PublicPort != -1);
 }
コード例 #6
0
ファイル: Main.cs プロジェクト: tralivali1234/Mono.Nat
        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);
            }
        }
コード例 #7
0
    //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));
        }
    }
コード例 #8
0
        private void DeviceFound(object sender, DeviceEventArgs args)
        {
            INatDevice device = args.Device;

            PublicIP.Invoke(new Action(() => PublicIP.Text = device.GetExternalIP().ToString()));

            if (isStarted == true)
            {
                if (portEnabled.Value == true)
                {
                    device.GetSpecificMapping(Protocol.Tcp, Convert.ToInt32(portTxt.Text));

                    device.CreatePortMap(new Mapping(Protocol.Tcp, Convert.ToInt32(portTxt.Text), Convert.ToInt32(portTxt.Text)));
                }
                else
                {
                    device.GetSpecificMapping(Protocol.Tcp, 10578);

                    device.CreatePortMap(new Mapping(Protocol.Tcp, 10578, 10578));
                }
            }
        }
コード例 #9
0
        private void DeviceFound(object sender, DeviceEventArgs args)
        {
            try {
                INatDevice device = args.Device;

                fLogger.WriteInfo("Device found, type: {0}", device.GetType().Name);
                if (device.GetType().Name == "PmpNatDevice")
                {
                    fLogger.WriteInfo("Device skipped");
                    return;
                }

                fLogger.WriteInfo("External IP: {0}", device.GetExternalIP());

                try {
                    Mapping m;

                    /*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(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(Protocol.Udp, DHTClient.PublicDHTPort);
                    if (m != null)
                    {
                        try {
                            device.DeletePortMap(m);
                        } catch {
                        }
                    }
                    m = new Mapping(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 (Exception ex) {
                    fLogger.WriteError("Couldn't create specific mapping", ex);
                }

                foreach (Mapping mp in device.GetAllMappings())
                {
                    fLogger.WriteInfo("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
                }

                fUPnPSem.Release();
            } catch (Exception ex) {
                fLogger.WriteError("NATMapper.DeviceFound()", ex);
            }
        }
コード例 #10
0
        public void DeviceFound(object sender, DeviceEventArgs args)
        {
            try
            {
                INatDevice device = args.Device;

                logger.Info("Trovato dispositivo con UPNP abilitato.");
                logger.Info("Tipo: {0}", device.GetType().Name);
                logger.Info("IP Esterno del dispositivo: {0}", device.GetExternalIP());

                Mapping mapTcp = new Mapping(Protocol.Tcp, Convert.ToInt32(tcpport), Convert.ToInt32(tcpport));
                logger.Info("Creazione del PortMapping sul dispositivo UPNP: Protocollo={0}, Porta Public={1}, private={2}", mapTcp.Protocol, mapTcp.PublicPort, mapTcp.PrivatePort);
                device.CreatePortMap(mapTcp);

                Mapping mapUdp = new Mapping(Protocol.Udp, Convert.ToInt32(udpport), Convert.ToInt32(udpport));
                logger.Info("Creazione del PortMapping sul dispositivo UPNP: Protocollo={0}, Porta Public={1}, private={2}", mapUdp.Protocol, mapUdp.PublicPort, mapUdp.PrivatePort);
                device.CreatePortMap(mapUdp);

                Mapping mapTcp2 = device.GetSpecificMapping(Protocol.Tcp, Convert.ToInt32(tcpport));
                PortMappingOkTcp = true;
                logger.Info("Verifica del PortMapping Protocollo={0}, Porta={1} passata con successo", mapTcp2.Protocol, mapTcp2.PublicPort);

                Mapping mapUdp2 = device.GetSpecificMapping(Protocol.Udp, Convert.ToInt32(udpport));
                PortMappingOkUdp = true;
                logger.Info("Verifica del PortMapping Protocollo={0}, Porta={1} passata con successo", mapUdp2.Protocol, mapUdp2.PublicPort);

                // Se il portfoward funziona interrompiamo il discovery
                // NOTA: rileviamo solo il primo router della lista
                NatUtility.StopDiscovery();
            }
            catch (Exception ex)
            {
                logger.Fatal("Procedura UPNP Fallita.");

                logger.Fatal(ex.Message);
                logger.Fatal(ex.StackTrace);
            }
        }
コード例 #11
0
    public static void StopUPnP(INatDevice device, Protocol protocol, int port)
    {
        if (GlobalVars.UserConfiguration.UPnP)
        {
            Mapping checker    = device.GetSpecificMapping(protocol, port);
            int     mapPublic  = checker.PublicPort;
            int     mapPrivate = checker.PrivatePort;

            if (mapPublic != -1 && mapPrivate != -1)
            {
                Mapping portmap = new Mapping(protocol, port, port);
                portmap.Description = "Novetus";
                device.DeletePortMap(portmap);
            }
        }
    }
コード例 #12
0
    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 Mapping(Protocol.Tcp, 1500, 3000));
        // Retrieve the details for the port map for external port 3000
        Mapping m = device.GetSpecificMapping(Protocol.Tcp, 3000);

        // 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();
    }
コード例 #13
0
ファイル: Main.cs プロジェクト: besser82/Mono.Nat
        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);
            }
        }
コード例 #14
0
    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.");
    }
コード例 #15
0
        public static bool isPortMappedOnRouter(INatDevice Router)
        {
            if (Registry.Has("Port") == false)
            {
                return(false);
            }

            int Port = Int32.Parse(Registry.Get("Port"));

            // Check with router to see if port is open
            // This allows the system to detect if port is still forwarded from a previous
            if (Router != null)
            {
                Mapping RoutedPort = Router.GetSpecificMapping(Protocol.Tcp, Port);

                return(RoutedPort.PrivatePort == 3389);
            }

            return(false);
        }
コード例 #16
0
        private static void InternalUnMapPort(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.PrivatePort > 0 && mapping.PublicPort > 0)
                    device.DeletePortMap(new Mapping(proto, port, port));
            }
        }
コード例 #17
0
        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));
            }
        }
コード例 #18
0
    void MapPort()
    {
        try
        {
            Debug.Log("Mapping port...");

            udpMapping = new Mapping(Protocol.Udp, Port, Port)
            {
                Description = "Diluvium (UDP)"
            };
            natDevice.BeginCreatePortMap(udpMapping, state =>
            {
                if (state.IsCompleted)
                {
                    Debug.Log("UDP Mapping complete! Testing...");
                    try
                    {
                        var m = natDevice.GetSpecificMapping(Protocol.Udp, Port);
                        if (m == null)
                        {
                            throw new InvalidOperationException("Mapping not found");
                        }
                        if (m.PrivatePort != Port || m.PublicPort != Port)
                        {
                            throw new InvalidOperationException("Mapping invalid");
                        }

                        Debug.Log("Success!");
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("Failed to validate UDP mapping :\n" + ex.ToString());
                    }
                }
            }, null);

            tcpMapping = new Mapping(Protocol.Tcp, Port, Port)
            {
                Description = "Diluvium (TCP)"
            };
            natDevice.BeginCreatePortMap(tcpMapping, state =>
            {
                if (state.IsCompleted)
                {
                    Debug.Log("TCP Mapping complete! Testing...");
                    try
                    {
                        var m = natDevice.GetSpecificMapping(Protocol.Tcp, Port);
                        if (m == null)
                        {
                            throw new InvalidOperationException("Mapping not found");
                        }
                        if (m.PrivatePort != Port || m.PublicPort != Port)
                        {
                            throw new InvalidOperationException("Mapping invalid");
                        }

                        Debug.Log("Success!");
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("Failed to validate TCP mapping :\n" + ex.ToString());
                    }
                }
            }, null);
        }
        catch (Exception ex)
        {
            Debug.Log("Failed to map port :\n" + ex.ToString());
        }
    }
コード例 #19
0
ファイル: Relay.cs プロジェクト: merveilles/WaitingForHorus
 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.");
 }