Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.DS_Store
/dist/
/build/
28 changes: 16 additions & 12 deletions ToggleProxy.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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__':
Expand Down