feat: use protobuf without JSON hack
This commit is contained in:
parent
337f89f25a
commit
3ad3d280d9
2 changed files with 223 additions and 41 deletions
|
@ -18,76 +18,82 @@ message Defense {
|
|||
repeated Level levels = 401;
|
||||
}
|
||||
message MCaptcha {
|
||||
uint32 visitor_threshold = 501;
|
||||
uint64 duration = 502;
|
||||
Defense defense = 503;
|
||||
}
|
||||
|
||||
message AddCaptcha {
|
||||
message AddCaptchaRequest {
|
||||
string id = 601;
|
||||
MCaptcha mcaptcha = 602;
|
||||
}
|
||||
|
||||
|
||||
message RenameCaptcha {
|
||||
message RenameCaptchaRequest {
|
||||
string name = 701;
|
||||
string rename_to = 702;
|
||||
}
|
||||
|
||||
|
||||
message CachePoW {
|
||||
message CachePowRequest {
|
||||
string string= 801;
|
||||
uint32 difficulty_factor = 802;
|
||||
uint64 duration = 803;
|
||||
string key = 804;
|
||||
}
|
||||
|
||||
message CacheResult {
|
||||
message CacheResultRequest {
|
||||
string token = 817;
|
||||
string key = 818;
|
||||
uint64 duration= 819;
|
||||
}
|
||||
|
||||
message DeleteCaptchaResult {
|
||||
message DeleteCaptchaResultRequest {
|
||||
string token = 821;
|
||||
}
|
||||
|
||||
|
||||
message DcacheRequest {
|
||||
oneof DcacheRequest {
|
||||
string AddVisitor = 1;
|
||||
AddCaptcha add_captcha = 2;
|
||||
RenameCaptcha rename_captcha = 3;
|
||||
string remove_captcha = 4;
|
||||
CachePoW cache_pow = 5;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
string delete_pow = 6;
|
||||
CacheResult cache_result = 7;
|
||||
DeleteCaptchaResult delete_captcha_result = 8;
|
||||
}
|
||||
message CaptchaID{
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message PoID{
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
|
||||
//message DcacheRequest {
|
||||
// oneof DcacheRequest {
|
||||
// string AddVisitor = 1;
|
||||
// AddCaptchaRequest add_captcha = 2;
|
||||
// RenameCaptchaRequest rename_captcha = 3;
|
||||
// string remove_captcha = 4;
|
||||
// CachePoWRequest cache_pow = 5;
|
||||
// string delete_pow = 6;
|
||||
// CacheResultRequest cache_result = 7;
|
||||
// }
|
||||
//}
|
||||
|
||||
//message AddVisitorRequest {
|
||||
// string id = 1;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
|
||||
message AddVisitorResult {
|
||||
uint64 duration = 901;
|
||||
uint32 difficulty_factor = 902;
|
||||
}
|
||||
|
||||
message OptionAddVisitorResult {
|
||||
optional AddVisitorResult result = 911;
|
||||
|
||||
optional AddVisitorResult result = 911;
|
||||
}
|
||||
|
||||
message DcacheResponse {
|
||||
oneof DcacheResponse {
|
||||
OptionAddVisitorResult option_add_visitor_result = 1;
|
||||
uint32 empty = 2;
|
||||
}
|
||||
}
|
||||
//message DcacheResponse {
|
||||
// oneof DcacheResponse {
|
||||
// OptionAddVisitorResult option_add_visitor_result = 1;
|
||||
// uint32 empty = 2;
|
||||
// }
|
||||
//}
|
||||
|
||||
message RaftRequest {
|
||||
string data = 1;
|
||||
|
@ -113,19 +119,27 @@ message Learner {
|
|||
}
|
||||
|
||||
service DcacheService {
|
||||
rpc AddCaptcha(AddCaptchaRequest) returns (RaftReply) {}
|
||||
rpc AddVisitor(CaptchaID) returns (OptionAddVisitorResult) {}
|
||||
rpc RenameCaptcha(RenameCaptchaRequest) returns (RaftReply) {}
|
||||
rpc RemoveCaptcha(CaptchaID) returns (RaftReply) {}
|
||||
rpc CachePow(CachePowRequest) returns (RaftReply) {}
|
||||
rpc CacheResult(CacheResultRequest) returns (RaftReply) {}
|
||||
|
||||
rpc AddLearner(Learner) returns (RaftReply) {}
|
||||
rpc Write(RaftRequest) returns (RaftReply) {}
|
||||
// rpc Get(GetRequest) returns (GetReply) {}
|
||||
|
||||
/// Forward a request to other
|
||||
rpc Forward(RaftRequest) returns (RaftReply) {}
|
||||
|
||||
// raft RPC
|
||||
rpc AddLearner(Learner) returns (RaftReply) {}
|
||||
rpc Write(RaftRequest) returns (RaftReply) {}
|
||||
// rpc Get(GetRequest) returns (GetReply) {}
|
||||
|
||||
rpc AppendEntries(RaftRequest) returns (RaftReply);
|
||||
rpc InstallSnapshot(RaftRequest) returns (RaftReply);
|
||||
rpc vote(RaftRequest) returns (RaftReply);
|
||||
/// Forward a request to other
|
||||
rpc Forward(RaftRequest) returns (RaftReply) {}
|
||||
|
||||
// raft RPC
|
||||
|
||||
rpc AppendEntries(RaftRequest) returns (RaftReply);
|
||||
rpc InstallSnapshot(RaftRequest) returns (RaftReply);
|
||||
rpc vote(RaftRequest) returns (RaftReply);
|
||||
}
|
||||
|
||||
//service Dcache {
|
||||
|
|
168
src/protobuf.rs
168
src/protobuf.rs
|
@ -1,5 +1,9 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use libmcaptcha::cache::messages as CacheMessages;
|
||||
use libmcaptcha::defense;
|
||||
use libmcaptcha::master::messages as MasterMessages;
|
||||
use libmcaptcha::mcaptcha;
|
||||
use openraft::BasicNode;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
@ -9,6 +13,7 @@ use dcache::dcache_service_server::DcacheService;
|
|||
use dcache::{Learner, RaftReply, RaftRequest};
|
||||
|
||||
use crate::app::DcacheApp;
|
||||
use crate::store::{DcacheRequest, DcacheResponse};
|
||||
|
||||
pub mod dcache {
|
||||
tonic::include_proto!("dcache"); // The string specified here must match the proto package name
|
||||
|
@ -41,6 +46,99 @@ impl DcacheService for MyDcacheImpl {
|
|||
Ok(Response::new(res.into()))
|
||||
}
|
||||
|
||||
async fn add_captcha(
|
||||
&self,
|
||||
request: tonic::Request<dcache::AddCaptchaRequest>,
|
||||
) -> std::result::Result<tonic::Response<RaftReply>, tonic::Status> {
|
||||
let req = request.into_inner();
|
||||
let res = self
|
||||
.app
|
||||
.raft
|
||||
.client_write(DcacheRequest::AddCaptcha(req.into()))
|
||||
.await;
|
||||
Ok(Response::new(res.into()))
|
||||
}
|
||||
|
||||
async fn add_visitor(
|
||||
&self,
|
||||
request: tonic::Request<dcache::CaptchaId>,
|
||||
) -> std::result::Result<tonic::Response<dcache::OptionAddVisitorResult>, tonic::Status> {
|
||||
let req = request.into_inner();
|
||||
let res = self
|
||||
.app
|
||||
.raft
|
||||
.client_write(DcacheRequest::AddVisitor(MasterMessages::AddVisitor(
|
||||
req.id,
|
||||
)))
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tonic::Status::new(tonic::Code::Internal, serde_json::to_string(&e).unwrap())
|
||||
})?;
|
||||
match res.data {
|
||||
DcacheResponse::AddVisitorResult(res) => {
|
||||
Ok(Response::new(dcache::OptionAddVisitorResult {
|
||||
result: res.map(|f| f.into()),
|
||||
}))
|
||||
}
|
||||
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn rename_captcha(
|
||||
&self,
|
||||
request: tonic::Request<dcache::RenameCaptchaRequest>,
|
||||
) -> std::result::Result<tonic::Response<dcache::RaftReply>, tonic::Status> {
|
||||
let req = request.into_inner();
|
||||
let res = self
|
||||
.app
|
||||
.raft
|
||||
.client_write(DcacheRequest::RenameCaptcha(req.into()))
|
||||
.await;
|
||||
Ok(Response::new(res.into()))
|
||||
}
|
||||
|
||||
async fn remove_captcha(
|
||||
&self,
|
||||
request: tonic::Request<dcache::CaptchaId>,
|
||||
) -> std::result::Result<tonic::Response<dcache::RaftReply>, tonic::Status> {
|
||||
let req = request.into_inner();
|
||||
let res = self
|
||||
.app
|
||||
.raft
|
||||
.client_write(DcacheRequest::RemoveCaptcha(MasterMessages::RemoveCaptcha(
|
||||
req.id,
|
||||
)))
|
||||
.await;
|
||||
Ok(Response::new(res.into()))
|
||||
}
|
||||
|
||||
async fn cache_pow(
|
||||
&self,
|
||||
request: tonic::Request<dcache::CachePowRequest>,
|
||||
) -> std::result::Result<tonic::Response<dcache::RaftReply>, tonic::Status> {
|
||||
let req = request.into_inner();
|
||||
let res = self
|
||||
.app
|
||||
.raft
|
||||
.client_write(DcacheRequest::CachePoW(req.into()))
|
||||
.await;
|
||||
Ok(Response::new(res.into()))
|
||||
}
|
||||
|
||||
async fn cache_result(
|
||||
&self,
|
||||
request: tonic::Request<dcache::CacheResultRequest>,
|
||||
) -> std::result::Result<tonic::Response<dcache::RaftReply>, tonic::Status> {
|
||||
let req = request.into_inner();
|
||||
let res = self
|
||||
.app
|
||||
.raft
|
||||
.client_write(DcacheRequest::CacheResult(req.into()))
|
||||
.await;
|
||||
Ok(Response::new(res.into()))
|
||||
}
|
||||
|
||||
async fn write(
|
||||
&self,
|
||||
request: tonic::Request<RaftRequest>,
|
||||
|
@ -126,3 +224,73 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<dcache::AddCaptchaRequest> for MasterMessages::AddSite {
|
||||
fn from(value: dcache::AddCaptchaRequest) -> Self {
|
||||
let req_mcaptcha = value.mcaptcha.unwrap();
|
||||
let mut defense = req_mcaptcha.defense.unwrap();
|
||||
let mut new_defense = defense::DefenseBuilder::default();
|
||||
for level in defense.levels.drain(0..) {
|
||||
new_defense
|
||||
.add_level(
|
||||
defense::LevelBuilder::default()
|
||||
.difficulty_factor(level.difficulty_factor)
|
||||
.unwrap()
|
||||
.visitor_threshold(level.visitor_threshold)
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let defense = new_defense.build().unwrap();
|
||||
let mcaptcha = mcaptcha::MCaptchaBuilder::default()
|
||||
.defense(defense)
|
||||
.duration(req_mcaptcha.duration)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
Self {
|
||||
id: value.id,
|
||||
mcaptcha,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<libmcaptcha::master::AddVisitorResult> for dcache::AddVisitorResult {
|
||||
fn from(value: libmcaptcha::master::AddVisitorResult) -> Self {
|
||||
Self {
|
||||
duration: value.duration,
|
||||
difficulty_factor: value.difficulty_factor,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<dcache::RenameCaptchaRequest> for MasterMessages::Rename {
|
||||
fn from(value: dcache::RenameCaptchaRequest) -> Self {
|
||||
Self {
|
||||
name: value.name,
|
||||
rename_to: value.rename_to,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<dcache::CachePowRequest> for CacheMessages::CachePoW {
|
||||
fn from(value: dcache::CachePowRequest) -> Self {
|
||||
Self {
|
||||
string: value.string,
|
||||
difficulty_factor: value.difficulty_factor,
|
||||
duration: value.duration,
|
||||
key: value.key,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<dcache::CacheResultRequest> for CacheMessages::CacheResult {
|
||||
fn from(value: dcache::CacheResultRequest) -> Self {
|
||||
Self {
|
||||
token: value.token,
|
||||
key: value.key,
|
||||
duration: value.duration,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue