mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:40:05 +08:00
chore(runtime): sync the profile refactor
This commit is contained in:
@@ -3,7 +3,7 @@ use crate::core::notify::NoticeStatus;
|
||||
use crate::{
|
||||
config::{
|
||||
prfitem::{build_merge_profile, build_script_profile},
|
||||
profiles::{load_profiles, profiles_append_item_to_safe, save_profiles},
|
||||
profiles::{append_to_profile_index, load_profiles, save_profiles},
|
||||
runtime::IRuntime,
|
||||
},
|
||||
constants::{files, timing},
|
||||
@@ -142,11 +142,11 @@ impl Config {
|
||||
|
||||
if profiles.latest_arc().get_item("Merge").is_err() {
|
||||
let merge_item = &mut build_merge_profile(Some("Merge".into()));
|
||||
profiles_append_item_to_safe(&profiles, merge_item).await?;
|
||||
append_to_profile_index(&profiles, merge_item).await?;
|
||||
}
|
||||
if profiles.latest_arc().get_item("Script").is_err() {
|
||||
let script_item = &mut build_script_profile(Some("Script".into()));
|
||||
profiles_append_item_to_safe(&profiles, script_item).await?;
|
||||
append_to_profile_index(&profiles, script_item).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ mod verge;
|
||||
|
||||
pub(crate) use self::config::Config;
|
||||
pub(crate) use self::prfitem::{
|
||||
build_remote_profile, download_subscription, prepare_remote_refresh, read_profile_item_file,
|
||||
build_remote_profile, download_subscription, prepare_remote_refresh, read_profile_item_file, subscription_proxy,
|
||||
};
|
||||
pub use self::{clash::*, encrypt::*, mixed_port::*, profiles::*, verge::*};
|
||||
pub use clash_verge_profile::{PrfExtra, PrfItem, PrfOption, PrfSelected};
|
||||
|
||||
@@ -45,35 +45,38 @@ pub(super) async fn build_profile(item: &PrfItem, file_data: Option<String>) ->
|
||||
}
|
||||
|
||||
async fn append_companion_item(item: &mut PrfItem) -> Result<String> {
|
||||
profiles::profiles_append_item_safe(item).await?;
|
||||
profiles::append_profile(item).await?;
|
||||
item.uid
|
||||
.clone()
|
||||
.ok_or_else(|| anyhow::anyhow!("appended companion item has no uid"))
|
||||
}
|
||||
|
||||
async fn ensure_companion_items(
|
||||
merge: &mut Option<String>,
|
||||
script: &mut Option<String>,
|
||||
rules: &mut Option<String>,
|
||||
proxies: &mut Option<String>,
|
||||
groups: &mut Option<String>,
|
||||
) -> Result<()> {
|
||||
if merge.is_none() {
|
||||
*merge = Some(append_companion_item(&mut build_merge_profile(None)).await?);
|
||||
async fn prepare_profile_options(source: Option<&PrfOption>) -> Result<PrfOption> {
|
||||
let mut options = PrfOption {
|
||||
update_interval: source.and_then(|options| options.update_interval),
|
||||
merge: source.and_then(|options| options.merge.clone()),
|
||||
script: source.and_then(|options| options.script.clone()),
|
||||
rules: source.and_then(|options| options.rules.clone()),
|
||||
proxies: source.and_then(|options| options.proxies.clone()),
|
||||
groups: source.and_then(|options| options.groups.clone()),
|
||||
..PrfOption::default()
|
||||
};
|
||||
if options.merge.is_none() {
|
||||
options.merge = Some(append_companion_item(&mut build_merge_profile(None)).await?);
|
||||
}
|
||||
if script.is_none() {
|
||||
*script = Some(append_companion_item(&mut build_script_profile(None)).await?);
|
||||
if options.script.is_none() {
|
||||
options.script = Some(append_companion_item(&mut build_script_profile(None)).await?);
|
||||
}
|
||||
if rules.is_none() {
|
||||
*rules = Some(append_companion_item(&mut from_rules()).await?);
|
||||
if options.rules.is_none() {
|
||||
options.rules = Some(append_companion_item(&mut from_rules()).await?);
|
||||
}
|
||||
if proxies.is_none() {
|
||||
*proxies = Some(append_companion_item(&mut from_proxies()).await?);
|
||||
if options.proxies.is_none() {
|
||||
options.proxies = Some(append_companion_item(&mut from_proxies()).await?);
|
||||
}
|
||||
if groups.is_none() {
|
||||
*groups = Some(append_companion_item(&mut from_groups()).await?);
|
||||
if options.groups.is_none() {
|
||||
options.groups = Some(append_companion_item(&mut from_groups()).await?);
|
||||
}
|
||||
Ok(())
|
||||
Ok(options)
|
||||
}
|
||||
|
||||
async fn from_local(
|
||||
@@ -84,15 +87,7 @@ async fn from_local(
|
||||
) -> Result<PrfItem> {
|
||||
let uid = help::get_uid("L").into();
|
||||
let file = format!("{uid}.yaml").into();
|
||||
let opt_ref = option.as_ref();
|
||||
let update_interval = opt_ref.and_then(|o| o.update_interval);
|
||||
let mut merge = opt_ref.and_then(|o| o.merge.clone());
|
||||
let mut script = opt_ref.and_then(|o| o.script.clone());
|
||||
let mut rules = opt_ref.and_then(|o| o.rules.clone());
|
||||
let mut proxies = opt_ref.and_then(|o| o.proxies.clone());
|
||||
let mut groups = opt_ref.and_then(|o| o.groups.clone());
|
||||
|
||||
ensure_companion_items(&mut merge, &mut script, &mut rules, &mut proxies, &mut groups).await?;
|
||||
let options = prepare_profile_options(option).await?;
|
||||
Ok(PrfItem {
|
||||
uid: Some(uid),
|
||||
itype: Some("local".into()),
|
||||
@@ -102,15 +97,7 @@ async fn from_local(
|
||||
url: None,
|
||||
selected: None,
|
||||
extra: None,
|
||||
option: Some(PrfOption {
|
||||
update_interval,
|
||||
merge,
|
||||
script,
|
||||
rules,
|
||||
proxies,
|
||||
groups,
|
||||
..PrfOption::default()
|
||||
}),
|
||||
option: Some(options),
|
||||
home: None,
|
||||
updated: Some(chrono::Local::now().timestamp() as usize),
|
||||
file_data: Some(file_data.unwrap_or_else(|| tmpl::ITEM_LOCAL.into())),
|
||||
@@ -140,7 +127,7 @@ pub(crate) async fn build_remote_profile(
|
||||
desc: Option<&String>,
|
||||
option: Option<&PrfOption>,
|
||||
) -> Result<PrfItem> {
|
||||
let mut downloaded = download_subscription(url, option).await?;
|
||||
let mut downloaded = download_subscription(url, option, subscription_proxy(option)).await?;
|
||||
let uid: String = help::get_uid("R").into();
|
||||
let file = format!("{uid}.yaml").into();
|
||||
let name = name
|
||||
@@ -172,31 +159,28 @@ pub(crate) async fn build_remote_profile(
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn download_subscription(url: &str, option: Option<&PrfOption>) -> Result<DownloadedSubscription> {
|
||||
let with_proxy = option.is_some_and(|o| o.with_proxy.unwrap_or(false));
|
||||
let self_proxy = option.is_some_and(|o| o.self_proxy.unwrap_or(false));
|
||||
let accept_invalid_certs = option.is_some_and(|o| o.danger_accept_invalid_certs.unwrap_or(false));
|
||||
let user_agent = option.and_then(|o| o.user_agent.clone());
|
||||
let timeout = option.and_then(|o| o.timeout_seconds).unwrap_or(20);
|
||||
|
||||
let proxy_type = if self_proxy {
|
||||
pub(crate) fn subscription_proxy(options: Option<&PrfOption>) -> ProxyType {
|
||||
if options.and_then(|options| options.self_proxy) == Some(true) {
|
||||
ProxyType::Localhost
|
||||
} else if with_proxy {
|
||||
} else if options.and_then(|options| options.with_proxy) == Some(true) {
|
||||
ProxyType::System
|
||||
} else {
|
||||
ProxyType::None
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn download_subscription(
|
||||
url: &str,
|
||||
option: Option<&PrfOption>,
|
||||
proxy: ProxyType,
|
||||
) -> Result<DownloadedSubscription> {
|
||||
let accept_invalid_certs = option.is_some_and(|o| o.danger_accept_invalid_certs.unwrap_or(false));
|
||||
let user_agent = option.and_then(|o| o.user_agent.clone());
|
||||
let timeout = option.and_then(|o| o.timeout_seconds).unwrap_or(20);
|
||||
let url = fix_dirty_url(url)?;
|
||||
|
||||
let resp = match NetworkManager::new()
|
||||
.get(
|
||||
url.as_str(),
|
||||
proxy_type,
|
||||
Some(timeout),
|
||||
user_agent,
|
||||
accept_invalid_certs,
|
||||
)
|
||||
.get(url.as_str(), proxy, Some(timeout), user_agent, accept_invalid_certs)
|
||||
.await
|
||||
{
|
||||
Ok(r) => r,
|
||||
@@ -232,31 +216,14 @@ pub(crate) async fn prepare_remote_refresh(
|
||||
downloaded: DownloadedSubscription,
|
||||
option: Option<&PrfOption>,
|
||||
) -> Result<RemoteProfileRefresh> {
|
||||
let mut resolved = PrfOption {
|
||||
update_interval: option
|
||||
.and_then(|o| o.update_interval)
|
||||
.or(downloaded.update_interval_minutes),
|
||||
merge: option.and_then(|o| o.merge.clone()),
|
||||
script: option.and_then(|o| o.script.clone()),
|
||||
rules: option.and_then(|o| o.rules.clone()),
|
||||
proxies: option.and_then(|o| o.proxies.clone()),
|
||||
groups: option.and_then(|o| o.groups.clone()),
|
||||
allow_auto_update: Some(allow_auto_update_enabled(option)),
|
||||
..PrfOption::default()
|
||||
};
|
||||
ensure_companion_items(
|
||||
&mut resolved.merge,
|
||||
&mut resolved.script,
|
||||
&mut resolved.rules,
|
||||
&mut resolved.proxies,
|
||||
&mut resolved.groups,
|
||||
)
|
||||
.await?;
|
||||
let mut options = prepare_profile_options(option).await?;
|
||||
options.update_interval = options.update_interval.or(downloaded.update_interval_minutes);
|
||||
options.allow_auto_update = Some(option.and_then(|options| options.allow_auto_update).unwrap_or(true));
|
||||
Ok(RemoteProfileRefresh {
|
||||
extra: downloaded.extra,
|
||||
home: downloaded.home,
|
||||
updated: chrono::Local::now().timestamp() as usize,
|
||||
option: resolved,
|
||||
option: options,
|
||||
body: downloaded.body,
|
||||
})
|
||||
}
|
||||
@@ -331,10 +298,6 @@ pub(crate) async fn read_profile_item_file(file: Option<&str>) -> Result<String>
|
||||
Ok(content.into())
|
||||
}
|
||||
|
||||
fn allow_auto_update_enabled(option: Option<&PrfOption>) -> bool {
|
||||
option.and_then(|o| o.allow_auto_update).unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Fix URLs where query parameters are incorrectly appended to the path segment
|
||||
///
|
||||
/// Incorrect Example: https://example.com/path¶m1=value1
|
||||
@@ -416,7 +379,7 @@ mod tests {
|
||||
update_interval: None,
|
||||
..option
|
||||
};
|
||||
let downloaded = download_subscription(&url, Some(&refresh_option)).await?;
|
||||
let downloaded = download_subscription(&url, Some(&refresh_option), ProxyType::None).await?;
|
||||
let refresh = prepare_remote_refresh(downloaded, Some(&refresh_option)).await?;
|
||||
server.await??;
|
||||
assert_eq!(item.itype.as_deref(), Some("remote"));
|
||||
|
||||
@@ -193,32 +193,6 @@ fn validate_profile_file(file: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates fields returned by a remote profile refresh.
|
||||
async fn apply_remote_refresh(profiles: &mut IProfiles, uid: &str, refresh: RemoteProfileRefresh) -> Result<()> {
|
||||
let _ = profiles.get_item(uid)?;
|
||||
|
||||
if let Some(each) = profiles
|
||||
.items
|
||||
.iter_mut()
|
||||
.flatten()
|
||||
.find(|item| item.uid.as_deref() == Some(uid))
|
||||
{
|
||||
each.extra = refresh.extra;
|
||||
each.updated = Some(refresh.updated);
|
||||
each.home = refresh.home;
|
||||
each.option = PrfOption::merge(each.option.as_ref(), Some(&refresh.option));
|
||||
let file = each
|
||||
.file
|
||||
.get_or_insert_with(|| format!("{}.yaml", help::get_uid("R")).into());
|
||||
let path = dirs::app_profiles_dir()?.join(file.as_str());
|
||||
fs::write(&path, refresh.body.as_bytes())
|
||||
.await
|
||||
.with_context(|| format!("failed to write to file \"{file}\""))?;
|
||||
}
|
||||
|
||||
save_profiles(profiles).await
|
||||
}
|
||||
|
||||
/// Raise intervals below `min_minutes`. `None`/`0` mean "never auto-update", left alone.
|
||||
/// Returns how many changed so the caller can skip a pointless write.
|
||||
pub(crate) fn raise_short_update_intervals(profiles: &mut IProfiles, min_minutes: u64) -> usize {
|
||||
@@ -259,9 +233,9 @@ pub(crate) async fn current_mapping(profiles: &IProfiles) -> Result<Mapping> {
|
||||
// These helpers serialize asynchronous operations against committed profile data.
|
||||
use crate::config::Config;
|
||||
|
||||
pub(crate) async fn profiles_append_item_with_filedata_safe(item: &PrfItem, file_data: Option<String>) -> Result<()> {
|
||||
pub(crate) async fn create_profile(item: &PrfItem, file_data: Option<String>) -> Result<()> {
|
||||
let item = &mut build_profile(item, file_data).await?;
|
||||
profiles_append_item_safe(item).await
|
||||
append_profile(item).await
|
||||
}
|
||||
|
||||
/// Applies `edit` to the committed profiles; commits only when the edit succeeds.
|
||||
@@ -279,12 +253,12 @@ where
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn profiles_append_item_safe(item: &mut PrfItem) -> Result<()> {
|
||||
pub(crate) async fn append_profile(item: &mut PrfItem) -> Result<()> {
|
||||
let profiles = Config::profiles().await;
|
||||
profiles_append_item_to_safe(&profiles, item).await
|
||||
append_to_profile_index(&profiles, item).await
|
||||
}
|
||||
|
||||
pub(super) async fn profiles_append_item_to_safe(profiles: &Draft<IProfiles>, item: &mut PrfItem) -> Result<()> {
|
||||
pub(super) async fn append_to_profile_index(profiles: &Draft<IProfiles>, item: &mut PrfItem) -> Result<()> {
|
||||
profiles
|
||||
.with_data_modify(|mut profiles| async move {
|
||||
append_item(&mut profiles, item).await?;
|
||||
@@ -293,7 +267,7 @@ pub(super) async fn profiles_append_item_to_safe(profiles: &Draft<IProfiles>, it
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn profiles_patch_item_safe(index: &str, item: &PrfItem) -> Result<()> {
|
||||
pub(crate) async fn patch_profile_item(index: &str, item: &PrfItem) -> Result<()> {
|
||||
with_profiles_edit(|mut profiles| async move {
|
||||
patch_item(&mut profiles, index, item).await?;
|
||||
Ok(profiles)
|
||||
@@ -301,7 +275,7 @@ pub(crate) async fn profiles_patch_item_safe(index: &str, item: &PrfItem) -> Res
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn profiles_reorder_safe(active_id: &str, over_id: &str) -> Result<()> {
|
||||
pub(crate) async fn reorder_profiles(active_id: &str, over_id: &str) -> Result<()> {
|
||||
with_profiles_edit(|mut profiles| async move {
|
||||
if profiles.reorder(active_id, over_id) {
|
||||
save_profiles(&profiles).await?;
|
||||
@@ -311,18 +285,29 @@ pub(crate) async fn profiles_reorder_safe(active_id: &str, over_id: &str) -> Res
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn profiles_save_file_safe() -> Result<()> {
|
||||
pub(crate) async fn save_committed_profiles() -> Result<()> {
|
||||
with_profiles_edit(|profiles| async move { save_profiles(&profiles).await.map(|_| profiles) }).await
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_remote_refresh_safe(index: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> {
|
||||
pub(crate) async fn save_remote_refresh(uid: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> {
|
||||
with_profiles_edit(|mut profiles| async move {
|
||||
let current = profiles.get_item(index)?;
|
||||
let profile = profiles.get_item_mut(uid)?;
|
||||
anyhow::ensure!(
|
||||
current.itype.as_deref() == Some("remote") && current.url.as_deref() == Some(url),
|
||||
profile.itype.as_deref() == Some("remote") && profile.url.as_deref() == Some(url),
|
||||
"profile source changed during refresh"
|
||||
);
|
||||
apply_remote_refresh(&mut profiles, index, refresh).await?;
|
||||
profile.extra = refresh.extra;
|
||||
profile.updated = Some(refresh.updated);
|
||||
profile.home = refresh.home;
|
||||
profile.option = PrfOption::merge(profile.option.as_ref(), Some(&refresh.option));
|
||||
let file = profile
|
||||
.file
|
||||
.get_or_insert_with(|| format!("{}.yaml", help::get_uid("R")).into());
|
||||
let path = dirs::app_profiles_dir()?.join(file.as_str());
|
||||
fs::write(&path, refresh.body.as_bytes())
|
||||
.await
|
||||
.with_context(|| format!("failed to write to file \"{file}\""))?;
|
||||
save_profiles(&profiles).await?;
|
||||
Ok(profiles)
|
||||
})
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user