Subversion Repositories web_pages

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 rodolico 1
<#
2
.SYNOPSIS
3
   Download OpenVPN profile from dl.php.
4
 
5
.DESCRIPTION
6
   Posts optional form fields (username/token) to the given dl.php URL and saves the response to a file.
7
   This is a sample script and may need to be modified to suit your needs.
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 
10
   particular router.
11
#>
12
 
13
$Url = 'http://localhost/totp_opnsense/dl.php'
14
$Username = ''
15
$Token = ''
16
$OutputFile = 'client.ovpn'
17
$Router = ''
18
$FileType = ''
19
$Insecure = $true
20
$Quiet = $false
21
 
22
if ($Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; };
23
 
24
$form = @{}; 
25
if ($Username -ne '') { $form['username'] = $Username; };
26
if ($Token -ne '') { $form['token'] = $Token; };
27
if ($Router -ne '') { $form['router'] = $Router; };
28
if ($FileType -ne '') { $form['filetype'] = $FileType; };
29
 
30
try {
31
   $resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -SessionVariable session -OutFile $OutputFile -ErrorAction Stop;
32
} catch {
33
   Write-Error 'Request failed:'; Write-Error $_.Exception.Message; exit 1;
34
};
35
 
36
# If server suggested a filename, use it
37
$cd = $null;
38
if ($resp.Headers.ContainsKey('Content-Disposition')) { $cd = $resp.Headers['Content-Disposition']; };
39
if ($cd -and ($cd -match 'filename="?([^\";]+)"?')) {
40
   $serverName = $Matches[1];
41
   if ($serverName -and ($serverName -ne $OutputFile)) {
42
      try {
43
         Move-Item -Force -Path $OutputFile -Destination $serverName;
44
         if (-not $Quiet) { Write-Output ('Saved to {0}' -f $serverName); };
45
         exit 0;
46
      } catch {
47
         Write-Warning 'Failed to rename downloaded file to server-suggested filename.'; 
48
         if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
49
         exit 0;
50
      };
51
   } else {
52
      if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
53
      exit 0;
54
   };
55
} else {
56
   if (-not $Quiet) { Write-Output ('Saved to {0}' -f $OutputFile); };
57
   exit 0;
58
};