Subversion Repositories web_pages

Rev

Rev 18 | Details | Compare with Previous | 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
 
19 rodolico 13
# Fill in these variables to match your setup
14
$Url = 'http://www.example.org/dl.php' # URL of dl.php
15
$Username = '' # The username on the OVPN Router
16
$Key = '' # The key for the OVPN Router
17
$OutputFile = 'client.ovpn' # the default output filename
18
$Router = '' # the router name as configured on the OVPN server
19
$FileType = 'ovpn'  # 'ovpn', 'qr', or 'refresh'
20
$Insecure = $false  # set to $true to ignore SSL certificate errors
21
$Quiet = $false     # set to $true to suppress output messages
18 rodolico 22
 
19 rodolico 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;
18 rodolico 28
 
29
 
19 rodolico 30
# Place this anywhere in your script. It defines a function to download the OpenVPN profile.
31
function Get-OpenVpnProfile {
32
   param(
33
      [string]$Url = '',
34
      [string]$Username = '',
35
      [string]$Key = '',
36
      [string]$OutputFile = '',
37
      [string]$Router = '',
38
      [string]$FileType = '',
39
      [switch]$Insecure,
40
      [switch]$Quiet
41
   );
42
   Write-Debug ('Downloading OpenVPN profile from {0}' -f $Url);
43
   if ($Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; };
18 rodolico 44
 
19 rodolico 45
   $form = @{}; 
46
   if ($Username -ne '') { $form['user'] = $Username; };
47
   if ($Key -ne '') { $form['key'] = $Key; };
48
   if ($Router -ne '') { $form['router'] = $Router; };
49
   if ($FileType -ne '') { $form['filetype'] = $FileType; };
50
 
51
   # First, make a request to see if we get a filename suggestion
52
   try {
53
      $resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -ErrorAction Stop;
54
   } catch {
55
      Write-Error 'Request failed:'; Write-Error $_.Exception.Message; exit 1;
18 rodolico 56
   };
19 rodolico 57
 
58
   # If server suggested a filename, use it
59
   $disposition = $null;
60
   if ($resp.Headers.ContainsKey('Content-Disposition')) { $disposition = $resp.Headers['Content-Disposition']; };
61
   # normalize to a single string if header is an array
62
   if ($disposition -is [array]) { $disposition = $disposition -join ';'; };
63
   $match = [regex]::Match($disposition, 'filename="([^"]+)"');
64
   if ($match.Success) { $OutputFile = $match.Groups[1].Value; }
65
 
66
   # now, actually download the file
67
   try {
68
      $resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -OutFile $OutputFile -ErrorAction Stop;
69
      return $OutputFile;
70
   } catch {
71
      Write-Warning 'Failed to Download'; 
72
   };
18 rodolico 73
};
19 rodolico 74
 
75