def stop(module): try: if module not in proc_handler: xlog.error("module %s not running", module) return if os.path.isfile(os.path.join(root_path, module, "__init__.py")): _start = proc_handler[module]["imp"].start xlog.debug("start to terminate %s module", module) _start.client.terminate() xlog.debug("module %s stopping", module) while _start.client.ready: time.sleep(0.1) else: proc_handler[module]["proc"].terminate() # Sends SIGTERM proc_handler[module]["proc"].wait() del proc_handler[module] xlog.info("module %s stopped", module) except Exception as e: xlog.exception("stop module %s fail:%s", module, e) return "Except:%s" % e return "stop success."
def dialog_yes_no(self, msg="msg", title="Title", data=None, callback=None): msg = unicode(msg) title = unicode(title) alert = NSAlert.alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_( title, "OK", "Cancel", None, msg) alert.setAlertStyle_(0) # informational style res = alert.runModal() xlog.debug("dialog_yes_no return %d", res) # The "ok" button is ``1`` and "cancel" is ``0``. if res == 0: res = 2 return res # Yes:1 No:2 if callback: callback(data, res) return res
def do_GET(self): refer = self.headers.getheader('Referer') if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader('host') if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # check for '..', which will leak file if re.search(r'(\.{2})', self.path) is not None: self.wfile.write(b'HTTP/1.1 404\r\n\r\n') xlog.warn('%s %s %s haking', self.address_string(), self.command, self.path ) return url_path = urlparse.urlparse(self.path).path if url_path == '/': return self.req_index_handler() url_path_list = self.path.split('/') if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", url_path) self.send_not_found() return if "imp" not in module_init.proc_handler[module]: xlog.warn("request module:%s start fail", module) self.send_not_found() return path = '/' + '/'.join(url_path_list[4:]) controler = module_init.proc_handler[module]["imp"].local.web_control.ControlHandler(self.client_address, self.headers, self.command, path, self.rfile, self.wfile) controler.do_GET() return else: relate_path = '/'.join(url_path_list[3:]) file_path = os.path.join(root_path, module, "web_ui", relate_path) if not os.path.isfile(file_path): return self.send_not_found() # i18n code lines (Both the locale dir & the template dir are module-dependent) locale_dir = os.path.abspath(os.path.join(root_path, module, 'lang')) content = i18n_translator.render(locale_dir, file_path) return self.send_response('text/html', content) else: file_path = os.path.join(current_path, 'web_ui' + url_path) xlog.debug ('launcher web_control %s %s %s ', self.address_string(), self.command, self.path) if os.path.isfile(file_path): if file_path.endswith('.js'): mimetype = 'application/javascript' elif file_path.endswith('.css'): mimetype = 'text/css' elif file_path.endswith('.html'): mimetype = 'text/html' elif file_path.endswith('.jpg'): mimetype = 'image/jpeg' elif file_path.endswith('.png'): mimetype = 'image/png' else: mimetype = 'text/plain' self.send_file(file_path, mimetype) elif url_path == '/config': self.req_config_handler() elif url_path == '/update': self.req_update_handler() elif url_path == '/init_module': self.req_init_module_handler() elif url_path == '/quit': self.send_response('text/html', '{"status":"success"}') module_init.stop_all() os._exit(0) elif url_path == '/restart': self.send_response('text/html', '{"status":"success"}') update_from_github.restart_xxnet() else: self.send_not_found() xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
def confirm_xxnet_exit(): """suppose xxnet is running, try to close it """ is_xxnet_exit = False xlog.debug("start confirm_xxnet_exit") for i in range(30): # gae_proxy(default port:8087) if http_request("http://127.0.0.1:8087/quit") == False: xlog.debug("good, xxnet:8087 cleared!") is_xxnet_exit = True break else: xlog.debug("<%d>: try to terminate xxnet:8087" % i) time.sleep(1) for i in range(30): # web_control(default port:8085) host_port = config.get(["modules", "launcher", "control_port"], 8085) req_url = "http://127.0.0.1:{port}/quit".format(port=host_port) if http_request(req_url) == False: xlog.debug("good, xxnet:%s clear!" % host_port) is_xxnet_exit = True break else: xlog.debug("<%d>: try to terminate xxnet:%s" % (i, host_port)) time.sleep(1) xlog.debug("finished confirm_xxnet_exit") return is_xxnet_exit
def req_config_handler(self): req = urlparse.urlparse(self.path).query reqs = urlparse.parse_qs(req, keep_blank_values=True) data = '' if reqs['cmd'] == ['get_config']: config.load() check_update = config.get(["update", "check_update"], 1) if check_update == 0: check_update = "dont-check" elif check_update == 1: check_update = "stable" data = '{ "check_update": "%s", "language": "%s", "popup_webui": %d, "allow_remote_connect": %d, \ "show_systray": %d, "auto_start": %d, "show_detail": %d, "php_enable": %d, "gae_proxy_enable": %d, \ "x_tunnel_enable": %d}' %\ (check_update , config.get(["language"], i18n_translator.lang) , config.get(["modules", "launcher", "popup_webui"], 1) , config.get(["modules", "launcher", "allow_remote_connect"], 0) , config.get(["modules", "launcher", "show_systray"], 1) , config.get(["modules", "launcher", "auto_start"], 0) , config.get(["modules", "gae_proxy", "show_detail"], 0) , config.get(["modules", "php_proxy", "auto_start"], 0) , config.get(["modules", "gae_proxy", "auto_start"], 0) , config.get(["modules", "x_tunnel", "auto_start"], 0) ) elif reqs['cmd'] == ['set_config']: if 'check_update' in reqs: check_update = reqs['check_update'][0] if check_update not in ["dont-check", "stable", "test"]: data = '{"res":"fail, check_update:%s"}' % check_update else: config.set(["update", "check_update"], check_update) config.save() data = '{"res":"success"}' elif 'language' in reqs: language = reqs['language'][0] if language not in i18n_translator.get_valid_languages(): data = '{"res":"fail, language:%s"}' % language else: config.set(["language"], language) config.save() i18n_translator.lang = language self.load_module_menus() data = '{"res":"success"}' elif 'popup_webui' in reqs: popup_webui = int(reqs['popup_webui'][0]) if popup_webui != 0 and popup_webui != 1: data = '{"res":"fail, popup_webui:%s"}' % popup_webui else: config.set(["modules", "launcher", "popup_webui"], popup_webui) config.save() data = '{"res":"success"}' elif 'allow_remote_connect' in reqs: allow_remote_connect = int(reqs['allow_remote_connect'][0]) if allow_remote_connect != 0 and allow_remote_connect != 1: data = '{"res":"fail, allow_remote_connect:%s"}' % allow_remote_connect else: config.set(["modules", "launcher", "allow_remote_connect"], allow_remote_connect) config.save() data = '{"res":"success"}' xlog.debug("restart web control.") stop() time.sleep(1) start() xlog.debug("launcher web control restarted.") elif 'show_systray' in reqs: show_systray = int(reqs['show_systray'][0]) if show_systray != 0 and show_systray != 1: data = '{"res":"fail, show_systray:%s"}' % show_systray else: config.set(["modules", "launcher", "show_systray"], show_systray) config.save() data = '{"res":"success"}' elif 'auto_start' in reqs: auto_start = int(reqs['auto_start'][0]) if auto_start != 0 and auto_start != 1: data = '{"res":"fail, auto_start:%s"}' % auto_start else: if auto_start: autorun.enable() else: autorun.disable() config.set(["modules", "launcher", "auto_start"], auto_start) config.save() data = '{"res":"success"}' elif 'show_detail' in reqs: show_detail = int(reqs['show_detail'][0]) if show_detail != 0 and show_detail != 1: data = '{"res":"fail, show_detail:%s"}' % show_detail else: config.set(["modules", "gae_proxy", "show_detail"], show_detail) config.save() data = '{"res":"success"}' elif 'gae_proxy_enable' in reqs : gae_proxy_enable = int(reqs['gae_proxy_enable'][0]) if gae_proxy_enable != 0 and gae_proxy_enable != 1: data = '{"res":"fail, gae_proxy_enable:%s"}' % gae_proxy_enable else: config.set(["modules", "gae_proxy", "auto_start"], gae_proxy_enable) config.save() if gae_proxy_enable: module_init.start("gae_proxy") else: module_init.stop("gae_proxy") self.load_module_menus() data = '{"res":"success"}' elif 'php_enable' in reqs : php_enable = int(reqs['php_enable'][0]) if php_enable != 0 and php_enable != 1: data = '{"res":"fail, php_enable:%s"}' % php_enable else: config.set(["modules", "php_proxy", "auto_start"], php_enable) config.save() if php_enable: module_init.start("php_proxy") else: module_init.stop("php_proxy") self.load_module_menus() data = '{"res":"success"}' elif 'x_tunnel_enable' in reqs : x_tunnel_enable = int(reqs['x_tunnel_enable'][0]) if x_tunnel_enable != 0 and x_tunnel_enable != 1: data = '{"res":"fail, x_tunnel_enable:%s"}' % x_tunnel_enable else: config.set(["modules", "x_tunnel", "auto_start"], x_tunnel_enable) config.save() if x_tunnel_enable: module_init.start("x_tunnel") else: module_init.stop("x_tunnel") self.load_module_menus() data = '{"res":"success"}' else: data = '{"res":"fail"}' self.send_response('text/html', data)
def do_GET(self): refer = self.headers.getheader('Referer') if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader('host') if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # check for '..', which will leak file if re.search(r'(\.{2})', self.path) is not None: self.wfile.write(b'HTTP/1.1 404\r\n\r\n') xlog.warn('%s %s %s haking', self.address_string(), self.command, self.path) return url_path = urlparse.urlparse(self.path).path if url_path == '/': return self.req_index_handler() url_path_list = self.path.split('/') if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", url_path) self.send_not_found() return if "imp" not in module_init.proc_handler[module]: xlog.warn("request module:%s start fail", module) self.send_not_found() return path = '/' + '/'.join(url_path_list[4:]) controler = module_init.proc_handler[module][ "imp"].local.web_control.ControlHandler( self.client_address, self.headers, self.command, path, self.rfile, self.wfile) controler.do_GET() return else: relate_path = '/'.join(url_path_list[3:]) file_path = os.path.join(root_path, module, "web_ui", relate_path) if not os.path.isfile(file_path): return self.send_not_found() # i18n code lines (Both the locale dir & the template dir are module-dependent) locale_dir = os.path.abspath( os.path.join(root_path, module, 'lang')) content = i18n_translator.render(locale_dir, file_path) return self.send_response('text/html', content) else: file_path = os.path.join(current_path, 'web_ui' + url_path) xlog.debug('launcher web_control %s %s %s ', self.address_string(), self.command, self.path) if os.path.isfile(file_path): if file_path.endswith('.js'): mimetype = 'application/javascript' elif file_path.endswith('.css'): mimetype = 'text/css' elif file_path.endswith('.html'): mimetype = 'text/html' elif file_path.endswith('.jpg'): mimetype = 'image/jpeg' elif file_path.endswith('.png'): mimetype = 'image/png' else: mimetype = 'text/plain' self.send_file(file_path, mimetype) elif url_path == '/config': self.req_config_handler() elif url_path == '/update': self.req_update_handler() elif url_path == '/init_module': self.req_init_module_handler() elif url_path == '/quit': self.send_response('text/html', '{"status":"success"}') module_init.stop_all() os._exit(0) elif url_path == '/restart': self.send_response('text/html', '{"status":"success"}') update_from_github.restart_xxnet() else: self.send_not_found() xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
def req_config_handler(self): req = urlparse.urlparse(self.path).query reqs = urlparse.parse_qs(req, keep_blank_values=True) data = '' if reqs['cmd'] == ['get_config']: config.load() check_update = config.get(["update", "check_update"], 1) if check_update == 0: check_update = "dont-check" elif check_update == 1: check_update = "stable" data = '{ "check_update": "%s", "language": "%s", "popup_webui": %d, "allow_remote_connect": %d, \ "show_systray": %d, "auto_start": %d, "show_detail": %d, "php_enable": %d, "gae_proxy_enable": %d, \ "x_tunnel_enable": %d}' %\ (check_update , config.get(["language"], i18n_translator.lang) , config.get(["modules", "launcher", "popup_webui"], 1) , config.get(["modules", "launcher", "allow_remote_connect"], 0) , config.get(["modules", "launcher", "show_systray"], 1) , config.get(["modules", "launcher", "auto_start"], 0) , config.get(["modules", "gae_proxy", "show_detail"], 0) , config.get(["modules", "php_proxy", "auto_start"], 0) , config.get(["modules", "gae_proxy", "auto_start"], 0) , config.get(["modules", "x_tunnel", "auto_start"], 0) ) elif reqs['cmd'] == ['set_config']: if 'check_update' in reqs: check_update = reqs['check_update'][0] if check_update not in ["dont-check", "stable", "test"]: data = '{"res":"fail, check_update:%s"}' % check_update else: config.set(["update", "check_update"], check_update) config.save() data = '{"res":"success"}' elif 'language' in reqs: language = reqs['language'][0] if language not in i18n_translator.get_valid_languages(): data = '{"res":"fail, language:%s"}' % language else: config.set(["language"], language) config.save() i18n_translator.lang = language self.load_module_menus() data = '{"res":"success"}' elif 'popup_webui' in reqs: popup_webui = int(reqs['popup_webui'][0]) if popup_webui != 0 and popup_webui != 1: data = '{"res":"fail, popup_webui:%s"}' % popup_webui else: config.set(["modules", "launcher", "popup_webui"], popup_webui) config.save() data = '{"res":"success"}' elif 'allow_remote_connect' in reqs: allow_remote_connect = int(reqs['allow_remote_connect'][0]) if allow_remote_connect != 0 and allow_remote_connect != 1: data = '{"res":"fail, allow_remote_connect:%s"}' % allow_remote_connect else: config.set(["modules", "launcher", "allow_remote_connect"], allow_remote_connect) config.save() data = '{"res":"success"}' xlog.debug("restart web control.") stop() time.sleep(1) start() xlog.debug("launcher web control restarted.") elif 'show_systray' in reqs: show_systray = int(reqs['show_systray'][0]) if show_systray != 0 and show_systray != 1: data = '{"res":"fail, show_systray:%s"}' % show_systray else: config.set(["modules", "launcher", "show_systray"], show_systray) config.save() data = '{"res":"success"}' elif 'auto_start' in reqs: auto_start = int(reqs['auto_start'][0]) if auto_start != 0 and auto_start != 1: data = '{"res":"fail, auto_start:%s"}' % auto_start else: if auto_start: autorun.enable() else: autorun.disable() config.set(["modules", "launcher", "auto_start"], auto_start) config.save() data = '{"res":"success"}' elif 'show_detail' in reqs: show_detail = int(reqs['show_detail'][0]) if show_detail != 0 and show_detail != 1: data = '{"res":"fail, show_detail:%s"}' % show_detail else: config.set(["modules", "gae_proxy", "show_detail"], show_detail) config.save() data = '{"res":"success"}' elif 'gae_proxy_enable' in reqs: gae_proxy_enable = int(reqs['gae_proxy_enable'][0]) if gae_proxy_enable != 0 and gae_proxy_enable != 1: data = '{"res":"fail, gae_proxy_enable:%s"}' % gae_proxy_enable else: config.set(["modules", "gae_proxy", "auto_start"], gae_proxy_enable) config.save() if gae_proxy_enable: module_init.start("gae_proxy") else: module_init.stop("gae_proxy") self.load_module_menus() data = '{"res":"success"}' elif 'php_enable' in reqs: php_enable = int(reqs['php_enable'][0]) if php_enable != 0 and php_enable != 1: data = '{"res":"fail, php_enable:%s"}' % php_enable else: config.set(["modules", "php_proxy", "auto_start"], php_enable) config.save() if php_enable: module_init.start("php_proxy") else: module_init.stop("php_proxy") self.load_module_menus() data = '{"res":"success"}' elif 'x_tunnel_enable' in reqs: x_tunnel_enable = int(reqs['x_tunnel_enable'][0]) if x_tunnel_enable != 0 and x_tunnel_enable != 1: data = '{"res":"fail, x_tunnel_enable:%s"}' % x_tunnel_enable else: config.set(["modules", "x_tunnel", "auto_start"], x_tunnel_enable) config.save() if x_tunnel_enable: module_init.start("x_tunnel") else: module_init.stop("x_tunnel") self.load_module_menus() data = '{"res":"success"}' else: data = '{"res":"fail"}' self.send_response('text/html', data)
def req_config_handler(self): req = urlparse.urlparse(self.path).query reqs = urlparse.parse_qs(req, keep_blank_values=True) data = '' current_version = update_from_github.current_version() if reqs['cmd'] == ['get_config']: config.load() check_update = config.get(["update", "check_update"], 1) if check_update == 0: check_update = "dont-check" elif check_update == 1: check_update = "stable" data = '{ "check_update": "%s", "popup_webui": %d, "allow_remote_connect": %d, "show_systray": %d, "auto_start": %d, "php_enable": %d, "gae_proxy_enable": %d }' %\ (check_update , config.get(["modules", "launcher", "popup_webui"], 1) , config.get(["modules", "launcher", "allow_remote_connect"], 0) , config.get(["modules", "launcher", "show_systray"], 1) , config.get(["modules", "launcher", "auto_start"], 0) , config.get(["modules", "php_proxy", "auto_start"], 0) , config.get(["modules", "gae_proxy", "auto_start"], 0)) elif reqs['cmd'] == ['set_config']: if 'check_update' in reqs: check_update = reqs['check_update'][0] if check_update not in ["dont-check", "stable", "test"]: data = '{"res":"fail, check_update:%s"}' % check_update else: config.set(["update", "check_update"], check_update) config.save() data = '{"res":"success"}' elif 'popup_webui' in reqs: popup_webui = int(reqs['popup_webui'][0]) if popup_webui != 0 and popup_webui != 1: data = '{"res":"fail, popup_webui:%s"}' % popup_webui else: config.set(["modules", "launcher", "popup_webui"], popup_webui) config.save() data = '{"res":"success"}' elif 'allow_remote_connect' in reqs: allow_remote_connect = int(reqs['allow_remote_connect'][0]) if allow_remote_connect != 0 and allow_remote_connect != 1: data = '{"res":"fail, allow_remote_connect:%s"}' % allow_remote_connect else: config.set(["modules", "launcher", "allow_remote_connect"], allow_remote_connect) config.save() data = '{"res":"success"}' xlog.debug("restart web control.") stop() time.sleep(1) start() xlog.debug("launcher web control restarted.") elif 'show_systray' in reqs: show_systray = int(reqs['show_systray'][0]) if show_systray != 0 and show_systray != 1: data = '{"res":"fail, show_systray:%s"}' % show_systray else: config.set(["modules", "launcher", "show_systray"], show_systray) config.save() data = '{"res":"success"}' elif 'auto_start' in reqs: auto_start = int(reqs['auto_start'][0]) if auto_start != 0 and auto_start != 1: data = '{"res":"fail, auto_start:%s"}' % auto_start else: if auto_start: autorun.enable() else: autorun.disable() config.set(["modules", "launcher", "auto_start"], auto_start) config.save() data = '{"res":"success"}' elif 'gae_proxy_enable' in reqs : gae_proxy_enable = int(reqs['gae_proxy_enable'][0]) if gae_proxy_enable != 0 and gae_proxy_enable != 1: data = '{"res":"fail, gae_proxy_enable:%s"}' % gae_proxy_enable else: config.set(["modules", "gae_proxy", "auto_start"], gae_proxy_enable) config.save() if gae_proxy_enable: module_init.start("gae_proxy") else: module_init.stop("gae_proxy") self.load_module_menus() data = '{"res":"success"}' elif 'php_enable' in reqs : php_enable = int(reqs['php_enable'][0]) if php_enable != 0 and php_enable != 1: data = '{"res":"fail, php_enable:%s"}' % php_enable else: config.set(["modules", "php_proxy", "auto_start"], php_enable) config.save() if php_enable: module_init.start("php_proxy") else: module_init.stop("php_proxy") self.load_module_menus() data = '{"res":"success"}' else: data = '{"res":"fail"}' elif reqs['cmd'] == ['get_new_version']: versions = update_from_github.get_github_versions() data = '{"res":"success", "test_version":"%s", "stable_version":"%s", "current_version":"%s"}' % (versions[0][1], versions[1][1], current_version) xlog.info("%s", data) elif reqs['cmd'] == ['update_version']: version = reqs['version'][0] try: update_from_github.update_version(version) data = '{"res":"success"}' except Exception as e: xlog.info("update_test_version fail:%r", e) data = '{"res":"fail", "error":"%s"}' % e self.send_response('text/html', data)
def do_GET(self): refer = self.headers.getheader("Referer") if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader("host") if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # check for '..', which will leak file if re.search(r"(\.{2})", self.path) is not None: self.wfile.write(b"HTTP/1.1 404\r\n\r\n") xlog.warn("%s %s %s haking", self.address_string(), self.command, self.path) return url_path = urlparse.urlparse(self.path).path if url_path == "/": return self.req_index_handler() url_path_list = self.path.split("/") if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", url_path) self.send_not_found() return if "imp" not in module_init.proc_handler[module]: xlog.warn("request module:%s start fail", module) self.send_not_found() return path = "/" + "/".join(url_path_list[4:]) controler = module_init.proc_handler[module]["imp"].local.web_control.ControlHandler( self.client_address, self.headers, self.command, path, self.rfile, self.wfile ) controler.do_GET() return else: file_path = os.path.join(root_path, module, url_path_list[3:].join("/")) else: file_path = os.path.join(current_path, "web_ui" + url_path) xlog.debug("launcher web_control %s %s %s ", self.address_string(), self.command, self.path) if os.path.isfile(file_path): if file_path.endswith(".js"): mimetype = "application/javascript" elif file_path.endswith(".css"): mimetype = "text/css" elif file_path.endswith(".html"): mimetype = "text/html" elif file_path.endswith(".jpg"): mimetype = "image/jpeg" elif file_path.endswith(".png"): mimetype = "image/png" else: mimetype = "text/plain" self.send_file(file_path, mimetype) elif url_path == "/config": self.req_config_handler() elif url_path == "/download": self.req_download_handler() elif url_path == "/init_module": self.req_init_module_handler() elif url_path == "/quit": self.send_response("text/html", '{"status":"success"}') module_init.stop_all() os._exit(0) elif url_path == "/restart": self.send_response("text/html", '{"status":"success"}') update_from_github.restart_xxnet() else: self.send_not_found() xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)