/// <summary>
        /// Adds new security enrty to collection.
        /// </summary>
        /// <param name="enabled">Specifies if security entry is enabled.</param>
        /// <param name="description">Security entry description text.</param>
        /// <param name="service">Specifies service for what security entry applies.</param>
        /// <param name="action">Specifies what action done if IP matches to security entry range.</param>
        /// <param name="startIP">Range start IP.</param>
        /// <param name="endIP">Range end IP.</param>
        /// <returns></returns>
        public IPSecurity Add(bool enabled, string description, Service_enum service, IPSecurityAction_enum action, IPAddress startIP, IPAddress endIP)
        {
            /* AddIPSecurityEntry <virtualServerID> "<securityEntryID>" enabled "<description>" <service> <action> "<startIP>" "<endIP>"
             *    Responses:
             +OK
             *      -ERR <errorText>
             */

            string id = Guid.NewGuid().ToString();

            // Call TCP AddIPSecurityEntry
            m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("AddIPSecurityEntry " +
                                                                  m_pVirtualServer.VirtualServerID + " " +
                                                                  TextUtils.QuoteString(id) + " " +
                                                                  enabled + " " +
                                                                  TextUtils.QuoteString(description) + " " +
                                                                  (int)service + " " +
                                                                  (int)action + " " +
                                                                  TextUtils.QuoteString(startIP.ToString()) + " " +
                                                                  TextUtils.QuoteString(endIP.ToString())
                                                                  );

            string response = m_pVirtualServer.Server.ReadLine();

            if (!response.ToUpper().StartsWith("+OK"))
            {
                throw new Exception(response);
            }

            IPSecurity entry = new IPSecurity(
                this,
                id,
                enabled,
                description,
                service,
                action,
                startIP,
                endIP
                );

            m_pEntries.Add(entry);

            return(entry);
        }
        /// <summary>
        /// Edit constructor.
        /// </summary>
        /// <param name="virtualServer">Virtual server.</param>
        /// <param name="securityEntry">Security entry to update.</param>
        public wfrm_Security_IPSecurityEntry(VirtualServer virtualServer,IPSecurity securityEntry)
        {
            m_pVirtualServer = virtualServer;
            m_pSecurityEntry = securityEntry;

            InitUI();

            m_pEnabled.Checked = securityEntry.Enabled;
            m_pDescription.Text = securityEntry.Description;
            m_pService.SelectedIndex = (int)securityEntry.Service - 1;
            m_pAction.SelectedIndex = (int)securityEntry.Action - 1;
            if(securityEntry.StartIP.Equals(securityEntry.EndIP)){
                m_pType.SelectedIndex = 0;
            }
            else{
                m_pType.SelectedIndex = 1;
            }
            m_pStartIP.Text = securityEntry.StartIP.ToString();
            m_pEndIP.Text = securityEntry.EndIP.ToString();
        }
Esempio n. 3
0
        /// <summary>
        /// Adds new security enrty to collection.
        /// </summary>
        /// <param name="enabled">Specifies if security entry is enabled.</param>
        /// <param name="description">Security entry description text.</param>
        /// <param name="service">Specifies service for what security entry applies.</param>
        /// <param name="action">Specifies what action done if IP matches to security entry range.</param>
        /// <param name="startIP">Range start IP.</param>
        /// <param name="endIP">Range end IP.</param>
        /// <returns></returns>
        public IPSecurity Add(bool enabled,string description,Service_enum service,IPSecurityAction_enum action,IPAddress startIP,IPAddress endIP)
        {
            /* AddIPSecurityEntry <virtualServerID> "<securityEntryID>" enabled "<description>" <service> <action> "<startIP>" "<endIP>"
                  Responses:
                    +OK                     
                    -ERR <errorText>
            */

            string id = Guid.NewGuid().ToString();

            // Call TCP AddIPSecurityEntry
            m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("AddIPSecurityEntry " + 
                m_pVirtualServer.VirtualServerID + " " + 
                TextUtils.QuoteString(id) + " " + 
                enabled + " " + 
                TextUtils.QuoteString(description) + " " +
                (int)service + " " +
                (int)action + " " +
                TextUtils.QuoteString(startIP.ToString()) + " " +
                TextUtils.QuoteString(endIP.ToString())
            );
                        
            string response = m_pVirtualServer.Server.ReadLine();
            if(!response.ToUpper().StartsWith("+OK")){
                throw new Exception(response);
            }

            IPSecurity entry = new IPSecurity(
                this,
                id,
                enabled,
                description,
                service,
                action,
                startIP,
                endIP
            );
            m_pEntries.Add(entry);

            return entry;
        }
        /// <summary>
        /// Deletes and removes specified security entry from collection.
        /// </summary>
        /// <param name="entry"></param>
        public void Remove(IPSecurity entry)
        {
            /* DeleteIPSecurityEntry <virtualServerID> "<securityEntryID>"
             *    Responses:
             +OK
             *      -ERR <errorText>
             */

            string id = Guid.NewGuid().ToString();

            // Call TCP DeleteIPSecurityEntry
            m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("DeleteIPSecurityEntry " + m_pVirtualServer.VirtualServerID + " " + TextUtils.QuoteString(entry.ID));

            string response = m_pVirtualServer.Server.ReadLine();

            if (!response.ToUpper().StartsWith("+OK"))
            {
                throw new Exception(response);
            }

            m_pEntries.Remove(entry);
        }
        private void m_pOk_Click(object sender, EventArgs e)
        {
            IPAddress startIP = null;
            IPAddress endIP   = null;

            //--- Validate values --------------------------------------------------------------------------------//
            if(m_pDescription.Text == ""){
                MessageBox.Show(this,"Please fill description !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            try{
                startIP = IPAddress.Parse(m_pStartIP.Text);

                if(m_pType.SelectedIndex == 0){
                    endIP = startIP;
                }
                else{
                    try{
                        endIP = IPAddress.Parse(m_pEndIP.Text);
                    }
                    catch{
                        MessageBox.Show(this,"Invalid end IP value !","Invalid IP value",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            catch{
                MessageBox.Show(this,"Invalid start IP value !","Invalid IP value",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            if(startIP.AddressFamily != endIP.AddressFamily){
                MessageBox.Show(this,"Start IP and End IP must be from same address familily, you can't mix IPv4 and IPv6 addresses !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            //-----------------------------------------------------------------------------------------------------//
            
            // Add new security entry
            if(m_pSecurityEntry == null){
                m_pSecurityEntry = m_pVirtualServer.IpSecurity.Add(
                    m_pEnabled.Checked,
                    m_pDescription.Text,
                    (Service_enum)((WComboBoxItem)m_pService.SelectedItem).Tag,
                    (IPSecurityAction_enum)((WComboBoxItem)m_pAction.SelectedItem).Tag,
                    startIP,
                    endIP
                );
            }
            // Update security entry
            else{
                m_pSecurityEntry.Enabled     = m_pEnabled.Checked;
                m_pSecurityEntry.Description = m_pDescription.Text;
                m_pSecurityEntry.Service     = (Service_enum)((WComboBoxItem)m_pService.SelectedItem).Tag;
                m_pSecurityEntry.Action      = (IPSecurityAction_enum)((WComboBoxItem)m_pAction.SelectedItem).Tag;
                m_pSecurityEntry.StartIP     = startIP;
                m_pSecurityEntry.EndIP       = endIP;
                m_pSecurityEntry.Commit();
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// Deletes and removes specified security entry from collection.
        /// </summary>
        /// <param name="entry"></param>
        public void Remove(IPSecurity entry)
        {
            /* DeleteIPSecurityEntry <virtualServerID> "<securityEntryID>"
                  Responses:
                    +OK
                    -ERR <errorText>
            */

            string id = Guid.NewGuid().ToString();

            // Call TCP DeleteIPSecurityEntry
            m_pVirtualServer.Server.Socket.WriteLine("DeleteIPSecurityEntry " + m_pVirtualServer.VirtualServerID + " " + TextUtils.QuoteString(entry.ID));

            string response = m_pVirtualServer.Server.Socket.ReadLine();
            if(!response.ToUpper().StartsWith("+OK")){
                throw new Exception(response);
            }

            m_pEntries.Remove(entry);
        }