Subversion Repositories web_pages

Rev

Rev 18 | 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.
#>

# Fill in these variables to match your setup
$Url = 'http://www.example.org/dl.php' # URL of dl.php
$Username = '' # The username on the OVPN Router
$Key = '' # The key for the OVPN Router
$OutputFile = 'client.ovpn' # the default output filename
$Router = '' # the router name as configured on the OVPN server
$FileType = 'ovpn'  # 'ovpn', 'qr', or 'refresh'
$Insecure = $false  # set to $true to ignore SSL certificate errors
$Quiet = $false     # set to $true to suppress output messages

# call the function with the script-level variables. This will download to the current directory.
# Get the ovpn file
$null = Get-OpenVpnProfile -Url $Url -Username $Username -Key $Key -OutputFile 'client.ovpn' -Router $Router -FileType 'ovpn' -Insecure:$Insecure -Quiet:$Quiet;
# get the qr code image
$null = Get-OpenVpnProfile -Url $Url -Username $Username -Key $Key -OutputFile 'client.png' -Router $Router -FileType 'qr' -Insecure:$Insecure -Quiet:$Quiet;


# Place this anywhere in your script. It defines a function to download the OpenVPN profile.
function Get-OpenVpnProfile {
   param(
      [string]$Url = '',
      [string]$Username = '',
      [string]$Key = '',
      [string]$OutputFile = '',
      [string]$Router = '',
      [string]$FileType = '',
      [switch]$Insecure,
      [switch]$Quiet
   );
   Write-Debug ('Downloading OpenVPN profile from {0}' -f $Url);
   if ($Insecure) { [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; };

   $form = @{}; 
   if ($Username -ne '') { $form['user'] = $Username; };
   if ($Key -ne '') { $form['key'] = $Key; };
   if ($Router -ne '') { $form['router'] = $Router; };
   if ($FileType -ne '') { $form['filetype'] = $FileType; };
   
   # First, make a request to see if we get a filename suggestion
   try {
      $resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -ErrorAction Stop;
   } catch {
      Write-Error 'Request failed:'; Write-Error $_.Exception.Message; exit 1;
   };

   # If server suggested a filename, use it
   $disposition = $null;
   if ($resp.Headers.ContainsKey('Content-Disposition')) { $disposition = $resp.Headers['Content-Disposition']; };
   # normalize to a single string if header is an array
   if ($disposition -is [array]) { $disposition = $disposition -join ';'; };
   $match = [regex]::Match($disposition, 'filename="([^"]+)"');
   if ($match.Success) { $OutputFile = $match.Groups[1].Value; }
   
   # now, actually download the file
   try {
      $resp = Invoke-WebRequest -Uri $Url -Method Post -Body $form -OutFile $OutputFile -ErrorAction Stop;
      return $OutputFile;
   } catch {
      Write-Warning 'Failed to Download'; 
   };
};