feat(desktop): adiciona Raven Service e corrige UAC
- Implementa Windows Service (raven-service) para operacoes privilegiadas - Comunicacao via Named Pipes sem necessidade de UAC adicional - Adiciona single-instance para evitar multiplos icones na bandeja - Corrige todos os warnings do clippy (rustdesk, lib, usb_control, agent) - Remove fallback de elevacao para evitar UAC desnecessario - USB Policy e RustDesk provisioning agora usam o servico quando disponivel 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
caa6c53b2b
commit
c4664ab1c7
16 changed files with 4209 additions and 143 deletions
|
|
@ -8,7 +8,9 @@
|
|||
"build": "tsc && vite build",
|
||||
"preview": "vite preview",
|
||||
"tauri": "node ./scripts/tauri-with-stub.mjs",
|
||||
"gen:icon": "node ./scripts/build-icon.mjs"
|
||||
"gen:icon": "node ./scripts/build-icon.mjs",
|
||||
"build:service": "cd service && cargo build --release",
|
||||
"build:all": "bun run build:service && bun run tauri build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@radix-ui/react-scroll-area": "^1.2.3",
|
||||
|
|
|
|||
1931
apps/desktop/service/Cargo.lock
generated
Normal file
1931
apps/desktop/service/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
70
apps/desktop/service/Cargo.toml
Normal file
70
apps/desktop/service/Cargo.toml
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
[package]
|
||||
name = "raven-service"
|
||||
version = "0.1.0"
|
||||
description = "Raven Windows Service - Executa operacoes privilegiadas para o Raven Desktop"
|
||||
authors = ["Esdras Renan"]
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "raven-service"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
# Windows Service
|
||||
windows-service = "0.7"
|
||||
|
||||
# Async runtime
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time", "io-util", "net", "signal"] }
|
||||
|
||||
# IPC via Named Pipes
|
||||
interprocess = { version = "2", features = ["tokio"] }
|
||||
|
||||
# Serialization
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
# Logging
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
# Windows Registry
|
||||
winreg = "0.55"
|
||||
|
||||
# Error handling
|
||||
thiserror = "1.0"
|
||||
|
||||
# HTTP client (para RustDesk)
|
||||
reqwest = { version = "0.12", features = ["json", "rustls-tls", "blocking"], default-features = false }
|
||||
|
||||
# Date/time
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
# Crypto (para RustDesk ID)
|
||||
sha2 = "0.10"
|
||||
|
||||
# UUID para request IDs
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
|
||||
# Parking lot para locks
|
||||
parking_lot = "0.12"
|
||||
|
||||
# Once cell para singletons
|
||||
once_cell = "1.19"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.58", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_System_Services",
|
||||
"Win32_System_Threading",
|
||||
"Win32_System_Pipes",
|
||||
"Win32_System_IO",
|
||||
"Win32_System_SystemServices",
|
||||
"Win32_Storage_FileSystem",
|
||||
] }
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
strip = true
|
||||
290
apps/desktop/service/src/ipc.rs
Normal file
290
apps/desktop/service/src/ipc.rs
Normal file
|
|
@ -0,0 +1,290 @@
|
|||
//! Modulo IPC - Servidor de Named Pipes
|
||||
//!
|
||||
//! Implementa comunicacao entre o Raven UI e o Raven Service
|
||||
//! usando Named Pipes do Windows com protocolo JSON-RPC simplificado.
|
||||
|
||||
use crate::{rustdesk, usb_policy};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum IpcError {
|
||||
#[error("Erro de IO: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("Erro de serializacao: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
/// Requisicao JSON-RPC simplificada
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Request {
|
||||
pub id: String,
|
||||
pub method: String,
|
||||
#[serde(default)]
|
||||
pub params: serde_json::Value,
|
||||
}
|
||||
|
||||
/// Resposta JSON-RPC simplificada
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Response {
|
||||
pub id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub result: Option<serde_json::Value>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<ErrorResponse>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ErrorResponse {
|
||||
pub code: i32,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn success(id: String, result: serde_json::Value) -> Self {
|
||||
Self {
|
||||
id,
|
||||
result: Some(result),
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(id: String, code: i32, message: String) -> Self {
|
||||
Self {
|
||||
id,
|
||||
result: None,
|
||||
error: Some(ErrorResponse { code, message }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Inicia o servidor de Named Pipes
|
||||
pub async fn run_server(pipe_name: &str) -> Result<(), IpcError> {
|
||||
info!("Iniciando servidor IPC em: {}", pipe_name);
|
||||
|
||||
loop {
|
||||
match accept_connection(pipe_name).await {
|
||||
Ok(()) => {
|
||||
debug!("Conexao processada com sucesso");
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Erro ao processar conexao: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Aceita uma conexao e processa requisicoes
|
||||
async fn accept_connection(pipe_name: &str) -> Result<(), IpcError> {
|
||||
use windows::Win32::Foundation::INVALID_HANDLE_VALUE;
|
||||
use windows::Win32::Security::{
|
||||
InitializeSecurityDescriptor, SetSecurityDescriptorDacl,
|
||||
PSECURITY_DESCRIPTOR, SECURITY_ATTRIBUTES, SECURITY_DESCRIPTOR,
|
||||
};
|
||||
use windows::Win32::Storage::FileSystem::PIPE_ACCESS_DUPLEX;
|
||||
use windows::Win32::System::Pipes::{
|
||||
ConnectNamedPipe, CreateNamedPipeW, DisconnectNamedPipe,
|
||||
PIPE_READMODE_MESSAGE, PIPE_TYPE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_WAIT,
|
||||
};
|
||||
use windows::Win32::System::SystemServices::SECURITY_DESCRIPTOR_REVISION;
|
||||
use windows::core::PCWSTR;
|
||||
|
||||
// Cria o named pipe com seguranca que permite acesso a todos os usuarios
|
||||
let pipe_name_wide: Vec<u16> = pipe_name.encode_utf16().chain(std::iter::once(0)).collect();
|
||||
|
||||
// Cria security descriptor com DACL nulo (permite acesso a todos)
|
||||
let mut sd = SECURITY_DESCRIPTOR::default();
|
||||
unsafe {
|
||||
let sd_ptr = PSECURITY_DESCRIPTOR(&mut sd as *mut _ as *mut _);
|
||||
let _ = InitializeSecurityDescriptor(sd_ptr, SECURITY_DESCRIPTOR_REVISION);
|
||||
// DACL nulo = acesso irrestrito
|
||||
let _ = SetSecurityDescriptorDacl(sd_ptr, true, None, false);
|
||||
}
|
||||
|
||||
let sa = SECURITY_ATTRIBUTES {
|
||||
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
|
||||
lpSecurityDescriptor: &mut sd as *mut _ as *mut _,
|
||||
bInheritHandle: false.into(),
|
||||
};
|
||||
|
||||
let pipe_handle = unsafe {
|
||||
CreateNamedPipeW(
|
||||
PCWSTR::from_raw(pipe_name_wide.as_ptr()),
|
||||
PIPE_ACCESS_DUPLEX,
|
||||
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
||||
PIPE_UNLIMITED_INSTANCES,
|
||||
4096, // out buffer
|
||||
4096, // in buffer
|
||||
0, // default timeout
|
||||
Some(&sa), // seguranca permissiva
|
||||
)
|
||||
};
|
||||
|
||||
// Verifica se o handle e valido
|
||||
if pipe_handle == INVALID_HANDLE_VALUE {
|
||||
return Err(IpcError::Io(std::io::Error::last_os_error()));
|
||||
}
|
||||
|
||||
// Aguarda conexao de um cliente
|
||||
info!("Aguardando conexao de cliente...");
|
||||
let connect_result = unsafe {
|
||||
ConnectNamedPipe(pipe_handle, None)
|
||||
};
|
||||
|
||||
if let Err(e) = connect_result {
|
||||
// ERROR_PIPE_CONNECTED (535) significa que o cliente ja estava conectado
|
||||
// o que e aceitavel
|
||||
let error_code = e.code().0 as u32;
|
||||
if error_code != 535 {
|
||||
warn!("Erro ao aguardar conexao: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
info!("Cliente conectado");
|
||||
|
||||
// Processa requisicoes do cliente
|
||||
let result = process_client(pipe_handle);
|
||||
|
||||
// Desconecta o cliente
|
||||
unsafe {
|
||||
let _ = DisconnectNamedPipe(pipe_handle);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Processa requisicoes de um cliente conectado
|
||||
fn process_client(pipe_handle: windows::Win32::Foundation::HANDLE) -> Result<(), IpcError> {
|
||||
use std::os::windows::io::{FromRawHandle, RawHandle};
|
||||
use std::fs::File;
|
||||
|
||||
// Cria File handle a partir do pipe
|
||||
let raw_handle = pipe_handle.0 as RawHandle;
|
||||
let file = unsafe { File::from_raw_handle(raw_handle) };
|
||||
|
||||
let reader = BufReader::new(file.try_clone()?);
|
||||
let mut writer = file;
|
||||
|
||||
// Le linhas (cada linha e uma requisicao JSON)
|
||||
for line in reader.lines() {
|
||||
let line = match line {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
if e.kind() == std::io::ErrorKind::BrokenPipe {
|
||||
info!("Cliente desconectou");
|
||||
break;
|
||||
}
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
debug!("Requisicao recebida: {}", line);
|
||||
|
||||
// Parse da requisicao
|
||||
let response = match serde_json::from_str::<Request>(&line) {
|
||||
Ok(request) => handle_request(request),
|
||||
Err(e) => Response::error(
|
||||
"unknown".to_string(),
|
||||
-32700,
|
||||
format!("Parse error: {}", e),
|
||||
),
|
||||
};
|
||||
|
||||
// Serializa e envia resposta
|
||||
let response_json = serde_json::to_string(&response)?;
|
||||
debug!("Resposta: {}", response_json);
|
||||
|
||||
writeln!(writer, "{}", response_json)?;
|
||||
writer.flush()?;
|
||||
}
|
||||
|
||||
// IMPORTANTE: Nao fechar o handle aqui, pois DisconnectNamedPipe precisa dele
|
||||
std::mem::forget(writer);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Processa uma requisicao e retorna a resposta
|
||||
fn handle_request(request: Request) -> Response {
|
||||
info!("Processando metodo: {}", request.method);
|
||||
|
||||
match request.method.as_str() {
|
||||
"health_check" => handle_health_check(request.id),
|
||||
"apply_usb_policy" => handle_apply_usb_policy(request.id, request.params),
|
||||
"get_usb_policy" => handle_get_usb_policy(request.id),
|
||||
"provision_rustdesk" => handle_provision_rustdesk(request.id, request.params),
|
||||
"get_rustdesk_status" => handle_get_rustdesk_status(request.id),
|
||||
_ => Response::error(
|
||||
request.id,
|
||||
-32601,
|
||||
format!("Metodo nao encontrado: {}", request.method),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Handlers de Requisicoes
|
||||
// =============================================================================
|
||||
|
||||
fn handle_health_check(id: String) -> Response {
|
||||
Response::success(
|
||||
id,
|
||||
serde_json::json!({
|
||||
"status": "ok",
|
||||
"service": "RavenService",
|
||||
"version": env!("CARGO_PKG_VERSION"),
|
||||
"timestamp": chrono::Utc::now().timestamp_millis()
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn handle_apply_usb_policy(id: String, params: serde_json::Value) -> Response {
|
||||
let policy = match params.get("policy").and_then(|p| p.as_str()) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
return Response::error(id, -32602, "Parametro 'policy' e obrigatorio".to_string())
|
||||
}
|
||||
};
|
||||
|
||||
match usb_policy::apply_policy(policy) {
|
||||
Ok(result) => Response::success(id, serde_json::to_value(result).unwrap()),
|
||||
Err(e) => Response::error(id, -32000, format!("Erro ao aplicar politica: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_get_usb_policy(id: String) -> Response {
|
||||
match usb_policy::get_current_policy() {
|
||||
Ok(policy) => Response::success(
|
||||
id,
|
||||
serde_json::json!({
|
||||
"policy": policy
|
||||
}),
|
||||
),
|
||||
Err(e) => Response::error(id, -32000, format!("Erro ao obter politica: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_provision_rustdesk(id: String, params: serde_json::Value) -> Response {
|
||||
let config_string = params.get("config").and_then(|c| c.as_str()).map(String::from);
|
||||
let password = params.get("password").and_then(|p| p.as_str()).map(String::from);
|
||||
let machine_id = params.get("machineId").and_then(|m| m.as_str()).map(String::from);
|
||||
|
||||
match rustdesk::ensure_rustdesk(config_string.as_deref(), password.as_deref(), machine_id.as_deref()) {
|
||||
Ok(result) => Response::success(id, serde_json::to_value(result).unwrap()),
|
||||
Err(e) => Response::error(id, -32000, format!("Erro ao provisionar RustDesk: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_get_rustdesk_status(id: String) -> Response {
|
||||
match rustdesk::get_status() {
|
||||
Ok(status) => Response::success(id, serde_json::to_value(status).unwrap()),
|
||||
Err(e) => Response::error(id, -32000, format!("Erro ao obter status: {}", e)),
|
||||
}
|
||||
}
|
||||
268
apps/desktop/service/src/main.rs
Normal file
268
apps/desktop/service/src/main.rs
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
//! Raven Service - Servico Windows para operacoes privilegiadas
|
||||
//!
|
||||
//! Este servico roda como LocalSystem e executa operacoes que requerem
|
||||
//! privilegios de administrador, como:
|
||||
//! - Aplicar politicas de USB
|
||||
//! - Provisionar e configurar RustDesk
|
||||
//! - Modificar chaves de registro em HKEY_LOCAL_MACHINE
|
||||
//!
|
||||
//! O app Raven UI comunica com este servico via Named Pipes.
|
||||
|
||||
mod ipc;
|
||||
mod rustdesk;
|
||||
mod usb_policy;
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::time::Duration;
|
||||
use tracing::{error, info};
|
||||
use windows_service::{
|
||||
define_windows_service,
|
||||
service::{
|
||||
ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
|
||||
ServiceType,
|
||||
},
|
||||
service_control_handler::{self, ServiceControlHandlerResult},
|
||||
service_dispatcher,
|
||||
};
|
||||
|
||||
const SERVICE_NAME: &str = "RavenService";
|
||||
const SERVICE_DISPLAY_NAME: &str = "Raven Desktop Service";
|
||||
const SERVICE_DESCRIPTION: &str = "Servico do Raven Desktop para operacoes privilegiadas (USB, RustDesk)";
|
||||
const PIPE_NAME: &str = r"\\.\pipe\RavenService";
|
||||
|
||||
define_windows_service!(ffi_service_main, service_main);
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Configura logging
|
||||
init_logging();
|
||||
|
||||
// Verifica argumentos de linha de comando
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
if args.len() > 1 {
|
||||
match args[1].as_str() {
|
||||
"install" => {
|
||||
install_service()?;
|
||||
return Ok(());
|
||||
}
|
||||
"uninstall" => {
|
||||
uninstall_service()?;
|
||||
return Ok(());
|
||||
}
|
||||
"run" => {
|
||||
// Modo de teste: roda sem registrar como servico
|
||||
info!("Executando em modo de teste (nao como servico)");
|
||||
run_standalone()?;
|
||||
return Ok(());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Inicia como servico Windows
|
||||
info!("Iniciando Raven Service...");
|
||||
service_dispatcher::start(SERVICE_NAME, ffi_service_main)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_logging() {
|
||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||
|
||||
// Tenta criar diretorio de logs
|
||||
let log_dir = std::env::var("PROGRAMDATA")
|
||||
.map(|p| std::path::PathBuf::from(p).join("RavenService").join("logs"))
|
||||
.unwrap_or_else(|_| std::path::PathBuf::from("C:\\ProgramData\\RavenService\\logs"));
|
||||
|
||||
let _ = std::fs::create_dir_all(&log_dir);
|
||||
|
||||
// Arquivo de log
|
||||
let log_file = log_dir.join("service.log");
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&log_file)
|
||||
.ok();
|
||||
|
||||
let filter = EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| EnvFilter::new("info"));
|
||||
|
||||
if let Some(file) = file {
|
||||
tracing_subscriber::registry()
|
||||
.with(filter)
|
||||
.with(fmt::layer().with_writer(file).with_ansi(false))
|
||||
.init();
|
||||
} else {
|
||||
tracing_subscriber::registry()
|
||||
.with(filter)
|
||||
.with(fmt::layer())
|
||||
.init();
|
||||
}
|
||||
}
|
||||
|
||||
fn service_main(arguments: Vec<OsString>) {
|
||||
if let Err(e) = run_service(arguments) {
|
||||
error!("Erro ao executar servico: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
fn run_service(_arguments: Vec<OsString>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
info!("Servico iniciando...");
|
||||
|
||||
// Canal para shutdown
|
||||
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();
|
||||
let shutdown_tx = std::sync::Arc::new(std::sync::Mutex::new(Some(shutdown_tx)));
|
||||
|
||||
// Registra handler de controle do servico
|
||||
let shutdown_tx_clone = shutdown_tx.clone();
|
||||
let status_handle = service_control_handler::register(SERVICE_NAME, move |control| {
|
||||
match control {
|
||||
ServiceControl::Stop | ServiceControl::Shutdown => {
|
||||
info!("Recebido comando de parada");
|
||||
if let Ok(mut guard) = shutdown_tx_clone.lock() {
|
||||
if let Some(tx) = guard.take() {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
}
|
||||
ServiceControlHandlerResult::NoError
|
||||
}
|
||||
ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
|
||||
_ => ServiceControlHandlerResult::NotImplemented,
|
||||
}
|
||||
})?;
|
||||
|
||||
// Atualiza status para Running
|
||||
status_handle.set_service_status(ServiceStatus {
|
||||
service_type: ServiceType::OWN_PROCESS,
|
||||
current_state: ServiceState::Running,
|
||||
controls_accepted: ServiceControlAccept::STOP | ServiceControlAccept::SHUTDOWN,
|
||||
exit_code: ServiceExitCode::Win32(0),
|
||||
checkpoint: 0,
|
||||
wait_hint: Duration::default(),
|
||||
process_id: None,
|
||||
})?;
|
||||
|
||||
info!("Servico em execucao, aguardando conexoes...");
|
||||
|
||||
// Cria runtime Tokio
|
||||
let runtime = tokio::runtime::Runtime::new()?;
|
||||
|
||||
// Executa servidor IPC
|
||||
runtime.block_on(async {
|
||||
tokio::select! {
|
||||
result = ipc::run_server(PIPE_NAME) => {
|
||||
if let Err(e) = result {
|
||||
error!("Erro no servidor IPC: {}", e);
|
||||
}
|
||||
}
|
||||
_ = async {
|
||||
let _ = shutdown_rx.await;
|
||||
} => {
|
||||
info!("Shutdown solicitado");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Atualiza status para Stopped
|
||||
status_handle.set_service_status(ServiceStatus {
|
||||
service_type: ServiceType::OWN_PROCESS,
|
||||
current_state: ServiceState::Stopped,
|
||||
controls_accepted: ServiceControlAccept::empty(),
|
||||
exit_code: ServiceExitCode::Win32(0),
|
||||
checkpoint: 0,
|
||||
wait_hint: Duration::default(),
|
||||
process_id: None,
|
||||
})?;
|
||||
|
||||
info!("Servico parado");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_standalone() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let runtime = tokio::runtime::Runtime::new()?;
|
||||
|
||||
runtime.block_on(async {
|
||||
info!("Servidor IPC iniciando em modo standalone...");
|
||||
|
||||
tokio::select! {
|
||||
result = ipc::run_server(PIPE_NAME) => {
|
||||
if let Err(e) = result {
|
||||
error!("Erro no servidor IPC: {}", e);
|
||||
}
|
||||
}
|
||||
_ = tokio::signal::ctrl_c() => {
|
||||
info!("Ctrl+C recebido, encerrando...");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn install_service() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use windows_service::{
|
||||
service::{ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType},
|
||||
service_manager::{ServiceManager, ServiceManagerAccess},
|
||||
};
|
||||
|
||||
info!("Instalando servico...");
|
||||
|
||||
let manager = ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::CREATE_SERVICE)?;
|
||||
|
||||
let exe_path = std::env::current_exe()?;
|
||||
|
||||
let service_info = ServiceInfo {
|
||||
name: OsString::from(SERVICE_NAME),
|
||||
display_name: OsString::from(SERVICE_DISPLAY_NAME),
|
||||
service_type: ServiceType::OWN_PROCESS,
|
||||
start_type: ServiceStartType::AutoStart,
|
||||
error_control: ServiceErrorControl::Normal,
|
||||
executable_path: exe_path,
|
||||
launch_arguments: vec![],
|
||||
dependencies: vec![],
|
||||
account_name: None, // LocalSystem
|
||||
account_password: None,
|
||||
};
|
||||
|
||||
let service = manager.create_service(&service_info, ServiceAccess::CHANGE_CONFIG)?;
|
||||
|
||||
// Define descricao
|
||||
service.set_description(SERVICE_DESCRIPTION)?;
|
||||
|
||||
info!("Servico instalado com sucesso: {}", SERVICE_NAME);
|
||||
println!("Servico '{}' instalado com sucesso!", SERVICE_DISPLAY_NAME);
|
||||
println!("Para iniciar: sc start {}", SERVICE_NAME);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn uninstall_service() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use windows_service::{
|
||||
service::ServiceAccess,
|
||||
service_manager::{ServiceManager, ServiceManagerAccess},
|
||||
};
|
||||
|
||||
info!("Desinstalando servico...");
|
||||
|
||||
let manager = ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::CONNECT)?;
|
||||
|
||||
let service = manager.open_service(
|
||||
SERVICE_NAME,
|
||||
ServiceAccess::STOP | ServiceAccess::DELETE | ServiceAccess::QUERY_STATUS,
|
||||
)?;
|
||||
|
||||
// Tenta parar o servico primeiro
|
||||
let status = service.query_status()?;
|
||||
if status.current_state != ServiceState::Stopped {
|
||||
info!("Parando servico...");
|
||||
let _ = service.stop();
|
||||
std::thread::sleep(Duration::from_secs(2));
|
||||
}
|
||||
|
||||
// Remove o servico
|
||||
service.delete()?;
|
||||
|
||||
info!("Servico desinstalado com sucesso");
|
||||
println!("Servico '{}' removido com sucesso!", SERVICE_DISPLAY_NAME);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
846
apps/desktop/service/src/rustdesk.rs
Normal file
846
apps/desktop/service/src/rustdesk.rs
Normal file
|
|
@ -0,0 +1,846 @@
|
|||
//! Modulo RustDesk - Provisionamento e gerenciamento do RustDesk
|
||||
//!
|
||||
//! Gerencia a instalacao, configuracao e provisionamento do RustDesk.
|
||||
//! Como o servico roda como LocalSystem, nao precisa de elevacao.
|
||||
|
||||
use chrono::Utc;
|
||||
use once_cell::sync::Lazy;
|
||||
use parking_lot::Mutex;
|
||||
use reqwest::blocking::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{self, Write};
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use thiserror::Error;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
const RELEASES_API: &str = "https://api.github.com/repos/rustdesk/rustdesk/releases/latest";
|
||||
const USER_AGENT: &str = "RavenService/1.0";
|
||||
const SERVER_HOST: &str = "rust.rever.com.br";
|
||||
const SERVER_KEY: &str = "0mxocQKmK6GvTZQYKgjrG9tlNkKOqf81gKgqwAmnZuI=";
|
||||
const DEFAULT_PASSWORD: &str = "FMQ9MA>e73r.FI<b*34Vmx_8P";
|
||||
const SERVICE_NAME: &str = "RustDesk";
|
||||
const CACHE_DIR_NAME: &str = "Rever\\RustDeskCache";
|
||||
const LOCAL_SERVICE_CONFIG: &str = r"C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config";
|
||||
const LOCAL_SYSTEM_CONFIG: &str = r"C:\Windows\System32\config\systemprofile\AppData\Roaming\RustDesk\config";
|
||||
const SECURITY_VERIFICATION_VALUE: &str = "use-permanent-password";
|
||||
const SECURITY_APPROVE_MODE_VALUE: &str = "password";
|
||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
|
||||
static PROVISION_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum RustdeskError {
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(#[from] reqwest::Error),
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
Io(#[from] io::Error),
|
||||
|
||||
#[error("Release asset nao encontrado para Windows x86_64")]
|
||||
AssetMissing,
|
||||
|
||||
#[error("Falha ao executar comando {command}: status {status:?}")]
|
||||
CommandFailed { command: String, status: Option<i32> },
|
||||
|
||||
#[error("Falha ao detectar ID do RustDesk")]
|
||||
MissingId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RustdeskResult {
|
||||
pub id: String,
|
||||
pub password: String,
|
||||
pub installed_version: Option<String>,
|
||||
pub updated: bool,
|
||||
pub last_provisioned_at: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RustdeskStatus {
|
||||
pub installed: bool,
|
||||
pub running: bool,
|
||||
pub id: Option<String>,
|
||||
pub version: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReleaseAsset {
|
||||
name: String,
|
||||
browser_download_url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReleaseResponse {
|
||||
tag_name: String,
|
||||
assets: Vec<ReleaseAsset>,
|
||||
}
|
||||
|
||||
/// Provisiona o RustDesk
|
||||
pub fn ensure_rustdesk(
|
||||
config_string: Option<&str>,
|
||||
password_override: Option<&str>,
|
||||
machine_id: Option<&str>,
|
||||
) -> Result<RustdeskResult, RustdeskError> {
|
||||
let _guard = PROVISION_MUTEX.lock();
|
||||
info!("Iniciando provisionamento do RustDesk");
|
||||
|
||||
// Prepara ACLs dos diretorios de servico
|
||||
if let Err(e) = ensure_service_profiles_writable() {
|
||||
warn!("Aviso ao preparar ACL: {}", e);
|
||||
}
|
||||
|
||||
// Le ID existente antes de qualquer limpeza
|
||||
let preserved_remote_id = read_remote_id_from_profiles();
|
||||
if let Some(ref id) = preserved_remote_id {
|
||||
info!("ID existente preservado: {}", id);
|
||||
}
|
||||
|
||||
let exe_path = detect_executable_path();
|
||||
let (installed_version, freshly_installed) = ensure_installed(&exe_path)?;
|
||||
|
||||
info!(
|
||||
"RustDesk {}: {}",
|
||||
if freshly_installed { "instalado" } else { "ja presente" },
|
||||
exe_path.display()
|
||||
);
|
||||
|
||||
// Para processos existentes
|
||||
let _ = stop_rustdesk_processes();
|
||||
|
||||
// Limpa perfis apenas se instalacao fresca
|
||||
if freshly_installed {
|
||||
let _ = purge_existing_rustdesk_profiles();
|
||||
}
|
||||
|
||||
// Aplica configuracao
|
||||
if let Some(config) = config_string.filter(|c| !c.trim().is_empty()) {
|
||||
if let Err(e) = run_with_args(&exe_path, &["--config", config]) {
|
||||
warn!("Falha ao aplicar config inline: {}", e);
|
||||
}
|
||||
} else {
|
||||
let config_path = write_config_files()?;
|
||||
if let Err(e) = apply_config(&exe_path, &config_path) {
|
||||
warn!("Falha ao aplicar config via CLI: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Define senha
|
||||
let password = password_override
|
||||
.map(|v| v.trim().to_string())
|
||||
.filter(|v| !v.is_empty())
|
||||
.unwrap_or_else(|| DEFAULT_PASSWORD.to_string());
|
||||
|
||||
if let Err(e) = set_password(&exe_path, &password) {
|
||||
warn!("Falha ao definir senha: {}", e);
|
||||
} else {
|
||||
let _ = ensure_password_files(&password);
|
||||
let _ = propagate_password_profile();
|
||||
}
|
||||
|
||||
// Define ID customizado
|
||||
let custom_id = if let Some(ref existing_id) = preserved_remote_id {
|
||||
if !freshly_installed {
|
||||
Some(existing_id.clone())
|
||||
} else {
|
||||
define_custom_id(&exe_path, machine_id)
|
||||
}
|
||||
} else {
|
||||
define_custom_id(&exe_path, machine_id)
|
||||
};
|
||||
|
||||
// Inicia servico
|
||||
if let Err(e) = ensure_service_running(&exe_path) {
|
||||
warn!("Falha ao iniciar servico: {}", e);
|
||||
}
|
||||
|
||||
// Obtem ID final
|
||||
let final_id = match query_id_with_retries(&exe_path, 5) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
read_remote_id_from_profiles()
|
||||
.or_else(|| custom_id.clone())
|
||||
.ok_or(RustdeskError::MissingId)?
|
||||
}
|
||||
};
|
||||
|
||||
// Garante ID em todos os arquivos
|
||||
ensure_remote_id_files(&final_id);
|
||||
|
||||
let version = query_version(&exe_path).ok().or(installed_version);
|
||||
let last_provisioned_at = Utc::now().timestamp_millis();
|
||||
|
||||
info!("Provisionamento concluido. ID: {}, Versao: {:?}", final_id, version);
|
||||
|
||||
Ok(RustdeskResult {
|
||||
id: final_id,
|
||||
password,
|
||||
installed_version: version,
|
||||
updated: freshly_installed,
|
||||
last_provisioned_at,
|
||||
})
|
||||
}
|
||||
|
||||
/// Retorna status do RustDesk
|
||||
pub fn get_status() -> Result<RustdeskStatus, RustdeskError> {
|
||||
let exe_path = detect_executable_path();
|
||||
let installed = exe_path.exists();
|
||||
|
||||
let running = if installed {
|
||||
query_service_state().map(|s| s == "running").unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let id = if installed {
|
||||
query_id(&exe_path).ok().or_else(read_remote_id_from_profiles)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let version = if installed {
|
||||
query_version(&exe_path).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(RustdeskStatus {
|
||||
installed,
|
||||
running,
|
||||
id,
|
||||
version,
|
||||
})
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Funcoes Auxiliares
|
||||
// =============================================================================
|
||||
|
||||
fn detect_executable_path() -> PathBuf {
|
||||
let program_files = env::var("PROGRAMFILES").unwrap_or_else(|_| "C:/Program Files".to_string());
|
||||
Path::new(&program_files).join("RustDesk").join("rustdesk.exe")
|
||||
}
|
||||
|
||||
fn ensure_installed(exe_path: &Path) -> Result<(Option<String>, bool), RustdeskError> {
|
||||
if exe_path.exists() {
|
||||
return Ok((None, false));
|
||||
}
|
||||
|
||||
let cache_root = PathBuf::from(env::var("PROGRAMDATA").unwrap_or_else(|_| "C:/ProgramData".to_string()))
|
||||
.join(CACHE_DIR_NAME);
|
||||
fs::create_dir_all(&cache_root)?;
|
||||
|
||||
let (installer_path, version_tag) = download_latest_installer(&cache_root)?;
|
||||
run_installer(&installer_path)?;
|
||||
thread::sleep(Duration::from_secs(20));
|
||||
|
||||
Ok((Some(version_tag), true))
|
||||
}
|
||||
|
||||
fn download_latest_installer(cache_root: &Path) -> Result<(PathBuf, String), RustdeskError> {
|
||||
let client = Client::builder()
|
||||
.user_agent(USER_AGENT)
|
||||
.timeout(Duration::from_secs(60))
|
||||
.build()?;
|
||||
|
||||
let release: ReleaseResponse = client.get(RELEASES_API).send()?.error_for_status()?.json()?;
|
||||
|
||||
let asset = release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|a| a.name.ends_with("x86_64.exe"))
|
||||
.ok_or(RustdeskError::AssetMissing)?;
|
||||
|
||||
let target_path = cache_root.join(&asset.name);
|
||||
if target_path.exists() {
|
||||
return Ok((target_path, release.tag_name));
|
||||
}
|
||||
|
||||
info!("Baixando RustDesk: {}", asset.name);
|
||||
let mut response = client.get(&asset.browser_download_url).send()?.error_for_status()?;
|
||||
let mut output = File::create(&target_path)?;
|
||||
response.copy_to(&mut output)?;
|
||||
|
||||
Ok((target_path, release.tag_name))
|
||||
}
|
||||
|
||||
fn run_installer(installer_path: &Path) -> Result<(), RustdeskError> {
|
||||
let status = hidden_command(installer_path)
|
||||
.arg("--silent-install")
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()?;
|
||||
|
||||
if !status.success() {
|
||||
return Err(RustdeskError::CommandFailed {
|
||||
command: format!("{} --silent-install", installer_path.display()),
|
||||
status: status.code(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn program_data_config_dir() -> PathBuf {
|
||||
PathBuf::from(env::var("PROGRAMDATA").unwrap_or_else(|_| "C:/ProgramData".to_string()))
|
||||
.join("RustDesk")
|
||||
.join("config")
|
||||
}
|
||||
|
||||
/// Retorna todos os diretorios AppData\Roaming\RustDesk\config de usuarios do sistema
|
||||
/// Como o servico roda como LocalSystem, precisamos enumerar os profiles de usuarios
|
||||
fn all_user_appdata_config_dirs() -> Vec<PathBuf> {
|
||||
let mut dirs = Vec::new();
|
||||
|
||||
// Enumera C:\Users\*\AppData\Roaming\RustDesk\config
|
||||
let users_dir = Path::new("C:\\Users");
|
||||
if let Ok(entries) = fs::read_dir(users_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
// Ignora pastas de sistema
|
||||
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
if name == "Public" || name == "Default" || name == "Default User" || name == "All Users" {
|
||||
continue;
|
||||
}
|
||||
let rustdesk_config = path.join("AppData").join("Roaming").join("RustDesk").join("config");
|
||||
// Verifica se o diretorio pai existe (usuario real)
|
||||
if path.join("AppData").join("Roaming").exists() {
|
||||
dirs.push(rustdesk_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tambem tenta o APPDATA do ambiente (pode ser util em alguns casos)
|
||||
if let Ok(appdata) = env::var("APPDATA") {
|
||||
let path = Path::new(&appdata).join("RustDesk").join("config");
|
||||
if !dirs.contains(&path) {
|
||||
dirs.push(path);
|
||||
}
|
||||
}
|
||||
|
||||
dirs
|
||||
}
|
||||
|
||||
fn service_profile_dirs() -> Vec<PathBuf> {
|
||||
vec![
|
||||
PathBuf::from(LOCAL_SERVICE_CONFIG),
|
||||
PathBuf::from(LOCAL_SYSTEM_CONFIG),
|
||||
]
|
||||
}
|
||||
|
||||
fn remote_id_directories() -> Vec<PathBuf> {
|
||||
let mut dirs = Vec::new();
|
||||
dirs.push(program_data_config_dir());
|
||||
dirs.extend(service_profile_dirs());
|
||||
dirs.extend(all_user_appdata_config_dirs());
|
||||
dirs
|
||||
}
|
||||
|
||||
fn write_config_files() -> Result<PathBuf, RustdeskError> {
|
||||
let config_contents = format!(
|
||||
r#"[options]
|
||||
key = "{key}"
|
||||
relay-server = "{host}"
|
||||
custom-rendezvous-server = "{host}"
|
||||
api-server = "https://{host}"
|
||||
verification-method = "{verification}"
|
||||
approve-mode = "{approve}"
|
||||
"#,
|
||||
host = SERVER_HOST,
|
||||
key = SERVER_KEY,
|
||||
verification = SECURITY_VERIFICATION_VALUE,
|
||||
approve = SECURITY_APPROVE_MODE_VALUE,
|
||||
);
|
||||
|
||||
let main_path = program_data_config_dir().join("RustDesk2.toml");
|
||||
write_file(&main_path, &config_contents)?;
|
||||
|
||||
for service_dir in service_profile_dirs() {
|
||||
let service_profile = service_dir.join("RustDesk2.toml");
|
||||
let _ = write_file(&service_profile, &config_contents);
|
||||
}
|
||||
|
||||
Ok(main_path)
|
||||
}
|
||||
|
||||
fn write_file(path: &Path, contents: &str) -> Result<(), io::Error> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(path)?;
|
||||
file.write_all(contents.as_bytes())
|
||||
}
|
||||
|
||||
fn apply_config(exe_path: &Path, config_path: &Path) -> Result<(), RustdeskError> {
|
||||
run_with_args(exe_path, &["--import-config", &config_path.to_string_lossy()])
|
||||
}
|
||||
|
||||
fn set_password(exe_path: &Path, secret: &str) -> Result<(), RustdeskError> {
|
||||
run_with_args(exe_path, &["--password", secret])
|
||||
}
|
||||
|
||||
fn define_custom_id(exe_path: &Path, machine_id: Option<&str>) -> Option<String> {
|
||||
let value = machine_id.and_then(|raw| {
|
||||
let trimmed = raw.trim();
|
||||
if trimmed.is_empty() { None } else { Some(trimmed) }
|
||||
})?;
|
||||
|
||||
let custom_id = derive_numeric_id(value);
|
||||
if run_with_args(exe_path, &["--set-id", &custom_id]).is_ok() {
|
||||
info!("ID deterministico definido: {}", custom_id);
|
||||
Some(custom_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn derive_numeric_id(machine_id: &str) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(machine_id.as_bytes());
|
||||
let hash = hasher.finalize();
|
||||
let mut bytes = [0u8; 8];
|
||||
bytes.copy_from_slice(&hash[..8]);
|
||||
let value = u64::from_le_bytes(bytes);
|
||||
let num = (value % 900_000_000) + 100_000_000;
|
||||
format!("{:09}", num)
|
||||
}
|
||||
|
||||
fn ensure_service_running(exe_path: &Path) -> Result<(), RustdeskError> {
|
||||
ensure_service_installed(exe_path)?;
|
||||
let _ = run_sc(&["config", SERVICE_NAME, "start=", "auto"]);
|
||||
let _ = run_sc(&["start", SERVICE_NAME]);
|
||||
remove_rustdesk_autorun_artifacts();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_service_installed(exe_path: &Path) -> Result<(), RustdeskError> {
|
||||
if run_sc(&["query", SERVICE_NAME]).is_ok() {
|
||||
return Ok(());
|
||||
}
|
||||
run_with_args(exe_path, &["--install-service"])
|
||||
}
|
||||
|
||||
fn stop_rustdesk_processes() -> Result<(), RustdeskError> {
|
||||
let _ = run_sc(&["stop", SERVICE_NAME]);
|
||||
thread::sleep(Duration::from_secs(2));
|
||||
|
||||
let status = hidden_command("taskkill")
|
||||
.args(["/F", "/T", "/IM", "rustdesk.exe"])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()?;
|
||||
|
||||
if status.success() || matches!(status.code(), Some(128)) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(RustdeskError::CommandFailed {
|
||||
command: "taskkill".into(),
|
||||
status: status.code(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn purge_existing_rustdesk_profiles() -> Result<(), String> {
|
||||
let files = [
|
||||
"RustDesk.toml",
|
||||
"RustDesk_local.toml",
|
||||
"RustDesk2.toml",
|
||||
"password",
|
||||
"passwd",
|
||||
"passwd.txt",
|
||||
];
|
||||
|
||||
for dir in remote_id_directories() {
|
||||
if !dir.exists() {
|
||||
continue;
|
||||
}
|
||||
for name in files {
|
||||
let path = dir.join(name);
|
||||
if path.exists() {
|
||||
let _ = fs::remove_file(&path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_password_files(secret: &str) -> Result<(), String> {
|
||||
for dir in remote_id_directories() {
|
||||
let password_path = dir.join("RustDesk.toml");
|
||||
let _ = write_toml_kv(&password_path, "password", secret);
|
||||
|
||||
let local_path = dir.join("RustDesk_local.toml");
|
||||
let _ = write_toml_kv(&local_path, "verification-method", SECURITY_VERIFICATION_VALUE);
|
||||
let _ = write_toml_kv(&local_path, "approve-mode", SECURITY_APPROVE_MODE_VALUE);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn propagate_password_profile() -> io::Result<bool> {
|
||||
// Encontra um diretorio de usuario que tenha arquivos de config
|
||||
let user_dirs = all_user_appdata_config_dirs();
|
||||
let src_dir = user_dirs.iter().find(|d| d.join("RustDesk.toml").exists());
|
||||
|
||||
let Some(src_dir) = src_dir else {
|
||||
// Se nenhum usuario tem config, usa ProgramData como fonte
|
||||
let pd = program_data_config_dir();
|
||||
if !pd.join("RustDesk.toml").exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
return propagate_from_dir(&pd);
|
||||
};
|
||||
|
||||
propagate_from_dir(src_dir)
|
||||
}
|
||||
|
||||
fn propagate_from_dir(src_dir: &Path) -> io::Result<bool> {
|
||||
let propagation_files = ["RustDesk.toml", "RustDesk_local.toml", "RustDesk2.toml"];
|
||||
let mut propagated = false;
|
||||
|
||||
for filename in propagation_files {
|
||||
let src_path = src_dir.join(filename);
|
||||
if !src_path.exists() {
|
||||
continue;
|
||||
}
|
||||
|
||||
for dest_root in remote_id_directories() {
|
||||
if dest_root == src_dir {
|
||||
continue; // Nao copiar para si mesmo
|
||||
}
|
||||
let target_path = dest_root.join(filename);
|
||||
if copy_overwrite(&src_path, &target_path).is_ok() {
|
||||
propagated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(propagated)
|
||||
}
|
||||
|
||||
fn ensure_remote_id_files(id: &str) {
|
||||
for dir in remote_id_directories() {
|
||||
let path = dir.join("RustDesk_local.toml");
|
||||
let _ = write_remote_id_value(&path, id);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_remote_id_value(path: &Path, id: &str) -> io::Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
let replacement = format!("remote_id = '{}'\n", id);
|
||||
if let Ok(existing) = fs::read_to_string(path) {
|
||||
let mut replaced = false;
|
||||
let mut buffer = String::with_capacity(existing.len() + replacement.len());
|
||||
for line in existing.lines() {
|
||||
if line.trim_start().starts_with("remote_id") {
|
||||
buffer.push_str(&replacement);
|
||||
replaced = true;
|
||||
} else {
|
||||
buffer.push_str(line);
|
||||
buffer.push('\n');
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
buffer.push_str(&replacement);
|
||||
}
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(path)?;
|
||||
file.write_all(buffer.as_bytes())
|
||||
} else {
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(path)?;
|
||||
file.write_all(replacement.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
fn write_toml_kv(path: &Path, key: &str, value: &str) -> io::Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
let sanitized = value.replace('\\', "\\\\").replace('"', "\\\"");
|
||||
let replacement = format!("{key} = \"{sanitized}\"\n");
|
||||
let existing = fs::read_to_string(path).unwrap_or_default();
|
||||
let mut replaced = false;
|
||||
let mut buffer = String::with_capacity(existing.len() + replacement.len());
|
||||
for line in existing.lines() {
|
||||
let trimmed = line.trim_start();
|
||||
if trimmed.starts_with(&format!("{key} ")) || trimmed.starts_with(&format!("{key}=")) {
|
||||
buffer.push_str(&replacement);
|
||||
replaced = true;
|
||||
} else {
|
||||
buffer.push_str(line);
|
||||
buffer.push('\n');
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
buffer.push_str(&replacement);
|
||||
}
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(path)?;
|
||||
file.write_all(buffer.as_bytes())
|
||||
}
|
||||
|
||||
fn read_remote_id_from_profiles() -> Option<String> {
|
||||
for dir in remote_id_directories() {
|
||||
for candidate in [dir.join("RustDesk_local.toml"), dir.join("RustDesk.toml")] {
|
||||
if let Some(id) = read_remote_id_file(&candidate) {
|
||||
if !id.is_empty() {
|
||||
return Some(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn read_remote_id_file(path: &Path) -> Option<String> {
|
||||
let content = fs::read_to_string(path).ok()?;
|
||||
for line in content.lines() {
|
||||
if let Some(value) = parse_assignment(line, "remote_id") {
|
||||
return Some(value);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn parse_assignment(line: &str, key: &str) -> Option<String> {
|
||||
let trimmed = line.trim();
|
||||
if !trimmed.starts_with(key) {
|
||||
return None;
|
||||
}
|
||||
let (_, rhs) = trimmed.split_once('=')?;
|
||||
let value = rhs.trim().trim_matches(|c| c == '\'' || c == '"');
|
||||
if value.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn query_id_with_retries(exe_path: &Path, attempts: usize) -> Result<String, RustdeskError> {
|
||||
for attempt in 0..attempts {
|
||||
match query_id(exe_path) {
|
||||
Ok(value) if !value.trim().is_empty() => return Ok(value),
|
||||
_ => {}
|
||||
}
|
||||
if attempt + 1 < attempts {
|
||||
thread::sleep(Duration::from_millis(800));
|
||||
}
|
||||
}
|
||||
Err(RustdeskError::MissingId)
|
||||
}
|
||||
|
||||
fn query_id(exe_path: &Path) -> Result<String, RustdeskError> {
|
||||
let output = hidden_command(exe_path).arg("--get-id").output()?;
|
||||
if !output.status.success() {
|
||||
return Err(RustdeskError::CommandFailed {
|
||||
command: format!("{} --get-id", exe_path.display()),
|
||||
status: output.status.code(),
|
||||
});
|
||||
}
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
if stdout.is_empty() {
|
||||
return Err(RustdeskError::MissingId);
|
||||
}
|
||||
Ok(stdout)
|
||||
}
|
||||
|
||||
fn query_version(exe_path: &Path) -> Result<String, RustdeskError> {
|
||||
let output = hidden_command(exe_path).arg("--version").output()?;
|
||||
if !output.status.success() {
|
||||
return Err(RustdeskError::CommandFailed {
|
||||
command: format!("{} --version", exe_path.display()),
|
||||
status: output.status.code(),
|
||||
});
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
|
||||
}
|
||||
|
||||
fn query_service_state() -> Option<String> {
|
||||
let output = hidden_command("sc")
|
||||
.args(["query", SERVICE_NAME])
|
||||
.output()
|
||||
.ok()?;
|
||||
|
||||
if !output.status.success() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
for line in stdout.lines() {
|
||||
let lower = line.to_lowercase();
|
||||
if lower.contains("running") {
|
||||
return Some("running".to_string());
|
||||
}
|
||||
if lower.contains("stopped") {
|
||||
return Some("stopped".to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn run_sc(args: &[&str]) -> Result<(), RustdeskError> {
|
||||
let status = hidden_command("sc")
|
||||
.args(args)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()?;
|
||||
if !status.success() {
|
||||
return Err(RustdeskError::CommandFailed {
|
||||
command: format!("sc {}", args.join(" ")),
|
||||
status: status.code(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_with_args(exe_path: &Path, args: &[&str]) -> Result<(), RustdeskError> {
|
||||
let status = hidden_command(exe_path)
|
||||
.args(args)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()?;
|
||||
if !status.success() {
|
||||
return Err(RustdeskError::CommandFailed {
|
||||
command: format!("{} {}", exe_path.display(), args.join(" ")),
|
||||
status: status.code(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_rustdesk_autorun_artifacts() {
|
||||
// Remove atalhos de inicializacao automatica
|
||||
let mut startup_paths: Vec<PathBuf> = Vec::new();
|
||||
if let Ok(appdata) = env::var("APPDATA") {
|
||||
startup_paths.push(
|
||||
Path::new(&appdata)
|
||||
.join("Microsoft\\Windows\\Start Menu\\Programs\\Startup\\RustDesk.lnk"),
|
||||
);
|
||||
}
|
||||
startup_paths.push(PathBuf::from(
|
||||
r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\RustDesk.lnk",
|
||||
));
|
||||
|
||||
for path in startup_paths {
|
||||
if path.exists() {
|
||||
let _ = fs::remove_file(&path);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove entradas de registro
|
||||
for hive in ["HKCU", "HKLM"] {
|
||||
let reg_path = format!(r"{}\Software\Microsoft\Windows\CurrentVersion\Run", hive);
|
||||
let _ = hidden_command("reg")
|
||||
.args(["delete", ®_path, "/v", "RustDesk", "/f"])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status();
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_service_profiles_writable() -> Result<(), String> {
|
||||
for dir in service_profile_dirs() {
|
||||
if !can_write_dir(&dir) {
|
||||
fix_profile_acl(&dir)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn can_write_dir(dir: &Path) -> bool {
|
||||
if fs::create_dir_all(dir).is_err() {
|
||||
return false;
|
||||
}
|
||||
let probe = dir.join(".raven_acl_probe");
|
||||
match OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(&probe)
|
||||
{
|
||||
Ok(mut file) => {
|
||||
if file.write_all(b"ok").is_err() {
|
||||
let _ = fs::remove_file(&probe);
|
||||
return false;
|
||||
}
|
||||
let _ = fs::remove_file(&probe);
|
||||
true
|
||||
}
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn fix_profile_acl(target: &Path) -> Result<(), String> {
|
||||
let target_str = target.display().to_string();
|
||||
|
||||
// Como ja estamos rodando como LocalSystem, podemos usar takeown/icacls diretamente
|
||||
let _ = hidden_command("takeown")
|
||||
.args(["/F", &target_str, "/R", "/D", "Y"])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status();
|
||||
|
||||
let status = hidden_command("icacls")
|
||||
.args([
|
||||
&target_str,
|
||||
"/grant",
|
||||
"*S-1-5-32-544:(OI)(CI)F",
|
||||
"*S-1-5-19:(OI)(CI)F",
|
||||
"*S-1-5-32-545:(OI)(CI)M",
|
||||
"/T",
|
||||
"/C",
|
||||
"/Q",
|
||||
])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.map_err(|e| format!("Erro ao executar icacls: {}", e))?;
|
||||
|
||||
if status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("icacls retornou codigo {}", status.code().unwrap_or(-1)))
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_overwrite(src: &Path, dst: &Path) -> io::Result<()> {
|
||||
if let Some(parent) = dst.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
if dst.is_dir() {
|
||||
fs::remove_dir_all(dst)?;
|
||||
} else if dst.exists() {
|
||||
fs::remove_file(dst)?;
|
||||
}
|
||||
fs::copy(src, dst)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn hidden_command(program: impl AsRef<OsStr>) -> Command {
|
||||
let mut cmd = Command::new(program);
|
||||
cmd.creation_flags(CREATE_NO_WINDOW);
|
||||
cmd
|
||||
}
|
||||
259
apps/desktop/service/src/usb_policy.rs
Normal file
259
apps/desktop/service/src/usb_policy.rs
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
//! Modulo USB Policy - Controle de dispositivos USB
|
||||
//!
|
||||
//! Implementa o controle de armazenamento USB no Windows.
|
||||
//! Como o servico roda como LocalSystem, nao precisa de elevacao.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
use thiserror::Error;
|
||||
use tracing::{error, info, warn};
|
||||
use winreg::enums::*;
|
||||
use winreg::RegKey;
|
||||
|
||||
// GUID para Removable Storage Devices (Disk)
|
||||
const REMOVABLE_STORAGE_GUID: &str = "{53f56307-b6bf-11d0-94f2-00a0c91efb8b}";
|
||||
|
||||
// Chaves de registro
|
||||
const REMOVABLE_STORAGE_PATH: &str = r"Software\Policies\Microsoft\Windows\RemovableStorageDevices";
|
||||
const USBSTOR_PATH: &str = r"SYSTEM\CurrentControlSet\Services\USBSTOR";
|
||||
const STORAGE_POLICY_PATH: &str = r"SYSTEM\CurrentControlSet\Control\StorageDevicePolicies";
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum UsbPolicy {
|
||||
Allow,
|
||||
BlockAll,
|
||||
Readonly,
|
||||
}
|
||||
|
||||
impl UsbPolicy {
|
||||
pub fn from_str(s: &str) -> Option<Self> {
|
||||
match s.to_uppercase().as_str() {
|
||||
"ALLOW" => Some(Self::Allow),
|
||||
"BLOCK_ALL" => Some(Self::BlockAll),
|
||||
"READONLY" => Some(Self::Readonly),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Allow => "ALLOW",
|
||||
Self::BlockAll => "BLOCK_ALL",
|
||||
Self::Readonly => "READONLY",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UsbPolicyResult {
|
||||
pub success: bool,
|
||||
pub policy: String,
|
||||
pub error: Option<String>,
|
||||
pub applied_at: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum UsbControlError {
|
||||
#[error("Politica USB invalida: {0}")]
|
||||
InvalidPolicy(String),
|
||||
|
||||
#[error("Erro de registro do Windows: {0}")]
|
||||
RegistryError(String),
|
||||
|
||||
#[error("Permissao negada")]
|
||||
PermissionDenied,
|
||||
|
||||
#[error("Erro de I/O: {0}")]
|
||||
Io(#[from] io::Error),
|
||||
}
|
||||
|
||||
/// Aplica uma politica de USB
|
||||
pub fn apply_policy(policy_str: &str) -> Result<UsbPolicyResult, UsbControlError> {
|
||||
let policy = UsbPolicy::from_str(policy_str)
|
||||
.ok_or_else(|| UsbControlError::InvalidPolicy(policy_str.to_string()))?;
|
||||
|
||||
let now = chrono::Utc::now().timestamp_millis();
|
||||
|
||||
info!("Aplicando politica USB: {:?}", policy);
|
||||
|
||||
// 1. Aplicar Removable Storage Policy
|
||||
apply_removable_storage_policy(policy)?;
|
||||
|
||||
// 2. Aplicar USBSTOR
|
||||
apply_usbstor_policy(policy)?;
|
||||
|
||||
// 3. Aplicar WriteProtect se necessario
|
||||
if policy == UsbPolicy::Readonly {
|
||||
apply_write_protect(true)?;
|
||||
} else {
|
||||
apply_write_protect(false)?;
|
||||
}
|
||||
|
||||
// 4. Atualizar Group Policy (opcional)
|
||||
if let Err(e) = refresh_group_policy() {
|
||||
warn!("Falha ao atualizar group policy: {}", e);
|
||||
}
|
||||
|
||||
info!("Politica USB aplicada com sucesso: {:?}", policy);
|
||||
|
||||
Ok(UsbPolicyResult {
|
||||
success: true,
|
||||
policy: policy.as_str().to_string(),
|
||||
error: None,
|
||||
applied_at: Some(now),
|
||||
})
|
||||
}
|
||||
|
||||
/// Retorna a politica USB atual
|
||||
pub fn get_current_policy() -> Result<String, UsbControlError> {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
|
||||
// Verifica Removable Storage Policy primeiro
|
||||
let full_path = format!(r"{}\{}", REMOVABLE_STORAGE_PATH, REMOVABLE_STORAGE_GUID);
|
||||
|
||||
if let Ok(key) = hklm.open_subkey_with_flags(&full_path, KEY_READ) {
|
||||
let deny_read: u32 = key.get_value("Deny_Read").unwrap_or(0);
|
||||
let deny_write: u32 = key.get_value("Deny_Write").unwrap_or(0);
|
||||
|
||||
if deny_read == 1 && deny_write == 1 {
|
||||
return Ok("BLOCK_ALL".to_string());
|
||||
}
|
||||
|
||||
if deny_read == 0 && deny_write == 1 {
|
||||
return Ok("READONLY".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// Verifica USBSTOR como fallback
|
||||
if let Ok(key) = hklm.open_subkey_with_flags(USBSTOR_PATH, KEY_READ) {
|
||||
let start: u32 = key.get_value("Start").unwrap_or(3);
|
||||
if start == 4 {
|
||||
return Ok("BLOCK_ALL".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Ok("ALLOW".to_string())
|
||||
}
|
||||
|
||||
fn apply_removable_storage_policy(policy: UsbPolicy) -> Result<(), UsbControlError> {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
let full_path = format!(r"{}\{}", REMOVABLE_STORAGE_PATH, REMOVABLE_STORAGE_GUID);
|
||||
|
||||
match policy {
|
||||
UsbPolicy::Allow => {
|
||||
// Tenta remover as restricoes, se existirem
|
||||
if let Ok(key) = hklm.open_subkey_with_flags(&full_path, KEY_ALL_ACCESS) {
|
||||
let _ = key.delete_value("Deny_Read");
|
||||
let _ = key.delete_value("Deny_Write");
|
||||
let _ = key.delete_value("Deny_Execute");
|
||||
}
|
||||
// Tenta remover a chave inteira se estiver vazia
|
||||
let _ = hklm.delete_subkey(&full_path);
|
||||
}
|
||||
UsbPolicy::BlockAll => {
|
||||
let (key, _) = hklm
|
||||
.create_subkey(&full_path)
|
||||
.map_err(map_winreg_error)?;
|
||||
|
||||
key.set_value("Deny_Read", &1u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
key.set_value("Deny_Write", &1u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
key.set_value("Deny_Execute", &1u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
}
|
||||
UsbPolicy::Readonly => {
|
||||
let (key, _) = hklm
|
||||
.create_subkey(&full_path)
|
||||
.map_err(map_winreg_error)?;
|
||||
|
||||
// Permite leitura, bloqueia escrita
|
||||
key.set_value("Deny_Read", &0u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
key.set_value("Deny_Write", &1u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
key.set_value("Deny_Execute", &0u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_usbstor_policy(policy: UsbPolicy) -> Result<(), UsbControlError> {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
|
||||
let key = hklm
|
||||
.open_subkey_with_flags(USBSTOR_PATH, KEY_ALL_ACCESS)
|
||||
.map_err(map_winreg_error)?;
|
||||
|
||||
match policy {
|
||||
UsbPolicy::Allow => {
|
||||
// Start = 3 habilita o driver
|
||||
key.set_value("Start", &3u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
}
|
||||
UsbPolicy::BlockAll => {
|
||||
// Start = 4 desabilita o driver
|
||||
key.set_value("Start", &4u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
}
|
||||
UsbPolicy::Readonly => {
|
||||
// Readonly mantem driver ativo
|
||||
key.set_value("Start", &3u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_write_protect(enable: bool) -> Result<(), UsbControlError> {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
|
||||
if enable {
|
||||
let (key, _) = hklm
|
||||
.create_subkey(STORAGE_POLICY_PATH)
|
||||
.map_err(map_winreg_error)?;
|
||||
|
||||
key.set_value("WriteProtect", &1u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
} else if let Ok(key) = hklm.open_subkey_with_flags(STORAGE_POLICY_PATH, KEY_ALL_ACCESS) {
|
||||
let _ = key.set_value("WriteProtect", &0u32);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn refresh_group_policy() -> Result<(), UsbControlError> {
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::process::Command;
|
||||
|
||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
|
||||
let output = Command::new("gpupdate")
|
||||
.args(["/target:computer", "/force"])
|
||||
.creation_flags(CREATE_NO_WINDOW)
|
||||
.output()
|
||||
.map_err(UsbControlError::Io)?;
|
||||
|
||||
if !output.status.success() {
|
||||
warn!(
|
||||
"gpupdate retornou erro: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn map_winreg_error(error: io::Error) -> UsbControlError {
|
||||
if let Some(code) = error.raw_os_error() {
|
||||
if code == 5 {
|
||||
return UsbControlError::PermissionDenied;
|
||||
}
|
||||
}
|
||||
UsbControlError::RegistryError(error.to_string())
|
||||
}
|
||||
17
apps/desktop/src-tauri/Cargo.lock
generated
17
apps/desktop/src-tauri/Cargo.lock
generated
|
|
@ -80,10 +80,12 @@ dependencies = [
|
|||
"tauri-plugin-notification",
|
||||
"tauri-plugin-opener",
|
||||
"tauri-plugin-process",
|
||||
"tauri-plugin-single-instance",
|
||||
"tauri-plugin-store",
|
||||
"tauri-plugin-updater",
|
||||
"thiserror 1.0.69",
|
||||
"tokio",
|
||||
"uuid",
|
||||
"winreg",
|
||||
]
|
||||
|
||||
|
|
@ -4748,6 +4750,21 @@ dependencies = [
|
|||
"tauri-plugin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-single-instance"
|
||||
version = "2.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd707f8c86b4e3004e2c141fa24351f1909ba40ce1b8437e30d5ed5277dd3710"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
"thiserror 2.0.17",
|
||||
"tracing",
|
||||
"windows-sys 0.60.2",
|
||||
"zbus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-store"
|
||||
version = "2.4.0"
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ tauri-plugin-updater = "2.9.0"
|
|||
tauri-plugin-process = "2.3.0"
|
||||
tauri-plugin-notification = "2"
|
||||
tauri-plugin-deep-link = "2"
|
||||
tauri-plugin-single-instance = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sysinfo = { version = "0.31", default-features = false, features = ["multithread", "network", "system", "disk"] }
|
||||
|
|
@ -41,6 +42,7 @@ hostname = "0.4"
|
|||
base64 = "0.22"
|
||||
sha2 = "0.10"
|
||||
convex = "0.10.2"
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
# SSE usa reqwest com stream, nao precisa de websocket
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
|
|
|
|||
|
|
@ -1,20 +1,97 @@
|
|||
; Hooks customizadas do instalador NSIS (Tauri)
|
||||
;
|
||||
; Objetivo: remover a marca "Nullsoft Install System" exibida no canto inferior esquerdo.
|
||||
; Objetivo:
|
||||
; - Remover a marca "Nullsoft Install System" exibida no canto inferior esquerdo
|
||||
; - Instalar o Raven Service para operacoes privilegiadas sem UAC
|
||||
;
|
||||
; Nota: o bundler do Tauri injeta estes macros no script principal do instalador.
|
||||
|
||||
BrandingText " "
|
||||
|
||||
!macro NSIS_HOOK_PREINSTALL
|
||||
; Para qualquer instancia anterior do servico antes de atualizar
|
||||
DetailPrint "Parando servicos anteriores..."
|
||||
|
||||
; Para o servico
|
||||
nsExec::ExecToLog 'sc stop RavenService'
|
||||
|
||||
; Aguarda o servico parar completamente (ate 10 segundos)
|
||||
nsExec::ExecToLog 'powershell -Command "$$i=0; while((Get-Service RavenService -ErrorAction SilentlyContinue).Status -eq \"Running\" -and $$i -lt 10){Start-Sleep 1;$$i++}"'
|
||||
|
||||
; Forca encerramento de processos remanescentes
|
||||
nsExec::ExecToLog 'taskkill /F /IM raven-service.exe'
|
||||
nsExec::ExecToLog 'taskkill /F /IM appsdesktop.exe'
|
||||
|
||||
; Aguarda liberacao dos arquivos
|
||||
Sleep 2000
|
||||
!macroend
|
||||
|
||||
!macro NSIS_HOOK_POSTINSTALL
|
||||
; =========================================================================
|
||||
; Instala e inicia o Raven Service
|
||||
; =========================================================================
|
||||
|
||||
DetailPrint "Instalando Raven Service..."
|
||||
|
||||
; O servico ja esta em $INSTDIR (copiado como resource pelo Tauri)
|
||||
; Registra o servico Windows
|
||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" install'
|
||||
Pop $0
|
||||
|
||||
${If} $0 != 0
|
||||
DetailPrint "Aviso: Falha ao registrar servico (codigo: $0)"
|
||||
; Tenta remover e reinstalar
|
||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" uninstall'
|
||||
Sleep 500
|
||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" install'
|
||||
Pop $0
|
||||
${EndIf}
|
||||
|
||||
; Inicia o servico
|
||||
DetailPrint "Iniciando Raven Service..."
|
||||
nsExec::ExecToLog 'sc start RavenService'
|
||||
Pop $0
|
||||
|
||||
${If} $0 == 0
|
||||
DetailPrint "Raven Service iniciado com sucesso!"
|
||||
${Else}
|
||||
DetailPrint "Aviso: Servico sera iniciado na proxima reinicializacao"
|
||||
${EndIf}
|
||||
|
||||
; =========================================================================
|
||||
; Verifica se RustDesk esta instalado
|
||||
; Se nao estiver, o Raven Service instalara automaticamente no primeiro uso
|
||||
; =========================================================================
|
||||
|
||||
IfFileExists "$PROGRAMFILES\RustDesk\rustdesk.exe" rustdesk_found rustdesk_not_found
|
||||
|
||||
rustdesk_not_found:
|
||||
DetailPrint "RustDesk sera instalado automaticamente pelo Raven Service."
|
||||
Goto rustdesk_done
|
||||
|
||||
rustdesk_found:
|
||||
DetailPrint "RustDesk ja esta instalado."
|
||||
|
||||
rustdesk_done:
|
||||
!macroend
|
||||
|
||||
!macro NSIS_HOOK_PREUNINSTALL
|
||||
; =========================================================================
|
||||
; Para e remove o Raven Service
|
||||
; =========================================================================
|
||||
|
||||
DetailPrint "Parando Raven Service..."
|
||||
nsExec::ExecToLog 'sc stop RavenService'
|
||||
Sleep 1000
|
||||
|
||||
DetailPrint "Removendo Raven Service..."
|
||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" uninstall'
|
||||
|
||||
; Aguarda um pouco para garantir que o servico foi removido
|
||||
Sleep 500
|
||||
!macroend
|
||||
|
||||
!macro NSIS_HOOK_POSTUNINSTALL
|
||||
; Nada adicional necessario
|
||||
!macroend
|
||||
|
||||
|
|
|
|||
|
|
@ -708,7 +708,7 @@ fn collect_windows_extended() -> serde_json::Value {
|
|||
}
|
||||
|
||||
fn decode_utf16_le_to_string(bytes: &[u8]) -> Option<String> {
|
||||
if bytes.len() % 2 != 0 {
|
||||
if !bytes.len().is_multiple_of(2) {
|
||||
return None;
|
||||
}
|
||||
let utf16: Vec<u16> = bytes
|
||||
|
|
@ -1086,7 +1086,7 @@ pub fn collect_profile() -> Result<MachineProfile, AgentError> {
|
|||
let system = collect_system();
|
||||
|
||||
let os_name = System::name()
|
||||
.or_else(|| System::long_os_version())
|
||||
.or_else(System::long_os_version)
|
||||
.unwrap_or_else(|| "desconhecido".to_string());
|
||||
let os_version = System::os_version();
|
||||
let architecture = std::env::consts::ARCH.to_string();
|
||||
|
|
@ -1146,7 +1146,7 @@ async fn post_heartbeat(
|
|||
.into_owned();
|
||||
let os = MachineOs {
|
||||
name: System::name()
|
||||
.or_else(|| System::long_os_version())
|
||||
.or_else(System::long_os_version)
|
||||
.unwrap_or_else(|| "desconhecido".to_string()),
|
||||
version: System::os_version(),
|
||||
architecture: Some(std::env::consts::ARCH.to_string()),
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ mod agent;
|
|||
mod chat;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod rustdesk;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod service_client;
|
||||
mod usb_control;
|
||||
|
||||
use agent::{collect_inventory_plain, collect_profile, AgentRuntime, MachineProfile};
|
||||
|
|
@ -68,21 +70,21 @@ pub fn log_agent(level: &str, message: &str) {
|
|||
#[macro_export]
|
||||
macro_rules! log_info {
|
||||
($($arg:tt)*) => {
|
||||
$crate::log_agent("INFO", &format!($($arg)*))
|
||||
$crate::log_agent("INFO", format!($($arg)*).as_str())
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_error {
|
||||
($($arg:tt)*) => {
|
||||
$crate::log_agent("ERROR", &format!($($arg)*))
|
||||
$crate::log_agent("ERROR", format!($($arg)*).as_str())
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_warn {
|
||||
($($arg:tt)*) => {
|
||||
$crate::log_agent("WARN", &format!($($arg)*))
|
||||
$crate::log_agent("WARN", format!($($arg)*).as_str())
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -189,6 +191,32 @@ fn run_rustdesk_ensure(
|
|||
password: Option<String>,
|
||||
machine_id: Option<String>,
|
||||
) -> Result<RustdeskProvisioningResult, String> {
|
||||
// Tenta usar o servico primeiro (sem UAC)
|
||||
if service_client::is_service_available() {
|
||||
log_info!("Usando Raven Service para provisionar RustDesk");
|
||||
match service_client::provision_rustdesk(
|
||||
config_string.as_deref(),
|
||||
password.as_deref(),
|
||||
machine_id.as_deref(),
|
||||
) {
|
||||
Ok(result) => {
|
||||
return Ok(RustdeskProvisioningResult {
|
||||
id: result.id,
|
||||
password: result.password,
|
||||
installed_version: result.installed_version,
|
||||
updated: result.updated,
|
||||
last_provisioned_at: result.last_provisioned_at,
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
log_warn!("Falha ao usar servico para RustDesk: {e}");
|
||||
// Continua para fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: chamada direta (pode pedir UAC)
|
||||
log_info!("Usando chamada direta para provisionar RustDesk (pode pedir UAC)");
|
||||
rustdesk::ensure_rustdesk(
|
||||
config_string.as_deref(),
|
||||
password.as_deref(),
|
||||
|
|
@ -208,14 +236,50 @@ fn run_rustdesk_ensure(
|
|||
|
||||
#[tauri::command]
|
||||
fn apply_usb_policy(policy: String) -> Result<UsbPolicyResult, String> {
|
||||
let policy_enum = UsbPolicy::from_str(&policy)
|
||||
// Valida a politica primeiro
|
||||
let _policy_enum = UsbPolicy::from_str(&policy)
|
||||
.ok_or_else(|| format!("Politica USB invalida: {}. Use ALLOW, BLOCK_ALL ou READONLY.", policy))?;
|
||||
|
||||
usb_control::apply_usb_policy(policy_enum).map_err(|e| e.to_string())
|
||||
// Tenta usar o servico primeiro (sem UAC)
|
||||
#[cfg(target_os = "windows")]
|
||||
if service_client::is_service_available() {
|
||||
log_info!("Usando Raven Service para aplicar politica USB: {}", policy);
|
||||
match service_client::apply_usb_policy(&policy) {
|
||||
Ok(result) => {
|
||||
return Ok(UsbPolicyResult {
|
||||
success: result.success,
|
||||
policy: result.policy,
|
||||
error: result.error,
|
||||
applied_at: result.applied_at,
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
log_warn!("Falha ao usar servico para USB policy: {e}");
|
||||
// Continua para fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: chamada direta (pode pedir UAC)
|
||||
log_info!("Usando chamada direta para aplicar politica USB (pode pedir UAC)");
|
||||
usb_control::apply_usb_policy(_policy_enum).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_usb_policy() -> Result<String, String> {
|
||||
// Tenta usar o servico primeiro
|
||||
#[cfg(target_os = "windows")]
|
||||
if service_client::is_service_available() {
|
||||
match service_client::get_usb_policy() {
|
||||
Ok(policy) => return Ok(policy),
|
||||
Err(e) => {
|
||||
log_warn!("Falha ao obter USB policy via servico: {e}");
|
||||
// Continua para fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: leitura direta (nao precisa elevacao para ler)
|
||||
usb_control::get_current_policy()
|
||||
.map(|p| p.as_str().to_string())
|
||||
.map_err(|e| e.to_string())
|
||||
|
|
@ -452,6 +516,14 @@ pub fn run() {
|
|||
.plugin(tauri_plugin_process::init())
|
||||
.plugin(tauri_plugin_notification::init())
|
||||
.plugin(tauri_plugin_deep_link::init())
|
||||
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
|
||||
// Quando uma segunda instância tenta iniciar, foca a janela existente
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
let _ = window.show();
|
||||
let _ = window.unminimize();
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
}))
|
||||
.on_window_event(|window, event| {
|
||||
if let WindowEvent::CloseRequested { api, .. } = event {
|
||||
api.prevent_close();
|
||||
|
|
@ -481,7 +553,7 @@ pub fn run() {
|
|||
{
|
||||
let start_in_background = std::env::args().any(|arg| arg == "--background");
|
||||
setup_raven_autostart();
|
||||
setup_tray(&app.handle())?;
|
||||
setup_tray(app.handle())?;
|
||||
if start_in_background {
|
||||
if let Some(win) = app.get_webview_window("main") {
|
||||
let _ = win.hide();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
#![cfg(target_os = "windows")]
|
||||
|
||||
use crate::RustdeskProvisioningResult;
|
||||
use chrono::{Local, Utc};
|
||||
use once_cell::sync::Lazy;
|
||||
|
|
@ -30,7 +28,9 @@ const LOCAL_SERVICE_CONFIG: &str = r"C:\\Windows\\ServiceProfiles\\LocalService\
|
|||
const LOCAL_SYSTEM_CONFIG: &str = r"C:\\Windows\\System32\\config\\systemprofile\\AppData\\Roaming\\RustDesk\\config";
|
||||
const APP_IDENTIFIER: &str = "br.com.esdrasrenan.sistemadechamados";
|
||||
const MACHINE_STORE_FILENAME: &str = "machine-agent.json";
|
||||
#[allow(dead_code)]
|
||||
const ACL_FLAG_FILENAME: &str = "rustdesk_acl_unlocked.flag";
|
||||
#[allow(dead_code)]
|
||||
const RUSTDESK_ACL_STORE_KEY: &str = "rustdeskAclUnlockedAt";
|
||||
const SECURITY_VERIFICATION_VALUE: &str = "use-permanent-password";
|
||||
const SECURITY_APPROVE_MODE_VALUE: &str = "password";
|
||||
|
|
@ -85,11 +85,11 @@ fn define_custom_id_from_machine(exe_path: &Path, machine_id: Option<&str>) -> O
|
|||
}) {
|
||||
match set_custom_id(exe_path, value) {
|
||||
Ok(custom) => {
|
||||
log_event(&format!("ID determinístico definido: {custom}"));
|
||||
log_event(format!("ID determinístico definido: {custom}"));
|
||||
Some(custom)
|
||||
}
|
||||
Err(error) => {
|
||||
log_event(&format!("Falha ao definir ID determinístico: {error}"));
|
||||
log_event(format!("Falha ao definir ID determinístico: {error}"));
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -107,7 +107,7 @@ pub fn ensure_rustdesk(
|
|||
log_event("Iniciando preparo do RustDesk");
|
||||
|
||||
if let Err(error) = ensure_service_profiles_writable_preflight() {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Aviso: não foi possível preparar ACL dos perfis do serviço ({error}). Continuando mesmo assim; o serviço pode não aplicar a senha."
|
||||
));
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ pub fn ensure_rustdesk(
|
|||
// Isso preserva o ID quando o Raven é reinstalado mas o RustDesk permanece
|
||||
let preserved_remote_id = read_remote_id_from_profiles();
|
||||
if let Some(ref id) = preserved_remote_id {
|
||||
log_event(&format!("ID existente preservado antes da limpeza: {}", id));
|
||||
log_event(format!("ID existente preservado antes da limpeza: {}", id));
|
||||
}
|
||||
|
||||
let exe_path = detect_executable_path();
|
||||
|
|
@ -129,7 +129,7 @@ pub fn ensure_rustdesk(
|
|||
|
||||
match stop_rustdesk_processes() {
|
||||
Ok(_) => log_event("Instâncias existentes do RustDesk encerradas"),
|
||||
Err(error) => log_event(&format!(
|
||||
Err(error) => log_event(format!(
|
||||
"Aviso: não foi possível parar completamente o RustDesk antes da reprovisionamento ({error})"
|
||||
)),
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ pub fn ensure_rustdesk(
|
|||
if freshly_installed {
|
||||
match purge_existing_rustdesk_profiles() {
|
||||
Ok(_) => log_event("Configurações antigas do RustDesk limpas (instalação fresca)"),
|
||||
Err(error) => log_event(&format!(
|
||||
Err(error) => log_event(format!(
|
||||
"Aviso: não foi possível limpar completamente os perfis existentes do RustDesk ({error})"
|
||||
)),
|
||||
}
|
||||
|
|
@ -152,19 +152,19 @@ pub fn ensure_rustdesk(
|
|||
if trimmed.is_empty() { None } else { Some(trimmed) }
|
||||
}) {
|
||||
if let Err(error) = run_with_args(&exe_path, &["--config", value]) {
|
||||
log_event(&format!("Falha ao aplicar configuração inline: {error}"));
|
||||
log_event(format!("Falha ao aplicar configuração inline: {error}"));
|
||||
} else {
|
||||
log_event("Configuração aplicada via --config");
|
||||
}
|
||||
} else {
|
||||
let config_path = write_config_files()?;
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Arquivo de configuração atualizado em {}",
|
||||
config_path.display()
|
||||
));
|
||||
|
||||
if let Err(error) = apply_config(&exe_path, &config_path) {
|
||||
log_event(&format!("Falha ao aplicar configuração via CLI: {error}"));
|
||||
log_event(format!("Falha ao aplicar configuração via CLI: {error}"));
|
||||
} else {
|
||||
log_event("Configuração aplicada via CLI");
|
||||
}
|
||||
|
|
@ -176,7 +176,7 @@ pub fn ensure_rustdesk(
|
|||
.unwrap_or_else(|| DEFAULT_PASSWORD.to_string());
|
||||
|
||||
if let Err(error) = set_password(&exe_path, &password) {
|
||||
log_event(&format!("Falha ao definir senha padrão: {error}"));
|
||||
log_event(format!("Falha ao definir senha padrão: {error}"));
|
||||
} else {
|
||||
log_event("Senha padrão definida com sucesso");
|
||||
log_event("Aplicando senha nos perfis do RustDesk");
|
||||
|
|
@ -185,21 +185,21 @@ pub fn ensure_rustdesk(
|
|||
log_event("Senha e flags de segurança gravadas em todos os perfis do RustDesk");
|
||||
log_password_replication(&password);
|
||||
}
|
||||
Err(error) => log_event(&format!("Falha ao persistir senha nos perfis: {error}")),
|
||||
Err(error) => log_event(format!("Falha ao persistir senha nos perfis: {error}")),
|
||||
}
|
||||
|
||||
match propagate_password_profile() {
|
||||
Ok(_) => log_event("Perfil base propagado para ProgramData e perfis de serviço"),
|
||||
Err(error) => log_event(&format!("Falha ao copiar perfil de senha: {error}")),
|
||||
Err(error) => log_event(format!("Falha ao copiar perfil de senha: {error}")),
|
||||
}
|
||||
|
||||
match replicate_password_artifacts() {
|
||||
Ok(_) => log_event("Artefatos de senha replicados para o serviço do RustDesk"),
|
||||
Err(error) => log_event(&format!("Falha ao replicar artefatos de senha: {error}")),
|
||||
Err(error) => log_event(format!("Falha ao replicar artefatos de senha: {error}")),
|
||||
}
|
||||
|
||||
if let Err(error) = enforce_security_flags() {
|
||||
log_event(&format!("Falha ao reforçar configuração de senha permanente: {error}"));
|
||||
log_event(format!("Falha ao reforçar configuração de senha permanente: {error}"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -207,7 +207,7 @@ pub fn ensure_rustdesk(
|
|||
// Isso garante que reinstalar o Raven nao muda o ID do RustDesk
|
||||
let custom_id = if let Some(ref existing_id) = preserved_remote_id {
|
||||
if !freshly_installed {
|
||||
log_event(&format!("Reutilizando ID existente do RustDesk: {}", existing_id));
|
||||
log_event(format!("Reutilizando ID existente do RustDesk: {}", existing_id));
|
||||
Some(existing_id.clone())
|
||||
} else {
|
||||
// Instalacao fresca - define novo ID baseado no machine_id
|
||||
|
|
@ -219,7 +219,7 @@ pub fn ensure_rustdesk(
|
|||
};
|
||||
|
||||
if let Err(error) = ensure_service_running(&exe_path) {
|
||||
log_event(&format!("Falha ao reiniciar serviço do RustDesk: {error}"));
|
||||
log_event(format!("Falha ao reiniciar serviço do RustDesk: {error}"));
|
||||
} else {
|
||||
log_event("Serviço RustDesk reiniciado/run ativo");
|
||||
}
|
||||
|
|
@ -227,10 +227,10 @@ pub fn ensure_rustdesk(
|
|||
let reported_id = match query_id_with_retries(&exe_path, 5) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
log_event(&format!("Falha ao obter ID após múltiplas tentativas: {error}"));
|
||||
log_event(format!("Falha ao obter ID após múltiplas tentativas: {error}"));
|
||||
match read_remote_id_from_profiles().or_else(|| custom_id.clone()) {
|
||||
Some(value) => {
|
||||
log_event(&format!("ID obtido via arquivos de perfil: {value}"));
|
||||
log_event(format!("ID obtido via arquivos de perfil: {value}"));
|
||||
value
|
||||
}
|
||||
None => return Err(error),
|
||||
|
|
@ -242,7 +242,7 @@ pub fn ensure_rustdesk(
|
|||
|
||||
if let Some(expected) = custom_id.as_ref() {
|
||||
if expected != &reported_id {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"ID retornado difere do determinístico ({expected}) -> reaplicando ID determinístico"
|
||||
));
|
||||
|
||||
|
|
@ -252,25 +252,25 @@ pub fn ensure_rustdesk(
|
|||
Ok(_) => match query_id_with_retries(&exe_path, 3) {
|
||||
Ok(rechecked) => {
|
||||
if &rechecked == expected {
|
||||
log_event(&format!("ID determinístico aplicado com sucesso: {rechecked}"));
|
||||
log_event(format!("ID determinístico aplicado com sucesso: {rechecked}"));
|
||||
final_id = rechecked;
|
||||
enforced = true;
|
||||
} else {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"ID ainda difere após reaplicação (esperado {expected}, reportado {rechecked}); usando ID reportado"
|
||||
));
|
||||
final_id = rechecked;
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao consultar ID após reaplicação: {error}; usando ID reportado ({reported_id})"
|
||||
));
|
||||
final_id = reported_id.clone();
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao reaplicar ID determinístico ({expected}): {error}; usando ID reportado ({reported_id})"
|
||||
));
|
||||
final_id = reported_id.clone();
|
||||
|
|
@ -308,7 +308,7 @@ pub fn ensure_rustdesk(
|
|||
"lastError": serde_json::Value::Null
|
||||
});
|
||||
if let Err(error) = upsert_machine_store_value("rustdesk", rustdesk_data) {
|
||||
log_event(&format!("Aviso: falha ao salvar dados do RustDesk no store: {error}"));
|
||||
log_event(format!("Aviso: falha ao salvar dados do RustDesk no store: {error}"));
|
||||
} else {
|
||||
log_event("Dados do RustDesk salvos no machine-agent.json");
|
||||
}
|
||||
|
|
@ -316,7 +316,7 @@ pub fn ensure_rustdesk(
|
|||
// Sincroniza com o backend imediatamente apos provisionar
|
||||
// O Rust faz o HTTP direto, sem passar pelo CSP do webview
|
||||
if let Err(error) = sync_remote_access_with_backend(&result) {
|
||||
log_event(&format!("Aviso: falha ao sincronizar com backend: {error}"));
|
||||
log_event(format!("Aviso: falha ao sincronizar com backend: {error}"));
|
||||
} else {
|
||||
log_event("Acesso remoto sincronizado com backend");
|
||||
// Atualiza lastSyncedAt no store
|
||||
|
|
@ -330,13 +330,13 @@ pub fn ensure_rustdesk(
|
|||
"lastError": serde_json::Value::Null
|
||||
});
|
||||
if let Err(e) = upsert_machine_store_value("rustdesk", synced_data) {
|
||||
log_event(&format!("Aviso: falha ao atualizar lastSyncedAt: {e}"));
|
||||
log_event(format!("Aviso: falha ao atualizar lastSyncedAt: {e}"));
|
||||
} else {
|
||||
log_event("lastSyncedAt atualizado com sucesso");
|
||||
}
|
||||
}
|
||||
|
||||
log_event(&format!("Provisionamento concluído. ID final: {final_id}. Versão: {:?}", version));
|
||||
log_event(format!("Provisionamento concluído. ID final: {final_id}. Versão: {:?}", version));
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
|
@ -403,7 +403,7 @@ fn write_config_files() -> Result<PathBuf, RustdeskError> {
|
|||
let config_contents = build_config_contents();
|
||||
let main_path = program_data_config_dir().join("RustDesk2.toml");
|
||||
write_file(&main_path, &config_contents)?;
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Config principal gravada em {}",
|
||||
main_path.display()
|
||||
));
|
||||
|
|
@ -412,7 +412,7 @@ fn write_config_files() -> Result<PathBuf, RustdeskError> {
|
|||
for service_dir in service_profile_dirs() {
|
||||
let service_profile = service_dir.join("RustDesk2.toml");
|
||||
if let Err(error) = write_file(&service_profile, &config_contents) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao gravar config no perfil do serviço ({}): {error}",
|
||||
service_profile.display()
|
||||
));
|
||||
|
|
@ -421,7 +421,7 @@ fn write_config_files() -> Result<PathBuf, RustdeskError> {
|
|||
|
||||
if let Some(appdata_path) = user_appdata_config_path("RustDesk2.toml") {
|
||||
if let Err(error) = write_file(&appdata_path, &config_contents) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao atualizar config no AppData do usuário: {error}"
|
||||
));
|
||||
}
|
||||
|
|
@ -516,7 +516,7 @@ fn ensure_service_running(exe_path: &Path) -> Result<(), RustdeskError> {
|
|||
ensure_service_installed(exe_path)?;
|
||||
|
||||
if let Err(error) = configure_service_startup() {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Aviso: não foi possível reforçar autostart/recuperação do serviço RustDesk: {error}"
|
||||
));
|
||||
}
|
||||
|
|
@ -553,7 +553,7 @@ fn ensure_service_running(exe_path: &Path) -> Result<(), RustdeskError> {
|
|||
let _ = run_with_args(exe_path, &["--install-service"]);
|
||||
let _ = run_sc(&["config", SERVICE_NAME, &format!("start= {}", "auto")]);
|
||||
if let Err(error) = start_sequence() {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao subir o serviço RustDesk mesmo após reinstalação: {error}"
|
||||
));
|
||||
}
|
||||
|
|
@ -631,8 +631,8 @@ fn remove_rustdesk_autorun_artifacts() {
|
|||
for path in startup_paths {
|
||||
if path.exists() {
|
||||
match fs::remove_file(&path) {
|
||||
Ok(_) => log_event(&format!("Atalho de inicialização do RustDesk removido: {}", path.display())),
|
||||
Err(error) => log_event(&format!(
|
||||
Ok(_) => log_event(format!("Atalho de inicialização do RustDesk removido: {}", path.display())),
|
||||
Err(error) => log_event(format!(
|
||||
"Falha ao remover atalho de inicialização do RustDesk ({}): {}",
|
||||
path.display(),
|
||||
error
|
||||
|
|
@ -650,7 +650,7 @@ fn remove_rustdesk_autorun_artifacts() {
|
|||
.status();
|
||||
if let Ok(code) = status {
|
||||
if code.success() {
|
||||
log_event(&format!("Entrada de auto-run RustDesk removida de {}", reg_path));
|
||||
log_event(format!("Entrada de auto-run RustDesk removida de {}", reg_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -658,7 +658,7 @@ fn remove_rustdesk_autorun_artifacts() {
|
|||
|
||||
fn stop_rustdesk_processes() -> Result<(), RustdeskError> {
|
||||
if let Err(error) = try_stop_service() {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Não foi possível parar o serviço RustDesk antes da sincronização: {error}"
|
||||
));
|
||||
}
|
||||
|
|
@ -774,12 +774,12 @@ fn ensure_remote_id_files(id: &str) {
|
|||
for dir in remote_id_directories() {
|
||||
let path = dir.join("RustDesk_local.toml");
|
||||
match write_remote_id_value(&path, id) {
|
||||
Ok(_) => log_event(&format!(
|
||||
Ok(_) => log_event(format!(
|
||||
"remote_id atualizado para {} em {}",
|
||||
id,
|
||||
path.display()
|
||||
)),
|
||||
Err(error) => log_event(&format!(
|
||||
Err(error) => log_event(format!(
|
||||
"Falha ao atualizar remote_id em {}: {error}",
|
||||
path.display()
|
||||
)),
|
||||
|
|
@ -821,7 +821,7 @@ fn ensure_password_files(secret: &str) -> Result<(), String> {
|
|||
if let Err(error) = write_toml_kv(&password_path, "password", secret) {
|
||||
errors.push(format!("{} -> {}", password_path.display(), error));
|
||||
} else {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Senha escrita via fallback em {}",
|
||||
password_path.display()
|
||||
));
|
||||
|
|
@ -829,12 +829,12 @@ fn ensure_password_files(secret: &str) -> Result<(), String> {
|
|||
|
||||
let local_path = dir.join("RustDesk_local.toml");
|
||||
if let Err(error) = write_toml_kv(&local_path, "verification-method", SECURITY_VERIFICATION_VALUE) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao ajustar verification-method em {}: {error}",
|
||||
local_path.display()
|
||||
));
|
||||
} else {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"verification-method atualizado para {} em {}",
|
||||
SECURITY_VERIFICATION_VALUE,
|
||||
local_path.display()
|
||||
|
|
@ -843,19 +843,19 @@ fn ensure_password_files(secret: &str) -> Result<(), String> {
|
|||
|
||||
let rustdesk2_path = dir.join("RustDesk2.toml");
|
||||
if let Err(error) = enforce_security_in_rustdesk2(&rustdesk2_path) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao ajustar flags no RustDesk2.toml em {}: {error}",
|
||||
rustdesk2_path.display()
|
||||
));
|
||||
}
|
||||
|
||||
if let Err(error) = write_toml_kv(&local_path, "approve-mode", SECURITY_APPROVE_MODE_VALUE) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao ajustar approve-mode em {}: {error}",
|
||||
local_path.display()
|
||||
));
|
||||
} else {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"approve-mode atualizado para {} em {}",
|
||||
SECURITY_APPROVE_MODE_VALUE,
|
||||
local_path.display()
|
||||
|
|
@ -877,7 +877,7 @@ fn enforce_security_flags() -> Result<(), String> {
|
|||
if let Err(error) = write_toml_kv(&local_path, "verification-method", SECURITY_VERIFICATION_VALUE) {
|
||||
errors.push(format!("{} -> {}", local_path.display(), error));
|
||||
} else {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"verification-method atualizado para {} em {}",
|
||||
SECURITY_VERIFICATION_VALUE,
|
||||
local_path.display()
|
||||
|
|
@ -887,7 +887,7 @@ fn enforce_security_flags() -> Result<(), String> {
|
|||
if let Err(error) = write_toml_kv(&local_path, "approve-mode", SECURITY_APPROVE_MODE_VALUE) {
|
||||
errors.push(format!("{} -> {}", local_path.display(), error));
|
||||
} else {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"approve-mode atualizado para {} em {}",
|
||||
SECURITY_APPROVE_MODE_VALUE,
|
||||
local_path.display()
|
||||
|
|
@ -921,7 +921,7 @@ fn propagate_password_profile() -> io::Result<bool> {
|
|||
if !src_path.exists() {
|
||||
continue;
|
||||
}
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Copiando {} para ProgramData/serviços",
|
||||
src_path.display()
|
||||
));
|
||||
|
|
@ -929,7 +929,7 @@ fn propagate_password_profile() -> io::Result<bool> {
|
|||
for dest_root in propagation_destinations() {
|
||||
let target_path = dest_root.join(filename);
|
||||
copy_overwrite(&src_path, &target_path)?;
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"{} propagado para {}",
|
||||
filename,
|
||||
target_path.display()
|
||||
|
|
@ -969,7 +969,7 @@ fn replicate_password_artifacts() -> io::Result<()> {
|
|||
|
||||
let target_path = dest.join(name);
|
||||
copy_overwrite(&source_path, &target_path)?;
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Artefato de senha {name} replicado para {}",
|
||||
target_path.display()
|
||||
));
|
||||
|
|
@ -981,13 +981,11 @@ fn replicate_password_artifacts() -> io::Result<()> {
|
|||
|
||||
fn purge_existing_rustdesk_profiles() -> Result<(), String> {
|
||||
let mut errors = Vec::new();
|
||||
let mut cleaned_any = false;
|
||||
|
||||
for dir in remote_id_directories() {
|
||||
match purge_config_dir(&dir) {
|
||||
Ok(true) => {
|
||||
cleaned_any = true;
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Perfis antigos removidos em {}",
|
||||
dir.display()
|
||||
));
|
||||
|
|
@ -997,9 +995,7 @@ fn purge_existing_rustdesk_profiles() -> Result<(), String> {
|
|||
}
|
||||
}
|
||||
|
||||
if cleaned_any {
|
||||
Ok(())
|
||||
} else if errors.is_empty() {
|
||||
if errors.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(errors.join(" | "))
|
||||
|
|
@ -1030,6 +1026,7 @@ fn purge_config_dir(dir: &Path) -> Result<bool, io::Error> {
|
|||
Ok(removed)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn run_powershell_elevated(script: &str) -> Result<(), String> {
|
||||
let temp_dir = env::temp_dir();
|
||||
let payload = temp_dir.join("raven_payload.ps1");
|
||||
|
|
@ -1077,6 +1074,7 @@ exit $process.ExitCode
|
|||
Err(format!("elevated ps exit {:?}", status.code()))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn fix_profile_acl(target: &Path) -> Result<(), String> {
|
||||
let target_str = target.display().to_string();
|
||||
let transcript = env::temp_dir().join("raven_acl_ps.log");
|
||||
|
|
@ -1111,7 +1109,7 @@ try {{
|
|||
let result = run_powershell_elevated(&script);
|
||||
if result.is_err() {
|
||||
if let Ok(content) = fs::read_to_string(&transcript) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"ACL transcript para {}:\n{}",
|
||||
target.display(), content
|
||||
));
|
||||
|
|
@ -1122,6 +1120,9 @@ try {{
|
|||
}
|
||||
|
||||
fn ensure_service_profiles_writable_preflight() -> Result<(), String> {
|
||||
// Verificamos se os diretorios de perfil sao graváveis
|
||||
// Se nao forem, apenas logamos aviso - o Raven Service deve lidar com isso
|
||||
// Nao usamos elevacao para evitar UAC adicional
|
||||
let mut blocked_dirs = Vec::new();
|
||||
for dir in service_profile_dirs() {
|
||||
if !can_write_dir(&dir) {
|
||||
|
|
@ -1133,53 +1134,46 @@ fn ensure_service_profiles_writable_preflight() -> Result<(), String> {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
if has_acl_unlock_flag() {
|
||||
log_event("Perfis do serviço voltaram a bloquear escrita; reaplicando correção de ACL");
|
||||
} else {
|
||||
log_event("Executando ajuste inicial de ACL dos perfis do serviço (requer UAC)");
|
||||
}
|
||||
// Apenas logamos aviso - o serviço RavenService deve lidar com permissões
|
||||
log_event(format!(
|
||||
"Aviso: alguns perfis de serviço não são graváveis: {:?}. O Raven Service deve configurar permissões.",
|
||||
blocked_dirs.iter().map(|d| d.display().to_string()).collect::<Vec<_>>()
|
||||
));
|
||||
|
||||
let mut last_error: Option<String> = None;
|
||||
for dir in blocked_dirs.iter() {
|
||||
log_event(&format!(
|
||||
"Tentando corrigir ACL via UAC (preflight) em {}...",
|
||||
dir.display()
|
||||
));
|
||||
if let Err(error) = fix_profile_acl(dir) {
|
||||
last_error = Some(error);
|
||||
continue;
|
||||
}
|
||||
if can_write_dir(dir) {
|
||||
log_event(&format!(
|
||||
"ACL ajustada com sucesso em {}",
|
||||
dir.display()
|
||||
));
|
||||
} else {
|
||||
last_error = Some(format!(
|
||||
"continua sem permissão para {} mesmo após preflight",
|
||||
dir.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if blocked_dirs.iter().all(|dir| can_write_dir(dir)) {
|
||||
mark_acl_unlock_flag();
|
||||
// Retornamos Ok para não bloquear o fluxo
|
||||
// O Raven Service, rodando como LocalSystem, pode gravar nesses diretórios
|
||||
Ok(())
|
||||
} else {
|
||||
Err(last_error.unwrap_or_else(|| "nenhum perfil de serviço acessível".into()))
|
||||
}
|
||||
}
|
||||
|
||||
fn stop_service_elevated() -> Result<(), String> {
|
||||
let script = r#"
|
||||
$ErrorActionPreference='Stop'
|
||||
$service = Get-Service -Name 'RustDesk' -ErrorAction SilentlyContinue
|
||||
if ($service -and $service.Status -ne 'Stopped') {
|
||||
Stop-Service -Name 'RustDesk' -Force -ErrorAction Stop
|
||||
$service.WaitForStatus('Stopped','00:00:10')
|
||||
// Tentamos parar o serviço RustDesk sem elevação
|
||||
// Se falhar, apenas logamos aviso - o Raven Service pode lidar com isso
|
||||
// Não usamos elevação para evitar UAC adicional
|
||||
let output = Command::new("sc")
|
||||
.args(["stop", "RustDesk"])
|
||||
.output();
|
||||
|
||||
match output {
|
||||
Ok(result) => {
|
||||
if result.status.success() {
|
||||
// Aguarda um pouco para o serviço parar
|
||||
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||
Ok(())
|
||||
} else {
|
||||
let stderr = String::from_utf8_lossy(&result.stderr);
|
||||
log_event(format!(
|
||||
"Aviso: não foi possível parar o serviço RustDesk sem elevação: {}",
|
||||
stderr.trim()
|
||||
));
|
||||
// Retornamos Ok para não bloquear - o serviço pode estar já parado
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log_event(format!("Aviso: falha ao executar sc stop RustDesk: {e}"));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
"#;
|
||||
run_powershell_elevated(script)
|
||||
}
|
||||
|
||||
fn can_write_dir(dir: &Path) -> bool {
|
||||
|
|
@ -1339,21 +1333,21 @@ fn log_password_replication(secret: &str) {
|
|||
fn log_password_match(path: &Path, secret: &str) {
|
||||
match read_password_from_file(path) {
|
||||
Some(value) if value == secret => {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Senha confirmada em {} ({})",
|
||||
path.display(),
|
||||
mask_secret(&value)
|
||||
));
|
||||
}
|
||||
Some(value) => {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Aviso: senha divergente ({}) em {}",
|
||||
mask_secret(&value),
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
None => {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Aviso: chave 'password' não encontrada em {}",
|
||||
path.display()
|
||||
));
|
||||
|
|
@ -1469,21 +1463,24 @@ fn write_machine_store_object(map: JsonMap<String, JsonValue>) -> Result<(), Str
|
|||
}
|
||||
|
||||
fn upsert_machine_store_value(key: &str, value: JsonValue) -> Result<(), String> {
|
||||
let mut map = read_machine_store_object().unwrap_or_else(JsonMap::new);
|
||||
let mut map = read_machine_store_object().unwrap_or_default();
|
||||
map.insert(key.to_string(), value);
|
||||
write_machine_store_object(map)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn machine_store_key_exists(key: &str) -> bool {
|
||||
read_machine_store_object()
|
||||
.map(|map| map.contains_key(key))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn acl_flag_file_path() -> Option<PathBuf> {
|
||||
raven_appdata_root().map(|dir| dir.join(ACL_FLAG_FILENAME))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn has_acl_unlock_flag() -> bool {
|
||||
if let Some(flag) = acl_flag_file_path() {
|
||||
if flag.exists() {
|
||||
|
|
@ -1493,6 +1490,7 @@ fn has_acl_unlock_flag() -> bool {
|
|||
machine_store_key_exists(RUSTDESK_ACL_STORE_KEY)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn mark_acl_unlock_flag() {
|
||||
let timestamp = Utc::now().timestamp_millis();
|
||||
if let Some(flag_path) = acl_flag_file_path() {
|
||||
|
|
@ -1500,7 +1498,7 @@ fn mark_acl_unlock_flag() {
|
|||
let _ = fs::create_dir_all(parent);
|
||||
}
|
||||
if let Err(error) = fs::write(&flag_path, timestamp.to_string()) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao gravar flag de ACL em {}: {error}",
|
||||
flag_path.display()
|
||||
));
|
||||
|
|
@ -1508,7 +1506,7 @@ fn mark_acl_unlock_flag() {
|
|||
}
|
||||
|
||||
if let Err(error) = upsert_machine_store_value(RUSTDESK_ACL_STORE_KEY, JsonValue::from(timestamp)) {
|
||||
log_event(&format!(
|
||||
log_event(format!(
|
||||
"Falha ao registrar flag de ACL no machine-agent: {error}"
|
||||
));
|
||||
}
|
||||
|
|
@ -1547,7 +1545,7 @@ fn sync_remote_access_with_backend(result: &crate::RustdeskProvisioningResult) -
|
|||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("https://tickets.esdrasrenan.com.br");
|
||||
|
||||
log_event(&format!("Sincronizando com backend: {} (machineId: {})", api_base_url, machine_id));
|
||||
log_event(format!("Sincronizando com backend: {} (machineId: {})", api_base_url, machine_id));
|
||||
|
||||
// Monta payload conforme schema esperado pelo backend
|
||||
// Schema: { machineToken, provider, identifier, password?, url?, username?, notes? }
|
||||
|
|
@ -1575,13 +1573,13 @@ fn sync_remote_access_with_backend(result: &crate::RustdeskProvisioningResult) -
|
|||
.send()?;
|
||||
|
||||
if response.status().is_success() {
|
||||
log_event(&format!("Sync com backend OK: status {}", response.status()));
|
||||
log_event(format!("Sync com backend OK: status {}", response.status()));
|
||||
Ok(())
|
||||
} else {
|
||||
let status = response.status();
|
||||
let body = response.text().unwrap_or_default();
|
||||
let body_preview = if body.len() > 200 { &body[..200] } else { &body };
|
||||
log_event(&format!("Sync com backend falhou: {} - {}", status, body_preview));
|
||||
log_event(format!("Sync com backend falhou: {} - {}", status, body_preview));
|
||||
Err(RustdeskError::CommandFailed {
|
||||
command: "sync_remote_access".to_string(),
|
||||
status: Some(status.as_u16() as i32)
|
||||
|
|
|
|||
244
apps/desktop/src-tauri/src/service_client.rs
Normal file
244
apps/desktop/src-tauri/src/service_client.rs
Normal file
|
|
@ -0,0 +1,244 @@
|
|||
//! Cliente IPC para comunicacao com o Raven Service
|
||||
//!
|
||||
//! Este modulo permite que o app Tauri se comunique com o Raven Service
|
||||
//! via Named Pipes para executar operacoes privilegiadas.
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::time::Duration;
|
||||
use thiserror::Error;
|
||||
|
||||
const PIPE_NAME: &str = r"\\.\pipe\RavenService";
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ServiceClientError {
|
||||
#[error("Servico nao disponivel: {0}")]
|
||||
ServiceUnavailable(String),
|
||||
|
||||
#[error("Erro de comunicacao: {0}")]
|
||||
CommunicationError(String),
|
||||
|
||||
#[error("Erro de serializacao: {0}")]
|
||||
SerializationError(#[from] serde_json::Error),
|
||||
|
||||
#[error("Erro do servico: {message} (code: {code})")]
|
||||
ServiceError { code: i32, message: String },
|
||||
|
||||
#[error("Timeout aguardando resposta")]
|
||||
Timeout,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct Request {
|
||||
id: String,
|
||||
method: String,
|
||||
params: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Response {
|
||||
id: String,
|
||||
result: Option<serde_json::Value>,
|
||||
error: Option<ErrorResponse>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ErrorResponse {
|
||||
code: i32,
|
||||
message: String,
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Tipos de Resultado
|
||||
// =============================================================================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UsbPolicyResult {
|
||||
pub success: bool,
|
||||
pub policy: String,
|
||||
pub error: Option<String>,
|
||||
pub applied_at: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RustdeskResult {
|
||||
pub id: String,
|
||||
pub password: String,
|
||||
pub installed_version: Option<String>,
|
||||
pub updated: bool,
|
||||
pub last_provisioned_at: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RustdeskStatus {
|
||||
pub installed: bool,
|
||||
pub running: bool,
|
||||
pub id: Option<String>,
|
||||
pub version: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HealthCheckResult {
|
||||
pub status: String,
|
||||
pub service: String,
|
||||
pub version: String,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Cliente
|
||||
// =============================================================================
|
||||
|
||||
/// Verifica se o servico esta disponivel
|
||||
pub fn is_service_available() -> bool {
|
||||
health_check().is_ok()
|
||||
}
|
||||
|
||||
/// Verifica saude do servico
|
||||
pub fn health_check() -> Result<HealthCheckResult, ServiceClientError> {
|
||||
let response = call_service("health_check", serde_json::json!({}))?;
|
||||
serde_json::from_value(response).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Aplica politica de USB
|
||||
pub fn apply_usb_policy(policy: &str) -> Result<UsbPolicyResult, ServiceClientError> {
|
||||
let response = call_service(
|
||||
"apply_usb_policy",
|
||||
serde_json::json!({ "policy": policy }),
|
||||
)?;
|
||||
serde_json::from_value(response).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Obtem politica de USB atual
|
||||
pub fn get_usb_policy() -> Result<String, ServiceClientError> {
|
||||
let response = call_service("get_usb_policy", serde_json::json!({}))?;
|
||||
response
|
||||
.get("policy")
|
||||
.and_then(|p| p.as_str())
|
||||
.map(String::from)
|
||||
.ok_or_else(|| ServiceClientError::CommunicationError("Resposta invalida".into()))
|
||||
}
|
||||
|
||||
/// Provisiona RustDesk
|
||||
pub fn provision_rustdesk(
|
||||
config: Option<&str>,
|
||||
password: Option<&str>,
|
||||
machine_id: Option<&str>,
|
||||
) -> Result<RustdeskResult, ServiceClientError> {
|
||||
let params = serde_json::json!({
|
||||
"config": config,
|
||||
"password": password,
|
||||
"machineId": machine_id,
|
||||
});
|
||||
let response = call_service("provision_rustdesk", params)?;
|
||||
serde_json::from_value(response).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Obtem status do RustDesk
|
||||
pub fn get_rustdesk_status() -> Result<RustdeskStatus, ServiceClientError> {
|
||||
let response = call_service("get_rustdesk_status", serde_json::json!({}))?;
|
||||
serde_json::from_value(response).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Comunicacao IPC
|
||||
// =============================================================================
|
||||
|
||||
fn call_service(
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
) -> Result<serde_json::Value, ServiceClientError> {
|
||||
// Gera ID unico para a requisicao
|
||||
let id = uuid::Uuid::new_v4().to_string();
|
||||
|
||||
let request = Request {
|
||||
id: id.clone(),
|
||||
method: method.to_string(),
|
||||
params,
|
||||
};
|
||||
|
||||
// Serializa requisicao
|
||||
let request_json = serde_json::to_string(&request)?;
|
||||
|
||||
// Conecta ao pipe
|
||||
let mut pipe = connect_to_pipe()?;
|
||||
|
||||
// Envia requisicao
|
||||
writeln!(pipe, "{}", request_json).map_err(|e| {
|
||||
ServiceClientError::CommunicationError(format!("Erro ao enviar requisicao: {}", e))
|
||||
})?;
|
||||
pipe.flush().map_err(|e| {
|
||||
ServiceClientError::CommunicationError(format!("Erro ao flush: {}", e))
|
||||
})?;
|
||||
|
||||
// Le resposta
|
||||
let mut reader = BufReader::new(pipe);
|
||||
let mut response_line = String::new();
|
||||
|
||||
reader.read_line(&mut response_line).map_err(|e| {
|
||||
ServiceClientError::CommunicationError(format!("Erro ao ler resposta: {}", e))
|
||||
})?;
|
||||
|
||||
// Parse da resposta
|
||||
let response: Response = serde_json::from_str(&response_line)?;
|
||||
|
||||
// Verifica se o ID bate
|
||||
if response.id != id {
|
||||
return Err(ServiceClientError::CommunicationError(
|
||||
"ID de resposta nao corresponde".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Verifica erro
|
||||
if let Some(error) = response.error {
|
||||
return Err(ServiceClientError::ServiceError {
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
|
||||
// Retorna resultado
|
||||
response
|
||||
.result
|
||||
.ok_or_else(|| ServiceClientError::CommunicationError("Resposta sem resultado".into()))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn connect_to_pipe() -> Result<std::fs::File, ServiceClientError> {
|
||||
// Tenta conectar ao pipe com retry
|
||||
let mut attempts = 0;
|
||||
let max_attempts = 3;
|
||||
|
||||
loop {
|
||||
match std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(PIPE_NAME)
|
||||
{
|
||||
Ok(file) => return Ok(file),
|
||||
Err(e) => {
|
||||
attempts += 1;
|
||||
if attempts >= max_attempts {
|
||||
return Err(ServiceClientError::ServiceUnavailable(format!(
|
||||
"Nao foi possivel conectar ao servico apos {} tentativas: {}",
|
||||
max_attempts, e
|
||||
)));
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn connect_to_pipe() -> Result<std::fs::File, ServiceClientError> {
|
||||
Err(ServiceClientError::ServiceUnavailable(
|
||||
"Named Pipes so estao disponiveis no Windows".into(),
|
||||
))
|
||||
}
|
||||
|
|
@ -93,23 +93,11 @@ mod windows_impl {
|
|||
applied_at: Some(now),
|
||||
}),
|
||||
Err(err) => {
|
||||
// Tenta elevação se faltou permissão
|
||||
// Se faltou permissão, retorna erro - o serviço deve ser usado
|
||||
// Não fazemos elevação aqui para evitar UAC adicional
|
||||
if is_permission_error(&err) {
|
||||
if let Err(elevated_err) = apply_policy_with_elevation(policy) {
|
||||
return Err(elevated_err);
|
||||
}
|
||||
// Revalida a policy após elevação
|
||||
let current = get_current_policy()?;
|
||||
if current != policy {
|
||||
return Err(UsbControlError::PermissionDenied);
|
||||
}
|
||||
return Ok(UsbPolicyResult {
|
||||
success: true,
|
||||
policy: policy.as_str().to_string(),
|
||||
error: None,
|
||||
applied_at: Some(now),
|
||||
});
|
||||
}
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -219,11 +207,9 @@ mod windows_impl {
|
|||
|
||||
key.set_value("WriteProtect", &1u32)
|
||||
.map_err(map_winreg_error)?;
|
||||
} else {
|
||||
if let Ok(key) = hklm.open_subkey_with_flags(STORAGE_POLICY_PATH, KEY_ALL_ACCESS) {
|
||||
} else if let Ok(key) = hklm.open_subkey_with_flags(STORAGE_POLICY_PATH, KEY_ALL_ACCESS) {
|
||||
let _ = key.set_value("WriteProtect", &0u32);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -269,6 +255,7 @@ mod windows_impl {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn apply_policy_with_elevation(policy: UsbPolicy) -> Result<(), UsbControlError> {
|
||||
// Cria script temporário para aplicar as chaves via PowerShell elevado
|
||||
let temp_dir = std::env::temp_dir();
|
||||
|
|
@ -321,7 +308,7 @@ try {{
|
|||
policy = policy_str
|
||||
);
|
||||
|
||||
fs::write(&script_path, script).map_err(|e| UsbControlError::Io(e))?;
|
||||
fs::write(&script_path, script).map_err(UsbControlError::Io)?;
|
||||
|
||||
// Start-Process com RunAs para acionar UAC
|
||||
let arg = format!(
|
||||
|
|
@ -333,7 +320,7 @@ try {{
|
|||
.arg("-Command")
|
||||
.arg(arg)
|
||||
.status()
|
||||
.map_err(|e| UsbControlError::Io(e))?;
|
||||
.map_err(UsbControlError::Io)?;
|
||||
|
||||
if !status.success() {
|
||||
return Err(UsbControlError::PermissionDenied);
|
||||
|
|
@ -362,7 +349,7 @@ try {{
|
|||
.args(["/target:computer", "/force"])
|
||||
.creation_flags(CREATE_NO_WINDOW)
|
||||
.output()
|
||||
.map_err(|e| UsbControlError::Io(e))?;
|
||||
.map_err(UsbControlError::Io)?;
|
||||
|
||||
if !output.status.success() {
|
||||
// Nao e critico se falhar, apenas log
|
||||
|
|
|
|||
|
|
@ -50,6 +50,9 @@
|
|||
"icons/icon.png",
|
||||
"icons/Raven.png"
|
||||
],
|
||||
"resources": {
|
||||
"../service/target/release/raven-service.exe": "raven-service.exe"
|
||||
},
|
||||
"windows": {
|
||||
"webviewInstallMode": {
|
||||
"type": "skip"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue