mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 02:20:07 +08:00
refactor(subscription): decode deep links in the subscription crate
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use crate::core::notify::NoticeStatus;
|
||||
use anyhow::Result;
|
||||
use percent_encoding::percent_decode_str;
|
||||
use tauri::Url;
|
||||
use tristr::alias::String;
|
||||
|
||||
@@ -10,6 +9,7 @@ use crate::{
|
||||
utils::help,
|
||||
};
|
||||
use clash_verge_logging::{Type, logging, logging_error};
|
||||
use clash_verge_subscription::decode_subscription_link;
|
||||
|
||||
pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
|
||||
let param_str = if param.starts_with("[") && param.len() > 4 {
|
||||
@@ -26,7 +26,7 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
|
||||
let link_parsed = Url::parse(param_str)
|
||||
.map_err(|e| anyhow::anyhow!("failed to parse deep link: {e:?}, param: {masked_deep_link}"))?;
|
||||
|
||||
let Some((url, name)) = extract_subscription_info(&link_parsed) else {
|
||||
let Some(link) = decode_subscription_link(&link_parsed) else {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
@@ -35,51 +35,11 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
import_subscription(&url, name.as_ref()).await;
|
||||
let name = link.name.map(String::from);
|
||||
import_subscription(&link.url, name.as_ref()).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_subscription_info(link_parsed: &Url) -> Option<(std::string::String, Option<String>)> {
|
||||
if !matches!(link_parsed.scheme(), "clash" | "clash-verge") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = link_parsed
|
||||
.query_pairs()
|
||||
.find(|(key, _)| key == "name")
|
||||
.map(|(_, value)| value.into());
|
||||
let url = extract_subscription_url(link_parsed)?;
|
||||
Some((url, name))
|
||||
}
|
||||
|
||||
fn extract_subscription_url(link_parsed: &Url) -> Option<std::string::String> {
|
||||
let query = link_parsed.query()?;
|
||||
let prefix = "url=";
|
||||
let pos = query.find(prefix)?;
|
||||
let raw_url = query[pos + prefix.len()..].trim();
|
||||
Some(decode_subscription_url(raw_url))
|
||||
}
|
||||
|
||||
fn decode_subscription_url(raw_url: &str) -> std::string::String {
|
||||
// Avoid double-decoding nested subscription URLs; decode only when needed.
|
||||
if Url::parse(raw_url).is_ok() {
|
||||
return raw_url.to_string();
|
||||
}
|
||||
|
||||
let mut candidate = raw_url.to_string();
|
||||
for _ in 0..2 {
|
||||
let next = percent_decode_str(&candidate).decode_utf8_lossy().to_string();
|
||||
if next == candidate {
|
||||
break;
|
||||
}
|
||||
candidate = next;
|
||||
if Url::parse(&candidate).is_ok() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
candidate
|
||||
}
|
||||
|
||||
async fn import_subscription(url: &str, name: Option<&String>) {
|
||||
let had_current_profile = {
|
||||
let profiles = Config::profiles().await;
|
||||
|
||||
Reference in New Issue
Block a user