VPN status detection ?

Feature requests for the Mac OS X version of Transmission
Post Reply
somefanofyours
Posts: 2
Joined: Sat May 21, 2011 11:47 am

VPN status detection ?

Post by somefanofyours »

Hello,

As many people other the web, I'm using a VPN connection to download files over torrents P2P. I wrote a little AppleScript which allows me to detect when my VPN connection is on or not. If it is off, it tells Transmission to quit. Why don't you add a feature like this in the preferences of the application ? The user could set the preference to allow downloads only when the computer is connected to a VPN server.
bulljit
Posts: 17
Joined: Mon Jan 24, 2011 4:31 pm

Re: VPN status detection ?

Post by bulljit »

You may find this of interest:

https://trac.transmissionbt.com/ticket/2313

Do you mind sharing your applescript code, others may find it useful :)
somefanofyours
Posts: 2
Joined: Sat May 21, 2011 11:47 am

Re: VPN status detection ?

Post by somefanofyours »

Here is the applescript version of the code :

Code: Select all

on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "REPLACE_THIS_BY_THE_VPN_SERVICE_NAME"
			if current configuration of myConnection is not connected then
				tell application "Transmission"
					quit-- Warning be sure that Transmission don't ask you anything before you quit it when a transfer is on!
				end tell
			end if
		end tell
		return 30-- = Time to wait until next check in seconds
	end tell
end idle
Make an app of that code in AppleScript Editor (in Utilities folder) and launch it every time you use Transmission to be sure to not download anything if the VPN connection is down.

And using Obj-C Cocoa :

Code: Select all

- (void)getVPNStatusWithThread {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString * networkConfigStr = [self computerNetworkToString];
	NSAssert(self.vpnServiceName!=nil&&[self.vpnServiceName length]>0, @"ERROR : You didn't set the network service name of the VPN service to check.");
	
	if ([networkConfigStr rangeOfString:self.vpnServiceName].location != NSNotFound) {
		NSString * vpnStatus = [networkConfigStr substringFromIndex:[networkConfigStr rangeOfString:self.vpnServiceName].location];
		
		if ([vpnStatus rangeOfString:@"Service Order"].location != NSNotFound) {
			vpnStatus = [vpnStatus substringToIndex:[vpnStatus rangeOfString:@"Service Order"].location];
			
			if ([vpnStatus rangeOfString:@"Has IP Assigned: "].location != NSNotFound) {
				vpnStatus = [vpnStatus substringFromIndex:[vpnStatus rangeOfString:@"Has IP Assigned: "].location];
				
				if ([vpnStatus rangeOfString:@"\n"].location != NSNotFound) {
					vpnStatus = [vpnStatus substringToIndex:[vpnStatus rangeOfString:@"\n"].location];
					
					self.connected = ([vpnStatus rangeOfString:@"Yes"].location != NSNotFound);
					
					for (id<VPNStateDelegate> aDelegate in _vpnStateDelegatesObjects) {
						if ([aDelegate respondsToSelector:@selector(vpnStateDidChange:)]) {
							[aDelegate performSelector:@selector(vpnStateDidChange:) withObject:self];
						}
					}
				}
			}
		}
	}
	[self performSelectorOnMainThread:@selector(getVPNStatus) withObject:nil waitUntilDone:NO];
	[networkConfigStr release];
	[pool release];
}
- (NSString *)computerNetworkToString {
	NSString * strResult=nil;
	NSPipe * thePipe=[[NSPipe alloc] init];
	NSTask * theTask=[[NSTask alloc] init];
	
	NSString * path = [NSString stringWithString:@"/usr/sbin/system_profiler"];
	[theTask setLaunchPath: path];
	NSString * arg1 = [NSString stringWithString:@"SPNetworkDataType"];
	[theTask setArguments: [NSArray arrayWithObjects: arg1, nil, nil,nil]];
	[theTask setStandardOutput: thePipe];
	[theTask launch];
	[theTask waitUntilExit];
	if ([theTask terminationStatus] == 0) { 
		strResult = [[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding];
	} else {
		strResult = [[NSString alloc] initWithFormat:@"Erreur (%d)", [theTask terminationStatus]];
	}
	[theTask release];
	[thePipe release];
	return strResult;
}
concernedsaudi
Posts: 3
Joined: Tue Jul 05, 2011 9:22 pm

Re: VPN status detection ?

Post by concernedsaudi »

Thank you so much for this, it's exactly what I was looking for. Sorry if this question is silly but what counts as the VPN "name"? Its IP adress? Its domain? the name of its website? Please forgive my ignorance. :)

I use tunnelblick

Image
bazzardl
Posts: 3
Joined: Fri Oct 21, 2011 8:04 am

Re: VPN status detection ?

Post by bazzardl »

Hi, I have been trying to solve this with an applescript I can't use the ones above as I connect to VPN through the client and not network prefs so I posted this problem on the apple support boards

https://discussions.apple.com/message/16465964#16465964

Camelot pointed my at the solution:

on idle
set activeInterfaces to do shell script "ifconfig -lu"
if activeInterfaces is not "lo0 en0 fw0 en1 utun0 tun0" then
tell application "Transmission" to quit
end if
return 10 -- 30 is default value, but it doesn't hurt to include it here
end idle

to get this to work change "lo0 en0 fw0 en1 utun0 tun0" to whatever you get when you run 'ifconfig -lu' in terminal when VPN is running. I use the HMA Pro VPN cline t for mac and when VPN is down tun0 is not in the active interfaces so the script will tell transmission to quit. Change the 10 to whatever number you like (I like to have it rerun every 10 seconds).

Open applescript editor , file new, cut and paste the code, save as application and tick stay open.
Post Reply