Refactor ProxyWarden routing and settings flow

This commit is contained in:
2026-07-09 11:51:16 +03:00
parent db0c1dede9
commit 1bb795a532
18 changed files with 1018 additions and 210 deletions
+60 -99
View File
@@ -12,12 +12,14 @@ use crate::component_detection::{
proxyfier_component_from_detection, singbox_component_from_detection, DetectedProxyfier,
DetectedSingBox, ProxyfierDetectionHost, SystemProxyfierDetectionHost,
};
use crate::elevated_scripts;
use crate::models::{
ActivityEntry, ActivityLevel, ComponentId, ComponentState, ComponentStatus, LocalSingBoxConfig,
Profile, ProfileInput, ProfileItem, ProfileItemInput, ProfileItemType, Protocol, ProxyProtocol,
SubscriptionCache, SubscriptionServer, Target, TargetInput, TargetKind,
};
use crate::process::command_no_window;
use crate::safe_fs;
use crate::singbox_service::{
build_singbox_setup_status, ensure_safe_singbox_install_dir,
parse_service_command_output as parse_singbox_service_command_output, service_control_script,
@@ -718,54 +720,71 @@ pub fn select_singbox_server(
}
#[tauri::command]
pub fn ping_singbox_server(
pub async fn ping_singbox_server(
state: tauri::State<'_, CommandState>,
input: PingSingBoxServerInputDto,
) -> Result<PingServerResponse, CommandError> {
ping_singbox_server_in_storage(&state.storage(), input)
let storage = state.storage();
tauri::async_runtime::spawn_blocking(move || ping_singbox_server_in_storage(&storage, input))
.await
.map_err(background_task_error)?
}
#[tauri::command]
pub fn ping_all_singbox_servers(
pub async fn ping_all_singbox_servers(
state: tauri::State<'_, CommandState>,
) -> Result<Vec<PingServerResponse>, CommandError> {
ping_all_singbox_servers_in_storage(&state.storage())
let storage = state.storage();
tauri::async_runtime::spawn_blocking(move || ping_all_singbox_servers_in_storage(&storage))
.await
.map_err(background_task_error)?
}
#[tauri::command]
pub fn ping_proxy_target(
pub async fn ping_proxy_target(
input: PingProxyTargetInputDto,
) -> Result<ProxyTargetCheckResponse, CommandError> {
ping_proxy_target_endpoint(input)
tauri::async_runtime::spawn_blocking(move || ping_proxy_target_endpoint(input))
.await
.map_err(background_task_error)?
}
#[tauri::command]
pub fn generate_singbox_config(
pub async fn generate_singbox_config(
state: tauri::State<'_, CommandState>,
) -> Result<GenerateSingBoxConfigResponse, CommandError> {
let detected = detect_singbox_install();
let binary_path = detected
.as_ref()
.map(|detected| detected.executable_path.as_path());
generate_singbox_config_with_services(
&state.storage(),
&SingBoxAdapter::default(),
&SingBoxCommandChecker,
&SystemClock,
binary_path,
)
let storage = state.storage();
tauri::async_runtime::spawn_blocking(move || {
let detected = detect_singbox_install();
let binary_path = detected
.as_ref()
.map(|detected| detected.executable_path.as_path());
generate_singbox_config_with_services(
&storage,
&SingBoxAdapter::default(),
&SingBoxCommandChecker,
&SystemClock,
binary_path,
)
})
.await
.map_err(background_task_error)?
}
#[tauri::command]
pub fn apply_profiles(
pub async fn apply_profiles(
state: tauri::State<'_, CommandState>,
) -> Result<ApplyProfilesResponse, CommandError> {
let storage = state.storage();
let adapter = ProxiFyreAdapter::default();
let helper = DetectedProxyApplyHelper::system();
let clock = SystemClock;
tauri::async_runtime::spawn_blocking(move || {
let adapter = ProxiFyreAdapter::default();
let helper = DetectedProxyApplyHelper::system();
let clock = SystemClock;
apply_profiles_with_services(&storage, &adapter, &helper, &clock)
apply_profiles_with_services(&storage, &adapter, &helper, &clock)
})
.await
.map_err(background_task_error)?
}
#[tauri::command]
@@ -2193,11 +2212,7 @@ fn write_elevated_singbox_service_script(
config_source: Option<&Path>,
config_target: Option<&Path>,
) -> Result<PathBuf, CommandError> {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0);
let script_path = env::temp_dir().join(format!("proxywarden-singbox-service-{nonce}.ps1"));
let script_path = elevated_scripts::temp_script_path("proxywarden-singbox-service");
let script =
elevated_singbox_service_script(action, service_name, config_source, config_target);
@@ -2374,10 +2389,6 @@ fn run_elevated_singbox_package_script(
installer_args: Vec<String>,
artifact_dir: &Path,
) -> Result<(), CommandError> {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0);
fs::create_dir_all(artifact_dir).map_err(|error| {
CommandError::new(
action.error_code(),
@@ -2388,18 +2399,12 @@ fn run_elevated_singbox_package_script(
)
})?;
let installer_path = artifact_dir.join(format!(
"proxywarden-singbox-{}-{nonce}.ps1",
action.file_label()
));
let runner_path = artifact_dir.join(format!(
"proxywarden-singbox-{}-{nonce}.runner.ps1",
action.file_label()
));
let result_path = artifact_dir.join(format!(
"proxywarden-singbox-{}-{nonce}.log",
action.file_label()
));
let prefix = format!("proxywarden-singbox-{}", action.file_label());
let installer_path = elevated_scripts::artifact_path(artifact_dir, &prefix, "ps1");
let runner_path =
elevated_scripts::artifact_path(artifact_dir, &format!("{prefix}.runner"), "ps1");
let result_path =
elevated_scripts::artifact_path(artifact_dir, &format!("{prefix}.result"), "log");
write_powershell_script(&installer_path, installer_body).map_err(|error| {
CommandError::new(
@@ -2735,10 +2740,7 @@ fn resolved_app(item: &ProfileItem, warnings: &mut Vec<String>) -> ResolvedAppDt
}
fn write_generated_config(path: &Path, contents: &str) -> Result<(), CommandError> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(storage_error)?;
}
fs::write(path, contents).map_err(storage_error)
safe_fs::write_with_backup(path, contents.as_bytes()).map_err(storage_error)
}
fn open_file_or_select(path: &Path) -> Result<(), CommandError> {
@@ -3041,11 +3043,7 @@ fn write_elevated_service_script(
action: ServiceControlAction,
service_names: &[String],
) -> Result<PathBuf, CommandError> {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0);
let script_path = env::temp_dir().join(format!("proxywarden-proxifyre-service-{nonce}.ps1"));
let script_path = elevated_scripts::temp_script_path("proxywarden-proxifyre-service");
let script = elevated_service_script(action, service_names);
write_powershell_script(&script_path, &script).map_err(|error| {
@@ -3405,10 +3403,6 @@ fn run_elevated_package_script(
body: String,
artifact_dir: &Path,
) -> Result<(), CommandError> {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0);
fs::create_dir_all(artifact_dir).map_err(|error| {
CommandError::new(
action.error_code(),
@@ -3418,14 +3412,10 @@ fn run_elevated_package_script(
),
)
})?;
let script_path = artifact_dir.join(format!(
"proxywarden-proxifyre-{}-{nonce}.ps1",
action.file_label()
));
let result_path = artifact_dir.join(format!(
"proxywarden-proxifyre-{}-{nonce}.log",
action.file_label()
));
let prefix = format!("proxywarden-proxifyre-{}", action.file_label());
let script_path = elevated_scripts::artifact_path(artifact_dir, &prefix, "ps1");
let result_path =
elevated_scripts::artifact_path(artifact_dir, &format!("{prefix}.result"), "log");
let script = wrap_elevated_package_script(&body, &result_path);
write_powershell_script(&script_path, &script).map_err(|error| {
@@ -3845,46 +3835,17 @@ fn apply_to_detected_proxyfier(
return staged_apply_result(request);
};
if let Some(parent) = config_path.parent() {
fs::create_dir_all(parent).map_err(|error| {
safe_fs::write_with_backup(config_path, request.config_contents.as_bytes()).map_err(
|error| {
CommandError::new(
"proxyfier_apply_failed",
format!(
"Не удалось создать папку конфига ProxiFyre '{}': {error}",
parent.display()
),
)
})?;
}
if config_path.exists() {
let backup_path = config_path.with_file_name(format!(
"{}.bak",
config_path
.file_name()
.and_then(|value| value.to_str())
.unwrap_or("app-config.json")
));
fs::copy(config_path, backup_path).map_err(|error| {
CommandError::new(
"proxyfier_apply_failed",
format!(
"Не удалось создать backup текущего конфига ProxiFyre '{}': {error}",
"Не удалось безопасно записать конфиг ProxiFyre '{}': {error}",
config_path.display()
),
)
})?;
}
fs::write(config_path, request.config_contents).map_err(|error| {
CommandError::new(
"proxyfier_apply_failed",
format!(
"Не удалось записать конфиг ProxiFyre '{}': {error}",
config_path.display()
),
)
})?;
},
)?;
Ok(HelperApplyResult {
success: true,