Esempio n. 1
0
        private IPAddressEntry ParseIPAddressEntry(SqliteDataReader reader)
        {
            string         ipAddress         = reader.GetString(0);
            long           lastFailedLogin   = reader.GetInt64(1);
            long           failedLoginCount  = reader.GetInt64(2);
            object         banDateObj        = reader.GetValue(3);
            IPAddressState state             = (IPAddressState)(int)reader.GetInt32(4);
            object         banEndDateObj     = reader.GetValue(5);
            string         userName          = reader.GetString(6);
            string         source            = reader.GetString(7);
            long           banDateLong       = (banDateObj is null || banDateObj == DBNull.Value ? 0 : Convert.ToInt64(banDateObj));
            long           banEndDateLong    = (banEndDateObj is null || banEndDateObj == DBNull.Value ? 0 : Convert.ToInt64(banEndDateObj));
            DateTime?      banDate           = (banDateLong == 0 ? (DateTime?)null : banDateLong.ToDateTimeUnixMilliseconds());
            DateTime?      banEndDate        = (banDateLong == 0 ? (DateTime?)null : banEndDateLong.ToDateTimeUnixMilliseconds());
            DateTime       lastFailedLoginDt = lastFailedLogin.ToDateTimeUnixMilliseconds();

            return(new IPAddressEntry
            {
                IPAddress = ipAddress,
                LastFailedLogin = lastFailedLoginDt,
                FailedLoginCount = (int)failedLoginCount,
                BanStartDate = banDate,
                State = state,
                BanEndDate = banEndDate,
                UserName = userName,
                Source = source
            });
        }
Esempio n. 2
0
 /// <summary>
 /// Get an ip address state
 /// </summary>
 /// <param name="ipAddress">IP address</param>
 /// <param name="state">Receives ip address state or default if not found</param>
 /// <param name="transaction">Transaction</param>
 /// <returns>True if ip address found, false otherwise</returns>
 public bool TryGetIPAddressState(string ipAddress, out IPAddressState state, object transaction = null)
 {
     if (IPAddress.TryParse(ipAddress, out IPAddress ipAddressObj))
     {
         IPBanDBTransaction tran    = transaction as IPBanDBTransaction;
         byte[]             ipBytes = ipAddressObj.GetAddressBytes();
         state = (IPAddressState)ExecuteScalar <int>(tran?.DBConnection, tran?.DBTransaction, "SELECT State FROM IPAddresses WHERE IPAddress = @Param0", ipBytes);
         return(true);
     }
     state = IPAddressState.Active;
     return(false);
 }
Esempio n. 3
0
        /// <summary>
        /// Set state of ip addresses
        /// </summary>
        /// <param name="ipAddresses">IP addresses to set state for. Pass null to set the entire database.</param>
        /// <param name="state">State to set</param>
        /// <param name="transaction">Transaction</param>
        /// <returns>Number of rows affected</returns>
        public int SetIPAddressesState(IEnumerable <string> ipAddresses, IPAddressState state, object transaction = null)
        {
            int count = 0;
            IPBanDBTransaction ipDBTransaction = transaction as IPBanDBTransaction;
            bool             commit            = (transaction == null);
            SqliteConnection conn = (ipDBTransaction?.DBConnection ?? CreateConnection());

            if (commit)
            {
                OpenConnection(conn);
            }
            SqliteTransaction tran = (ipDBTransaction?.DBTransaction ?? conn.BeginTransaction(transactionLevel));
            int stateInt           = (int)state;

            try
            {
                if (ipAddresses == null)
                {
                    count += ExecuteNonQuery(conn, tran, "UPDATE IPAddresses SET State = @Param0", stateInt);
                }
                else
                {
                    foreach (string ipAddress in ipAddresses)
                    {
                        if (IPAddress.TryParse(ipAddress, out IPAddress ipAddressObj))
                        {
                            byte[] ipBytes = ipAddressObj.GetAddressBytes();
                            count += ExecuteNonQuery(conn, tran, "UPDATE IPAddresses SET State = @Param0 WHERE IPAddress = @Param1", stateInt, ipBytes);
                        }
                    }
                }
            }
            finally
            {
                if (commit)
                {
                    tran.Commit();
                    tran.Dispose();
                    CloseConnection(conn);
                }
            }
            return(count);
        }
Esempio n. 4
0
        /// <summary>
        /// Set state of ip addresses
        /// </summary>
        /// <param name="ipAddresses">IP addresses to set state for. Pass null to set the entire database.</param>
        /// <param name="state">State to set</param>
        /// <param name="transaction">Transaction</param>
        /// <returns>Number of rows affected</returns>
        public int SetIPAddressesState(IEnumerable <string> ipAddresses, IPAddressState state, object transaction = null)
        {
            if (ipAddresses is null)
            {
                return(0);
            }

            int count                = 0;
            int stateInt             = (int)state;
            SqliteDBTransaction tran = transaction as SqliteDBTransaction;
            bool commit              = (transaction is null);

            tran ??= BeginTransaction() as SqliteDBTransaction;
            try
            {
                foreach (string ipAddress in ipAddresses)
                {
                    if (IPAddress.TryParse(ipAddress, out IPAddress ipAddressObj))
                    {
                        ipAddressObj = ipAddressObj.Clean();
                        byte[] ipBytes = ipAddressObj.GetAddressBytes();
                        count += ExecuteNonQuery("UPDATE IPAddresses SET State = @Param0 WHERE IPAddress = @Param1", tran.DBConnection, tran.DBTransaction, stateInt, ipBytes);
                    }
                }
            }
            catch
            {
                if (commit)
                {
                    commit = false;
                    RollbackTransaction(tran);
                }
                throw;
            }
            finally
            {
                if (commit)
                {
                    CommitTransaction(tran);
                }
            }
            return(count);
        }
Esempio n. 5
0
 /// <summary>
 /// Delete ip addresses with a specific state from the database
 /// </summary>
 /// <param name="sate">IP address state delete</param>
 /// <returns>Number of rows modified</returns>
 public int DeleteIPAddresses(IPAddressState state)
 {
     return(ExecuteNonQuery("DELETE FROM IPAddresses WHERE State = @Param0", (int)IPAddressState.RemovePending));
 }