Files
inquiry_robot/deploy/_remote_env_encoding_diag.ps1
T

53 lines
1.9 KiB
PowerShell

# Diagnose E:\wwwroot\.env encoding without printing secret values
$ErrorActionPreference = 'Stop'
$path = 'E:\wwwroot\.env'
$bytes = [System.IO.File]::ReadAllBytes($path)
Write-Output ("bytes=" + $bytes.Length)
Write-Output ("bom_utf8=" + ($bytes.Length -ge 3 -and $bytes[0] -eq 239 -and $bytes[1] -eq 187 -and $bytes[2] -eq 191))
Write-Output ("bom_utf16le=" + ($bytes.Length -ge 2 -and $bytes[0] -eq 255 -and $bytes[1] -eq 254))
function Show-Head([string]$label, [string]$text) {
$lines = $text -split "`r?`n" | Select-Object -First 8
Write-Output ("=== " + $label + " ===")
foreach ($line in $lines) {
if ($line -match '^(#|YTD_ENV=|PUBLIC_BASE_URL=|WECOM_CALLBACK_REPLY_TEXT=)') {
if ($line -match '^WECOM_CALLBACK_REPLY_TEXT=') {
$v = $line.Substring($line.IndexOf('=') + 1)
Write-Output ("WECOM_CALLBACK_REPLY_TEXT len=" + $v.Length + " sample=" + $v.Substring(0, [Math]::Min(24, $v.Length)))
} else {
Write-Output $line
}
} elseif ($line -match '^[A-Z0-9_]+=') {
$k = $line.Substring(0, $line.IndexOf('='))
Write-Output ($k + "=***")
} else {
Write-Output $line
}
}
}
$utf8 = [System.Text.Encoding]::UTF8.GetString($bytes)
Show-Head 'as_utf8' $utf8
$gbk = [System.Text.Encoding]::GetEncoding(936).GetString($bytes)
Show-Head 'as_gbk' $gbk
# try: utf8 mis-decoded as gbk then saved as utf8 -> recover by encode gbk getbytes then utf8 getstring
try {
$rec1 = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::GetEncoding(936).GetBytes($utf8))
Show-Head 'recover_utf8_via_gbk_bytes' $rec1
} catch {
Write-Output 'recover1_fail'
}
# try: latin1/cp1252 roundtrip
try {
$latin = [System.Text.Encoding]::GetEncoding(1252).GetString($bytes)
$rec2 = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::GetEncoding(1252).GetBytes($latin))
Show-Head 'recover_cp1252_to_utf8' $rec2
} catch {
Write-Output 'recover2_fail'
}
Write-Output 'ENV_DIAG_DONE'