User config file

This commit is contained in:
cedric 2026-01-19 09:14:29 +00:00
parent 4f3ee338ae
commit 720f440092

View file

@ -1,26 +1,9 @@
$config = @{
# =========================
# SSH configuration
# =========================
SshExe = "C:\Windows\System32\OpenSSH\ssh.exe"
AskPassExe = "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw64\bin\git-askpass.exe"
HostAlias = "alamaison"
LocalPort = 33333
# =========================
# Browser configuration
# =========================
BrowserExe = "C:\Program Files\Mozilla Firefox\firefox.exe"
BrowserProcessName = "firefox"
}
# ========================= # =========================
# Configuration du script # Configuration du script
# ========================= # =========================
Set-StrictMode -Version Latest Set-StrictMode -Version Latest
$script:DebugEnabled = $false $script:DebugEnabled = $false
#$script:ConfigFile = Join-Path $PSScriptRoot "socks-browser.config.psd1" $script:ConfigFile = Join-Path $PSScriptRoot "socks-browser.config.psd1"
$script:LogFile = Join-Path $env:TEMP "socks-browser.log" $script:LogFile = Join-Path $env:TEMP "socks-browser.log"
# ========================= # =========================
@ -40,41 +23,41 @@ function Write-DebugLog {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "[DEBUG $timestamp] $Message" $logEntry = "[DEBUG $timestamp] $Message"
# Affiche à l'écran # Affiche à l'écran
Write-Host $logEntry -ForegroundColor DarkGray Write-Host $logEntry -ForegroundColor DarkGray
# Écrit dans le fichier de log (dans %TEMP%) # Écrit dans le fichier de log (dans %TEMP%)
try { try {
$logEntry | Out-File -FilePath $script:LogFile -Encoding UTF8 -Append -ErrorAction SilentlyContinue $logEntry | Out-File -FilePath $script:LogFile -Encoding UTF8 -Append -ErrorAction SilentlyContinue
} catch { } catch {
# Ignore les erreurs d'écriture (ex: droits, disque plein) # Ignore les erreurs d'écriture (ex: droits, disque plein)
} }
} }
# ========================= # =========================
# Fonctions principales # Fonctions principales
# ========================= # =========================
#
#function Get-Configuration { function Get-Configuration {
# if (-not (Test-Path $script:ConfigFile)) { if (-not (Test-Path $script:ConfigFile)) {
# throw "Fichier de configuration introuvable : $script:ConfigFile" throw "Fichier de configuration introuvable : $script:ConfigFile"
# } }
#
# $config = Import-PowerShellDataFile $script:ConfigFile $config = Import-PowerShellDataFile $script:ConfigFile
#
# $requiredKeys = @( $requiredKeys = @(
# "SshExe", "AskPassExe", "HostAlias", "SshExe", "AskPassExe", "HostAlias",
# "LocalPort", "BrowserExe", "BrowserProcessName" "LocalPort", "BrowserExe", "BrowserProcessName"
# ) )
#
# foreach ($key in $requiredKeys) { foreach ($key in $requiredKeys) {
# if (-not $config.ContainsKey($key) -or [string]::IsNullOrWhiteSpace($config[$key])) { if (-not $config.ContainsKey($key) -or [string]::IsNullOrWhiteSpace($config[$key])) {
# throw "Valeur de configuration manquante ou vide : $key" throw "Valeur de configuration manquante ou vide : $key"
# } }
# } }
#
# return $config return $config
#} }
function Test-PortAvailable { function Test-PortAvailable {
param ([int]$Port) param ([int]$Port)
@ -95,12 +78,12 @@ function Start-SshTunnel {
$env:SSH_ASKPASS_REQUIRE = "force" $env:SSH_ASKPASS_REQUIRE = "force"
$env:DISPLAY = "dummy" $env:DISPLAY = "dummy"
Write-DebugLog "Démarrage du tunnel SSH : ssh -N -D $LocalPort $HostAlias" Write-DebugLog "Démarrage du tunnel SSH : ssh -N -D $LocalPort $HostAlias"
Start-Process $SshExe -ArgumentList "-N -D $LocalPort $HostAlias" -WindowStyle Hidden Start-Process $SshExe -ArgumentList "-N -D $LocalPort $HostAlias" -WindowStyle Hidden
for ($attempt = 0; $attempt -lt 10; $attempt++) { for ($attempt = 0; $attempt -lt 10; $attempt++) {
if (Get-NetTCPConnection -LocalPort $LocalPort -State Listen -ErrorAction SilentlyContinue) { if (Get-NetTCPConnection -LocalPort $LocalPort -State Listen -ErrorAction SilentlyContinue) {
Write-DebugLog "Le tunnel SSH écoute sur le port $LocalPort" Write-DebugLog "Le tunnel SSH écoute sur le port $LocalPort"
return $true return $true
} }
Start-Sleep 1 Start-Sleep 1
@ -112,7 +95,7 @@ function Start-SshTunnel {
function Stop-SshTunnelByPort { function Stop-SshTunnelByPort {
param ([int]$LocalPort) param ([int]$LocalPort)
Write-DebugLog "Arrêt du tunnel SSH sur le port $LocalPort" Write-DebugLog "Arrêt du tunnel SSH sur le port $LocalPort"
$sshProcessIds = Get-NetTCPConnection -LocalPort $LocalPort -State Listen -ErrorAction SilentlyContinue | $sshProcessIds = Get-NetTCPConnection -LocalPort $LocalPort -State Listen -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty OwningProcess -Unique Select-Object -ExpandProperty OwningProcess -Unique
@ -121,7 +104,7 @@ function Stop-SshTunnelByPort {
try { try {
$proc = Get-Process -Id $sshPid -ErrorAction Stop $proc = Get-Process -Id $sshPid -ErrorAction Stop
if ($proc.ProcessName -eq "ssh") { if ($proc.ProcessName -eq "ssh") {
Write-DebugLog "Arrêt du processus SSH PID $sshPid" Write-DebugLog "Arrêt du processus SSH PID $sshPid"
Stop-Process -Id $sshPid -ErrorAction SilentlyContinue Stop-Process -Id $sshPid -ErrorAction SilentlyContinue
} }
} catch {} } catch {}
@ -206,7 +189,7 @@ user_pref("startup.homepage_welcome_url.additional", "");
"@ "@
Set-Content -Path (Join-Path $tempProfile "user.js") -Value $userJs -Encoding ASCII Set-Content -Path (Join-Path $tempProfile "user.js") -Value $userJs -Encoding ASCII
Write-DebugLog "Profil temporaire créé : $tempProfile" Write-DebugLog "Profil temporaire créé : $tempProfile"
return $tempProfile return $tempProfile
} }
@ -231,12 +214,12 @@ function Launch-BrowserAndWaitForInstance {
$newProcessIds = $currentProcessIds | Where-Object { $_ -notin $existingProcessIds } $newProcessIds = $currentProcessIds | Where-Object { $_ -notin $existingProcessIds }
if ($newProcessIds) { if ($newProcessIds) {
$tempBrowserProcessId = $newProcessIds | Select-Object -First 1 $tempBrowserProcessId = $newProcessIds | Select-Object -First 1
Write-DebugLog "Instance temporaire détectée (PID $tempBrowserProcessId)" Write-DebugLog "Instance temporaire détectée (PID $tempBrowserProcessId)"
return $tempBrowserProcessId return $tempBrowserProcessId
} }
} }
throw "Impossible de détecter l'instance temporaire du navigateur" throw "Impossible de détecter l'instance temporaire du navigateur"
} }
function Wait-ForProcessExit { function Wait-ForProcessExit {
@ -266,7 +249,7 @@ function Remove-TemporaryProfile {
# Orchestrateur principal # Orchestrateur principal
# ========================= # =========================
try { try {
# Ajouter une ligne de séparation dans le log à chaque exécution # Ajouter une ligne de séparation dans le log à chaque exécution
if ($script:DebugEnabled) { if ($script:DebugEnabled) {
$separator = "=" * 60 $separator = "=" * 60
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
@ -278,11 +261,11 @@ try {
$ownerPid = (Get-NetTCPConnection -LocalPort $config.LocalPort -State Listen).OwningProcess $ownerPid = (Get-NetTCPConnection -LocalPort $config.LocalPort -State Listen).OwningProcess
$ownerProc = Get-Process -Id $ownerPid -ErrorAction SilentlyContinue $ownerProc = Get-Process -Id $ownerPid -ErrorAction SilentlyContinue
$procName = if ($ownerProc) { "$($ownerProc.Name) (PID $ownerPid)" } else { "PID $ownerPid" } $procName = if ($ownerProc) { "$($ownerProc.Name) (PID $ownerPid)" } else { "PID $ownerPid" }
throw "Le port $($config.LocalPort) est déjà utilisé par $procName" throw "Le port $($config.LocalPort) est déjà utilisé par $procName"
} }
if (-not (Start-SshTunnel -SshExe $config.SshExe -LocalPort $config.LocalPort -HostAlias $config.HostAlias -AskPassExe $config.AskPassExe)) { if (-not (Start-SshTunnel -SshExe $config.SshExe -LocalPort $config.LocalPort -HostAlias $config.HostAlias -AskPassExe $config.AskPassExe)) {
throw "Échec de l'établissement du tunnel SSH" throw "Échec de l'établissement du tunnel SSH"
} }
Write-Output "Tunnel SSH actif sur 127.0.0.1:$($config.LocalPort)" Write-Output "Tunnel SSH actif sur 127.0.0.1:$($config.LocalPort)"
@ -290,15 +273,15 @@ try {
$tempProfile = Create-TemporaryBrowserProfile -LocalPort $config.LocalPort $tempProfile = Create-TemporaryBrowserProfile -LocalPort $config.LocalPort
$browserProcessId = Launch-BrowserAndWaitForInstance -BrowserExe $config.BrowserExe -BrowserProcessName $config.BrowserProcessName -ProfilePath $tempProfile $browserProcessId = Launch-BrowserAndWaitForInstance -BrowserExe $config.BrowserExe -BrowserProcessName $config.BrowserProcessName -ProfilePath $tempProfile
Write-Output "Instance temporaire du navigateur détectée (PID $browserProcessId)" Write-Output "Instance temporaire du navigateur détectée (PID $browserProcessId)"
Wait-ForProcessExit -ProcessId $browserProcessId Wait-ForProcessExit -ProcessId $browserProcessId
Write-Output "Navigateur temporaire fermé" Write-Output "Navigateur temporaire fermé"
Remove-TemporaryProfile -ProfilePath $tempProfile Remove-TemporaryProfile -ProfilePath $tempProfile
Stop-SshTunnelByPort -LocalPort $config.LocalPort Stop-SshTunnelByPort -LocalPort $config.LocalPort
Write-Output "Tunnel SSH arrêté proprement" Write-Output "Tunnel SSH arrêté proprement"
} catch { } catch {
$errorMessage = "ERREUR: $($_.Exception.Message)" $errorMessage = "ERREUR: $($_.Exception.Message)"