So I travel around and this year I was at BSidesLV and Defcon23 which are dodgy places to use wifi.

I always connect to my VPN endpoint so that all my traffic leaves my devices over a secure tunnel. However, the biggest flaw with the inbuilt Apple VPN client is that it won’t auto reestablish the VPN session if it dies or you wake the laptop from sleep. The wifi at conferences is pretty flakey and I loose the connection regularly, as such when the connection is reestablished the VPN tunnel does not get reopened. This can lead to me sending data over the open air.

I decided to create a simple app using AppleScript and loosely based it on the code found here. I improved the code to make sure that there was network connectivity before attempting to start the VPN session.

The code is here should you wish to make it yourself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
delay 10
on idle
    tell application "System Events"
        tell current location of network preferences
            set myConnection to the service "home VPN"
            if myConnection is not null then
                set networkUp to true -- assume the best
                try
                    do shell script "ping -c 1 -t 2 www.google.com"
                on error -- network is down
                    set networkUp to false
                end try
                if current configuration of myConnection is not connected and networkUp then
                    say "Reconnecting VPN"
                    connect myConnection
                end if
            end if
        end tell
        return 10
    end tell
end idle

What should be noted is that my VPN connection is called “home VPN” so you will need to change this yourself to match the name of your VPN connection name. To do this just open the script using AppleScript.
Screen Shot 2015-08-05 at 16.15.08

Here’s the app for your use https://www.phillips321.co.uk/downloads/AutoVPN.app.zip be sure to rename the VPN connection name in the script from “home VPN” to the name of your connection.

You can set the script to run at start as well by using the normal dock option of “Open at Login”
Screen Shot 2015-08-05 at 16.16.14

Leave a Reply