From 457ea814e57cdadc2435a070642facc0bf6d3818 Mon Sep 17 00:00:00 2001 From: Loong <40141251+wangl-cc@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:05:40 +0100 Subject: [PATCH] fix: set min_tls_version to TLS 1.2 for copilot requests --- maa-cli/src/run/preset/copilot.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/maa-cli/src/run/preset/copilot.rs b/maa-cli/src/run/preset/copilot.rs index 4128c022..6f2177e1 100644 --- a/maa-cli/src/run/preset/copilot.rs +++ b/maa-cli/src/run/preset/copilot.rs @@ -59,10 +59,12 @@ impl super::IntoTaskConfig for CopilotParams { let base_dirs = config.resource.base_dirs(); let mut copilot_files = Vec::new(); + let client = build_client()?; + for uri in &self.uri_list { let copilot_file = CopilotFile::from_uri(uri)?; - copilot_file.push_path_to(&mut copilot_files, copilot_dir)?; + copilot_file.push_path_to(&mut copilot_files, &client, copilot_dir)?; } let is_task_list = copilot_files.len() > 1; @@ -171,7 +173,7 @@ impl TryFrom for MAAValue { let copilot_file = CopilotFile::from_uri(¶ms.uri)?; let mut paths = Vec::new(); - copilot_file.push_path_to(&mut paths, copilot_dir)?; + copilot_file.push_path_to(&mut paths, &build_client()?, copilot_dir)?; if paths.len() != 1 { bail!("SSS Copilot don't support task set"); @@ -227,6 +229,7 @@ impl<'a> CopilotFile<'a> { pub fn push_path_to( self, paths: &mut Vec>, + client: &reqwest::blocking::Client, base_dir: impl AsRef, ) -> Result<()> { let base_dir = base_dir.as_ref(); @@ -244,7 +247,9 @@ impl<'a> CopilotFile<'a> { const COPILOT_API: &str = "https://prts.maa.plus/copilot/get/"; let url = format!("{}{}", COPILOT_API, code); debug!("Cache miss, downloading copilot from {url}"); - let resp: JsonValue = reqwest::blocking::get(url) + let resp: JsonValue = client + .get(url) + .send() .context("Failed to send request")? .json() .context("Failed to parse response")?; @@ -291,7 +296,7 @@ impl<'a> CopilotFile<'a> { for id in ids { let id = id.as_i64().context("copilot_id is not an integer")?; - CopilotFile::Remote(id).push_path_to(paths, base_dir)?; + CopilotFile::Remote(id).push_path_to(paths, client, base_dir)?; } Ok(()) @@ -315,6 +320,13 @@ fn json_from_file(path: impl AsRef) -> Result { Ok(serde_json::from_reader(fs::File::open(path)?)?) } +fn build_client() -> Result { + reqwest::blocking::ClientBuilder::new() + .min_tls_version(reqwest::tls::Version::TLS_1_2) + .build() + .context("Failed to build reqwest client") +} + fn operator_table(value: &JsonValue) -> Result { let mut table = Table::new(); table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE); @@ -595,13 +607,15 @@ mod tests { serde_json::to_writer(fs::File::create(&test_file).unwrap(), &test_content).unwrap(); + let client = build_client().unwrap(); + // Remote assert_eq!( { let mut paths = Vec::new(); CopilotFile::from_uri("maa://40051") .unwrap() - .push_path_to(&mut paths, &test_root) + .push_path_to(&mut paths, &client, &test_root) .unwrap(); paths }, @@ -614,7 +628,7 @@ mod tests { let mut paths = Vec::new(); CopilotFile::from_uri("maa://23125") .unwrap() - .push_path_to(&mut paths, &test_root) + .push_path_to(&mut paths, &client, &test_root) .unwrap(); paths }, @@ -633,7 +647,7 @@ mod tests { let mut paths = Vec::new(); CopilotFile::from_uri(test_file.to_str().unwrap()) .unwrap() - .push_path_to(&mut paths, &test_root) + .push_path_to(&mut paths, &client, &test_root) .unwrap(); paths }, @@ -646,7 +660,7 @@ mod tests { let mut paths = Vec::new(); CopilotFile::from_uri("file.json") .unwrap() - .push_path_to(&mut paths, &test_root) + .push_path_to(&mut paths, &client, &test_root) .unwrap(); paths },