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 diff --git a/ToggleProxy.py b/ToggleProxy.py index 5070552..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): @@ -37,12 +39,12 @@ 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 """ - 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() @@ -59,17 +61,17 @@ 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 ) + self.statusitem.setImage_(self.active and self.active_image or self.inactive_image) # set tooltip 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 @@ -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 setwebproxystate %s %s" % ( + commands.getoutput("/usr/sbin/networksetup setsocksfirewallproxystate \"%s\" %s" % ( servicename, newstate )) + self.updateProxyStatus() if __name__ == '__main__':