Jump to content

Only torrent when VPN is active


Dijhammond

Recommended Posts

I have been looking for a solution for this but have not been able to find one as of yet.

I connect to a VPN and then my torrent client starts. However, since i am using a campus network, my VPN frequently disconnects and earn torrent is running when my VPN isn't, it kind of defeats the purpose. I was wondering if there was a way to prevent torrent from connecting when my VPN is down or force all traffic through the VPN so that when it goes down, utorrent thinks its been disconnected form the internet. .

Btw, all of this is done on a Mac mini used as a HTPC so I am not constantly monitoring the status foe the VPN connection and as a result I need an "automated" solution.

Any help would be greatly appreciated. Thanks.

Link to comment
Share on other sites

I use an applescript to check the vpn, and activate it if it goes down

on idle

tell application "System Events"

tell current location of network preferences

set myConnection to the service "VPN"

if current configuration of myConnection is not connected then

connect myConnection

end if

end tell

return 120

end tell

end idle

Save it as an application with the check box stay open, and then set it to start at login.

VPN is the name of you vpn, and the 120 checks every 2 minutes.

Link to comment
Share on other sites

  • 3 months later...
  • 1 year later...
  • 8 months later...

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!

------------------------------------------------

--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

Link to comment
Share on other sites

  • 3 weeks later...
Hey so like the topic says I am working on a script that will stop all the transfers when my VPN goes down. Currently the program is set to quit (but this takes a while) This is not my code and was changed from https://forum.transmissionbt.com/viewtopic.php?f=4&t=12330&p=56986&hilit=script+to+quit+vpn#p56986

on idle
set activeInterfaces to do shell script "ifconfig -lu"
if activeInterfaces = "lo0 en0 en1 fw0 utun0" then
tell application "uTorrent" to quit
tell application "Growl"
notify with name ¬
"VPN Down" title ¬
"VPN Down" description ¬
"I have quit uTorrent." application name "Drop BitTorrent"
end tell
end if
return 5 -- 30 is default value, but it doesn't hurt to include it here
end idle

Does any one know what command I could substitute for the line: ??

tell application "uTorrent" to quit

stop doesn't seem to work and I can't really guess. I also can't seem to find a key command to start/stop transfers

Also what would I add to reenable the transfers?

something like:

if activeInterfaces = "lo0 en0 en1 fw0 utun0 tun0"
tell application "uTorrent" to start (or something to that effect)
if activeInterfaces = "lo0 en0 en1 fw0 tun0 utun0"
tell application "uTorrent" to start (or something to that effect)

Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...

this what I settled on, seems to work better and will not drop with a simple reordering of interface types

on idle
set activeInterfaces to do shell script "ifconfig -lu"
if activeInterfaces is "lo0 en0 en1 fw0 tun0 utun0" then
return
else if activeInterfaces is "lo0 en0 en1 fw0 utun0 tun0" then
return
else
tell application "uTorrent" to quit
tell application "Growl"
notify with name ¬
"VPN Down" title ¬
"VPN Down" description ¬
"I have quit uTorrent." application name "Drop BitTorrent"
end tell
end if
return 5 -- 30 is default value, but it doesn't hurt to include it here
end idle

Link to comment
Share on other sites

  • 2 months later...

I recently upgraded my computer and because the interfaces list is different I modified the code above to this. Should produce more consistency.

on idle

set activeInterfaces to do shell script "ifconfig -lu"
set activeInterfacesString to (activeInterfaces) as string

set AppleScript's text item delimiters to " "
set the activeInterfacesList to every text item of the activeInterfacesString
set AppleScript's text item delimiters to ""
if activeInterfacesList does not contain "tun0" then
tell application "uTorrent" to quit
tell application "Growl"
notify with name ¬
"VPN Down" title ¬
"VPN Down" description ¬
"I have quit uTorrent." application name "Drop BitTorrent"
end tell
end if
return 5 -- 30 is default value, but it doesn't hurt to include it here
end idle

Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...

Before: I left my computer downloading movies via uTorrent, and came home to find my vpn disconnected, uTorrent still running, and my IP being sprayed all over the place.

After: The script first connects my vpn of choice and opens uTorrent. Then it repeats the following indefinitely: watches the Tunnelblick connection every 1 second. If it is connected, it just keeps checking every second until it detects a disconnect. Upon disconnect, it quits uTorrent, reconnects the vpn connection via tunnelblick, and restarts uTorrent. Please find the applescript below:

tell application "Tunnelblick"

connect "config"

get state of first configuration where name = "config"

repeat until result = "CONNECTED"

delay 1

get state of first configuration where name = "config"

end repeat

if result = "connected" then run application "uTorrent"

end tell

repeat

tell application "Tunnelblick"

get state of configuration 1 whose name = "config"

if result = "connected" then repeat until result = "exiting"

delay 1

get state of configuration 1 whose name = "config"

end repeat

if result = "exiting" then quit application "uTorrent"

tell application "Tunnelblick"

connect "config"

get state of first configuration where name = "config"

repeat until result = "CONNECTED"

delay 1

get state of first configuration where name = "config"

end repeat

run application "uTorrent"

end tell

end tell

end repeat

"config" is replaced with my vpn of choice as it appears in the tunnelblick drop down menu. I was so excited that it worked, I had to share it.

You should be able to copy the script into applescript editor and just replace that which does not apply to your set up with the relevant info.

Link to comment
Share on other sites

  • 4 weeks later...

JunkBox,

I just started using a VPN, and although I'm pretty proficient with OS X, AppleScript is a skill that I lack. Would you be so kind as to tell me which parts of that code need to be switched out, as you mentioned?

I'm using tunnelblick and uTorrent on 10.6.8. I can't see anything else in the code that stands out as something that would need to be changed.

Thanks in advance for helping out a noob!

steiney

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...
I have been looking for a solution for this but have not been able to find one as of yet.

I connect to a VPN and then my torrent client starts. However, since i am using a campus network, my VPN frequently disconnects and earn torrent is running when my VPN isn't, it kind of defeats the purpose. I was wondering if there was a way to prevent torrent from connecting when my VPN is down or force all traffic through the VPN so that when it goes down, utorrent thinks its been disconnected form the internet. .

Btw, all of this is done on a Mac mini used as a HTPC so I am not constantly monitoring the status foe the VPN connection and as a result I need an "automated" solution.

Any help would be greatly appreciated. Thanks.

I have tried a few VPN provider before and i would say Torguard VPN is the most stable to use since they have quite a number of server location worldwide.But the package is quite pricey compared to other VPN provider.

Here is the list of Best P2P and top Torrent VPN Service Providers that help you to download via torrent and share p2p files anonymously. Now you can Access to any Internet resource without restrictions and bandwidth limitation safe, secure and fast with Torrent VPN accounts.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...