Skip to content

Commit

Permalink
feat: 支持校验 mirrorhello
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed May 1, 2024
1 parent c6f1870 commit 0862466
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/entrances/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use reqwest::blocking::get;
use toml::{to_string_pretty, Value};

use crate::{
types::mirror::{MirrorHello, ServiceKeys},
types::{
mirror::{MirrorHello, ServiceKeys},
verifiable::Verifiable,
},
utils::{
fs::{ensure_dir_exist, try_recycle},
get_path_mirror,
Expand All @@ -24,6 +27,9 @@ pub fn mirror_add(url: &String, should_match_name: Option<String>) -> Result<()>
}
}

// 校验
res.verify_self(&"".to_string())?;

// 写 mirror 目录
let p = get_path_mirror()?.join(&res.name);
ensure_dir_exist(&p)?;
Expand All @@ -44,7 +50,7 @@ pub fn mirror_update(name: &String) -> Result<()> {
}

pub fn mirror_remove(name: &String) -> Result<()> {
// 获取 meta.toml 路径
// 获取目录路径
let (_, p) = read_local_mirror_meta(name)?;
// 移除目录
try_recycle(p)
Expand Down
16 changes: 16 additions & 0 deletions src/types/mirror.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::utils::mirror::filter_service_from_meta;
use anyhow::Result;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::verifiable::Verifiable;

#[derive(Debug, PartialEq, Clone)]
pub enum Locale {
ZhCn,
Expand Down Expand Up @@ -96,3 +100,15 @@ impl<'de> Deserialize<'de> for ServiceKeys {
}
}
}

impl Verifiable for MirrorHello {
fn verify_self(&self, _located: &String) -> Result<()> {
// 必须有 hello 服务
let hello_res = filter_service_from_meta(self.clone(), ServiceKeys::Hello);
if let Err(e) = hello_res {
return Err(e);
}

Ok(())
}
}
6 changes: 5 additions & 1 deletion src/utils/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use toml::from_str;

use crate::{
p2s,
types::mirror::{MirrorHello, Service, ServiceKeys},
types::{
mirror::{MirrorHello, Service, ServiceKeys},
verifiable::Verifiable,
},
utils::get_path_mirror,
};

Expand All @@ -20,6 +23,7 @@ pub fn read_local_mirror_meta(name: &String) -> Result<(MirrorHello, PathBuf)> {
let text = read_to_string(&p)?;
let meta: MirrorHello = from_str(&text)
.map_err(|e| anyhow!("Error:Invalid meta content at '{fp}' : {e}", fp = p2s!(p)))?;
meta.verify_self(&"".to_string())?;
Ok((meta, dir_path))
}

Expand Down

0 comments on commit 0862466

Please sign in to comment.