This document outlines my findings after hacking with OpenVPN.
I had issues with the latest release of OpenVPN Connect but version 2.5.2 worked fine;
Download and install with the default parameters.
Head over to your Surfshark settings page and grab the username and password from the credentials tab.
Whilst here, head to the locations tab and download a configurations file in .udp format
Next create an auth.txt
file and add your username and password (from above) on seperate lines
e.g.
myusername
mypassword
With your configurations file you need to open it in your text editor and add the line auth-user-pass auth.txt
To keep things tidy lets move auth.txt
and the .ovpn
file to the OpenVPN directory at C:\Program Files\OpenVPN\bin
Now we're all set up you can open a Powershell console with Administrator privilidges (very important) and change directory to C:\Program Files\OpenVPN\bin
Assuming the application is installed correctly you can connect to the VPN with the Start-Process
command
(We'll assume my .ovpn
file is be-bru.prod.surfshark.comsurfshark_openvpn_udp.ovpn
)
Start-Process -FilePath "openvpn.exe" -ArgumentList "--config surfshark.comsurfshark_openvpn_udp.ovpn" -WorkingDirectory "C:\Program Files\OpenVPN\bin\"
You should see a console pop-up window with the OpenVPN logs 🥳
To stop the service and close the connection you can simply close pop-up window that was created.
Or you may find it more benefical to stop the process using the Stop-Process
command
Stop-Process -Name openvpn
So we can easily start and quit our VPN connection with Powershell but how about with Python?
After many failed attempts with subprocess.Popen
I finally had some luck with invoking a Powershell script from Python.
It requires a bit of extra setup.
First lets create start.ps1
, it simply contains the Start-Process
command from previous.
Start-Process -FilePath "openvpn.exe" -ArgumentList "--config surfshark.comsurfshark_openvpn_udp.ovpn" -WorkingDirectory "C:\Program Files\OpenVPN\bin\"
Then create stop.ps1
, which contains the Stop-Process
command.
Stop-Process -Name openvpn
Let's invoke these from a Python console, but first you're going to need the path to your powershell.exe
.
>>> import subprocess
>>> psexe = 'C:\\Program Files\\PowerShell\\7\\pwsh.exe'
>>> subprocess.run([exe, "start.ps1"]) # Start the VPN connection.
CompletedProcess(args=['C:\\Program Files\\PowerShell\\7\\pwsh.exe', 'start.ps1'], returncode=0)
>>> subprocess.run([exe, "stop.ps1"]) # Stop the VPN connection.
CompletedProcess(args=['C:\\Program Files\\PowerShell\\7\\pwsh.exe', 'stop.ps1'], returncode=0)