This repository has been archived on 2022-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
tmp2/src/master.rs

117 lines
3.3 KiB
Rust
Raw Normal View History

2021-03-07 19:54:41 +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/>.
*/
2021-03-08 19:43:26 +05:30
//! [Master] actor module that manages [MCaptcha] actors
2021-03-07 19:54:41 +05:30
use std::collections::BTreeMap;
use actix::dev::*;
use derive_builder::Builder;
use crate::mcaptcha::MCaptcha;
2021-03-08 19:43:26 +05:30
/// This Actor manages the [MCaptcha] actors.
/// A service can have several [MCaptcha] actors with
/// varying [Defense][crate::defense::Defense] configurations
/// so a "master" actor is needed to manage them all
2021-03-07 19:54:41 +05:30
#[derive(Clone)]
pub struct Master {
sites: BTreeMap<String, Addr<MCaptcha>>,
2021-03-07 19:54:41 +05:30
}
impl Master {
2021-03-08 19:43:26 +05:30
/// add [MCaptcha] actor to [Master]
2021-03-07 19:54:41 +05:30
pub fn add_site(&mut self, details: AddSite) {
self.sites.insert(details.id, details.addr.to_owned());
}
2021-03-08 19:43:26 +05:30
/// create new master
2021-03-07 19:54:41 +05:30
pub fn new() -> Self {
Master {
sites: BTreeMap::new(),
}
}
2021-03-08 19:43:26 +05:30
/// get [MCaptcha] actor from [Master]
2021-03-07 19:54:41 +05:30
pub fn get_site<'a, 'b>(&'a self, id: &'b str) -> Option<&'a Addr<MCaptcha>> {
self.sites.get(id)
}
}
impl Actor for Master {
2021-03-07 19:54:41 +05:30
type Context = Context<Self>;
}
2021-03-08 19:43:26 +05:30
/// Message to get an [MCaptcha] actor from master
2021-03-07 19:54:41 +05:30
#[derive(Message)]
#[rtype(result = "Option<Addr<MCaptcha>>")]
2021-03-08 17:02:36 +05:30
pub struct GetSite(pub String);
2021-03-07 19:54:41 +05:30
impl Handler<GetSite> for Master {
2021-03-08 17:02:36 +05:30
type Result = MessageResult<GetSite>;
2021-03-07 19:54:41 +05:30
fn handle(&mut self, m: GetSite, _ctx: &mut Self::Context) -> Self::Result {
2021-03-08 17:02:36 +05:30
let addr = self.get_site(&m.0);
2021-03-07 19:54:41 +05:30
if addr.is_none() {
return MessageResult(None);
} else {
return MessageResult(Some(addr.unwrap().clone()));
}
}
}
2021-03-08 19:43:26 +05:30
/// Message to add an [MCaptcha] actor to [Master]
2021-03-07 19:54:41 +05:30
#[derive(Message, Builder)]
#[rtype(result = "()")]
pub struct AddSite {
pub id: String,
2021-03-07 19:54:41 +05:30
pub addr: Addr<MCaptcha>,
}
impl Handler<AddSite> for Master {
2021-03-07 19:54:41 +05:30
type Result = ();
fn handle(&mut self, m: AddSite, _ctx: &mut Self::Context) -> Self::Result {
self.add_site(m);
}
}
#[cfg(test)]
mod tests {
use super::*;
2021-03-08 19:43:26 +05:30
use crate::mcaptcha::tests::*;
2021-03-07 19:54:41 +05:30
#[actix_rt::test]
2021-03-08 19:43:26 +05:30
async fn master_actor_works() {
2021-03-07 19:54:41 +05:30
let addr = Master::new().start();
let id = "yo";
let mcaptcha = get_counter().start();
let msg = AddSiteBuilder::default()
.id(id.into())
2021-03-08 19:43:26 +05:30
.addr(mcaptcha.clone())
2021-03-07 19:54:41 +05:30
.build()
.unwrap();
addr.send(msg).await.unwrap();
2021-03-08 19:43:26 +05:30
let mcaptcha_addr = addr.send(GetSite(id.into())).await.unwrap();
assert_eq!(mcaptcha_addr, Some(mcaptcha));
let addr_doesnt_exist = addr.send(GetSite("a".into())).await.unwrap();
assert!(addr_doesnt_exist.is_none());
2021-03-07 19:54:41 +05:30
}
}