# SSL-Konfiguration für MYP-Plattform # Dieses Skript führt alle notwendigen Schritte zur SSL-Konfiguration durch # Titel anzeigen Write-Host "====================================================" -ForegroundColor Cyan Write-Host " SSL-Konfiguration für MYP-Plattform" -ForegroundColor Cyan Write-Host "====================================================" -ForegroundColor Cyan Write-Host "" # Prüfen, ob PowerShell als Administrator ausgeführt wird $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "WARNUNG: Dieses Skript sollte als Administrator ausgeführt werden, um Zertifikate im System zu installieren." -ForegroundColor Yellow Write-Host "Einige Funktionen sind möglicherweise eingeschränkt." -ForegroundColor Yellow Write-Host "" $continueAnyway = Read-Host "Möchten Sie trotzdem fortfahren? (j/n)" if ($continueAnyway -ne "j") { Write-Host "Installation abgebrochen." -ForegroundColor Red exit } } # Funktion zum Ausführen von Befehlen mit Ausgabe function Exec-Command { param ( [string]$Command, [string]$Description ) Write-Host "> $Description..." -ForegroundColor Yellow try { Invoke-Expression $Command if ($LASTEXITCODE -eq 0 -or $null -eq $LASTEXITCODE) { Write-Host "✓ Erfolgreich abgeschlossen!" -ForegroundColor Green return $true } else { Write-Host "✗ Fehler beim Ausführen des Befehls. Exit-Code: $LASTEXITCODE" -ForegroundColor Red return $false } } catch { Write-Host "✗ Fehler: $_" -ForegroundColor Red return $false } } # Prüfen, ob Python und Node.js installiert sind $pythonInstalled = Get-Command python -ErrorAction SilentlyContinue $nodeInstalled = Get-Command node -ErrorAction SilentlyContinue if (-not $pythonInstalled) { Write-Host "✗ Python ist nicht installiert oder nicht im PATH. Bitte installieren Sie Python 3.6 oder höher." -ForegroundColor Red exit 1 } if (-not $nodeInstalled) { Write-Host "✗ Node.js ist nicht installiert oder nicht im PATH. Bitte installieren Sie Node.js 14 oder höher." -ForegroundColor Red exit 1 } # 1. Prüfen, ob die notwendigen Abhängigkeiten installiert sind Write-Host "1. Prüfe Abhängigkeiten..." -ForegroundColor Cyan # Python-Abhängigkeiten prüfen $cryptographyInstalled = python -c "try: import cryptography; print('True'); except ImportError: print('False')" 2>$null if ($cryptographyInstalled -ne "True") { Write-Host "Installiere Python-Abhängigkeit 'cryptography'..." Exec-Command "pip install cryptography" "Installiere cryptography-Paket" } # 2. SSL-Zertifikat generieren Write-Host "" Write-Host "2. Generiere SSL-Zertifikat..." -ForegroundColor Cyan $certGenSuccess = Exec-Command "cd backend && python generate_ssl_cert.py" "Generiere SSL-Zertifikat" if (-not $certGenSuccess) { Write-Host "✗ Fehler bei der Zertifikatsgenerierung. Abbruch." -ForegroundColor Red exit 1 } # 3. Zertifikat im System installieren (nur wenn als Administrator ausgeführt) Write-Host "" Write-Host "3. Installiere Zertifikat im System..." -ForegroundColor Cyan if ($isAdmin) { $certPath = Resolve-Path ".\backend\app\instance\ssl\myp.crt" Write-Host "Installiere Zertifikat im System-Zertifikatsspeicher..." # In Root-Zertifikatsspeicher installieren Exec-Command "certutil -addstore -f 'ROOT' '$certPath'" "Installiere im Root-Zertifikatsspeicher" # In CA-Zertifikatsspeicher installieren Exec-Command "certutil -addstore -f 'CA' '$certPath'" "Installiere im CA-Zertifikatsspeicher" # In Persönlichen Zertifikatsspeicher installieren Exec-Command "certutil -addstore -f 'MY' '$certPath'" "Installiere im Persönlichen Zertifikatsspeicher" } else { Write-Host "Überspringen der System-Installation, da das Skript nicht als Administrator ausgeführt wird." -ForegroundColor Yellow Write-Host "Sie können das Zertifikat später manuell installieren durch:" -ForegroundColor Yellow Write-Host "certutil -addstore -f ROOT .\backend\app\instance\ssl\myp.crt" -ForegroundColor Gray } # 4. Frontend konfigurieren Write-Host "" Write-Host "4. Konfiguriere Frontend..." -ForegroundColor Cyan $frontendConfigSuccess = Exec-Command "cd frontend && node configure_ssl.js" "Konfiguriere Frontend" if (-not $frontendConfigSuccess) { Write-Host "✗ Warnung: Frontend-Konfiguration konnte nicht vollständig durchgeführt werden." -ForegroundColor Yellow } # 5. Docker-Compose Datei aktualisieren Write-Host "" Write-Host "5. Aktualisiere Docker-Compose-Konfiguration..." -ForegroundColor Cyan $dockerComposeFile = "docker-compose.yml" if (Test-Path $dockerComposeFile) { $dockerCompose = Get-Content $dockerComposeFile -Raw # Sicherstellen, dass dual-protocol aktiv ist if (-not $dockerCompose.Contains("--dual-protocol")) { $dockerCompose = $dockerCompose -replace "command: python -m app\.app", "command: python -m app.app --dual-protocol" $dockerCompose | Set-Content $dockerComposeFile Write-Host "✓ Docker-Compose-Datei wurde aktualisiert, um den dual-protocol-Modus zu aktivieren." -ForegroundColor Green } else { Write-Host "✓ Docker-Compose-Datei ist bereits korrekt konfiguriert." -ForegroundColor Green } } else { Write-Host "✗ Docker-Compose-Datei nicht gefunden. Überspringe diese Konfiguration." -ForegroundColor Yellow } # 6. Systemdienste aktualisieren (falls auf Raspberry Pi) Write-Host "" Write-Host "6. Möchten Sie die Konfiguration auf den Raspberry Pi übertragen?" -ForegroundColor Cyan $raspberryConfig = Read-Host "Übertragen und konfigurieren (j/n)" if ($raspberryConfig -eq "j") { Write-Host "Konfiguriere Raspberry Pi..." -ForegroundColor Yellow # Raspberry Pi-Konfiguration ausführen $sshUser = Read-Host "Benutzername für SSH-Verbindung zum Raspberry Pi (Standard: pi)" if (-not $sshUser) { $sshUser = "pi" } $sshHost = Read-Host "Hostname oder IP-Adresse des Raspberry Pi (Standard: raspberrypi)" if (-not $sshHost) { $sshHost = "raspberrypi" } $sshDestDir = "/home/$sshUser/myp/ssl" $systemCertDir = "/usr/local/share/ca-certificates" # Verzeichnis erstellen Exec-Command "ssh $sshUser@$sshHost 'mkdir -p $sshDestDir'" "Erstelle Verzeichnis auf dem Raspberry Pi" # Dateien kopieren $certPath = Resolve-Path ".\backend\app\instance\ssl\myp.crt" $keyPath = Resolve-Path ".\backend\app\instance\ssl\myp.key" Exec-Command "scp '$certPath' $sshUser@$sshHost`:$sshDestDir/myp.crt" "Kopiere Zertifikat auf den Raspberry Pi" Exec-Command "scp '$keyPath' $sshUser@$sshHost`:$sshDestDir/myp.key" "Kopiere Schlüssel auf den Raspberry Pi" # Berechtigungen setzen Exec-Command "ssh $sshUser@$sshHost 'chmod 600 $sshDestDir/myp.key'" "Setze Berechtigungen für den Schlüssel" # Im System installieren Exec-Command "ssh $sshUser@$sshHost 'sudo mkdir -p $systemCertDir && sudo cp $sshDestDir/myp.crt $systemCertDir/ && sudo update-ca-certificates'" "Installiere Zertifikat im System" # Docker-Container neu starten, falls vorhanden Write-Host "Möchten Sie die Docker-Container auf dem Raspberry Pi neu starten?" -ForegroundColor Yellow $restartDocker = Read-Host "Neu starten (j/n)" if ($restartDocker -eq "j") { Exec-Command "ssh $sshUser@$sshHost 'cd /home/$sshUser/myp && docker-compose down && docker-compose up -d'" "Starte Docker-Container neu" } } # Abschluss Write-Host "" Write-Host "====================================================" -ForegroundColor Cyan Write-Host " SSL-Konfiguration abgeschlossen" -ForegroundColor Cyan Write-Host "====================================================" -ForegroundColor Cyan Write-Host "" Write-Host "Das SSL-Zertifikat wurde erfolgreich generiert und konfiguriert." -ForegroundColor Green Write-Host "Sie können nun auf folgende Weise auf die Anwendung zugreifen:" -ForegroundColor Green Write-Host "- Backend: https://raspberrypi:443" -ForegroundColor Green Write-Host "- Frontend: https://localhost:3000" -ForegroundColor Green Write-Host "" Write-Host "Hinweis: Bei der ersten Verbindung müssen Sie möglicherweise" -ForegroundColor Yellow Write-Host "das selbstsignierte Zertifikat in Ihrem Browser akzeptieren." -ForegroundColor Yellow Write-Host "" # Pause, damit das Fenster nicht sofort geschlossen wird if ($Host.Name -eq "ConsoleHost") { Write-Host "Drücken Sie eine beliebige Taste, um fortzufahren..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") }