Skip to content

Commit

Permalink
fix: set min_tls_version to TLS 1.2 for copilot requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Sep 4, 2024
1 parent dd56852 commit 457ea81
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions maa-cli/src/run/preset/copilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,7 +173,7 @@ impl TryFrom<SSSCopilotParams> for MAAValue {

let copilot_file = CopilotFile::from_uri(&params.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");
Expand Down Expand Up @@ -227,6 +229,7 @@ impl<'a> CopilotFile<'a> {
pub fn push_path_to(
self,
paths: &mut Vec<Cow<'a, Path>>,
client: &reqwest::blocking::Client,
base_dir: impl AsRef<Path>,
) -> Result<()> {
let base_dir = base_dir.as_ref();
Expand All @@ -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")?;
Expand Down Expand Up @@ -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(())
Expand All @@ -315,6 +320,13 @@ fn json_from_file(path: impl AsRef<Path>) -> Result<JsonValue> {
Ok(serde_json::from_reader(fs::File::open(path)?)?)
}

fn build_client() -> Result<reqwest::blocking::Client> {
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<Table> {
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
Expand Down Expand Up @@ -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
},
Expand All @@ -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
},
Expand All @@ -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
},
Expand All @@ -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
},
Expand Down

0 comments on commit 457ea81

Please sign in to comment.