clean up systemgroup interface

This commit is contained in:
realaravinth 2021-08-20 19:17:40 +05:30
parent 068c49080e
commit a8c3eaa617
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 113 additions and 106 deletions

124
Cargo.lock generated
View file

@ -487,9 +487,9 @@ dependencies = [
[[package]]
name = "bitflags"
version = "1.3.1"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da1976d75adbe5fbc88130ecd119529cf1cc6a93ae1546d8696ee66f0d21af1"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitvec"
@ -543,12 +543,6 @@ dependencies = [
"libc",
]
[[package]]
name = "build_const"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ae4235e6dac0694637c763029ecea1a2ec9e4e06ec2729bd21ba4d9c863eb7"
[[package]]
name = "bumpalo"
version = "3.7.0"
@ -729,13 +723,19 @@ checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba"
[[package]]
name = "crc"
version = "1.8.1"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
checksum = "10c2722795460108a7872e1cd933a85d6ec38abc4baecad51028f702da28889f"
dependencies = [
"build_const",
"crc-catalog",
]
[[package]]
name = "crc-catalog"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403"
[[package]]
name = "crc16"
version = "0.4.0"
@ -1085,6 +1085,17 @@ dependencies = [
"futures-util",
]
[[package]]
name = "futures-intrusive"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62007592ac46aa7c2b6416f7deb9a8a8f63a01e0f1d6e1787d5630170db2b63e"
dependencies = [
"futures-core",
"lock_api",
"parking_lot",
]
[[package]]
name = "futures-io"
version = "0.3.16"
@ -1307,9 +1318,9 @@ dependencies = [
[[package]]
name = "httparse"
version = "1.4.1"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68"
checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503"
[[package]]
name = "httpdate"
@ -1382,18 +1393,18 @@ checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8"
[[package]]
name = "jobserver"
version = "0.1.23"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5ca711fd837261e14ec9e674f092cbb931d3fa1482b017ae59328ddc6f3212b"
checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa"
dependencies = [
"libc",
]
[[package]]
name = "js-sys"
version = "0.3.52"
version = "0.3.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce791b7ca6638aae45be056e068fc756d871eb3b3b10b8efa62d1c9cec616752"
checksum = "e4bf49d50e2961077d9c99f4b7997d770a1114f087c3c2e0069b36c13fc2979d"
dependencies = [
"wasm-bindgen",
]
@ -1457,7 +1468,7 @@ checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765"
[[package]]
name = "libmcaptcha"
version = "0.1.4"
source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#ec5a38093cbfb37323cf706b9f26ce0c3f6e57c7"
source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#b2684d93482cec8e40cd6ccbc2cee4be3b1eef25"
dependencies = [
"actix",
"derive_builder",
@ -1632,9 +1643,9 @@ dependencies = [
[[package]]
name = "memchr"
version = "2.4.0"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "memory_units"
@ -1858,9 +1869,9 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.35"
version = "0.10.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549430950c79ae24e6d02e0b7404534ecf311d94cc9f861e9e4020187d13d885"
checksum = "8d9facdb76fec0b73c406f125d44d86fdad818d66fef0531eec9233ca425ff4a"
dependencies = [
"bitflags",
"cfg-if 1.0.0",
@ -1887,9 +1898,9 @@ dependencies = [
[[package]]
name = "openssl-sys"
version = "0.9.65"
version = "0.9.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a7907e3bfa08bb85105209cdfcb6c63d109f8f6c1ed6ca318fff5c1853fbc1d"
checksum = "1996d2d305e561b70d1ee0c53f1542833f4e1ac6ce9a6708b6ff2738ca67dc82"
dependencies = [
"autocfg",
"cc",
@ -2618,9 +2629,9 @@ dependencies = [
[[package]]
name = "serde_yaml"
version = "0.8.17"
version = "0.8.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23"
checksum = "039ba818c784248423789eec090aab9fb566c7b94d6ebbfa1814a9fd52c8afb2"
dependencies = [
"dtoa",
"linked-hash-map",
@ -2718,9 +2729,9 @@ dependencies = [
[[package]]
name = "sqlx"
version = "0.5.5"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba82f79b31f30acebf19905bcd8b978f46891b9d0723f578447361a8910b6584"
checksum = "b977121ecc75cadd442a6a8c600f5ded6d7117a46d55ed417a815bd94b2af237"
dependencies = [
"sqlx-core",
"sqlx-macros",
@ -2728,9 +2739,9 @@ dependencies = [
[[package]]
name = "sqlx-core"
version = "0.5.5"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f23af36748ec8ea8d49ef8499839907be41b0b1178a4e82b8cb45d29f531dc9"
checksum = "6da11206ae9ba3ececf83d44aef533c0d8176ae430cfaa14b408ce8b6bc4ab4b"
dependencies = [
"ahash",
"atoi",
@ -2746,6 +2757,7 @@ dependencies = [
"either",
"futures-channel",
"futures-core",
"futures-intrusive",
"futures-util",
"hashlink",
"hex",
@ -2779,9 +2791,9 @@ dependencies = [
[[package]]
name = "sqlx-macros"
version = "0.5.5"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e4a2349d1ffd60a03ca0de3f116ba55d7f406e55a0d84c64a5590866d94c06"
checksum = "419f9005ad3b065ab4e774361b0d56a24e8e3dc9b2c9ddeaf018b5fdc1166ec2"
dependencies = [
"dotenv",
"either",
@ -2802,9 +2814,9 @@ dependencies = [
[[package]]
name = "sqlx-rt"
version = "0.5.5"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8199b421ecf3493ee9ef3e7bc90c904844cfb2ea7ea2f57347a93f52bfd3e057"
checksum = "52d39baa13314f936c5991429b0b656980f52d23dfb15078bbea2c51f5086eeb"
dependencies = [
"actix-rt",
"once_cell",
@ -3144,9 +3156,9 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052"
checksum = "2ca517f43f0fb96e0c3072ed5c275fe5eece87e8cb52f4a77b69226d3b1c9df8"
dependencies = [
"lazy_static",
]
@ -3319,9 +3331,9 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.75"
version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b608ecc8f4198fe8680e2ed18eccab5f0cd4caaf3d83516fa5fb2e927fda2586"
checksum = "8ce9b1b516211d33767048e5d47fa2a381ed8b76fc48d2ce4aa39877f9f183e0"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen-macro",
@ -3329,9 +3341,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.75"
version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "580aa3a91a63d23aac5b6b267e2d13cb4f363e31dce6c352fca4752ae12e479f"
checksum = "cfe8dc78e2326ba5f845f4b5bf548401604fa20b1dd1d365fb73b6c1d6364041"
dependencies = [
"bumpalo",
"lazy_static",
@ -3344,9 +3356,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.25"
version = "0.4.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16646b21c3add8e13fdb8f20172f8a28c3dbf62f45406bcff0233188226cfe0c"
checksum = "95fded345a6559c2cfee778d562300c581f7d4ff3edb9b0d230d69800d213972"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
@ -3356,9 +3368,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.75"
version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "171ebf0ed9e1458810dfcb31f2e766ad6b3a89dbda42d8901f2b268277e5f09c"
checksum = "44468aa53335841d9d6b6c023eaab07c0cd4bddbcfdee3e2bb1e8d2cb8069fef"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -3366,9 +3378,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.75"
version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c2657dd393f03aa2a659c25c6ae18a13a4048cebd220e147933ea837efc589f"
checksum = "0195807922713af1e67dc66132c7328206ed9766af3858164fb583eedc25fbad"
dependencies = [
"proc-macro2",
"quote",
@ -3379,15 +3391,15 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.75"
version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e0c4a743a309662d45f4ede961d7afa4ba4131a59a639f29b0069c3798bbcc2"
checksum = "acdb075a845574a1fa5f09fd77e43f7747599301ea3417a9fbffdeedfc1f4a29"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.25"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce783b6c3854292723f498b7bfcf65a782a320b6f1cb3012d08dfbc603fa62f5"
checksum = "046f563713e2e7a602f0b45d49a564355f9a8a404cac3fa27b9855718f6ba97a"
dependencies = [
"console_error_panic_hook",
"js-sys",
@ -3399,9 +3411,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.25"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3859815cf8435b92f3a34381bef950daffc1403bbb77ef99e35422a7b0abb194"
checksum = "3b561e538d0e87b844dd2ce4608581373854f19c4fa2c473570d9fa3797eeb56"
dependencies = [
"proc-macro2",
"quote",
@ -3409,9 +3421,9 @@ dependencies = [
[[package]]
name = "web-sys"
version = "0.3.52"
version = "0.3.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01c70a82d842c9979078c772d4a1344685045f1a5628f677c2b2eab4dd7d2696"
checksum = "224b2f6b67919060055ef1a67807367c2066ed520c3862cc013d26cf893a783c"
dependencies = [
"js-sys",
"wasm-bindgen",
@ -3450,9 +3462,9 @@ dependencies = [
[[package]]
name = "whoami"
version = "1.1.2"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4abacf325c958dfeaf1046931d37f2a901b6dfe0968ee965a29e94c6766b2af6"
checksum = "f7741161a40200a867c96dfa5574544efa4178cf4c8f770b62dd1cc0362d7ae1"
dependencies = [
"wasm-bindgen",
"web-sys",

View file

@ -65,11 +65,11 @@ pub async fn get_config(
match res.exists {
Some(true) => {
match data.captcha.get_pow(payload.key.clone()).await {
Some(config) => {
Ok(Some(config)) => {
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
None => {
Ok(None) => {
init_mcaptcha(&data, &payload.key).await?;
let config = data
.captcha
@ -81,6 +81,7 @@ pub async fn get_config(
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
Err(e) => Err(e.into()),
}
}

View file

@ -44,6 +44,29 @@ use sqlx::PgPool;
use crate::errors::ServiceResult;
use crate::SETTINGS;
macro_rules! enum_system_actor {
($name:ident, $type:ident) => {
pub async fn $name(&self, msg: $type) -> ServiceResult<()> {
match self {
Self::Embedded(val) => val.master.send(msg).await?.await??,
Self::Redis(val) => val.master.send(msg).await?.await??,
};
Ok(())
}
};
}
macro_rules! enum_system_wrapper {
($name:ident, $type:ty, $return_type:ty) => {
pub async fn $name(&self, msg: $type) -> $return_type {
match self {
Self::Embedded(val) => val.$name(msg).await,
Self::Redis(val) => val.$name(msg).await,
}
}
};
}
/// Represents mCaptcha cache and master system.
/// When Redis is configured, [SystemGroup::Redis] is used and
/// in its absense, [SystemGroup::Embedded] is used
@ -52,60 +75,31 @@ pub enum SystemGroup {
Redis(System<RedisCache, RedisMaster>),
}
#[allow(unused_doc_comments)]
impl SystemGroup {
/// utility function to get difficulty factor of site `id` and cache it
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
match self {
Self::Embedded(val) => val.get_pow(id).await,
Self::Redis(val) => val.get_pow(id).await,
}
}
// TODO find a way to document these methods
/// utility function to verify [Work]
pub async fn verify_pow(&self, work: Work) -> CaptchaResult<String> {
match self {
Self::Embedded(val) => val.verify_pow(work).await,
Self::Redis(val) => val.verify_pow(work).await,
}
}
// utility function to get difficulty factor of site `id` and cache it
enum_system_wrapper!(get_pow, String, CaptchaResult<Option<PoWConfig>>);
/// utility function to validate verification tokens
pub async fn validate_verification_tokens(
&self,
msg: VerifyCaptchaResult,
) -> CaptchaResult<bool> {
match self {
Self::Embedded(val) => val.validate_verification_tokens(msg).await,
Self::Redis(val) => val.validate_verification_tokens(msg).await,
}
}
// utility function to verify [Work]
enum_system_wrapper!(verify_pow, Work, CaptchaResult<String>);
/// utility function to AddSite
pub async fn add_site(&self, msg: AddSite) -> ServiceResult<()> {
match self {
Self::Embedded(val) => val.master.send(msg).await?.await?,
Self::Redis(val) => val.master.send(msg).await?.await?,
}?;
Ok(())
}
// utility function to validate verification tokens
enum_system_wrapper!(
validate_verification_tokens,
VerifyCaptchaResult,
CaptchaResult<bool>
);
/// utility function to rename captcha
pub async fn rename(&self, msg: Rename) -> ServiceResult<()> {
match self {
Self::Embedded(val) => val.master.send(msg).await?.await?,
Self::Redis(val) => val.master.send(msg).await?.await?,
}?;
Ok(())
}
// utility function to AddSite
enum_system_actor!(add_site, AddSite);
/// utility function to remove captcha
pub async fn remove(&self, msg: RemoveCaptcha) -> ServiceResult<()> {
match self {
Self::Embedded(val) => val.master.send(msg).await?.await?,
Self::Redis(val) => val.master.send(msg).await?.await?,
}?;
Ok(())
}
// utility function to rename captcha
enum_system_actor!(rename, Rename);
// utility function to remove captcha
enum_system_actor!(remove, RemoveCaptcha);
fn new_system<A: Save, B: MasterTrait>(m: Addr<B>, c: Addr<A>) -> System<A, B> {
let pow = PoWConfigBuilder::default()