Skip to content

Commit

Permalink
fix update self
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team committed Jul 10, 2024
1 parent 04a64ea commit b16406f
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions observer_ward/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ impl<'a> Helper<'a> {
}
pub fn update_fingerprint(&self) {
let fingerprint_path = self.config.config_dir.join("web_fingerprint_v4.json");
self.download_file_from_github(
if let Err(err) = self.download_file_from_github(
"https://0x727.github.io/FingerprintHub/web_fingerprint_v4.json",
fingerprint_path
.to_str()
.unwrap_or("web_fingerprint_v4.json"),
);
) {
error!("{}update fingerprint err: {}", Emoji("💢", ""), err);
return;
};
if let Ok(f) = std::fs::File::open(&fingerprint_path) {
match serde_json::from_reader::<File, Vec<Template>>(f) {
Ok(ts) => {
Expand All @@ -42,20 +45,29 @@ impl<'a> Helper<'a> {
}
}
}
fn download_file_from_github(&self, download_url: &str, filename: &str) {
let client = self
.config
.http_client_builder()
.build()
.unwrap_or_default();
fn download_file_from_github(
&self,
download_url: &str,
filename: &str,
) -> Result<(), std::io::Error> {
let mut client_builder = self.config.http_client_builder();
client_builder = client_builder.redirect(engine::slinger::redirect::Policy::Limit(10));
let client = client_builder.build().unwrap_or_default();
match client.get(download_url).send() {
Ok(response) => match File::create(filename) {
Ok(mut f) => {
if !response.status_code().is_success() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"NotFound",
));
}
let mut content = Cursor::new(response.body().clone().unwrap_or_default().to_vec());
std::io::copy(&mut content, &mut f).unwrap_or_default();
}
Err(err) => {
error!("{}create file: {}", Emoji("💢", ""), err);
return Err(err);
}
},
Err(err) => {
Expand All @@ -65,8 +77,10 @@ impl<'a> Helper<'a> {
download_url,
err
);
return Err(std::io::Error::new(std::io::ErrorKind::NotFound, err));
}
}
Ok(())
}
pub fn update_self(&self) {
// https://doc.rust-lang.org/reference/conditional-compilation.html
Expand All @@ -84,20 +98,29 @@ impl<'a> Helper<'a> {
};
base_url.push_str(download_name);
let save_filename = "update_".to_owned() + download_name;
self.download_file_from_github(&base_url, &save_filename);
info!(
"{} please rename the file {} => {}",
Emoji("ℹ️", ""),
save_filename,
download_name
);
match self.download_file_from_github(&base_url, &save_filename) {
Ok(_) => {
info!(
"{} please rename the file {} => {}",
Emoji("ℹ️", ""),
save_filename,
download_name
);
}
Err(err) => {
error!("{},{}", Emoji("💢", ""), err);
}
};
}
pub fn update_plugins(&self) {
let plugins_zip_path = self.config.config_dir.join("plugins.zip");
self.download_file_from_github(
if let Err(err) = self.download_file_from_github(
"https://github.com/0x727/FingerprintHub/releases/download/defaultv4/plugins.zip",
plugins_zip_path.to_str().unwrap_or("plugins.zip"),
);
) {
error!("{}{}", Emoji("💢", ""), err);
return;
};
let plugins_path = self.config.config_dir.join("plugins");
if plugins_path.exists() {
std::fs::remove_dir_all(&plugins_path).unwrap_or_default();
Expand All @@ -119,7 +142,7 @@ impl<'a> Helper<'a> {
};
}
Err(err) => {
error!("{}{:?}", Emoji("💢", ""), err);
error!("{}{}", Emoji("💢", ""), err);
warn!(
"{}Please manually unzip the plugins to the directory",
Emoji("⚠️", "")
Expand Down

0 comments on commit b16406f

Please sign in to comment.