Skip to content

Commit

Permalink
Viscosity modification, and initial public release.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTGIV committed Mar 15, 2014
1 parent dcb9a42 commit 609c621
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 1 deletion.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm not sure what the original author had in mind… I'm still trying to find him and make contact. If it was up to me, I would assume that the MIT license is perhaps, most appropriate. -Louis T. Getterman IV - http://Thad.Getterman.org/
11 changes: 11 additions & 0 deletions NOTES
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Based almost entirely on https://forum.utorrent.com/viewtopic.php?pid=654898 by Jack0817 at 2012-04-16 19:42:30
Code update is for handling Viscosity, instead of System Events-based VPN via OSX.

NOTES/TODO (I keep telling myself that at some point, I'll come back and add in OSX notifications, and other features...)

Need Cocoa-AppleScript Applet in order to send notifications from AppleScript to OSX Notifications Center: http://macscripter.net/viewtopic.php?id=39189

If you want to remove applications from notifications, check http://stackoverflow.com/questions/11993145/remove-application-from-notification-center
--tell application "System Events"
-- get properties
--end tell
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
chameleon
Chameleon
=========

OSX AppleScript that maintains a persistent VPN connection through Viscosity, and shuts down uTorrent upon any interruption.

Why the name? A Chameleon blends into it's surroundings, and uses a sticky tongue to retrieve objects. I'd like to think that similar is happening here with blending into network surroundings, and using a sticky/persistent connection to retrieve objects.

----------
IF YOU'RE READING NOTHING ELSE, THEN *PLEASE* READ THIS!!!
* TURN OFF QUIT CONFIRMATION DIALOG IN UTORRENT!
* GO TO ADVANCED OPTIONS (CMD-OPTION-,) AND TURN OFF GRACEFUL SHUTDOWN!!!
----------

PLEASE NOTE: This is a slight modification of code, and based almost entirely on https://forum.utorrent.com/viewtopic.php?pid=654898 by Jack0817 at 2012-04-16 19:42:30.

There have been several times where I've needed to download Ubuntu and Raspbian via BitTorrent, and a motel/hotel has nearly everything blocked, and I need to punch through and get one of those distributions so that I can be on my merry way for developing. The specific need for this came up when I had a BitTorrent download of Ubuntu *and* Raspbian going on a VPN connection, ran out to get dinner, returned back to my hotel room and the connection had dropped; and worse yet, the hotel had blocked my computer from their network for attempting to use BitTorrent (even though it was legitimate downloads!)

I don't use OSX VPN, and instead, I use Viscosity; so I slightly modified this original script from the uTorrent forum in order to play nice with Viscosity, and after thumbing through "Controlling Viscosity with AppleScript" from the support pages (http://www.sparklabs.com/support/controlling_viscosity_with_app/)

Best regards,
Louis T. Getterman IV - http://Thad.Getterman.org/
200 changes: 200 additions & 0 deletions chameleon.scpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
(*
Based almost entirely on https://forum.utorrent.com/viewtopic.php?pid=654898 by Jack0817 at 2012-04-16 19:42:30
A copy of the original is included in "original.scpt".
Code update is for handling Viscosity, instead of OSX VPN via calls to "System Events".
Modifications for Viscosity: Louis T. Getterman IV - http://Thad.Getterman.org/
*)

------------------------------------------------
--Main Routine
------------------------------------------------
on idle

--Script Variables
set appName to "uTorrent"
set vpnName to "IPredator"
set waitTime to 1 as integer

--Main Script Logic
if isViscosityVPNConnected(vpnName) then
-- notifyOSX("VPN Predator Connected", "Connection to "+vpnName+" established...")
startApplication(appName)
else
-- notifyOSX("Resetting Viscosity Connection", "Connection to "+vpnName+" razed. Attempting to reset & waiting 30 seconds...")
stopApplication(appName)

-- disconnect connection(s) since Viscosity sometimes hangs on attempting to reconnect to IPredator, and doing so will also reset the network interfaces (assuming you remembered to set the option, right?)
--tell application "Viscosity" to disconnectall
tell application "Viscosity" to disconnect vpnName

-- wait few seconds so that we can also give time for network connection(s) to reset, and reconnect
delay 30

-- attempt to reconnect
-- notifyOSX("Reestablishing Viscosity connection", "30 seconds is up, attempting to connect to "+vpnName+"...")
connectViscosityVPNConnection(vpnName)

-- give some time to cool off, and hope that we have a winner (as compare to some obscure TLS-handshake issues)
delay 60

if isViscosityVPNConnected(vpnName) then
-- notifyOSX("Oh, the humanity!", "We waited 60 seconds, and still bupkas. Life can be cruel sometimes... Did you forget to pay the bill?")
end if

end if

return waitTime

end idle

------------------------------------------------
--Sub Routine - Determines if specified Viscosity vpn is connected
------------------------------------------------
on notifyOSX(notifyTitle, notifyMsg)

set theNotif to current application's NSUserNotification's alloc()'s init()
tell theNotif
setTitle_(notifyTitle)
setInformativeText_(notifyMsg)
end tell
tell current application's NSUserNotificationCenter's defaultUserNotificationCenter()
setDelegate_(me)
deliverNotification_(theNotif)
end tell

-- this was causing errors since it's sitting inside of a sub-routine :'-(
-- on userNotificationCenter_shouldPresentNotification_(cen, notif) -- delegate method
-- return yes
-- end userNotificationCenter_shouldPresentNotification_

end notifyOSX

------------------------------------------------
--Sub Routine - Determines if specified Viscosity vpn is connected
------------------------------------------------
on isViscosityVPNConnected(vpnName)

--Init return value to default
set isConnected to false
set connectionState to {"Disconnected"}

tell application "Viscosity"
set connectionState to state of connections where name is equal to vpnName
end tell

if connectionState is equal to {"Connected"} then
set isConnected to true
else
set isConnected to false
end if

return isConnected

end isViscosityVPNConnected

------------------------------------------------
--Sub Routine - Determines if specified vpn is connected
------------------------------------------------
on isVPNConnected(vpnName)

--Init return value to default
set isConnected to false

tell application "System Events"
tell current location of network preferences
set vpnConnection to the service vpnName
set isConnected to current configuration of vpnConnection is connected
end tell
end tell

return isConnected

end isVPNConnected

------------------------------------------------
--Sub Routine - Attempts to connect to the Viscosity specified VPN
------------------------------------------------
on connectViscosityVPNConnection(vpnName)

if viscosityVPNConnectionIsExist(vpnName) is true then
tell application "Viscosity" to connect vpnName
end if

end connectViscosityVPNConnection

------------------------------------------------
--Sub Routine - Attempts to connect to the specified VPN
------------------------------------------------
on connectVPNConnection(vpnName)

tell application "System Events"
tell current location of network preferences
set vpnConnection to the service vpnName
if vpnConnection is not null then
connect vpnConnection
end if
end tell
end tell

end connectVPNConnection

------------------------------------------------
--Sub Routine - Starts an application if it is not already running
------------------------------------------------
on startApplication(appName)

if appIsRunning(appName) is false then
tell application appName to activate
end if

end startApplication

------------------------------------------------
--Sub Routine - Stop an application if it is running
------------------------------------------------
on stopApplication(appName)

if appIsRunning(appName) then
tell application appName to quit
end if

end stopApplication

------------------------------------------------
--Sub Routine - Determines if specified connection exists in Viscosity
------------------------------------------------
on viscosityVPNConnectionIsExist(vpnName)

--Init return value to default
set isExist to false
set connectionState to {}

tell application "Viscosity"
set connectionState to state of connections where name is equal to vpnName
end tell

if connectionState is not equal to {} then
set isExist to true
else
set isExist to false
end if

return isExist

end viscosityVPNConnectionIsExist

------------------------------------------------
--Sub Routine - Determines if specified app is currently running
------------------------------------------------
on appIsRunning(appName)

set isRunning to false

tell application "System Events"
set isRunning to (name of processes) contains appName
end tell

return isRunning

end appIsRunning
104 changes: 104 additions & 0 deletions original.scpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
(*
Original post:
----------
Here is a script I've created that will get the job done until uTorrent (for Mac) can bind to a VPN service. SImply update the "Scrip Variables" section with your corresponding values, save the script AS AN APP, and add it to your Login Startup Items.
The Gist: This script will monitor your VPN connection, if it detects it is no longer connected it will gracefully shutdown uTorrent (keeping all DL progress intact). Also, when the VPN is down, it will continue to try to re-establish a connection and upon successful re-connecting, it will then re-launch uTorrent to resume where it left off.
Hope this helps!
Last edited by Jack0817 (2012-04-16 19:44:36)
*)
------------------------------------------------
--Main Routine
------------------------------------------------
on idle

--Script Variables
set appName to "uTorrent"
set vpnName to "IPredator"
set waitTIme to 60 as integer

--Main Script Logic
if isVPNConnected(vpnName) then
startApplication(appName)
else
stopApplication(appName)
connectVPNConnection(vpnName)
end if

return waitTIme

end idle

------------------------------------------------
--Sub Routine - Determines if specified vpn is connected
------------------------------------------------
on isVPNConnected(vpnName)

--Init return value to default
set isConnected to false

tell application "System Events"
tell current location of network preferences
set vpnConnection to the service vpnName
set isConnected to current configuration of vpnConnection is connected
end tell
end tell

return isConnected

end isVPNConnected

------------------------------------------------
--Sub Routine - Attempts to connect to the specified VPN
------------------------------------------------
on connectVPNConnection(vpnName)

tell application "System Events"
tell current location of network preferences
set vpnConnection to the service vpnName
if vpnConnection is not null then
connect vpnConnection
end if
end tell
end tell

end connectVPNConnection

------------------------------------------------
--Sub Routine - Starts an application if it is not already running
------------------------------------------------
on startApplication(appName)

if appIsRunning(appName) is false then
tell application appName to activate
end if

end startApplication

------------------------------------------------
--Sub Routine - Stop an application if it is running
------------------------------------------------
on stopApplication(appName)

if appIsRunning(appName) then
tell application appName to quit
end if

end stopApplication

------------------------------------------------
--Sub Routine - Determines if specified app is currently running
------------------------------------------------
on appIsRunning(appName)

set isRunning to false

tell application "System Events"
set isRunning to (name of processes) contains appName
end tell

return isRunning

end appIsRunning

0 comments on commit 609c621

Please sign in to comment.