Пример #1
1
        /// <summary>
        /// Opens a port with the UPnP protocol.
        /// </summary>
        /// <param name="PortNumber">The number of the port to add.</param>
        /// <param name="protocol">The protocol used for the port.</param>
        public static void OpenPort(int PortNumber, Protocol protocol)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                mappings.Add(PortNumber, protocol.ToString(), PortNumber, Global.GetMyLocalIP(), true, "iKiwi");

                Utilities.Log.Write("Opened the port " + PortNumber + " with the UPnP protocol", Utilities.Log.LogCategory.Info);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets info about a UPnP port.
        /// </summary>
        /// <param name="PortNumber">The number of the port to get info.</param>
        /// <returns>The info about the UPnP port. Return null if is the UPnP port is not exist.</returns>
        public static UpnpPort GetInfoUpnpPort(int PortNumber)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                foreach (NATUPNPLib.IStaticPortMapping portMapping in mappings)
                {
                    if (portMapping.ExternalPort == PortNumber)
                    {
                        UpnpPort port = new UpnpPort();

                        port.Description = portMapping.Description;
                        port.Enabled     = portMapping.Enabled;
                        port.InternalIP  = portMapping.InternalClient;
                        port.ExternalIP  = portMapping.ExternalIPAddress;
                        port.Port        = portMapping.ExternalPort;
                        port.Protocol    = (Protocol)Enum.Parse(typeof(Protocol), portMapping.Protocol, true);

                        return(port);
                    }
                }
            }

            return(null);
        }
Пример #3
0
        private bool checkOpen()
        {
            UDPopen = false;
            TCPopen = false;

            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings == null)
            {
                Status = "UPnP is disabled in your router. Try enabling it and hit refresh.";
            }
            else
            {
                foreach (NATUPNPLib.IStaticPortMapping mapping in mappings)
                {
                    if (mapping.InternalClient == ip && mapping.InternalPort == 12345)
                    {
                        switch (mapping.Protocol.ToUpper())
                        {
                        case "UDP":
                            UDPopen = true;
                            break;

                        case "TCP":
                            TCPopen = true;
                            break;
                        }
                    }
                }
            }

            return(TCPopen && UDPopen);
        }
Пример #4
0
        /// <summary>
        /// Gets info about a UPnP port.
        /// </summary>
        /// <param name="PortNumber">The number of the port to get info.</param>
        /// <returns>The info about the UPnP port. Return null if is the UPnP port is not exist.</returns>
        public static UpnpPort GetInfoUpnpPort(int PortNumber)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                foreach(NATUPNPLib.IStaticPortMapping portMapping in mappings)
                {
                    if (portMapping.ExternalPort == PortNumber)
                    {
                        UpnpPort port = new UpnpPort();

                        port.Description = portMapping.Description;
                        port.Enabled = portMapping.Enabled;
                        port.InternalIP = portMapping.InternalClient;
                        port.ExternalIP = portMapping.ExternalIPAddress;
                        port.Port = portMapping.ExternalPort;
                        port.Protocol = (Protocol)Enum.Parse(typeof(Protocol), portMapping.Protocol, true);

                        return port;
                    }
                }
            }

            return null;
        }
Пример #5
0
        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (!(TCPopen && UDPopen))
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

                if (mappings == null)
                {
                    Status = "UPnP is disabled in your router. Try enabling it and hit refresh.";
                }
                else
                {
                    try
                    {
                        mappings.Add(ServerService.Helper.Settings.Instance.Port, "UDP", ServerService.Helper.Settings.Instance.Port, ip, true, "CubeWorld UDP");
                        mappings.Add(ServerService.Helper.Settings.Instance.Port, "TCP", ServerService.Helper.Settings.Instance.Port, ip, true, "CubeWorld TCP");

                        RefreshButton_Click(null, null);
                    }
                    catch
                    {
                        Status = "Could not open ports. Is UPnP enabled?";
                    }
                }
            }
            else
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

                if (mappings == null)
                {
                    Status = "UPnP is disabled in your router. Try enabling it and hit refresh.";
                }
                else
                {
                    try
                    {
                        if (UDPopen)
                        {
                            mappings.Remove(ServerService.Helper.Settings.Instance.Port, "UDP");
                        }

                        if (TCPopen)
                        {
                            mappings.Remove(ServerService.Helper.Settings.Instance.Port, "TCP");
                        }

                        RefreshButton_Click(null, null);
                    }
                    catch
                    {
                        Status = "Could not close ports. Is UPnP enabled?";
                    }
                }
            }
        }
Пример #6
0
        public Client(Form1 form, string serverIP, DisplayMessageTx displayDelegate, EndTransferToServer endDelegate,
                      EndConnectToServer connectDelegate, SetServerIPAddress serverIPDelegate, bool bDiscover)
        {
            _parentForm          = form;
            _displayDelegate     = displayDelegate;
            _endTransferDelegate = endDelegate;
            _connectDelegate     = connectDelegate;
            _setServerIP         = serverIPDelegate;

            _serverIPAddress = serverIP;

            // Get directory of job
            _directoryWork = Directory.GetCurrentDirectory();

            // Create thread
            _threadClient      = new Thread(new ThreadStart(ClientThreadFunction));
            _threadClient.Name = "Client Thread";
            _threadClient.Start();

            // Create thread that listen for server remote commands
            _threadListenFromServer      = new Thread(new ThreadStart(ClientListenServerThread));
            _threadListenFromServer.Name = "Client Listen from Server";
            _threadListenFromServer.Start();

            try
            {
                // How enable port forwarding
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                _mappingsPort = upnpnat.StaticPortMappingCollection;

                // Open a UDP Port to forward to a specific Computer on the Private Network
                _mappingsPort.Add(_numberOfPortToListen, "TCP", _numberOfPortToListen, GetLocalIPAddress(), true, "Client TCP");
            }
            catch (Exception excp)
            {
                MessageBox.Show("Impossible discover server because port " + _numberOfPortToListen + " is already occupied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Send information to form
                _parentForm.Invoke(this._connectDelegate, (object)false);
                return;
            }

            // Get client public ip
            _clientPublicAddress = GetPublicIPAddress();

            if (bDiscover == true)
            {
            }
            else
            {
                // Start initialization
                _eventHandle[0].Set();
            }
        }
Пример #7
0
        /// <summary>
        /// Closes a port with the UPnP protocol.
        /// </summary>
        /// <param name="PortNumber">The number of the port to close.</param>
        /// <param name="protocol">The protocol used for the port.</param>
        public static void ClosePort(int PortNumber, Protocol protocol)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                mappings.Remove(PortNumber, protocol.ToString());

                Utilities.Log.Write("Closed the port " + PortNumber + " with the UPnP protocol", Utilities.Log.LogCategory.Info);
            }
        }
Пример #8
0
        /// <summary>
        /// Closes a port with the UPnP protocol.
        /// </summary>
        /// <param name="PortNumber">The number of the port to close.</param>
        /// <param name="protocol">The protocol used for the port.</param>
        public static void ClosePort(int PortNumber, Protocol protocol)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                mappings.Remove(PortNumber, protocol.ToString());

                Utilities.Log.Write("Closed the port " + PortNumber + " with the UPnP protocol", Utilities.Log.LogCategory.Info);
            }
        }
Пример #9
0
        /// <summary>
        /// Opens a port with the UPnP protocol.
        /// </summary>
        /// <param name="PortNumber">The number of the port to add.</param>
        /// <param name="protocol">The protocol used for the port.</param>
        public static void OpenPort(int PortNumber, Protocol protocol)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                mappings.Add(PortNumber, protocol.ToString(), PortNumber, Global.GetMyLocalIP(), true, "iKiwi");

                Utilities.Log.Write("Opened the port " + PortNumber + " with the UPnP protocol", Utilities.Log.LogCategory.Info);
            }
        }
Пример #10
0
        /// <summary>
        /// Use NATUPNPLib to try and open a port forward
        /// </summary>
        /// <param name="intPortNumber">The port to open</param>
        /// <param name="strFriendlyName">Friendly name for the service</param>
        /// <returns>Boolean indicating whether the forward worked</returns>
        public static bool OpenUPnP(int intPortNumber, string strFriendlyName)
        {
            try {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                upnpnat.StaticPortMappingCollection.Add(intPortNumber, "TCP", intPortNumber, GetListenIP().ToString(), true, "[YAMS] " + strFriendlyName);

                Database.AddLog("Forwarded port " + intPortNumber + " for " + strFriendlyName, "networking");
                return(true);
            }
            catch (Exception e) {
                Database.AddLog("Unable foward port " + intPortNumber + " for " + strFriendlyName + ": Exception - " + e.Message, "networking", "warn");
                return(false);
            }
        }
Пример #11
0
        // Constructor
        public Server(ServerForm form, StartStopServer del, ServerMessageReceived action,
                      ServerMessageResponse actionResponse, AddClientsToServer addClient,
                      RemoveClientsToServer removeClient, SetServerIPAddress serverIPDelegate)
        {
            // Create thread
            _serverThread      = new Thread(new ThreadStart(ServerMainThreadFunction));
            _serverThread.Name = "Server Main Thread";
            _serverThread.Start();

            // Create thread for UDP connection
            _threadSendToClient      = new Thread(new ThreadStart(ThreadReceiveResponse));
            _threadSendToClient.Name = "Server Thread Listener";
            _threadSendToClient.Start();

            _parentForm            = form;
            _endActionDelegate     = del;
            _messageReceivedAction = action;
            _messageResponseAction = actionResponse;
            _addClientDelegate     = addClient;
            _removeClientDelegate  = removeClient;
            _displayServerIP       = serverIPDelegate;

            // Get directory of job
            _directoryWork = Directory.GetCurrentDirectory();

            // Get local IP address
            _serverIPAddress = GetPublicIPAddress();

            _parentForm.Invoke(_displayServerIP, (object)_serverIPAddress);

            // How enable port forwarding
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
            _mappingsPort = upnpnat.StaticPortMappingCollection;

            GetLocalIPAddress();

            try
            {
                _mappingsPort.Add(_numberOfPortToListen, "TCP", _numberOfPortToListen, GetLocalIPAddress(), true, "CNC TCP Server");
            }
            catch (Exception excp)
            {
                MessageBox.Show(excp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Send action
                _parentForm.Invoke(this._endActionDelegate, (object)false);
                return;
            }
        }
Пример #12
0
        /// <summary>
        /// Use NATUPNPLib to try and close a previously set port forward
        /// </summary>
        /// <param name="intPortNumber">The port to open</param>
        /// <returns>Boolean indicating whether the forward worked</returns>
        public static bool CloseUPnP(int intPortNumber)
        {
            try
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                upnpnat.StaticPortMappingCollection.Remove(intPortNumber, "TCP");

                Database.AddLog("Un-forwarded port " + intPortNumber, "networking");
                return true;
            }
            catch (Exception e)
            {
                Database.AddLog("Unable to un-forward port " + intPortNumber + ": Exception - " + e.Message, "networking", "warn");
                return false;
            }
        }
Пример #13
0
        /// <summary>
        /// Use NATUPNPLib to try and close a previously set port forward
        /// </summary>
        /// <param name="intPortNumber">The port to open</param>
        /// <returns>Boolean indicating whether the forward worked</returns>
        public static bool CloseUPnP(int intPortNumber)
        {
            try
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                upnpnat.StaticPortMappingCollection.Remove(intPortNumber, "TCP");

                Database.AddLog("Un-forwarded port " + intPortNumber, "networking");
                return(true);
            }
            catch (Exception e)
            {
                Database.AddLog("Unable to un-forward port " + intPortNumber + ": Exception - " + e.Message, "networking", "warn");
                return(false);
            }
        }
Пример #14
0
        private void Main_Load(object sender, EventArgs e)
        {
            try
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

                SendMessage(txtip.Handle, 0x1501, 1, "192.168.1.1");
                SendMessage(txtport.Handle, 0x1501, 1, "Örn:4444");
                SendMessage(txtdesc.Handle, 0x1501, 1, "Cyber-Warrior.Org");
                mappings = upnpnat.StaticPortMappingCollection;
            }
            catch
            {
                MessageBox.Show("Modeminizin Upnp Özelliğini Desteklemiyor Veya Devre Dışı Bırakılmış Olabilir!", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }
Пример #15
0
        /// <summary>
        /// Checks if the port is already used with the UPnP protocol or not.
        /// </summary>
        /// <param name="PortNumber">The port number to check.</param>
        /// <returns>Returns true if the port is used, else returns false.</returns>
        public static bool IsUsedPort(int PortNumber)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                foreach (NATUPNPLib.IStaticPortMapping portMapping in mappings)
                {
                    if (portMapping.ExternalPort == PortNumber)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #16
0
        /// <summary>
        /// Use NATUPNPLib to try and open a port forward
        /// </summary>
        /// <param name="intPortNumber">The port to open</param>
        /// <param name="strFriendlyName">Friendly name for the service</param>
        /// <returns>Boolean indicating whether the forward worked</returns>
        public static bool OpenUPnP(int intPortNumber, string strFriendlyName, string ipAddress = "")
        {
            if (ipAddress == "")
            {
                ipAddress = GetListenIP().ToString();
            }
            try {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                upnpnat.StaticPortMappingCollection.Add(intPortNumber, "TCP", intPortNumber, ipAddress, true, "[YAMS] " + strFriendlyName);

                Database.AddLog("Forwarded port " + intPortNumber + " to " + ipAddress + " for " + strFriendlyName, "networking");
                return(true);
            }
            catch (Exception e) {
                string strMessage = e.Message;
                if (e.Message == "Object reference not set to an instance of an object.")
                {
                    strMessage = "No UPnP Router Found";
                }
                Database.AddLog("Unable foward port " + intPortNumber + " for " + strFriendlyName + ": Exception - " + e.Message, "networking", "warn");
                return(false);
            }
        }
Пример #17
0
        static void Main(string[] args)
        {
            //bool x = UPnP.NAT.Discover();
            //UPnP.NAT.ForwardPort(8502, ProtocolType.Tcp, "test");

            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
            //Добавление порта
            mappings.Add(12345, "TCP", 12345, GetLocalIPAddress(), true, "Some name");

            //Console.WriteLine(UPnP.NAT.GetExternalIP().ToString());
            bw.DoWork += Bw_DoWork;
            bw.WorkerReportsProgress = true;
            bw.ProgressChanged      += Bw_ProgressChanged;
            bw.RunWorkerAsync();


            Application.Run(mainForm);
            //mainForm.Show();

            Console.ReadLine();
            //Удаление порта
            mappings.Remove(12345, "TCP");
        }
Пример #18
0
        /// <summary>
        /// Use NATUPNPLib to try and open a port forward
        /// </summary>
        /// <param name="intPortNumber">The port to open</param>
        /// <param name="strFriendlyName">Friendly name for the service</param>
        /// <returns>Boolean indicating whether the forward worked</returns>
        public static bool OpenUPnP(int intPortNumber, string strFriendlyName, string ipAddress = "")
        {
            if (ipAddress == "") ipAddress = GetListenIP().ToString();
            try {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                upnpnat.StaticPortMappingCollection.Add(intPortNumber, "TCP", intPortNumber, ipAddress, true, "[YAMS] " + strFriendlyName);

                Database.AddLog("Forwarded port " + intPortNumber + " to " + ipAddress + " for " + strFriendlyName, "networking");
                return true;
            }
            catch(Exception e) {
                string strMessage = e.Message;
                if (e.Message == "Object reference not set to an instance of an object.") strMessage = "No UPnP Router Found";
                Database.AddLog("Unable foward port " + intPortNumber + " for " + strFriendlyName + ": Exception - " + e.Message, "networking", "warn");
                return false;
            }
        }
Пример #19
0
        private bool checkOpen()
        {
            UDPopen = false;
            TCPopen = false;

            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings == null)
            {
                Status = "UPnP is disabled in your router. Try enabling it and hit refresh.";
            }
            else
            {
                foreach (NATUPNPLib.IStaticPortMapping mapping in mappings)
                {
                    if (mapping.InternalClient == ip && mapping.InternalPort == 12345)
                    {
                        switch (mapping.Protocol.ToUpper())
                        {
                            case "UDP":
                                UDPopen = true;
                                break;
                            case "TCP":
                                TCPopen = true;
                                break;
                        }
                    }
                }
            }

            return (TCPopen && UDPopen);
        }
Пример #20
0
        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (!(TCPopen && UDPopen))
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

                if (mappings == null)
                {
                    Status = "UPnP is disabled in your router. Try enabling it and hit refresh.";
                }
                else
                {
                    try
                    {
                        mappings.Add(ServerService.Helper.Settings.Instance.Port, "UDP", ServerService.Helper.Settings.Instance.Port, ip, true, "CubeWorld UDP");
                        mappings.Add(ServerService.Helper.Settings.Instance.Port, "TCP", ServerService.Helper.Settings.Instance.Port, ip, true, "CubeWorld TCP");

                        RefreshButton_Click(null, null);
                    }
                    catch
                    {
                        Status = "Could not open ports. Is UPnP enabled?";
                    }
                }
            }
            else
            {
                NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
                NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

                if (mappings == null)
                {
                    Status = "UPnP is disabled in your router. Try enabling it and hit refresh.";
                }
                else
                {
                    try
                    {
                        if (UDPopen)
                            mappings.Remove(ServerService.Helper.Settings.Instance.Port, "UDP");

                        if (TCPopen)
                            mappings.Remove(ServerService.Helper.Settings.Instance.Port, "TCP");

                        RefreshButton_Click(null, null);
                    }
                    catch
                    {
                        Status = "Could not close ports. Is UPnP enabled?";
                    }
                }
            }
        }
Пример #21
0
        private void chkUpnp_CheckedChanged(object sender, EventArgs e)
        {
            NATUPNPLib.UPnPNATClass UPnPcfg;
            try
            {
                UPnPcfg = new NATUPNPLib.UPnPNATClass();
                int i = UPnPcfg.DynamicPortMappingCollection.Count;
                if (chkUpnp.Checked == true)
                {

                    UPnPcfg.StaticPortMappingCollection.Add(Util.covertChannel.useTCP.localPort, "TCP", Util.covertChannel.useTCP.localPort, Util.LocalIPAddress(), true, "Covert Chanel");
                }
                else
                {
                    UPnPcfg.StaticPortMappingCollection.Remove(Util.covertChannel.useTCP.localPort, "TCP");
                }

            }
            catch
            {
                chkUpnp_CheckedChanged(sender,e);
            }
        }
Пример #22
0
        /// <summary>
        /// Checks if the port is already used with the UPnP protocol or not.
        /// </summary>
        /// <param name="PortNumber">The port number to check.</param>
        /// <returns>Returns true if the port is used, else returns false.</returns>
        public static bool IsUsedPort(int PortNumber)
        {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();

            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings != null)
            {
                foreach(NATUPNPLib.IStaticPortMapping portMapping in mappings)
                {
                    if (portMapping.ExternalPort == PortNumber)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Пример #23
0
 public static void Init()
 {
     MappedPorts = new List <int>();
     UpnpNat     = new NATUPNPLib.UPnPNATClass();
     UpnpMap     = UpnpNat.StaticPortMappingCollection;
 }