1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use lazy_static::lazy_static;
use redis_module::NotifyEvent;
use redis_module::{redis_command, redis_event_handler, redis_module};
use redis_module::{NextArg, RedisResult};
use redis_module::Context;
mod bucket;
mod challenge;
mod errors;
mod mcaptcha;
mod safety;
mod utils;
use bucket::MCAPTCHA_BUCKET_TYPE;
use challenge::MCAPTCHA_CHALLENGE_TYPE;
use mcaptcha::MCAPTCHA_MCAPTCHA_TYPE;
use safety::MCAPTCHA_SAFETY_TYPE;
pub const HIT_PER_SECOND: usize = 100;
pub const PKG_NAME: &str = "mcap";
pub const PKG_VERSION: usize = 0;
pub const PREFIX_BUCKET_TIMER: &str = "timer:";
pub const PREFIX_SAFETY: &str = "safety:";
pub const BUCKET_EXPIRY_OFFSET: u64 = 30;
lazy_static! {
pub static ref ID: usize = {
use rand::prelude::*;
let mut rng = rand::thread_rng();
rng.gen()
};
pub static ref PREFIX_CAPTCHA: String = format!("{}:captcha::", PKG_NAME);
pub static ref PREFIX_BUCKET: String = format!("{}:bucket:{{{}}}:", PKG_NAME, *ID);
pub static ref PREFIX_CHALLENGE: String = format!("{}:CHALLENGE", PKG_NAME);
}
pub fn on_delete(ctx: &Context, event_type: NotifyEvent, event: &str, key_name: &str) {
let msg = format!(
"Received event: {:?} on key: {} via event: {}",
event_type, key_name, event
);
ctx.log_debug(msg.as_str());
if utils::is_bucket_timer(key_name) {
bucket::Bucket::on_delete(ctx, event_type, event, key_name);
} else if utils::is_mcaptcha_safety(key_name) {
crate::safety::MCaptchaSafety::on_delete(ctx, event_type, event, key_name);
}
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub mod redis {
use super::*;
redis_module! {
name: "mcaptcha_cahce",
version: PKG_VERSION,
data_types: [MCAPTCHA_BUCKET_TYPE, MCAPTCHA_MCAPTCHA_TYPE, MCAPTCHA_SAFETY_TYPE, MCAPTCHA_CHALLENGE_TYPE],
commands: [
["MCAPTCHA_CACHE.ADD_VISITOR", bucket::Bucket::counter_create, "write", 1, 1, 1],
["MCAPTCHA_CACHE.GET", mcaptcha::MCaptcha::get_count, "readonly", 1, 1, 1],
["MCAPTCHA_CACHE.ADD_CAPTCHA", mcaptcha::MCaptcha::add_captcha, "readonly", 1, 1, 1],
["MCAPTCHA_CACHE.DELETE_CAPTCHA", mcaptcha::MCaptcha::delete_captcha, "write", 1, 1, 1],
["MCAPTCHA_CACHE.RENAME_CAPTCHA", mcaptcha::MCaptcha::rename, "write", 1, 1, 1],
["MCAPTCHA_CACHE.CAPTCHA_EXISTS", mcaptcha::MCaptcha::captcha_exists, "readonly", 1, 1, 1],
["MCAPTCHA_CACHE.ADD_CHALLENGE", challenge::Challenge::create_challenge, "write", 1, 1, 1],
["MCAPTCHA_CACHE.GET_CHALLENGE", challenge::Challenge::get_challenge, "write", 1, 1, 1],
["MCAPTCHA_CACHE.DELETE_CHALLENGE", challenge::Challenge::delete_challenge, "write", 1, 1, 1],
],
event_handlers: [
[@EXPIRED @EVICTED: on_delete],
]
}
}