From 6f4dc3313742b17a05a020b46e2920ae9a1a51cf Mon Sep 17 00:00:00 2001 From: Martin Koistinen Date: Fri, 15 Feb 2013 00:32:42 +0200 Subject: [PATCH 1/4] Now uses network name rather than just network type, which will help users with multiple 'Ethernet' ports Previous, this code would grab the network TYPE (e.g., 'Ethernet') rather than the network NAME (e.g., 'Built-in Ethernet'). This creates issues for people with multiple interfaces of the same type. Signed-off-by: Martin Koistinen --- ToggleProxy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ToggleProxy.py b/ToggleProxy.py index 5070552..98e32f0 100644 --- a/ToggleProxy.py +++ b/ToggleProxy.py @@ -37,8 +37,8 @@ def loadNetworkServices(self): """ load list of network services """ self.services = {} output = commands.getoutput("/usr/sbin/networksetup listnetworkserviceorder") - for service, device in re.findall(r'Hardware Port:\s*(.*?), Device:\s*(.*?)\)', output): - self.services[device] = service + for servicename, service, device in re.findall(r'\(\d\)\s*(.*?)(?:\n|\r\n?)\(Hardware Port:\s*(.*?), Device:\s*(.*?)\)', output, re.MULTILINE): + self.services[device] = servicename def watchForProxyChanges(self): """ install a watcher for proxy changes """ @@ -87,7 +87,7 @@ def toggleProxy_(self, sender): NSLog("interface '%s' not found in services?" % self.interface) return newstate = self.active and "off" or "on" - commands.getoutput("/usr/sbin/networksetup setwebproxystate %s %s" % ( + commands.getoutput("/usr/sbin/networksetup setsocksfirewallproxystate \"%s\" %s" % ( servicename, newstate )) From 7307e17a1e2ef7911f91389507cffc5fc5512370 Mon Sep 17 00:00:00 2001 From: Martin Koistinen Date: Fri, 15 Feb 2013 00:33:48 +0200 Subject: [PATCH 2/4] Changed to work with SOCKS proxies rather than HTTP Proxies. Obviously, the best thing would be to support both. Coming soon? Signed-off-by: Martin Koistinen --- ToggleProxy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ToggleProxy.py b/ToggleProxy.py index 98e32f0..f2f7696 100644 --- a/ToggleProxy.py +++ b/ToggleProxy.py @@ -59,7 +59,7 @@ def updateProxyStatus(self): # get status for primary interface status = proxydict['__SCOPED__'][self.interface] - self.active = status.get('HTTPEnable', False) and True or False + self.active = status.get('SOCKSEnable', False) and True or False # set image self.statusitem.setImage_( self.active and self.active_image or self.inactive_image ) @@ -68,8 +68,8 @@ def updateProxyStatus(self): if self.active: tooltip = "[%s] proxy active on %s:%s" % ( self.interface, - proxydict.get('HTTPProxy', '??'), - proxydict.get('HTTPPort', '??'), + proxydict.get('SOCKSProxy', '??'), + proxydict.get('SOCKSPort', '??'), ) else: tooltip = "[%s] proxy not active" % self.interface From e6c10b1f5da9536feed649c9ffddef5f12f11daf Mon Sep 17 00:00:00 2001 From: Martin Koistinen Date: Fri, 15 Feb 2013 00:34:28 +0200 Subject: [PATCH 3/4] Whitespace changes and removed superfluous import. Signed-off-by: Martin Koistinen --- ToggleProxy.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ToggleProxy.py b/ToggleProxy.py index f2f7696..00644f1 100644 --- a/ToggleProxy.py +++ b/ToggleProxy.py @@ -1,9 +1,11 @@ #!/usr/bin/env python -from Foundation import * -from AppKit import * -from SystemConfiguration import * -import commands, re, time +from Foundation import * +from AppKit import * +from SystemConfiguration import * +import commands +import re + class ToggleProxy(NSObject): @@ -42,7 +44,7 @@ def loadNetworkServices(self): def watchForProxyChanges(self): """ install a watcher for proxy changes """ - SCDynamicStoreSetNotificationKeys(self.store, None, [ 'State:/Network/Global/Proxies' ]) + SCDynamicStoreSetNotificationKeys(self.store, None, ['State:/Network/Global/Proxies']) source = SCDynamicStoreCreateRunLoopSource(None, self.store, 0) loop = NSRunLoop.currentRunLoop().getCFRunLoop() @@ -62,7 +64,7 @@ def updateProxyStatus(self): self.active = status.get('SOCKSEnable', False) and True or False # set image - self.statusitem.setImage_( self.active and self.active_image or self.inactive_image ) + self.statusitem.setImage_(self.active and self.active_image or self.inactive_image) # set tooltip if self.active: @@ -86,11 +88,13 @@ def toggleProxy_(self, sender): if not servicename: NSLog("interface '%s' not found in services?" % self.interface) return + newstate = self.active and "off" or "on" commands.getoutput("/usr/sbin/networksetup setsocksfirewallproxystate \"%s\" %s" % ( servicename, newstate )) + self.updateProxyStatus() if __name__ == '__main__': From 9574afef654e6de2be70acf3732ef692879a7426 Mon Sep 17 00:00:00 2001 From: Martin Koistinen Date: Fri, 15 Feb 2013 00:54:25 +0200 Subject: [PATCH 4/4] Ignoring dist/, build/ and .DS_Store Signed-off-by: Martin Koistinen --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9ef71d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.DS_Store +/dist/ +/build/ \ No newline at end of file