Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<#
.SYNOPSIS
Download OpenVPN profile from dl.php.
.DESCRIPTION
Posts optional form fields (username/token) to the given dl.php URL and saves the response to a file.
This is a sample script and may need to be modified to suit your needs.
You will need to set the URL, Username, Token, router, filetype, and output file as needed.
FileType may be one of 'ovpn', 'qr' or 'refresh'. refresh will generate a refresh request to the server for that
particular router.
#>
$Url = 'http://localhost/totp_opnsense/dl.php'
$Username = ''
$Token = ''
$OutputFile = 'client.ovpn'
$Router = ''
$FileType = ''
$Insecure = $true
$Quiet = $false
if ($Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; };
$form = @{};
if ($Username -ne '') { $form['username'] = $Username; };
if ($Token -ne '') { $form['token'] = $Token; };
if ($Router -ne '') { $form['router'] = $Router; };
if ($FileType -ne '') { $form['filetype'] = $FileType; };
try {
$resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -SessionVariable session -OutFile $OutputFile -ErrorAction Stop;
} catch {
Write-Error 'Request failed:'; Write-Error $_.Exception.Message; exit 1;
};
# If server suggested a filename, use it
$cd = $null;
if ($resp.Headers.ContainsKey('Content-Disposition')) { $cd = $resp.Headers['Content-Disposition']; };
if ($cd -and ($cd -match 'filename="?([^\";]+)"?')) {
$serverName = $Matches[1];
if ($serverName -and ($serverName -ne $OutputFile)) {
try {
Move-Item -Force -Path $OutputFile -Destination $serverName;
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $serverName); };
exit 0;
} catch {
Write-Warning 'Failed to rename downloaded file to server-suggested filename.';
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
exit 0;
};
} else {
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
exit 0;
};
} else {
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
exit 0;
};