Revert "merge: apply windows standalone service installer and awHostname hardening"

This reverts commit e643576aa9, reversing
changes made to 669501f20a.
This commit is contained in:
igor04091968
2026-05-08 00:41:00 +03:00
parent e643576aa9
commit 6f5e5eb751
71 changed files with 23918 additions and 19579 deletions
+15 -85
View File
@@ -1,4 +1,4 @@
[CmdletBinding()]
[CmdletBinding()]
param(
[string]$ConfigPath = 'C:\ProgramData\AWatch-rus\deployment-config.json',
[string]$ServerHost,
@@ -22,9 +22,6 @@ Add-Type -AssemblyName System.Net.Http
$script:KnownBuckets = @{}
$script:Hostname = $env:COMPUTERNAME
$script:SessionId = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
$script:WalPath = $null
$script:HealthPath = $null
$script:WalFlushing = $false
# Настройка логирования
$script:LogPath = $LogPath
@@ -46,88 +43,28 @@ function Write-FileCollectorLog {
} catch {}
}
function Add-WalEntry {
function Invoke-AwJsonPost {
param(
[Parameter(Mandatory = $true)][string]$Uri,
[Parameter(Mandatory = $true)][string]$Json
)
if ([string]::IsNullOrWhiteSpace($script:WalPath)) { return }
$httpClient = $null
try {
$entry = @{ ts = (Get-Date).ToUniversalTime().ToString('o'); uri = $Uri; json = $Json } | ConvertTo-Json -Compress
Add-Content -LiteralPath $script:WalPath -Value $entry -Encoding UTF8
} catch {}
}
function Flush-Wal {
if ([string]::IsNullOrWhiteSpace($script:WalPath) -or -not (Test-Path -LiteralPath $script:WalPath)) { return }
$remaining = New-Object System.Collections.Generic.List[string]
try {
$script:WalFlushing = $true
foreach ($line in (Get-Content -LiteralPath $script:WalPath -ErrorAction SilentlyContinue)) {
if ([string]::IsNullOrWhiteSpace($line)) { continue }
try {
$entry = $line | ConvertFrom-Json
if ($null -eq $entry -or -not $entry.uri -or -not $entry.json) { continue }
if (-not (Invoke-AwJsonPost -Uri ([string]$entry.uri) -Json ([string]$entry.json))) { $remaining.Add($line) }
} catch { $remaining.Add($line) }
}
if ($remaining.Count -eq 0) {
Remove-Item -LiteralPath $script:WalPath -Force -ErrorAction SilentlyContinue
} else {
Set-Content -LiteralPath $script:WalPath -Value ($remaining -join [Environment]::NewLine) -Encoding UTF8
$httpClient = New-Object System.Net.Http.HttpClient
$content = New-Object System.Net.Http.StringContent($Json, [System.Text.Encoding]::UTF8, "application/json")
$response = $httpClient.PostAsync($Uri, $content).Result
if (-not $response.IsSuccessStatusCode) {
$status = [int]$response.StatusCode
$reason = [string]$response.ReasonPhrase
$body = $response.Content.ReadAsStringAsync().Result
Write-FileCollectorLog ("POST failed: uri={0} status={1} reason={2} body={3}" -f $Uri, $status, $reason, $body)
}
} catch {
Write-FileCollectorLog "POST Error: $($_.Exception.Message)"
} finally {
$script:WalFlushing = $false
}
}
function Write-CollectorHealth {
param([string]$Status = 'running')
if ([string]::IsNullOrWhiteSpace($script:HealthPath)) { return }
try {
$walDepth = 0
if ($script:WalPath -and (Test-Path -LiteralPath $script:WalPath)) { $walDepth = @((Get-Content -LiteralPath $script:WalPath)).Count }
$health = @{
collector = 'file-operations'; hostname = $script:Hostname; sessionId = $script:SessionId;
status = $Status; apiBase = $script:ApiBase; walDepth = $walDepth; ts = (Get-Date).ToUniversalTime().ToString('o')
} | ConvertTo-Json -Depth 5
Set-Content -LiteralPath $script:HealthPath -Value $health -Encoding UTF8
} catch {}
}
function Invoke-AwJsonPost {
param(
[Parameter(Mandatory = $true)][string]$Uri,
[Parameter(Mandatory = $true)][string]$Json,
[int]$MaxAttempts = 5,
[int]$InitialBackoffMs = 500
)
$attempt = 1
$backoff = [Math]::Max(100, $InitialBackoffMs)
while ($attempt -le $MaxAttempts) {
$httpClient = $null
try {
$httpClient = New-Object System.Net.Http.HttpClient
$content = New-Object System.Net.Http.StringContent($Json, [System.Text.Encoding]::UTF8, "application/json")
$response = $httpClient.PostAsync($Uri, $content).Result
if ($response.IsSuccessStatusCode) { return $true }
if ($attempt -ge $MaxAttempts) {
if (-not $script:WalFlushing) { Add-WalEntry -Uri $Uri -Json $Json }
return $false
}
} catch {
if ($attempt -ge $MaxAttempts) {
if (-not $script:WalFlushing) { Add-WalEntry -Uri $Uri -Json $Json }
return $false
}
} finally {
if ($null -ne $httpClient) {
$httpClient.Dispose()
}
if ($null -ne $httpClient) {
$httpClient.Dispose()
}
Start-Sleep -Milliseconds $backoff
$backoff = [Math]::Min($backoff * 2, 10000)
$attempt++
}
}
@@ -208,15 +145,11 @@ function Send-FileOperationEvent {
$config = Get-DeploymentConfig -Path $ConfigPath
if (-not $config) { throw "Configuration file not found: $ConfigPath" }
$script:Hostname = if ($config.PSObject.Properties.Name -contains 'awHostname' -and -not [string]::IsNullOrWhiteSpace([string]$config.awHostname)) { [string]$config.awHostname } else { [string]$env:COMPUTERNAME }
$scheme = if ($ServerScheme) { $ServerScheme } elseif ($config.server.scheme) { $config.server.scheme } else { 'http' }
$hostName = if ($ServerHost) { $ServerHost } elseif ($config.server.host) { $config.server.host } else { 'localhost' }
$port = if ($ServerPort) { $ServerPort } elseif ($config.server.port) { $config.server.port } else { 5600 }
$script:ApiBase = "{0}://{1}:{2}/api/0" -f $scheme, $hostName, $port
$stateRoot = if ($config.paths -and $config.paths.stateRoot) { [string]$config.paths.stateRoot } else { 'C:\ProgramData\AWatch-rus' }
$script:WalPath = Join-Path $stateRoot 'wal-file-operations.ndjson'
$script:HealthPath = Join-Path $stateRoot 'health-file-operations.json'
$bucketId = 'aw-file-operations_' + $script:Hostname
Ensure-Bucket -BucketId $bucketId -ClientName 'aw-file-operations' -BucketType 'aw.file.operation'
@@ -273,14 +206,11 @@ Write-FileCollectorLog "Collector started. Waiting for events..."
try {
while ($true) {
Flush-Wal
Write-CollectorHealth -Status 'running'
Start-Sleep -Seconds $PollSeconds
}
}
finally {
Write-FileCollectorLog "Stopping collector..."
Write-CollectorHealth -Status 'stopped'
foreach ($sub in @($subscriptions)) {
try {
if ($sub -and $sub.Id) {