Exemple #1
0
def confirm_ossftp_exit():
    launcher_log.debug("start confirm_ossftp_exit")
    for i in range(30):
        if http_request("http://127.0.0.1:8192/quit") == False:
            return True
        time.sleep(1)
    launcher_log.debug("finished confirm_ossftp_exit")
    return False
Exemple #2
0
def confirm_ossftp_exit():
    launcher_log.debug("start confirm_ossftp_exit")
    for i in range(30):
        if http_request("http://127.0.0.1:8192/quit") == False:
            return True
        time.sleep(1)
    launcher_log.debug("finished confirm_ossftp_exit")
    return False
Exemple #3
0
    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()
        launcher_log.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 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()
        launcher_log.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
Exemple #5
0
def stop(module):
    try:
        if not module in proc_handler:
            launcher_log.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
            _start.client.terminate()
            launcher_log.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]

        launcher_log.info("module %s stopped", module)
    except Exception as e:
        launcher_log.exception("stop module %s fail:%s", module, e)
        return "Except:%s" % e
    return "stop success."
Exemple #6
0
def stop(module):
    try:
        if not module in proc_handler:
            launcher_log.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
            _start.client.config.keep_run = False
            launcher_log.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]

        launcher_log.info("module %s stopped", module)
    except Exception as e:
        launcher_log.exception("stop module %s fail:%s", module, e)
        return "Except:%s" % e
    return "stop success."
Exemple #7
0
def confirm_xxnet_exit():
    """suppose xxnet is running, try to close it

    """
    is_xxnet_exit = False
    launcher_log.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:
            launcher_log.debug("good, xxnet:8087 cleared!")
            is_xxnet_exit = True
            break
        else:
            launcher_log.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:
            launcher_log.debug("good, xxnet:%s clear!" % host_port)
            is_xxnet_exit = True
            break
        else:
            launcher_log.debug("<%d>: try to terminate xxnet:%s" % (i, host_port))
        time.sleep(1)
    launcher_log.debug("finished confirm_xxnet_exit")
    return is_xxnet_exit
Exemple #8
0
    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 = "long-term-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", "long-term-stable", "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"}'

                    launcher_log.debug("restart web control.")
                    stop()
                    time.sleep(1)
                    start()
                    launcher_log.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)
            launcher_log.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:
                launcher_log.info("update_test_version fail:%r", e)
                data = '{"res":"fail", "error":"%s"}' % e

        self.send_response('text/html', data)
Exemple #9
0
    def do_GET(self):
        try:
            refer = self.headers.getheader('Referer')
            netloc = urlparse.urlparse(refer).netloc
            if not netloc.startswith("127.0.0.1") and not netloc.startswitch("localhost"):
                launcher_log.warn("web control ref:%s refuse", netloc)
                return
        except:
            pass

        # 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')
            launcher_log.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:
                    launcher_log.warn("request %s no module in path", url_path)
                    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)


        launcher_log.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.wfile.write(b'HTTP/1.1 404\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n404 Not Found')
            launcher_log.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
Exemple #10
0
def confirm_xxnet_exit():
    """suppose xxnet is running, try to close it

    """
    is_xxnet_exit = False
    launcher_log.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:
            launcher_log.debug("good, xxnet:8087 cleared!")
            is_xxnet_exit = True
            break
        else:
            launcher_log.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:
            launcher_log.debug("good, xxnet:%s clear!" % host_port)
            is_xxnet_exit = True
            break
        else:
            launcher_log.debug("<%d>: try to terminate xxnet:%s" %
                               (i, host_port))
        time.sleep(1)
    launcher_log.debug("finished confirm_xxnet_exit")
    return is_xxnet_exit
Exemple #11
0
    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 = "long-term-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", "long-term-stable", "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"}'

                    launcher_log.debug("restart web control.")
                    stop()
                    time.sleep(1)
                    start()
                    launcher_log.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)
            launcher_log.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:
                launcher_log.info("update_test_version fail:%r", e)
                data = '{"res":"fail", "error":"%s"}' % e

        self.send_response('text/html', data)
Exemple #12
0
    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:
                launcher_log.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')
            launcher_log.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:
                    launcher_log.warn("request %s no module in path", url_path)
                    self.send_not_found()
                    return

                if "imp" not in module_init.proc_handler[module]:
                    launcher_log.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)

        launcher_log.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.wfile.write(
                b'HTTP/1.1 404\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n404 Not Found'
            )
            launcher_log.info('%s "%s %s HTTP/1.1" 404 -',
                              self.address_string(), self.command, self.path)
Exemple #13
0
def confirm_xxnet_exit():
    # suppose xxnet is running, try to close it
    is_xxnet_exit = False
    launcher_log.debug("start confirm_xxnet_exit")
    for i in range(30):
        if http_request("http://127.0.0.1:8087/quit") == False:
            launcher_log.debug("good, xxnet:8087 cleared!")
            is_xxnet_exit = True
            break
        else:
            launcher_log.debug("<%d>: try to terminate xxnet:8087" % i)
        time.sleep(1)
    for i in range(30):
        if http_request("http://127.0.0.1:8085/quit") == False:
            launcher_log.debug("good, xxnet:8085 clear!")
            is_xxnet_exit = True
            break
        else:
            launcher_log.debug("<%d>: try to terminate xxnet:8085" % i)
        time.sleep(1)
    launcher_log.debug("finished confirm_xxnet_exit")
    return is_xxnet_exit
Exemple #14
0
    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:
                launcher_log.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")
            launcher_log.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:
                    launcher_log.warn("request %s no module in path", url_path)
                    self.send_not_found()
                    return

                if "imp" not in module_init.proc_handler[module]:
                    launcher_log.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)

        launcher_log.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.wfile.write(b"HTTP/1.1 404\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n404 Not Found")
            launcher_log.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)