From 51e7a4069212b389ad07f86771eca9e0df749ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9B=BD=E6=99=94?= Date: Sun, 27 Mar 2016 16:50:45 +0800 Subject: [PATCH 1/4] Mac Tray: Fix Update Alert --- launcher/mac_tray.py | 12 ++++++++++++ launcher/update.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/launcher/mac_tray.py b/launcher/mac_tray.py index 68929ba272..d67bb9d641 100644 --- a/launcher/mac_tray.py +++ b/launcher/mac_tray.py @@ -143,6 +143,18 @@ def validateMenuItem_(self, menuItem): menuItem != self.globalGaeProxyMenuItem and menuItem != self.disableGaeProxyMenuItem) + def presentAlert_withTitle_(self, msg, title): + self.performSelectorOnMainThread_withObject_waitUntilDone_('presentAlertWithInfo:', [title, msg], True) + return self.alertReturn + + def presentAlertWithInfo_(self, info): + alert = NSAlert.alloc().init() + alert.setMessageText_(info[0]) + alert.setInformativeText_(info[1]) + alert.addButtonWithTitle_("OK") + alert.addButtonWithTitle_("Cancel") + self.alertReturn = alert.runModal() == NSAlertFirstButtonReturn + def updateConfig(self, newStatus): config.set(["modules", "launcher", "proxy"], newStatus) config.save() diff --git a/launcher/update.py b/launcher/update.py index 2f5702f68c..8cb2a16d67 100644 --- a/launcher/update.py +++ b/launcher/update.py @@ -192,7 +192,7 @@ def download_module(module, new_version): ignore_module(module, new_version) elif sys.platform == "darwin": from mac_tray import sys_tray - if sys_tray.dialog_yes_no(msg, u"Install", None, None) == 1: + if sys_tray.presentAlert_withTitle_(msg, "Install"): install_module(module, new_version) else: ignore_module(module, new_version) @@ -314,7 +314,7 @@ def check_push_update(): elif sys.platform == "darwin": from mac_tray import sys_tray msg = "Module %s new version: %s, Download?" % (module, new_version) - if sys_tray.dialog_yes_no(msg, u"Download", None, None) == 1: + if sys_tray.presentAlert_withTitle_(msg, "Download"): download_module(module, new_version) else: ignore_module(module, new_version) From e2f42fcfdd5d71b27bb744093036408d67dc5338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9B=BD=E6=99=94?= Date: Sun, 27 Mar 2016 19:00:59 +0800 Subject: [PATCH 2/4] Mac Tray: Add Root Helper no more password --- launcher/mac_helper | Bin 0 -> 18960 bytes launcher/mac_helper.c | 32 ++++++++++++ launcher/mac_tray.py | 116 +++++++++++++++++++++++++++--------------- 3 files changed, 108 insertions(+), 40 deletions(-) create mode 100755 launcher/mac_helper create mode 100644 launcher/mac_helper.c diff --git a/launcher/mac_helper b/launcher/mac_helper new file mode 100755 index 0000000000000000000000000000000000000000..e0b8ba6062c762a4f0ffab0ef81b965e5bf3d2c5 GIT binary patch literal 18960 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 G5%?!~ZtE2Q literal 0 HcmV?d00001 diff --git a/launcher/mac_helper.c b/launcher/mac_helper.c new file mode 100644 index 0000000000..6ef4a5f73b --- /dev/null +++ b/launcher/mac_helper.c @@ -0,0 +1,32 @@ +#include +#include +#include + +int main(int argc, const char * argv[]) { + if (geteuid() != 0) { + fprintf(stderr, "Must be run as root!\n"); + return 1; + } + if (argc == 4 && strcmp(argv[1], "enableauto") == 0) { + execl("/usr/sbin/networksetup", "networksetup", "-setautoproxyurl", argv[2], argv[3], NULL); + } else if (argc == 5 && strcmp(argv[1], "enablehttp") == 0) { + execl("/usr/sbin/networksetup", "networksetup", "-setwebproxy", argv[2], argv[3], argv[4], NULL); + } else if (argc == 5 && strcmp(argv[1], "enablehttps") == 0) { + execl("/usr/sbin/networksetup", "networksetup", "-setsecurewebproxy", argv[2], argv[3], argv[4], NULL); + } else if (argc == 3 && strcmp(argv[1], "disableauto") == 0) { + execl("/usr/sbin/networksetup", "networksetup", "-setautoproxystate", argv[2], "off", NULL); + } else if (argc == 3 && strcmp(argv[1], "disablehttp") == 0) { + execl("/usr/sbin/networksetup", "networksetup", "-setwebproxystate", argv[2], "off", NULL); + } else if (argc == 3 && strcmp(argv[1], "disablehttps") == 0) { + execl("/usr/sbin/networksetup", "networksetup", "-setsecurewebproxystate", argv[2], "off", NULL); + } else { + fprintf(stderr, "Usage:\n"); + fprintf(stderr, "%s enableauto \n", argv[0]); + fprintf(stderr, "%s enablehttp \n", argv[0]); + fprintf(stderr, "%s enablehttps \n", argv[0]); + fprintf(stderr, "%s disableauto \n", argv[0]); + fprintf(stderr, "%s disablehttp \n", argv[0]); + fprintf(stderr, "%s disablehttps \n", argv[0]); + } + return 0; +} diff --git a/launcher/mac_tray.py b/launcher/mac_tray.py index d67bb9d641..e94f187bef 100644 --- a/launcher/mac_tray.py +++ b/launcher/mac_tray.py @@ -6,6 +6,7 @@ import config current_path = os.path.dirname(os.path.abspath(__file__)) +helper_path = os.path.join(current_path, '..', 'data', 'launcher', 'helper') if __name__ == "__main__": python_path = os.path.abspath( os.path.join(current_path, os.pardir, 'python27', '1.0')) @@ -31,6 +32,7 @@ def __init__(self): pass def applicationDidFinishLaunching_(self, notification): + self.setupHelper() self.setupUI() self.registerObserver() @@ -39,19 +41,31 @@ def getProxyState(self, service): return # Check if auto proxy is enabled - checkAutoProxyUrlCommand = 'networksetup -getautoproxyurl "%s"' % service - executeResult = subprocess.check_output(checkAutoProxyUrlCommand, shell=True) + executeResult = subprocess.check_output(['networksetup', '-getautoproxyurl', service]) if ( executeResult.find('http://127.0.0.1:8086/proxy.pac\nEnabled: Yes') != -1 ): return "pac" # Check if global proxy is enabled - checkGlobalProxyUrlCommand = 'networksetup -getwebproxy "%s"' % service - executeResult = subprocess.check_output(checkGlobalProxyUrlCommand, shell=True) + executeResult = subprocess.check_output(['networksetup', '-getwebproxy', service]) if ( executeResult.find('Enabled: Yes\nServer: 127.0.0.1\nPort: 8087') != -1 ): return "gae" return "disable" + def setupHelper(self): + try: + with open(os.devnull) as devnull: + subprocess.check_call(helper_path, stderr=devnull) + except: + cpCommand = "cp \\\"%s\\\" \\\"%s\\\"" % (os.path.join(current_path, 'mac_helper'), helper_path) + chmodCommand = "chmod 4777 \\\"%s\\\"" % helper_path + chownCommand = "chown root \\\"%s\\\"" % helper_path + rootCommand = """osascript -e 'do shell script "%s;%s;%s" with administrator privileges' """ % (cpCommand, chmodCommand, chownCommand) + executeCommand = rootCommand.encode('utf-8') + + xlog.info("try setup helper:%s", executeCommand) + os.system(executeCommand) + def getCurrentServiceMenuItemTitle(self): if currentService: return 'Connection: %s' % currentService @@ -155,30 +169,28 @@ def presentAlertWithInfo_(self, info): alert.addButtonWithTitle_("Cancel") self.alertReturn = alert.runModal() == NSAlertFirstButtonReturn - def updateConfig(self, newStatus): - config.set(["modules", "launcher", "proxy"], newStatus) - config.save() - def registerObserver(self): nc = NSWorkspace.sharedWorkspace().notificationCenter() nc.addObserver_selector_name_object_(self, 'windowWillClose:', NSWorkspaceWillPowerOffNotification, None) def windowWillClose_(self, notification): - listNetworkServicesCommand = 'networksetup -listallnetworkservices' - executeResult = subprocess.check_output(listNetworkServicesCommand, shell=True) + executeResult = subprocess.check_output(['networksetup', '-listallnetworkservices']) services = executeResult.split('\n') services = filter(lambda service : service and service.find('*') == -1 and self.getProxyState(service) != 'disable', services) # Remove disabled services and empty lines if len(services) > 0: - disableAutoProxyCommand = ';'.join(map(self.getDisableAutoProxyCommand, services)) - disableGlobalProxyCommand = ';'.join(map(self.getDisableGlobalProxyCommand, services)) - rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, disableGlobalProxyCommand) - executeCommand = rootCommand.encode('utf-8') + try: + map(self.helperDisableAutoProxy, services) + map(self.helperDisableGlobalProxy, services) + except: + disableAutoProxyCommand = ';'.join(map(self.getDisableAutoProxyCommand, services)) + disableGlobalProxyCommand = ';'.join(map(self.getDisableGlobalProxyCommand, services)) + rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, disableGlobalProxyCommand) + executeCommand = rootCommand.encode('utf-8') + + xlog.info("try disable proxy:%s", executeCommand) + os.system(executeCommand) - xlog.info("try disable proxy:%s", executeCommand) - os.system(executeCommand) - - self.updateConfig('disable') module_init.stop_all() os._exit(0) NSApp.terminate_(self) @@ -192,37 +204,46 @@ def resetGoagent_(self, _): module_init.start("gae_proxy") def enableAutoProxy_(self, _): - disableGlobalProxyCommand = self.getDisableGlobalProxyCommand(currentService) - enableAutoProxyCommand = self.getEnableAutoProxyCommand(currentService) - rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableGlobalProxyCommand, enableAutoProxyCommand) - executeCommand = rootCommand.encode('utf-8') + try: + self.helperDisableGlobalProxy(currentService) + self.helperEnableAutoProxy(currentService) + except: + disableGlobalProxyCommand = self.getDisableGlobalProxyCommand(currentService) + enableAutoProxyCommand = self.getEnableAutoProxyCommand(currentService) + rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableGlobalProxyCommand, enableAutoProxyCommand) + executeCommand = rootCommand.encode('utf-8') - xlog.info("try enable auto proxy:%s", executeCommand) - os.system(executeCommand) + xlog.info("try enable auto proxy:%s", executeCommand) + os.system(executeCommand) self.updateStatusBarMenu() - self.updateConfig('pac') def enableGlobalProxy_(self, _): - disableAutoProxyCommand = self.getDisableAutoProxyCommand(currentService) - enableGlobalProxyCommand = self.getEnableGlobalProxyCommand(currentService) - rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, enableGlobalProxyCommand) - executeCommand = rootCommand.encode('utf-8') + try: + self.helperDisableAutoProxy(currentService) + self.helperEnableGlobalProxy(currentService) + except: + disableAutoProxyCommand = self.getDisableAutoProxyCommand(currentService) + enableGlobalProxyCommand = self.getEnableGlobalProxyCommand(currentService) + rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, enableGlobalProxyCommand) + executeCommand = rootCommand.encode('utf-8') - xlog.info("try enable global proxy:%s", executeCommand) - os.system(executeCommand) + xlog.info("try enable global proxy:%s", executeCommand) + os.system(executeCommand) self.updateStatusBarMenu() - self.updateConfig('gae') def disableProxy_(self, _): - disableAutoProxyCommand = self.getDisableAutoProxyCommand(currentService) - disableGlobalProxyCommand = self.getDisableGlobalProxyCommand(currentService) - rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, disableGlobalProxyCommand) - executeCommand = rootCommand.encode('utf-8') - - xlog.info("try disable proxy:%s", executeCommand) - os.system(executeCommand) + try: + self.helperDisableAutoProxy(currentService) + self.helperDisableGlobalProxy(currentService) + except: + disableAutoProxyCommand = self.getDisableAutoProxyCommand(currentService) + disableGlobalProxyCommand = self.getDisableGlobalProxyCommand(currentService) + rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, disableGlobalProxyCommand) + executeCommand = rootCommand.encode('utf-8') + + xlog.info("try disable proxy:%s", executeCommand) + os.system(executeCommand) self.updateStatusBarMenu() - self.updateConfig('disable') # Generate commands for Apple Script def getEnableAutoProxyCommand(self, service): @@ -241,6 +262,21 @@ def getDisableGlobalProxyCommand(self, service): disableHttpsProxyCommand = "networksetup -setsecurewebproxystate \\\"%s\\\" off" % service return "%s;%s" % (disableHttpProxyCommand, disableHttpsProxyCommand) + # Call helper + def helperEnableAutoProxy(self, service): + subprocess.check_call([helper_path, 'enableauto', service, 'http://127.0.0.1:8086/proxy.pac']) + + def helperDisableAutoProxy(self, service): + subprocess.check_call([helper_path, 'disableauto', service]) + + def helperEnableGlobalProxy(self, service): + subprocess.check_call([helper_path, 'enablehttp', service, '127.0.0.1', '8087']) + subprocess.check_call([helper_path, 'enablehttps', service, '127.0.0.1', '8087']) + + def helperDisableGlobalProxy(self, service): + subprocess.check_call([helper_path, 'disablehttp', service]) + subprocess.check_call([helper_path, 'disablehttps', service]) + sys_tray = MacTrayObject.alloc().init() currentService = None From 80a866439728a9a8ce1e7eaddd40edc8a040c18f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9B=BD=E6=99=94?= Date: Sun, 27 Mar 2016 20:12:10 +0800 Subject: [PATCH 3/4] Fix Proxy Detection on Mac Safari enforces https on Twitter so http ajax will fail --- gae_proxy/web_ui/status.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gae_proxy/web_ui/status.html b/gae_proxy/web_ui/status.html index 2483bca35a..ab5e3c5318 100644 --- a/gae_proxy/web_ui/status.html +++ b/gae_proxy/web_ui/status.html @@ -444,7 +444,7 @@

{{ _( "Diagnostic Info" ) }}

} function test_http_proxy_setting() { - if (window.http_proxy_setting === "OK") { + if (window.http_proxy_setting === "OK" || window.https_proxy_setting === "OK") { return "OK"; } From a5cf4109032ce557b9e142f391cb918eedb9e452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9B=BD=E6=99=94?= Date: Sun, 27 Mar 2016 22:41:30 +0800 Subject: [PATCH 4/4] Mac Tray: Refactor Function out of Class --- launcher/mac_tray.py | 167 ++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 83 deletions(-) diff --git a/launcher/mac_tray.py b/launcher/mac_tray.py index e94f187bef..c1252619ab 100644 --- a/launcher/mac_tray.py +++ b/launcher/mac_tray.py @@ -6,7 +6,7 @@ import config current_path = os.path.dirname(os.path.abspath(__file__)) -helper_path = os.path.join(current_path, '..', 'data', 'launcher', 'helper') +helper_path = os.path.join(current_path, os.pardir, 'data', 'launcher', 'helper') if __name__ == "__main__": python_path = os.path.abspath( os.path.join(current_path, os.pardir, 'python27', '1.0')) @@ -32,46 +32,10 @@ def __init__(self): pass def applicationDidFinishLaunching_(self, notification): - self.setupHelper() + setupHelper() self.setupUI() self.registerObserver() - def getProxyState(self, service): - if not service: - return - - # Check if auto proxy is enabled - executeResult = subprocess.check_output(['networksetup', '-getautoproxyurl', service]) - if ( executeResult.find('http://127.0.0.1:8086/proxy.pac\nEnabled: Yes') != -1 ): - return "pac" - - # Check if global proxy is enabled - executeResult = subprocess.check_output(['networksetup', '-getwebproxy', service]) - if ( executeResult.find('Enabled: Yes\nServer: 127.0.0.1\nPort: 8087') != -1 ): - return "gae" - - return "disable" - - def setupHelper(self): - try: - with open(os.devnull) as devnull: - subprocess.check_call(helper_path, stderr=devnull) - except: - cpCommand = "cp \\\"%s\\\" \\\"%s\\\"" % (os.path.join(current_path, 'mac_helper'), helper_path) - chmodCommand = "chmod 4777 \\\"%s\\\"" % helper_path - chownCommand = "chown root \\\"%s\\\"" % helper_path - rootCommand = """osascript -e 'do shell script "%s;%s;%s" with administrator privileges' """ % (cpCommand, chmodCommand, chownCommand) - executeCommand = rootCommand.encode('utf-8') - - xlog.info("try setup helper:%s", executeCommand) - os.system(executeCommand) - - def getCurrentServiceMenuItemTitle(self): - if currentService: - return 'Connection: %s' % currentService - else: - return 'Connection: None' - def setupUI(self): self.statusbar = NSStatusBar.systemStatusBar() self.statusitem = self.statusbar.statusItemWithLength_(NSSquareStatusItemLength) #NSSquareStatusItemLength #NSVariableStatusItemLength @@ -88,7 +52,7 @@ def setupUI(self): self.statusitem.setToolTip_("XX-Net") # Get current selected mode - proxyState = self.getProxyState(currentService) + proxyState = getProxyState(currentService) # Build a very simple menu self.menu = NSMenu.alloc().initWithTitle_('XX-Net') @@ -96,7 +60,7 @@ def setupUI(self): menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Config', 'config:', '') self.menu.addItem_(menuitem) - menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(self.getCurrentServiceMenuItemTitle(), None, '') + menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(getCurrentServiceMenuItemTitle(), None, '') self.menu.addItem_(menuitem) self.currentServiceMenuItem = menuitem @@ -131,7 +95,7 @@ def setupUI(self): NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited) def updateStatusBarMenu(self): - self.currentServiceMenuItem.setTitle_(self.getCurrentServiceMenuItemTitle()) + self.currentServiceMenuItem.setTitle_(getCurrentServiceMenuItemTitle()) # Remove Tick before All Menu Items self.autoGaeProxyMenuItem.setState_(NSOffState) @@ -139,7 +103,7 @@ def updateStatusBarMenu(self): self.disableGaeProxyMenuItem.setState_(NSOffState) # Get current selected mode - proxyState = self.getProxyState(currentService) + proxyState = getProxyState(currentService) # Update Tick before Menu Item if proxyState == 'pac': @@ -176,15 +140,15 @@ def registerObserver(self): def windowWillClose_(self, notification): executeResult = subprocess.check_output(['networksetup', '-listallnetworkservices']) services = executeResult.split('\n') - services = filter(lambda service : service and service.find('*') == -1 and self.getProxyState(service) != 'disable', services) # Remove disabled services and empty lines + services = filter(lambda service : service and service.find('*') == -1 and getProxyState(service) != 'disable', services) # Remove disabled services and empty lines if len(services) > 0: try: - map(self.helperDisableAutoProxy, services) - map(self.helperDisableGlobalProxy, services) + map(helperDisableAutoProxy, services) + map(helperDisableGlobalProxy, services) except: - disableAutoProxyCommand = ';'.join(map(self.getDisableAutoProxyCommand, services)) - disableGlobalProxyCommand = ';'.join(map(self.getDisableGlobalProxyCommand, services)) + disableAutoProxyCommand = ';'.join(map(getDisableAutoProxyCommand, services)) + disableGlobalProxyCommand = ';'.join(map(getDisableGlobalProxyCommand, services)) rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, disableGlobalProxyCommand) executeCommand = rootCommand.encode('utf-8') @@ -205,11 +169,11 @@ def resetGoagent_(self, _): def enableAutoProxy_(self, _): try: - self.helperDisableGlobalProxy(currentService) - self.helperEnableAutoProxy(currentService) + helperDisableGlobalProxy(currentService) + helperEnableAutoProxy(currentService) except: - disableGlobalProxyCommand = self.getDisableGlobalProxyCommand(currentService) - enableAutoProxyCommand = self.getEnableAutoProxyCommand(currentService) + disableGlobalProxyCommand = getDisableGlobalProxyCommand(currentService) + enableAutoProxyCommand = getEnableAutoProxyCommand(currentService) rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableGlobalProxyCommand, enableAutoProxyCommand) executeCommand = rootCommand.encode('utf-8') @@ -219,11 +183,11 @@ def enableAutoProxy_(self, _): def enableGlobalProxy_(self, _): try: - self.helperDisableAutoProxy(currentService) - self.helperEnableGlobalProxy(currentService) + helperDisableAutoProxy(currentService) + helperEnableGlobalProxy(currentService) except: - disableAutoProxyCommand = self.getDisableAutoProxyCommand(currentService) - enableGlobalProxyCommand = self.getEnableGlobalProxyCommand(currentService) + disableAutoProxyCommand = getDisableAutoProxyCommand(currentService) + enableGlobalProxyCommand = getEnableGlobalProxyCommand(currentService) rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, enableGlobalProxyCommand) executeCommand = rootCommand.encode('utf-8') @@ -233,11 +197,11 @@ def enableGlobalProxy_(self, _): def disableProxy_(self, _): try: - self.helperDisableAutoProxy(currentService) - self.helperDisableGlobalProxy(currentService) + helperDisableAutoProxy(currentService) + helperDisableGlobalProxy(currentService) except: - disableAutoProxyCommand = self.getDisableAutoProxyCommand(currentService) - disableGlobalProxyCommand = self.getDisableGlobalProxyCommand(currentService) + disableAutoProxyCommand = getDisableAutoProxyCommand(currentService) + disableGlobalProxyCommand = getDisableGlobalProxyCommand(currentService) rootCommand = """osascript -e 'do shell script "%s;%s" with administrator privileges' """ % (disableAutoProxyCommand, disableGlobalProxyCommand) executeCommand = rootCommand.encode('utf-8') @@ -245,37 +209,74 @@ def disableProxy_(self, _): os.system(executeCommand) self.updateStatusBarMenu() - # Generate commands for Apple Script - def getEnableAutoProxyCommand(self, service): - return "networksetup -setautoproxyurl \\\"%s\\\" \\\"http://127.0.0.1:8086/proxy.pac\\\"" % service - def getDisableAutoProxyCommand(self, service): - return "networksetup -setautoproxystate \\\"%s\\\" off" % service +def setupHelper(): + try: + with open(os.devnull) as devnull: + subprocess.check_call(helper_path, stderr=devnull) + except: + cpCommand = "cp \\\"%s\\\" \\\"%s\\\"" % (os.path.join(current_path, 'mac_helper'), helper_path) + chmodCommand = "chmod 4777 \\\"%s\\\"" % helper_path + chownCommand = "chown root \\\"%s\\\"" % helper_path + rootCommand = """osascript -e 'do shell script "%s;%s;%s" with administrator privileges' """ % (cpCommand, chmodCommand, chownCommand) + executeCommand = rootCommand.encode('utf-8') + + xlog.info("try setup helper:%s", executeCommand) + os.system(executeCommand) + +def getCurrentServiceMenuItemTitle(): + if currentService: + return 'Connection: %s' % currentService + else: + return 'Connection: None' + +def getProxyState(service): + if not service: + return + + # Check if auto proxy is enabled + executeResult = subprocess.check_output(['networksetup', '-getautoproxyurl', service]) + if ( executeResult.find('http://127.0.0.1:8086/proxy.pac\nEnabled: Yes') != -1 ): + return "pac" + + # Check if global proxy is enabled + executeResult = subprocess.check_output(['networksetup', '-getwebproxy', service]) + if ( executeResult.find('Enabled: Yes\nServer: 127.0.0.1\nPort: 8087') != -1 ): + return "gae" + + return "disable" + +# Generate commands for Apple Script +def getEnableAutoProxyCommand(service): + return "networksetup -setautoproxyurl \\\"%s\\\" \\\"http://127.0.0.1:8086/proxy.pac\\\"" % service + +def getDisableAutoProxyCommand(service): + return "networksetup -setautoproxystate \\\"%s\\\" off" % service - def getEnableGlobalProxyCommand(self, service): - enableHttpProxyCommand = "networksetup -setwebproxy \\\"%s\\\" 127.0.0.1 8087" % service - enableHttpsProxyCommand = "networksetup -setsecurewebproxy \\\"%s\\\" 127.0.0.1 8087" % service - return "%s;%s" % (enableHttpProxyCommand, enableHttpsProxyCommand) +def getEnableGlobalProxyCommand(service): + enableHttpProxyCommand = "networksetup -setwebproxy \\\"%s\\\" 127.0.0.1 8087" % service + enableHttpsProxyCommand = "networksetup -setsecurewebproxy \\\"%s\\\" 127.0.0.1 8087" % service + return "%s;%s" % (enableHttpProxyCommand, enableHttpsProxyCommand) - def getDisableGlobalProxyCommand(self, service): - disableHttpProxyCommand = "networksetup -setwebproxystate \\\"%s\\\" off" % service - disableHttpsProxyCommand = "networksetup -setsecurewebproxystate \\\"%s\\\" off" % service - return "%s;%s" % (disableHttpProxyCommand, disableHttpsProxyCommand) +def getDisableGlobalProxyCommand(service): + disableHttpProxyCommand = "networksetup -setwebproxystate \\\"%s\\\" off" % service + disableHttpsProxyCommand = "networksetup -setsecurewebproxystate \\\"%s\\\" off" % service + return "%s;%s" % (disableHttpProxyCommand, disableHttpsProxyCommand) - # Call helper - def helperEnableAutoProxy(self, service): - subprocess.check_call([helper_path, 'enableauto', service, 'http://127.0.0.1:8086/proxy.pac']) +# Call helper +def helperEnableAutoProxy(service): + subprocess.check_call([helper_path, 'enableauto', service, 'http://127.0.0.1:8086/proxy.pac']) - def helperDisableAutoProxy(self, service): - subprocess.check_call([helper_path, 'disableauto', service]) +def helperDisableAutoProxy(service): + subprocess.check_call([helper_path, 'disableauto', service]) - def helperEnableGlobalProxy(self, service): - subprocess.check_call([helper_path, 'enablehttp', service, '127.0.0.1', '8087']) - subprocess.check_call([helper_path, 'enablehttps', service, '127.0.0.1', '8087']) +def helperEnableGlobalProxy(service): + subprocess.check_call([helper_path, 'enablehttp', service, '127.0.0.1', '8087']) + subprocess.check_call([helper_path, 'enablehttps', service, '127.0.0.1', '8087']) - def helperDisableGlobalProxy(self, service): - subprocess.check_call([helper_path, 'disablehttp', service]) - subprocess.check_call([helper_path, 'disablehttps', service]) +def helperDisableGlobalProxy(service): + subprocess.check_call([helper_path, 'disablehttp', service]) + subprocess.check_call([helper_path, 'disablehttps', service]) sys_tray = MacTrayObject.alloc().init()