libmcaptcha/master/
mod.rs

1/*
2 * mCaptcha - A proof of work based DoS protection system
3 * Copyright © 2021 Aravinth Manivannan <realravinth@batsense.net>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18//! [Master] actor module that manages [MCaptcha] actors
19
20use serde::{Deserialize, Serialize};
21
22use crate::mcaptcha::*;
23
24#[cfg(feature = "full")]
25pub mod embedded;
26#[cfg(feature = "full")]
27pub mod redis;
28
29#[cfg(feature = "full")]
30/// Describes actor handler trait impls that are required by a cache implementation
31pub trait Master:
32    actix::Actor
33    + actix::Handler<messages::AddVisitor>
34    + actix::Handler<messages::AddSite>
35    + actix::Handler<messages::Rename>
36    + actix::Handler<messages::RemoveCaptcha>
37    + actix::Handler<messages::SetInternalData>
38    + actix::Handler<messages::GetInternalData>
39{
40}
41
42#[derive(Serialize, Deserialize)]
43/// [mCaptcha Redis module](https://github.com/mCaptcha/cache) uses this datatype for CAPTCHA
44/// registration
45pub struct CreateMCaptcha {
46    pub levels: Vec<crate::defense::Level>,
47    pub duration: u64,
48}
49
50/// Struct representing the return datatime of
51/// [AddVisitor] message. Contains MCaptcha lifetime
52/// and difficulty factor
53#[derive(Debug, Clone, Deserialize, Serialize)]
54pub struct AddVisitorResult {
55    pub duration: u64,
56    pub difficulty_factor: u32,
57}
58
59impl AddVisitorResult {
60    /// create new [AddVisitorResult] from [MCaptcha]
61    pub fn new(m: &MCaptcha) -> Self {
62        AddVisitorResult {
63            duration: m.get_duration(),
64            difficulty_factor: m.get_difficulty(),
65        }
66    }
67}
68
69#[cfg(feature = "full")]
70pub mod messages {
71    //! Messages that a [super::Master] should respond to
72    use std::collections::HashMap;
73    //    use std::sync::mpsc::Receiver;
74
75    use actix::dev::*;
76    use derive_builder::Builder;
77    use serde::{Deserialize, Serialize};
78    use tokio::sync::oneshot::Receiver;
79
80    use crate::errors::CaptchaResult;
81    use crate::mcaptcha::MCaptcha;
82
83    /// Message to add visitor to an [MCaptcha] actor
84    #[derive(Message, Clone, Debug, Deserialize, Serialize)]
85    #[rtype(result = "Receiver<CaptchaResult<Option<super::AddVisitorResult>>>")]
86    pub struct AddVisitor(pub String);
87
88    /// Message to add an [Counter] actor to [Master]
89    #[derive(Message, Builder, Clone, Debug, Deserialize, Serialize)]
90    #[rtype(result = "Receiver<CaptchaResult<()>>")]
91    pub struct AddSite {
92        pub id: String,
93        pub mcaptcha: MCaptcha,
94    }
95
96    /// Message to rename an MCaptcha actor
97    #[derive(Message, Builder, Clone, Debug, Deserialize, Serialize)]
98    #[rtype(result = "Receiver<CaptchaResult<()>>")]
99    pub struct Rename {
100        pub name: String,
101        pub rename_to: String,
102    }
103
104    /// Message to delete [Counter] actor
105    #[derive(Message, Clone, Debug, Deserialize, Serialize)]
106    #[rtype(result = "Receiver<CaptchaResult<()>>")]
107    pub struct RemoveCaptcha(pub String);
108
109    #[derive(Clone, Debug, Deserialize, Serialize)]
110    pub struct InternalData {
111        pub name: String,
112        pub mcaptcha: MCaptcha,
113    }
114
115    /// Gets internal Captcha data
116    #[derive(Message)]
117    #[rtype(result = "Receiver<CaptchaResult<HashMap<String, MCaptcha>>>")]
118    pub struct GetInternalData;
119
120    /// Sets internal Captcha data
121    #[derive(Message)]
122    #[rtype(result = "Receiver<CaptchaResult<()>>")]
123    pub struct SetInternalData {
124        pub mcaptcha: HashMap<String, MCaptcha>,
125    }
126}