Skip to content

Commit

Permalink
fix: subscription info parse issue, closing #729
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Apr 8, 2024
1 parent bc769ac commit 88ccfcd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
48 changes: 33 additions & 15 deletions backend/tauri/src/config/prfitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -161,6 +162,7 @@ impl PrfItem {

/// ## Remote type
/// create a new item from url
#[instrument]
pub async fn from_url(
url: &str,
name: Option<String>,
Expand Down Expand Up @@ -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?;
Expand All @@ -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::<String>(filename, "filename*") {
Expand All @@ -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::<u64>() {
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::<u64>() {
Ok(val) => Some(PrfOption {
update_interval: Some(val * 60), // hour -> min
..PrfOption::default()
}),
Err(_) => None,
}
}
None => None,
};

Expand Down
18 changes: 9 additions & 9 deletions backend/tauri/src/utils/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>(test_1, "upload=").unwrap(), 111);
assert_eq!(parse_str::<usize>(test_1, "download=").unwrap(), 2222);
assert_eq!(parse_str::<usize>(test_1, "total=").unwrap(), 3333);
assert_eq!(parse_str::<usize>(test_1, "expire=").unwrap(), 444);
assert_eq!(parse_str::<usize>(test_1, "upload").unwrap(), 111);
assert_eq!(parse_str::<usize>(test_1, "download").unwrap(), 2222);
assert_eq!(parse_str::<usize>(test_1, "total").unwrap(), 3333);
assert_eq!(parse_str::<usize>(test_1, "expire").unwrap(), 444);
assert_eq!(
parse_str::<String>(test_2, "filename=").unwrap(),
parse_str::<String>(test_2, "filename").unwrap(),
format!("Clash.yaml")
);

assert_eq!(parse_str::<usize>(test_1, "aaa="), None);
assert_eq!(parse_str::<usize>(test_1, "upload1="), None);
assert_eq!(parse_str::<usize>(test_1, "expire1="), None);
assert_eq!(parse_str::<usize>(test_2, "attachment="), None);
assert_eq!(parse_str::<usize>(test_1, "aaa"), None);
assert_eq!(parse_str::<usize>(test_1, "upload1"), None);
assert_eq!(parse_str::<usize>(test_1, "expire1"), None);
assert_eq!(parse_str::<usize>(test_2, "attachment"), None);
}

0 comments on commit 88ccfcd

Please sign in to comment.