rust的安全性是很高的,建议升级程序用rust来实现
Cargo.toml如下:
[package]
name = "update_exe"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dirs-next = "2.0"
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.26", features = ["full"] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
lazy_static = "1.4.0"
config.json如下:
{
"DirCombox": [
"归档1"
],
"appInfo": {
"name": "你的软件名",
"version": "1.0.65"
},
"chart_wait_time": 40,
"userData": {
"password": "",
"username": ""
}
}
main.rs如下:
use std::collections::HashMap;
use reqwest::header::HeaderMap;
use serde_json::value::Value;
use std::sync::Mutex;
use std::fs;
#[macro_use]
extern crate lazy_static;
lazy_static! {
static ref DATA_MAP: Mutex<HashMap<String, String>> = {
let mut map:HashMap<String, String> = HashMap::new();
Mutex::new(map)
};
}
async fn post(url: &str) -> Result<HashMap<String, Value>, reqwest::Error>{
// post 请求要创建client
let client = reqwest::Client::new();
// 组装header
let mut headers = HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
// 组装要提交的数据
//let mut data = HashMap::new();
//data.insert("user", "xiaoming");
//data.insert("password", "shandan");
// 发起post请求并返回
//Ok(client.post(url).headers(headers).json(&data).send().await?.json::<HashMap<String, Value>>().await?)
Ok(client.post(url).headers(headers).send().await?.json::<HashMap<String, Value>>().await?)
}
#[tokio::main]
async fn main() {
println!("你的软件更新程序");
let url ="https://你的接口/login/systemVersion";
//println!("{}",url);
let wix_path = dirs_next::cache_dir().unwrap();
//C:\\Users\\PC\\AppData\\Local
println!("context: {:#?}", wix_path);
if let Ok(res) = post(url).await {
println!("{:#?}", res);
let zipMd5 = res.get("data").unwrap().get("zipMd5").unwrap();
DATA_MAP.lock().unwrap().insert("zipMd5".to_string(),zipMd5.to_string());
let zipUrl = res.get("data").unwrap().get("zipUrl").unwrap();
DATA_MAP.lock().unwrap().insert("zipUrl".to_string(),zipUrl.to_string());
let currVersion = res.get("data").unwrap().get("currVersion").unwrap();
DATA_MAP.lock().unwrap().insert("currVersion".to_string(),currVersion.to_string());
}
println!("{:?}",DATA_MAP.lock().unwrap());
println!("{:?}",DATA_MAP.lock().unwrap().get("zipUrl"));
println!("{:?}",DATA_MAP.lock().unwrap().get("currVersion"));
let sales_and_products = {
let file_content = fs::read_to_string(std::env::current_dir().unwrap().display().to_string()+"\\src\\config.json").expect("LogRocket: error reading file");
serde_json::from_str::<Value>(&file_content).expect("LogRocket: error serializing to JSON")
};
println!("{:?}", serde_json::to_string_pretty(&sales_and_products).expect("LogRocket: error parsing to JSON"));
}
未完待续:
上面代码80%完成了,20%大家思考来完善,还要再增加一下从远端下载升级包.zip的文件,下载后根据接口反回的版本号与本地config.json里的版本号进行匹配,如果不一样就下载zip文件加压进行掩盖。如果一样就是不需要升级。然后再打开要执行的主程序exe就可以了。欢迎私聊,帮你构建rust的升级程序。