Functions/GenXdev.Windows.WireGuard/Get-WireGuardPeerQRCode.ps1
|
############################################################################### <# .SYNOPSIS Generates a QR code for a WireGuard VPN peer configuration. .DESCRIPTION This function generates a QR code for a WireGuard VPN peer configuration that can be scanned by mobile devices for easy setup. The QR code is displayed in the console and can be used to quickly configure WireGuard clients on smartphones and tablets. The function interacts with the linuxserver/wireguard Docker container to generate QR codes for peer configurations. .LICENSE Copyright (C) 2026 René Vaessen / GenXdev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. .PARAMETER PeerName The name of the peer to generate a QR code for. .PARAMETER NoDockerInitialize Skip Docker initialization (used when already called by parent function). .PARAMETER Force Force rebuild of Docker container and remove existing data. .PARAMETER ContainerName The name for the Docker container. .PARAMETER VolumeName The name for the Docker volume for persistent storage. .PARAMETER ServicePort The port number for the WireGuard service. .PARAMETER HealthCheckTimeout Maximum time in seconds to wait for service health check. .PARAMETER HealthCheckInterval Interval in seconds between health check attempts. .PARAMETER ImageName Custom Docker image name to use. .PARAMETER PUID User ID for permissions in the container. .PARAMETER PGID Group ID for permissions in the container. .PARAMETER TimeZone Timezone to use for the container. .EXAMPLE Get-WireGuardPeerQRCode -PeerName "MyPhone" .EXAMPLE Get-WireGuardPeerQRCode -PeerName "Tablet" -NoDockerInitialize .NOTES This function requires the container to be running (use EnsureWireGuard first) and the peer to exist (use Add-WireGuardPeer to create peers). #> function Get-WireGuardPeerQRCode { [CmdletBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] param( ############################################################################### [Parameter( Position = 0, Mandatory = $true, HelpMessage = 'The name of the peer to generate a QR code for' )] [ValidateNotNullOrEmpty()] [string] $PeerName, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Skip Docker initialization (used when already ' + 'called by parent function)') )] [switch] $NoDockerInitialize, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Force rebuild of Docker container and remove ' + 'existing data') )] [Alias('ForceRebuild')] [switch] $Force, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The name for the Docker container' )] [ValidateNotNullOrEmpty()] [string] $ContainerName = 'wireguard', ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The name for the Docker volume for persistent storage' )] [ValidateNotNullOrEmpty()] [string] $VolumeName = 'wireguard_data', ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The port number for the WireGuard service' )] [ValidateRange(1, 65535)] [int] $ServicePort = 51839, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Maximum time in seconds to wait for service ' + 'health check') )] [ValidateRange(10, 300)] [int] $HealthCheckTimeout = 60, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Interval in seconds between health check attempts' )] [ValidateRange(1, 10)] [int] $HealthCheckInterval = 3, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Custom Docker image name to use' )] [ValidateNotNullOrEmpty()] [string] $ImageName = 'linuxserver/wireguard', ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'User ID for permissions in the container' )] [ValidateNotNullOrEmpty()] [string] $PUID = '1000', ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Group ID for permissions in the container' )] [ValidateNotNullOrEmpty()] [string] $PGID = '1000', ############################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Timezone to use for the container' )] [ValidateNotNullOrEmpty()] [string] $TimeZone = 'Etc/UTC', ############################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Automatically consent to WireGuard installation ' + 'and set persistent flag.') )] [switch] $AutoConsent, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Automatically consent to third-party software ' + 'installation and set persistent flag for all packages.') )] [switch] $AutoConsentAllPackages, ############################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Use alternative settings stored in session for ' + 'preferences') )] [switch]$SessionOnly ############################################################################### ) begin { # ensure the WireGuard service is running if (-not $NoDockerInitialize) { Microsoft.PowerShell.Utility\Write-Verbose ` 'Ensuring WireGuard service is available' # copy matching parameters to pass to EnsureWireGuard function $ensureParams = GenXdev\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev\EnsureWireGuard' ` -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable ` -Scope Local ` -ErrorAction SilentlyContinue) # initialize WireGuard service with specified parameters $null = GenXdev\EnsureWireGuard @ensureParams } else { Microsoft.PowerShell.Utility\Write-Verbose ` 'Skipping Docker initialization as requested' } } process { try { Microsoft.PowerShell.Utility\Write-Verbose ` "Displaying QR code for peer: $PeerName" # use the container's built-in QR code display docker exec $ContainerName /app/show-peer $PeerName if ($LASTEXITCODE -ne 0) { throw "Failed to display QR code for peer '$PeerName'" } Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green ` "QR code displayed for peer '$PeerName'" } catch { Microsoft.PowerShell.Utility\Write-Error ` "Failed to generate QR code for peer '$PeerName': $_" throw } } } |