feat: record and fetch max recorded nonces
This commit is contained in:
parent
77e4a9c473
commit
ad4582cc16
2 changed files with 39 additions and 46 deletions
|
@ -5,6 +5,7 @@
|
|||
|
||||
//use actix::prelude::*;
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
use libmcaptcha::pow::PoWConfig;
|
||||
use libmcaptcha::{
|
||||
defense::LevelBuilder, master::messages::AddSiteBuilder, DefenseBuilder,
|
||||
MCaptchaBuilder,
|
||||
|
@ -21,7 +22,13 @@ pub struct GetConfigPayload {
|
|||
pub key: String,
|
||||
}
|
||||
|
||||
// API keys are mcaptcha actor names
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct ApiPoWConfig {
|
||||
pub string: String,
|
||||
pub difficulty_factor: u32,
|
||||
pub salt: String,
|
||||
pub max_recorded_nonce: u32,
|
||||
}
|
||||
|
||||
/// get PoW configuration for an mcaptcha key
|
||||
#[my_codegen::post(path = "V1_API_ROUTES.pow.get_config()")]
|
||||
|
@ -35,11 +42,9 @@ pub async fn get_config(
|
|||
}
|
||||
let payload = payload.into_inner();
|
||||
|
||||
let config: ServiceResult<PoWConfig> =
|
||||
match data.captcha.get_pow(payload.key.clone()).await {
|
||||
Ok(Some(config)) => {
|
||||
data.stats.record_fetch(&data, &payload.key).await?;
|
||||
Ok(HttpResponse::Ok().json(config))
|
||||
}
|
||||
Ok(Some(config)) => Ok(config),
|
||||
Ok(None) => {
|
||||
init_mcaptcha(&data, &payload.key).await?;
|
||||
let config = data
|
||||
|
@ -47,40 +52,24 @@ pub async fn get_config(
|
|||
.get_pow(payload.key.clone())
|
||||
.await
|
||||
.expect("mcaptcha should be initialized and ready to go");
|
||||
// background it. would require data::Data to be static
|
||||
// to satidfy lifetime
|
||||
data.stats.record_fetch(&data, &payload.key).await?;
|
||||
Ok(HttpResponse::Ok().json(config))
|
||||
Ok(config.unwrap())
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
};
|
||||
let config = config?;
|
||||
let max_nonce = data
|
||||
.db
|
||||
.get_max_nonce_for_level(&payload.key, config.difficulty_factor)
|
||||
.await?;
|
||||
data.stats.record_fetch(&data, &payload.key).await?;
|
||||
|
||||
// match res.exists {
|
||||
// Some(true) => {
|
||||
// match data.captcha.get_pow(payload.key.clone()).await {
|
||||
// Ok(Some(config)) => {
|
||||
// record_fetch(&payload.key, &data.db).await;
|
||||
// Ok(HttpResponse::Ok().json(config))
|
||||
// }
|
||||
// Ok(None) => {
|
||||
// init_mcaptcha(&data, &payload.key).await?;
|
||||
// let config = data
|
||||
// .captcha
|
||||
// .get_pow(payload.key.clone())
|
||||
// .await
|
||||
// .expect("mcaptcha should be initialized and ready to go");
|
||||
// // background it. would require data::Data to be static
|
||||
// // to satidfy lifetime
|
||||
// record_fetch(&payload.key, &data.db).await;
|
||||
// Ok(HttpResponse::Ok().json(config))
|
||||
// }
|
||||
// Err(e) => Err(e.into()),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Some(false) => Err(ServiceError::TokenNotFound),
|
||||
// None => Err(ServiceError::TokenNotFound),
|
||||
// }
|
||||
let config = ApiPoWConfig {
|
||||
string: config.string,
|
||||
difficulty_factor: config.difficulty_factor,
|
||||
salt: config.salt,
|
||||
max_recorded_nonce: max_nonce,
|
||||
};
|
||||
Ok(HttpResponse::Ok().json(config))
|
||||
}
|
||||
/// Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.
|
||||
///
|
||||
|
|
|
@ -65,6 +65,7 @@ pub async fn verify_pow(
|
|||
let payload = payload.into_inner();
|
||||
let worker_type = payload.worker_type.clone();
|
||||
let time = payload.time;
|
||||
let nonce = payload.nonce;
|
||||
let (res, difficulty_factor) = data.captcha.verify_pow(payload.into(), ip).await?;
|
||||
data.stats.record_solve(&data, &key).await?;
|
||||
if let (Some(time), Some(worker_type)) = (time, worker_type) {
|
||||
|
@ -75,6 +76,9 @@ pub async fn verify_pow(
|
|||
};
|
||||
data.db.analysis_save(&key, &analytics).await?;
|
||||
}
|
||||
data.db
|
||||
.update_max_nonce_for_level(&key, difficulty_factor, nonce as u32)
|
||||
.await?;
|
||||
let payload = ValidationToken { token: res };
|
||||
Ok(HttpResponse::Ok().json(payload))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue