From 88ccfcd8705cf192bd73f12f1e51b77accf46f7e Mon Sep 17 00:00:00 2001 From: Jonson Petard Date: Mon, 8 Apr 2024 22:25:08 +0800 Subject: [PATCH] fix: subscription info parse issue, closing #729 --- backend/tauri/src/config/prfitem.rs | 48 ++++++++++++++++++++--------- backend/tauri/src/utils/help.rs | 18 +++++------ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/backend/tauri/src/config/prfitem.rs b/backend/tauri/src/config/prfitem.rs index 7acf7d3fe5..6262e80cdf 100644 --- a/backend/tauri/src/config/prfitem.rs +++ b/backend/tauri/src/config/prfitem.rs @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize}; use serde_yaml::Mapping; use std::fs; use sysproxy::Sysproxy; +use tracing_attributes::instrument; use super::Config; @@ -161,6 +162,7 @@ impl PrfItem { /// ## Remote type /// create a new item from url + #[instrument] pub async fn from_url( url: &str, name: Option, @@ -211,7 +213,7 @@ impl PrfItem { } let version = dirs::get_app_version(); - let version = format!("clash-verge/v{version}"); + let version = format!("clash-nyanpasu/v{version}"); builder = builder.user_agent(user_agent.unwrap_or(version)); let resp = builder.build()?.get(url).send().await?; @@ -222,25 +224,35 @@ impl PrfItem { } let header = resp.headers(); + tracing::debug!("headers: {:#?}", header); // parse the Subscription UserInfo - let extra = match header.get("Subscription-Userinfo") { + let extra = match header + .get("subscription-userinfo") + .or(header.get("Subscription-Userinfo")) + { Some(value) => { + tracing::debug!("Subscription-Userinfo: {:?}", value); let sub_info = value.to_str().unwrap_or(""); Some(PrfExtra { - upload: help::parse_str(sub_info, "upload=").unwrap_or(0), - download: help::parse_str(sub_info, "download=").unwrap_or(0), - total: help::parse_str(sub_info, "total=").unwrap_or(0), - expire: help::parse_str(sub_info, "expire=").unwrap_or(0), + upload: help::parse_str(sub_info, "upload").unwrap_or(0), + download: help::parse_str(sub_info, "download").unwrap_or(0), + total: help::parse_str(sub_info, "total").unwrap_or(0), + expire: help::parse_str(sub_info, "expire").unwrap_or(0), }) } None => None, }; // parse the Content-Disposition - let filename = match header.get("Content-Disposition") { + let filename = match header + .get("content-disposition") + .or(header.get("Content-Disposition")) + { Some(value) => { + tracing::debug!("Content-Disposition: {:?}", value); + let filename = format!("{value:?}"); let filename = filename.trim_matches('"'); match help::parse_str::(filename, "filename*") { @@ -262,14 +274,20 @@ impl PrfItem { }; // parse the profile-update-interval - let option = match header.get("profile-update-interval") { - Some(value) => match value.to_str().unwrap_or("").parse::() { - Ok(val) => Some(PrfOption { - update_interval: Some(val * 60), // hour -> min - ..PrfOption::default() - }), - Err(_) => None, - }, + let option = match header + .get("profile-update-interval") + .or(header.get("Profile-Update-Interval")) + { + Some(value) => { + tracing::debug!("profile-update-interval: {:?}", value); + match value.to_str().unwrap_or("").parse::() { + Ok(val) => Some(PrfOption { + update_interval: Some(val * 60), // hour -> min + ..PrfOption::default() + }), + Err(_) => None, + } + } None => None, }; diff --git a/backend/tauri/src/utils/help.rs b/backend/tauri/src/utils/help.rs index 5d5096c59c..0d4c317693 100644 --- a/backend/tauri/src/utils/help.rs +++ b/backend/tauri/src/utils/help.rs @@ -326,17 +326,17 @@ fn test_parse_value() { let test_1 = "upload=111; download=2222; total=3333; expire=444"; let test_2 = "attachment; filename=Clash.yaml"; - assert_eq!(parse_str::(test_1, "upload=").unwrap(), 111); - assert_eq!(parse_str::(test_1, "download=").unwrap(), 2222); - assert_eq!(parse_str::(test_1, "total=").unwrap(), 3333); - assert_eq!(parse_str::(test_1, "expire=").unwrap(), 444); + assert_eq!(parse_str::(test_1, "upload").unwrap(), 111); + assert_eq!(parse_str::(test_1, "download").unwrap(), 2222); + assert_eq!(parse_str::(test_1, "total").unwrap(), 3333); + assert_eq!(parse_str::(test_1, "expire").unwrap(), 444); assert_eq!( - parse_str::(test_2, "filename=").unwrap(), + parse_str::(test_2, "filename").unwrap(), format!("Clash.yaml") ); - assert_eq!(parse_str::(test_1, "aaa="), None); - assert_eq!(parse_str::(test_1, "upload1="), None); - assert_eq!(parse_str::(test_1, "expire1="), None); - assert_eq!(parse_str::(test_2, "attachment="), None); + assert_eq!(parse_str::(test_1, "aaa"), None); + assert_eq!(parse_str::(test_1, "upload1"), None); + assert_eq!(parse_str::(test_1, "expire1"), None); + assert_eq!(parse_str::(test_2, "attachment"), None); }