2021-03-02 11:09:29 +05:30
|
|
|
/*
|
|
|
|
* mCaptcha - A proof of work based DoS protection system
|
|
|
|
* Copyright © 2021 Aravinth Manivannan <realravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Errors and Result module
|
2021-02-28 15:05:39 +05:30
|
|
|
use derive_more::{Display, Error};
|
2021-06-08 18:46:44 +05:30
|
|
|
#[cfg(feature = "full")]
|
2021-06-08 18:13:01 +05:30
|
|
|
use redis::RedisError;
|
2021-02-28 15:05:39 +05:30
|
|
|
|
2021-03-02 11:09:29 +05:30
|
|
|
/// Error datatype
|
2021-06-08 18:13:01 +05:30
|
|
|
#[derive(Debug, PartialEq, Display, Error)]
|
2021-02-28 15:05:39 +05:30
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
|
|
pub enum CaptchaError {
|
2021-06-01 16:41:49 +05:30
|
|
|
/// When configuring libmcaptcha, [DefenseBuilder][crate::defense::DefenseBuilder]
|
2021-03-02 11:09:29 +05:30
|
|
|
/// must be passed atleast one `LevelConfig` if not this error will arise
|
2021-02-28 15:05:39 +05:30
|
|
|
#[display(fmt = "LevelBuilder should have atleaset one level configured")]
|
|
|
|
LevelEmpty,
|
2021-02-28 23:26:32 +05:30
|
|
|
|
2021-03-02 11:09:29 +05:30
|
|
|
/// Visitor count must be a whole number(zero and above).
|
2021-06-01 16:41:49 +05:30
|
|
|
/// When configuring libmcaptcha, [LevelBuilder][crate::defense::LevelBuilder].
|
2021-03-02 11:09:29 +05:30
|
|
|
/// difficulty_factor must be set to greater than zero.
|
2021-02-28 15:05:39 +05:30
|
|
|
#[display(fmt = "difficulty factor must be greater than zero")]
|
|
|
|
DifficultyFactorZero,
|
2021-02-28 23:26:32 +05:30
|
|
|
|
2021-03-14 16:17:26 +05:30
|
|
|
/// captcha cooldown duration must be greater than 0
|
|
|
|
#[display(fmt = "difficulty factor must be greater than zero")]
|
|
|
|
CaptchaDurationZero,
|
|
|
|
|
2021-02-28 15:05:39 +05:30
|
|
|
/// Difficulty factor must be set
|
|
|
|
#[display(fmt = "Set difficulty factor")]
|
|
|
|
SetDifficultyFactor,
|
2021-02-28 23:26:32 +05:30
|
|
|
|
2021-03-02 12:01:15 +05:30
|
|
|
/// Visitor threshold must be set
|
|
|
|
#[display(fmt = "Set visitor threshold")]
|
|
|
|
SetVisitorThreshold,
|
2021-02-28 15:05:39 +05:30
|
|
|
|
|
|
|
/// Visitor count must be Unique
|
|
|
|
#[display(fmt = "Duplicate visitor count")]
|
|
|
|
DuplicateVisitorCount,
|
|
|
|
|
|
|
|
/// Difficulty factor should increase with level
|
|
|
|
#[display(fmt = "Difficulty factor should increase with level")]
|
|
|
|
DecreaseingDifficultyFactor,
|
2021-03-05 21:52:41 +05:30
|
|
|
|
|
|
|
/// Difficulty factor should increase with level
|
|
|
|
#[display(fmt = "Actor mailbox error")]
|
|
|
|
MailboxError,
|
2021-03-08 18:14:34 +05:30
|
|
|
|
|
|
|
/// Happens when submitted work doesn't satisfy the required
|
|
|
|
/// difficulty factor
|
|
|
|
#[display(fmt = "Insuffiencient Difficulty")]
|
|
|
|
InsuffiencientDifficulty,
|
|
|
|
|
|
|
|
/// Happens when submitted work is computed over string that
|
|
|
|
/// isn't in cache
|
|
|
|
#[display(fmt = "String now found")]
|
|
|
|
StringNotFound,
|
2021-03-14 16:17:26 +05:30
|
|
|
|
2021-04-09 23:20:14 +05:30
|
|
|
/// Happens when submitted work is computed over configuration intended for
|
|
|
|
/// a different mCAptcha sitekey
|
|
|
|
#[display(fmt = "PoW computed over configuration not intended for target sitekey")]
|
|
|
|
MCaptchaKeyValidationFail,
|
|
|
|
|
2021-04-10 11:40:59 +05:30
|
|
|
/// Submitted PoW is invalid
|
|
|
|
#[display(fmt = "Invalid PoW")]
|
|
|
|
InvalidPoW,
|
|
|
|
|
2021-03-14 16:17:26 +05:30
|
|
|
/// Used in builder structs when a value is not set
|
|
|
|
#[display(fmt = "Please set value: {}", _0)]
|
|
|
|
PleaseSetValue(#[error(not(source))] String),
|
2021-06-08 18:13:01 +05:30
|
|
|
|
|
|
|
/// RedisError
|
|
|
|
#[display(fmt = "{}", _0)]
|
2021-06-08 18:46:44 +05:30
|
|
|
#[cfg(feature = "full")]
|
2021-06-08 18:13:01 +05:30
|
|
|
RedisError(RedisError),
|
|
|
|
|
|
|
|
/// Weird behaviour from mcaptcha redis module
|
|
|
|
#[display(
|
|
|
|
fmt = "Something weird happening with mCaptcha redis module. Please file bug report"
|
|
|
|
)]
|
|
|
|
MCaptchaRedisModuleError,
|
2021-06-08 20:15:02 +05:30
|
|
|
|
|
|
|
/// When libmcaptcha is ordered to connect to a Redis instance that doesn't have mCaptcha
|
|
|
|
/// Redis module loaded
|
|
|
|
#[display(
|
|
|
|
fmt = "You are trying to connect to a Redis instance that doesn't have mCaptcha redis module loaded.
|
|
|
|
Please see https://github.com/mCaptcha/cache for details on how to install mCaptcha redis module moudle"
|
|
|
|
)]
|
|
|
|
MCaptchaRedisModuleIsNotLoaded,
|
|
|
|
|
|
|
|
/// MCaptcha redis module is loaded but it doesn't have the necessary Redis commands.
|
|
|
|
/// Usually a version mismatch
|
|
|
|
#[display(
|
|
|
|
fmt = "The Redis instance that libmcaptcha is trying to connect to has mCaptcha Redis module loaded,
|
|
|
|
but it's probably outdated and as a result, we are not able to find all required commands to operate mCaptcha
|
|
|
|
Command {} is not found",
|
|
|
|
_0
|
|
|
|
)]
|
|
|
|
MCaptchaRediSModuleCommandNotFound(#[error(not(source))] String),
|
2021-06-08 18:13:01 +05:30
|
|
|
}
|
|
|
|
|
2021-06-08 18:46:44 +05:30
|
|
|
#[cfg(feature = "full")]
|
2021-06-08 20:43:47 +05:30
|
|
|
#[cfg(not(tarpaulin_include))]
|
2021-06-08 18:13:01 +05:30
|
|
|
impl From<RedisError> for CaptchaError {
|
|
|
|
fn from(e: RedisError) -> Self {
|
|
|
|
Self::RedisError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:46:44 +05:30
|
|
|
#[cfg(feature = "full")]
|
2021-06-08 20:43:47 +05:30
|
|
|
#[cfg(not(tarpaulin_include))]
|
2021-06-08 18:13:01 +05:30
|
|
|
impl From<actix::MailboxError> for CaptchaError {
|
|
|
|
fn from(_: actix::MailboxError) -> Self {
|
|
|
|
Self::MailboxError
|
|
|
|
}
|
2021-02-28 15:05:39 +05:30
|
|
|
}
|
|
|
|
|
2021-06-01 16:41:49 +05:30
|
|
|
/// [Result] datatype for libmcaptcha
|
2021-02-28 15:05:39 +05:30
|
|
|
pub type CaptchaResult<V> = std::result::Result<V, CaptchaError>;
|