forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-trilink-agent.ps1
More file actions
202 lines (172 loc) · 7.33 KB
/
fix-trilink-agent.ps1
File metadata and controls
202 lines (172 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
$ErrorActionPreference = "Stop"
$installedRoot = "C:\Trilink\Remote\RustDesk"
$legacyAgentPath = Join-Path $installedRoot "trilink-agente.ps1"
$modularAgentPath = Join-Path $installedRoot "remote-agent\trilink-agente.ps1"
$modularSysproPath = Join-Path $installedRoot "remote-agent\providers\syspro.ps1"
$logPath = "C:\Trilink\Remote\Logs\agentRemote.log"
function Backup-File {
param([Parameter(Mandatory = $true)][string]$Path)
$backupPath = "$Path.bak-$(Get-Date -Format yyyyMMdd-HHmmss)"
Copy-Item -Path $Path -Destination $backupPath -Force
Write-Host "Backup criado em: $backupPath"
}
function Assert-ParseOk {
param([Parameter(Mandatory = $true)][string]$Path)
$tokens = $null
$errs = $null
[System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$tokens, [ref]$errs) | Out-Null
if ($errs -and $errs.Count -gt 0) {
Write-Host "Erros de parse encontrados em ${Path}:"
$errs | ForEach-Object { Write-Host "- $($_.Message)" }
throw "Parse do script falhou: $Path"
}
}
function Update-ModularSyspro {
param([Parameter(Mandatory = $true)][string]$Path)
$newModule = @'
function Get-SysproChangelogVersion {
param([string]$InstallPath)
try {
$changelogPath = Join-Path $InstallPath "Update\change-log.txt"
if (-not (Test-Path $changelogPath)) {
Write-Log "changelog nao encontrado: $changelogPath"
return ""
}
$content = $null
try {
$content = Get-Content -Path $changelogPath -Raw -Encoding UTF8 -ErrorAction Stop
} catch {
try {
$content = Get-Content -Path $changelogPath -Raw -Encoding Default -ErrorAction Stop
} catch {}
}
if ([string]::IsNullOrWhiteSpace($content)) { return "" }
$regexMatch = [regex]::Match(
$content,
'Revis\w*\s+Atual\s*[:\s]\s*(\d+)',
[System.Text.RegularExpressions.RegexOptions]::IgnoreCase
)
if ($regexMatch.Success) { return $regexMatch.Groups[1].Value.Trim() }
return ""
} catch {
Write-Log "changelog erro leitura: $InstallPath | $($_.Exception.Message)"
return ""
}
}
function Get-SysproUpdates {
Write-Log "Iniciando verificacao de caminho fixo: \Syspro\Server"
$results = @()
$drives = @(Get-PSDrive -PSProvider FileSystem -ErrorAction SilentlyContinue | Where-Object {
$_.Name -match "^[A-Za-z]$"
})
if (-not $drives -or $drives.Count -eq 0) {
Write-Log "Nenhuma unidade de sistema de arquivos encontrada para verificacao."
return $results
}
foreach ($drive in $drives) {
$targetFolder = "$($drive.Name):\Syspro\Server"
$exePath = Join-Path $targetFolder "SysproServer.exe"
if (Test-Path $exePath) {
try {
$fileInfo = Get-Item $exePath
$versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($exePath)
$clientName = "Syspro-Server-$($drive.Name)"
$changelogVersion = Get-SysproChangelogVersion -InstallPath $targetFolder
$results += [ordered]@{
clientName = $clientName
installPath = $targetFolder
version = $versionInfo.FileVersion
revisaoAtual = $changelogVersion
lastUpdateUtc = $fileInfo.LastWriteTime.ToUniversalTime().ToString("o")
empresa = $clientName
caminho = $exePath
ultimaAtualizacao = $fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
Write-Log "Syspro detectado em: $targetFolder | versao=$($versionInfo.FileVersion) | revisao=$changelogVersion"
} catch {
Write-Log "Erro ao ler metadados de ${exePath}: $($_.Exception.Message)"
}
}
}
Write-Log "Verificacao concluida. Total encontrado: $($results.Count)"
return $results
}
'@
Set-Content -Path $Path -Value $newModule -Encoding UTF8
}
function Update-LegacyAgent {
param([Parameter(Mandatory = $true)][string]$Path)
$raw = Get-Content -Raw -Path $Path
$start = $raw.IndexOf("function Get-SysproUpdates {")
$end = $raw.IndexOf("function Get-Sha256Hex {")
if ($start -lt 0 -or $end -lt 0 -or $end -le $start) {
throw "Nao foi possivel localizar o bloco da funcao Get-SysproUpdates no legado."
}
$before = $raw.Substring(0, $start)
$after = $raw.Substring($end)
$newFunction = @'
function Get-SysproUpdates {
Write-Log "Iniciando verificacao de caminho fixo: \\Syspro\\Server"
$results = @()
$drives = @(Get-PSDrive -PSProvider FileSystem -ErrorAction SilentlyContinue | Where-Object {
$_.Name -match "^[A-Za-z]$"
})
if (-not $drives -or $drives.Count -eq 0) {
Write-Log "Nenhuma unidade de sistema de arquivos encontrada para verificacao."
return $results
}
foreach ($drive in $drives) {
$targetFolder = "$($drive.Name):\Syspro\Server"
$exePath = Join-Path $targetFolder "SysproServer.exe"
if (Test-Path $exePath) {
try {
$fileInfo = Get-Item $exePath
$versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($exePath)
$clientName = "Syspro-Server-$($drive.Name)"
$results += [ordered]@{
clientName = $clientName
installPath = $targetFolder
version = $versionInfo.FileVersion
lastUpdateUtc = $fileInfo.LastWriteTime.ToUniversalTime().ToString("o")
empresa = $clientName
caminho = $exePath
ultimaAtualizacao = $fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
Write-Log "Syspro detectado em: $targetFolder | versao=$($versionInfo.FileVersion)"
} catch {
Write-Log "Erro ao ler metadados de ${exePath}: $($_.Exception.Message)"
}
}
}
Write-Log "Verificacao concluida. Total encontrado: $($results.Count)"
return $results
}
'@
Set-Content -Path $Path -Value ($before + $newFunction + "`r`n" + $after) -Encoding UTF8
}
if (Test-Path $modularSysproPath) {
Write-Host "Detectado agente modular. Aplicando patch em: $modularSysproPath"
Backup-File -Path $modularSysproPath
Update-ModularSyspro -Path $modularSysproPath
Assert-ParseOk -Path $modularSysproPath
$runTarget = if (Test-Path $modularAgentPath) { $modularAgentPath } else { $null }
} elseif (Test-Path $legacyAgentPath) {
Write-Host "Detectado agente legado. Aplicando patch em: $legacyAgentPath"
Backup-File -Path $legacyAgentPath
Update-LegacyAgent -Path $legacyAgentPath
Assert-ParseOk -Path $legacyAgentPath
$runTarget = $legacyAgentPath
} else {
throw "Nenhum agente encontrado em $installedRoot"
}
Write-Host "Parse OK."
if (Test-Path $logPath) {
Clear-Content $logPath -ErrorAction SilentlyContinue
}
if (-not [string]::IsNullOrWhiteSpace($runTarget)) {
powershell -NoProfile -ExecutionPolicy Bypass -File $runTarget
Write-Host "Execucao concluida. Ultimas linhas do log:"
Get-Content $logPath -Tail 60 -ErrorAction SilentlyContinue
} else {
Write-Host "Patch aplicado. Execucao do agente ignorada (entrypoint nao encontrado)."
}