$SshKey = "C:\Users\Administrator\.ssh\id_rsa_tokyo" $VpsHost = "158.179.179.29" function Invoke-RemoteSql([string]$sql) { $b64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($sql)) $remoteCmd = "echo '$b64' | base64 -d > /tmp/run_query.sql && sudo -u postgres psql -d classnotify -t -A -F '|' -f /tmp/run_query.sql 2>&1 && rm -f /tmp/run_query.sql" $out = ssh -i $SshKey -o StrictHostKeyChecking=no -o ConnectTimeout=15 -o BatchMode=yes root@$VpsHost $remoteCmd if ($out) { return ($out -join "`n").Trim() } return "" } Write-Host "=== TEST: Backend Expiration Truth & Invalidation Gate ===" -ForegroundColor Cyan # 1. Login as teacher $tUser = "teacher_b_7828" $tPass = "Teacher123" $loginBody = @{ username = $tUser; password = $tPass } | ConvertTo-Json $loginRes = Invoke-RestMethod -Uri "https://classnotify.paradise.eu.org/api/auth/login" -Method Post -Body $loginBody -ContentType "application/json" $token = $loginRes.token $headers = @{ Authorization = "Bearer $token" } # Get classroom ID $classes = Invoke-RestMethod -Uri "https://classnotify.paradise.eu.org/api/notification/classes" -Method Get -Headers $headers $classId = $classes[0].id $className = $classes[0].displayName Write-Host "Teacher authenticated. Target Class: $className ($classId)" # 2. Query original license $origUntil = Invoke-RemoteSql "SELECT \"LicensedUntil\" FROM \"Devices\" WHERE \"Id\" = 'dev_e059ad69f1064480';" Write-Host "Original LicensedUntil in DB: $origUntil" try { # 3. Temporarily set LicensedUntil to past (1 hour ago) Write-Host "`n[Step 1] Simulating license expiration: Setting LicensedUntil to 1 hour ago in DB..." -ForegroundColor Yellow Invoke-RemoteSql "UPDATE \"Devices\" SET \"LicensedUntil\" = NOW() - INTERVAL '1 hour' WHERE \"Id\" = 'dev_e059ad69f1064480';" # Verify device status via API $devices = Invoke-RestMethod -Uri "https://classnotify.paradise.eu.org/api/notification/classroom-devices" -Method Get -Headers $headers $dev = $devices | Where-Object { $_.deviceId -eq "dev_e059ad69f1064480" } Write-Host "Device LicenseStatus via API: $($dev.licenseStatus), RemainingDays: $($dev.remainingDays)" # 4. Attempt to send normal notification (Type = 0) Write-Host "`n[Step 2] Sending Normal Notice to expired device..." -ForegroundColor Yellow $normalReq = @{ classId = $classId studentName = "" title = "常规通知测试" message = "这应该被服务端 402 拦截" type = 0 # Notice voice = $false durationSeconds = 15 } | ConvertTo-Json $bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($normalReq) $intercepted = $false try { $sendRes = Invoke-RestMethod -Uri "https://classnotify.paradise.eu.org/api/notification/send" -Method Post -Headers $headers -Body $bodyBytes -ContentType "application/json; charset=utf-8" Write-Host "UNEXPECTED: Normal notification was NOT blocked! Status: Success" -ForegroundColor Red } catch { $resp = $_.Exception.Response if ($resp -and [int]$resp.StatusCode -eq 402) { $stream = $resp.GetResponseStream() $reader = New-Object System.IO.StreamReader($stream) $errorBody = $reader.ReadToEnd() Write-Host "SUCCESS: Server strictly intercepted normal notification with HTTP 402!" -ForegroundColor Green Write-Host "Response Body: $errorBody" -ForegroundColor Green $intercepted = $true } else { Write-Host "Got unexpected error: $_" -ForegroundColor Red } } if (-not $intercepted) { throw "Verification Failed: Normal notification was not rejected with 402." } # 5. Attempt to send Urgent notification (Type = 2, Life Safety Invariant) Write-Host "`n[Step 3] Sending Urgent Notice (Life Safety Invariant) to expired device..." -ForegroundColor Yellow $urgentReq = @{ classId = $classId studentName = "" title = "紧急避险演练" message = "火警演习:全体师生立即下楼" type = 2 # Urgent voice = $false durationSeconds = 15 } | ConvertTo-Json $urgentBytes = [System.Text.Encoding]::UTF8.GetBytes($urgentReq) $urgentSent = $false try { $urgentRes = Invoke-RestMethod -Uri "https://classnotify.paradise.eu.org/api/notification/send" -Method Post -Headers $headers -Body $urgentBytes -ContentType "application/json; charset=utf-8" if ($urgentRes.success) { Write-Host "SUCCESS: Urgent notification bypassed license gate as designed! MessageId: $($urgentRes.messageId)" -ForegroundColor Green $urgentSent = $true } } catch { Write-Host "Urgent notification failed: $_" -ForegroundColor Red } if (-not $urgentSent) { throw "Verification Failed: Urgent notification was unexpectedly blocked." } } finally { # 6. Restore original license Write-Host "`n[Step 4] Restoring original license in DB..." -ForegroundColor Cyan Invoke-RemoteSql "UPDATE \"Devices\" SET \"LicensedUntil\" = '$origUntil' WHERE \"Id\" = 'dev_e059ad69f1064480';" $restored = Invoke-RemoteSql "SELECT \"LicensedUntil\" FROM \"Devices\" WHERE \"Id\" = 'dev_e059ad69f1064480';" Write-Host "Restored LicensedUntil: $restored" $devices = Invoke-RestMethod -Uri "https://classnotify.paradise.eu.org/api/notification/classroom-devices" -Method Get -Headers $headers $dev = $devices | Where-Object { $_.deviceId -eq "dev_e059ad69f1064480" } Write-Host "Device Status Restored: $($dev.licenseStatus), RemainingDays: $($dev.remainingDays), RemainingHours: $($dev.remainingHours)" } Write-Host "`n=== BACKEND EXPIRATION TRUTH VERIFICATION COMPLETE: ALL GATES PASS ===" -ForegroundColor Green