Archive for février, 2012

VPN protected use of TransmissionBT – AppleScript

février 13th, 2012

It’s been a little while that the Ananoos service exists. Very useful and easy, powerful and cheap VPN service. It works on top of OpenVPN which provides high security and encrypted connections. Besides I’m using Transmission which is a free and efficient BitTorrrent client.

The goal is to make sure the VPN connection is always up when using Transmission. When starting Transmission or when the VPN link fails, the VNP connection is restarted. And if really there is no way to get back the VPN link, the Transmission app is simple closed.

# SET HERE YOUR TUNNELBLICK
# CONNECTION NAME
# IN MY CASE: Ananoos
set VPNConnection to "Ananoos"

repeat
	# If transmission is running
	if appIsRunning("Transmission") then
		set vpn to ""

		if appIsRunning("TunnelBlick") then
			# TunnelBlick is running
			# Checking whether it is connected
			tell application "Tunnelblick"
				set vpn to get state of first configuration where name = VPNConnection
			end tell
		else
			tell application "Tunnelblick" to activate
		end if

		# VPN is connected, all fine
		if vpn = "CONNECTED" then
			#display alert "Transmission is running and protected"
		else
			# VPN is not connected, forcing reconnect
			tell application "Tunnelblick"
				connect VPNConnection
			end tell

			# Waiting for reconnection
			repeat 10 times
				tell application "Tunnelblick"
					# Is VPN connected at last?
					set vpn to get state of first configuration where name = VPNConnection
					# Yes, exiting loop
					if vpn = "CONNECTED" then exit repeat
					do shell script "sleep 3"
				end tell
			end repeat

			# Checking whether TunnelBlick has reconnected
			if vpn = "CONNECTED" then
				display alert "ATTENTION: Transmission was running without VPN protection. We have successfully forced reconnection..."
			else
				# Not reconnected, better quit Transmission
				tell application "Transmission"
					quit
				end tell
				display alert "ATTENTION: Transmission was running unprotected. We were unable to reconnect to VPN therefore we have closed it..."
			end if

		end if
	end if
	do shell script "sleep 1"
end repeat

on appIsRunning(appName)
	tell application "System Events" to (name of processes) contains appName
end appIsRunning

Open the Mac AppleScript Editor. Copy and paste the above code. Save as an application and make sure this app is automatically started on login.
Attention 1: this script works only with TunnelBlick which is a openVPN Mac GUI. Ananoos offers either their own VPN client or the TunnelBlick config files. Prefer 2nd option.
Attention 2: line4, I refer to the « Ananoos » connection. If your have named it differently, update the script.

Tags: , , , , ,
Posted in Development, Technology | Comments (0)