| Line 8... |
Line 8... |
| 8 |
You will need to set the URL, Username, Token, router, filetype, and output file as needed.
|
8 |
You will need to set the URL, Username, Token, router, filetype, and output file as needed.
|
| 9 |
FileType may be one of 'ovpn', 'qr' or 'refresh'. refresh will generate a refresh request to the server for that
|
9 |
FileType may be one of 'ovpn', 'qr' or 'refresh'. refresh will generate a refresh request to the server for that
|
| 10 |
particular router.
|
10 |
particular router.
|
| 11 |
#>
|
11 |
#>
|
| 12 |
|
12 |
|
| - |
|
13 |
# Fill in these variables to match your setup
|
| 13 |
$Url = 'http://localhost/totp_opnsense/dl.php'
|
14 |
$Url = 'http://www.example.org/dl.php' # URL of dl.php
|
| 14 |
$Username = ''
|
15 |
$Username = '' # The username on the OVPN Router
|
| 15 |
$Token = ''
|
16 |
$Key = '' # The key for the OVPN Router
|
| 16 |
$OutputFile = 'client.ovpn'
|
17 |
$OutputFile = 'client.ovpn' # the default output filename
|
| 17 |
$Router = ''
|
18 |
$Router = '' # the router name as configured on the OVPN server
|
| 18 |
$FileType = ''
|
19 |
$FileType = 'ovpn' # 'ovpn', 'qr', or 'refresh'
|
| 19 |
$Insecure = $true
|
20 |
$Insecure = $false # set to $true to ignore SSL certificate errors
|
| 20 |
$Quiet = $false
|
21 |
$Quiet = $false # set to $true to suppress output messages
|
| 21 |
|
22 |
|
| 22 |
if ($Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; };
|
23 |
# call the function with the script-level variables. This will download to the current directory.
|
| - |
|
24 |
# Get the ovpn file
|
| - |
|
25 |
$null = Get-OpenVpnProfile -Url $Url -Username $Username -Key $Key -OutputFile 'client.ovpn' -Router $Router -FileType 'ovpn' -Insecure:$Insecure -Quiet:$Quiet;
|
| - |
|
26 |
# get the qr code image
|
| - |
|
27 |
$null = Get-OpenVpnProfile -Url $Url -Username $Username -Key $Key -OutputFile 'client.png' -Router $Router -FileType 'qr' -Insecure:$Insecure -Quiet:$Quiet;
|
| - |
|
28 |
|
| 23 |
|
29 |
|
| - |
|
30 |
# Place this anywhere in your script. It defines a function to download the OpenVPN profile.
|
| - |
|
31 |
function Get-OpenVpnProfile {
|
| 24 |
$form = @{};
|
32 |
param(
|
| - |
|
33 |
[string]$Url = '',
|
| 25 |
if ($Username -ne '') { $form['username'] = $Username; };
|
34 |
[string]$Username = '',
|
| - |
|
35 |
[string]$Key = '',
|
| 26 |
if ($Token -ne '') { $form['token'] = $Token; };
|
36 |
[string]$OutputFile = '',
|
| 27 |
if ($Router -ne '') { $form['router'] = $Router; };
|
37 |
[string]$Router = '',
|
| 28 |
if ($FileType -ne '') { $form['filetype'] = $FileType; };
|
38 |
[string]$FileType = '',
|
| 29 |
|
- |
|
| - |
|
39 |
[switch]$Insecure,
|
| - |
|
40 |
[switch]$Quiet
|
| 30 |
try {
|
41 |
);
|
| 31 |
$resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -SessionVariable session -OutFile $OutputFile -ErrorAction Stop;
|
42 |
Write-Debug ('Downloading OpenVPN profile from {0}' -f $Url);
|
| 32 |
} catch {
|
- |
|
| 33 |
Write-Error 'Request failed:'; Write-Error $_.Exception.Message; exit 1;
|
43 |
if ($Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; };
|
| 34 |
};
|
- |
|
| 35 |
|
44 |
|
| 36 |
# If server suggested a filename, use it
|
- |
|
| 37 |
$cd = $null;
|
45 |
$form = @{};
|
| 38 |
if ($resp.Headers.ContainsKey('Content-Disposition')) { $cd = $resp.Headers['Content-Disposition']; };
|
46 |
if ($Username -ne '') { $form['user'] = $Username; };
|
| 39 |
if ($cd -and ($cd -match 'filename="?([^\";]+)"?')) {
|
47 |
if ($Key -ne '') { $form['key'] = $Key; };
|
| 40 |
$serverName = $Matches[1];
|
48 |
if ($Router -ne '') { $form['router'] = $Router; };
|
| 41 |
if ($serverName -and ($serverName -ne $OutputFile)) {
|
49 |
if ($FileType -ne '') { $form['filetype'] = $FileType; };
|
| - |
|
50 |
|
| - |
|
51 |
# First, make a request to see if we get a filename suggestion
|
| 42 |
try {
|
52 |
try {
|
| 43 |
Move-Item -Force -Path $OutputFile -Destination $serverName;
|
53 |
$resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -ErrorAction Stop;
|
| - |
|
54 |
} catch {
|
| 44 |
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $serverName); };
|
55 |
Write-Error 'Request failed:'; Write-Error $_.Exception.Message; exit 1;
|
| 45 |
exit 0;
|
56 |
};
|
| - |
|
57 |
|
| - |
|
58 |
# If server suggested a filename, use it
|
| 46 |
} catch {
|
59 |
$disposition = $null;
|
| 47 |
Write-Warning 'Failed to rename downloaded file to server-suggested filename.';
|
60 |
if ($resp.Headers.ContainsKey('Content-Disposition')) { $disposition = $resp.Headers['Content-Disposition']; };
|
| - |
|
61 |
# normalize to a single string if header is an array
|
| 48 |
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
|
62 |
if ($disposition -is [array]) { $disposition = $disposition -join ';'; };
|
| 49 |
exit 0;
|
63 |
$match = [regex]::Match($disposition, 'filename="([^"]+)"');
|
| - |
|
64 |
if ($match.Success) { $OutputFile = $match.Groups[1].Value; }
|
| 50 |
};
|
65 |
|
| - |
|
66 |
# now, actually download the file
|
| 51 |
} else {
|
67 |
try {
|
| 52 |
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
|
68 |
$resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -OutFile $OutputFile -ErrorAction Stop;
|
| - |
|
69 |
return $OutputFile;
|
| 53 |
exit 0;
|
70 |
} catch {
|
| - |
|
71 |
Write-Warning 'Failed to Download';
|
| 54 |
};
|
72 |
};
|
| 55 |
} else {
|
- |
|
| 56 |
if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
|
- |
|
| 57 |
exit 0;
|
- |
|
| 58 |
};
|
73 |
};
|
| - |
|
74 |
|
| - |
|
75 |
|