fix(desktop): corrige reinstalacao do RavenService e uso via IPC
- NSIS: remove servico antigo antes de instalar novo (corrige reinstalacoes) - NSIS: adiciona retry ao iniciar servico - agent.rs: usa service_client para aplicar politica USB via IPC primeiro - Fallback para aplicacao direta se servico indisponivel 🤖 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
f6efc0d678
commit
8863fffc37
2 changed files with 80 additions and 21 deletions
|
|
@ -9,7 +9,7 @@
|
||||||
BrandingText " "
|
BrandingText " "
|
||||||
|
|
||||||
!macro NSIS_HOOK_PREINSTALL
|
!macro NSIS_HOOK_PREINSTALL
|
||||||
; Para qualquer instancia anterior do servico antes de atualizar
|
; Para e remove qualquer instancia anterior do servico antes de atualizar
|
||||||
DetailPrint "Parando servicos anteriores..."
|
DetailPrint "Parando servicos anteriores..."
|
||||||
|
|
||||||
; Para o servico
|
; Para o servico
|
||||||
|
|
@ -18,12 +18,20 @@ BrandingText " "
|
||||||
; Aguarda o servico parar completamente (ate 10 segundos)
|
; 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++}"'
|
nsExec::ExecToLog 'powershell -Command "$$i=0; while((Get-Service RavenService -ErrorAction SilentlyContinue).Status -eq \"Running\" -and $$i -lt 10){Start-Sleep 1;$$i++}"'
|
||||||
|
|
||||||
|
; Remove o servico antigo (IMPORTANTE para reinstalacoes)
|
||||||
|
DetailPrint "Removendo servico antigo..."
|
||||||
|
IfFileExists "$INSTDIR\raven-service.exe" 0 +2
|
||||||
|
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" uninstall'
|
||||||
|
|
||||||
|
; Fallback: remove via sc delete se o executavel nao existir
|
||||||
|
nsExec::ExecToLog 'sc delete RavenService'
|
||||||
|
|
||||||
; Forca encerramento de processos remanescentes
|
; Forca encerramento de processos remanescentes
|
||||||
nsExec::ExecToLog 'taskkill /F /IM raven-service.exe'
|
nsExec::ExecToLog 'taskkill /F /IM raven-service.exe'
|
||||||
nsExec::ExecToLog 'taskkill /F /IM appsdesktop.exe'
|
nsExec::ExecToLog 'taskkill /F /IM appsdesktop.exe'
|
||||||
|
|
||||||
; Aguarda liberacao dos arquivos
|
; Aguarda liberacao dos arquivos e remocao completa do servico
|
||||||
Sleep 2000
|
Sleep 3000
|
||||||
!macroend
|
!macroend
|
||||||
|
|
||||||
!macro NSIS_HOOK_POSTINSTALL
|
!macro NSIS_HOOK_POSTINSTALL
|
||||||
|
|
@ -33,6 +41,10 @@ BrandingText " "
|
||||||
|
|
||||||
DetailPrint "Instalando Raven Service..."
|
DetailPrint "Instalando Raven Service..."
|
||||||
|
|
||||||
|
; Garante que nao ha servico residual
|
||||||
|
nsExec::ExecToLog 'sc delete RavenService'
|
||||||
|
Sleep 1000
|
||||||
|
|
||||||
; O servico ja esta em $INSTDIR (copiado como resource pelo Tauri)
|
; O servico ja esta em $INSTDIR (copiado como resource pelo Tauri)
|
||||||
; Registra o servico Windows
|
; Registra o servico Windows
|
||||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" install'
|
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" install'
|
||||||
|
|
@ -40,13 +52,17 @@ BrandingText " "
|
||||||
|
|
||||||
${If} $0 != 0
|
${If} $0 != 0
|
||||||
DetailPrint "Aviso: Falha ao registrar servico (codigo: $0)"
|
DetailPrint "Aviso: Falha ao registrar servico (codigo: $0)"
|
||||||
; Tenta remover e reinstalar
|
; Tenta remover completamente e reinstalar
|
||||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" uninstall'
|
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" uninstall'
|
||||||
Sleep 500
|
nsExec::ExecToLog 'sc delete RavenService'
|
||||||
|
Sleep 1000
|
||||||
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" install'
|
nsExec::ExecToLog '"$INSTDIR\raven-service.exe" install'
|
||||||
Pop $0
|
Pop $0
|
||||||
${EndIf}
|
${EndIf}
|
||||||
|
|
||||||
|
; Aguarda registro do servico
|
||||||
|
Sleep 500
|
||||||
|
|
||||||
; Inicia o servico
|
; Inicia o servico
|
||||||
DetailPrint "Iniciando Raven Service..."
|
DetailPrint "Iniciando Raven Service..."
|
||||||
nsExec::ExecToLog 'sc start RavenService'
|
nsExec::ExecToLog 'sc start RavenService'
|
||||||
|
|
@ -55,7 +71,15 @@ BrandingText " "
|
||||||
${If} $0 == 0
|
${If} $0 == 0
|
||||||
DetailPrint "Raven Service iniciado com sucesso!"
|
DetailPrint "Raven Service iniciado com sucesso!"
|
||||||
${Else}
|
${Else}
|
||||||
DetailPrint "Aviso: Servico sera iniciado na proxima reinicializacao"
|
; Tenta novamente apos breve espera
|
||||||
|
Sleep 1000
|
||||||
|
nsExec::ExecToLog 'sc start RavenService'
|
||||||
|
Pop $0
|
||||||
|
${If} $0 == 0
|
||||||
|
DetailPrint "Raven Service iniciado com sucesso (segunda tentativa)!"
|
||||||
|
${Else}
|
||||||
|
DetailPrint "Aviso: Servico sera iniciado na proxima reinicializacao (codigo: $0)"
|
||||||
|
${EndIf}
|
||||||
${EndIf}
|
${EndIf}
|
||||||
|
|
||||||
; =========================================================================
|
; =========================================================================
|
||||||
|
|
|
||||||
|
|
@ -1225,7 +1225,8 @@ async fn check_and_apply_usb_policy(base_url: &str, token: &str) {
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
{
|
{
|
||||||
use crate::usb_control::{apply_usb_policy, get_current_policy, UsbPolicy};
|
use crate::usb_control::{get_current_policy, UsbPolicy};
|
||||||
|
use crate::service_client;
|
||||||
|
|
||||||
let policy = match UsbPolicy::from_str(&policy_str) {
|
let policy = match UsbPolicy::from_str(&policy_str) {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
|
|
@ -1259,24 +1260,58 @@ async fn check_and_apply_usb_policy(base_url: &str, token: &str) {
|
||||||
// Reporta APPLYING para progress bar real no frontend
|
// Reporta APPLYING para progress bar real no frontend
|
||||||
let _ = report_usb_policy_status(base_url, token, "APPLYING", None, None).await;
|
let _ = report_usb_policy_status(base_url, token, "APPLYING", None, None).await;
|
||||||
|
|
||||||
match apply_usb_policy(policy) {
|
// Tenta primeiro via RavenService (privilegiado)
|
||||||
|
crate::log_info!("Tentando aplicar politica via RavenService...");
|
||||||
|
match service_client::apply_usb_policy(&policy_str) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
crate::log_info!("Politica USB aplicada com sucesso: {:?}", result);
|
if result.success {
|
||||||
let reported = report_usb_policy_status(base_url, token, "APPLIED", None, Some(policy_str.clone())).await;
|
crate::log_info!("Politica USB aplicada com sucesso via RavenService: {:?}", result);
|
||||||
if !reported {
|
let reported = report_usb_policy_status(base_url, token, "APPLIED", None, Some(policy_str.clone())).await;
|
||||||
crate::log_error!("CRITICO: Politica aplicada mas falha ao reportar ao servidor!");
|
if !reported {
|
||||||
// Agenda retry em background
|
crate::log_error!("CRITICO: Politica aplicada mas falha ao reportar ao servidor!");
|
||||||
let base_url = base_url.to_string();
|
let base_url = base_url.to_string();
|
||||||
let token = token.to_string();
|
let token = token.to_string();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
tokio::time::sleep(Duration::from_secs(60)).await;
|
tokio::time::sleep(Duration::from_secs(60)).await;
|
||||||
crate::log_info!("Retry agendado: reportando politica USB...");
|
crate::log_info!("Retry agendado: reportando politica USB...");
|
||||||
let _ = report_usb_policy_status(&base_url, &token, "APPLIED", None, Some(policy_str)).await;
|
let _ = report_usb_policy_status(&base_url, &token, "APPLIED", None, Some(policy_str)).await;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
let err_msg = result.error.unwrap_or_else(|| "Erro desconhecido".to_string());
|
||||||
|
crate::log_error!("RavenService retornou erro: {}", err_msg);
|
||||||
|
report_usb_policy_status(base_url, token, "FAILED", Some(err_msg), None).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(service_client::ServiceClientError::ServiceUnavailable(msg)) => {
|
||||||
|
crate::log_warn!("RavenService nao disponivel: {}", msg);
|
||||||
|
// Tenta fallback direto (vai falhar se nao tiver privilegio)
|
||||||
|
crate::log_info!("Tentando aplicar politica diretamente...");
|
||||||
|
match crate::usb_control::apply_usb_policy(policy) {
|
||||||
|
Ok(result) => {
|
||||||
|
crate::log_info!("Politica USB aplicada com sucesso (direto): {:?}", result);
|
||||||
|
let reported = report_usb_policy_status(base_url, token, "APPLIED", None, Some(policy_str.clone())).await;
|
||||||
|
if !reported {
|
||||||
|
crate::log_error!("CRITICO: Politica aplicada mas falha ao reportar ao servidor!");
|
||||||
|
let base_url = base_url.to_string();
|
||||||
|
let token = token.to_string();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
tokio::time::sleep(Duration::from_secs(60)).await;
|
||||||
|
crate::log_info!("Retry agendado: reportando politica USB...");
|
||||||
|
let _ = report_usb_policy_status(&base_url, &token, "APPLIED", None, Some(policy_str)).await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = format!("RavenService indisponivel e aplicacao direta falhou: {}. Instale ou inicie o RavenService.", e);
|
||||||
|
crate::log_error!("{}", err_msg);
|
||||||
|
report_usb_policy_status(base_url, token, "FAILED", Some(err_msg), None).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
crate::log_error!("Falha ao aplicar politica USB: {e}");
|
crate::log_error!("Falha ao comunicar com RavenService: {e}");
|
||||||
report_usb_policy_status(base_url, token, "FAILED", Some(e.to_string()), None).await;
|
report_usb_policy_status(base_url, token, "FAILED", Some(e.to_string()), None).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue