Sunday, July 17, 2011

VPN only when specific APP is running

So, this is neat.

I started here:

http://fardjad.wordpress.com/2010/11/11/vpn-auto-connect-script/

then added the test if my application was running.

Say you need a VPN to be connected only if a certain program is running, not just if u want to visit a certain website like the VPN on demand OSX built in feature.

build this script:
============

on idle

 if application "Mail" is running then

  tell application "System Events"
   tell current location of network preferences
    set myConnection to the service "Name_of_VPN"
    if myConnection is not null then
     if current configuration of myConnection is not connected then
      connect myConnection
     end if
    end if
   end tell
  end tell

 else
  tell application "System Events"
   tell current location of network preferences
    set myConnection to the service "Name_of_VPN"
    if myConnection is not null then
     if current configuration of myConnection is connected then
      disconnect myConnection
     end if
    end if
   end tell
  end tell
 end if
 return 2

end idle

============


Replace  - Mail - with the app you want to monitor and - Name_of_VPN - with the name of your chosen vpn connection as it appears is system preferences under "network". might be something like "VPN (PPTP)" or "VPN (LT2P)". I have several so I name then differently to be able to tell them apart.
Then save the above script as an app and check "stay open".
Run it and it will check every two seconds if your chosen app (Mail in the example above) is running and if it IS connect to your VPN, when you close the app it closes the VPN connection.
Add the app under accounts, login items, and it will just be running there in the background making sure you check your mail only through your VPN.

If you look at the link I reference finding above it tells you how to keep the script icon from showing up on the dock. I don't mind it there tho, then I know I'm protected.

+++++++++++
Edit:

check out this freeware:
http://www.pearworks.com/pages/pearPortVPN_Screenshot.html
connects to a VPN based on network parameter tests. ie, ip-range or name of wifi network.
The script I wrote tho is different in that it forces specific apps to always use VPN.