Exemple #1
0
    async def make_var(self, local_var, controller_var):
        if controller_var == "none" or str(controller_var).replace(" ", "") == "" or str(controller_var).replace(" ",
                                                                                                                 "") == "{}":
            pass
        else:
            if self.timer_app == "" or self.timer_app is None:
                controller_var = json.loads(controller_var)

                for x in controller_var:
                    key = x + "&&" + self.only_id + "&&text"
                    redis.set(key, controller_var[x]["text"], ex=60 * 1)

        if local_var == "none" or str(local_var).replace(" ", "") == "" or str(local_var).replace(" ", "") == "[]":
            pass
        else:
            local_var = json.loads(local_var)
            # 局部变量
            local_var_data = {}

            for x in local_var:
                local_var_data[x["key"]] = x["value"]

            if len(local_var_data) > 0:
                self.local_var_data = local_var_data

        global_var_data = {}

        global_var_list = Variablen.select('key', 'value').where("status", 0).get()

        for x in global_var_list:
            global_var_data[str(x.key)] = str(x.value)

        if len(global_var_data) > 0:
            self.global_var_data = global_var_data
Exemple #2
0
def _get_login_auth_code(phone):
    """
    使用手机号获得登录验证码
    :param phone:
    :return:
    """
    key = "%s:%d" % (app.config.get("MANAGER_LOGIN_MSG_CODE"), int(phone))
    auth_count_key = "AUTH_COUNT_{0}:{1}".format(
        app.config.get("MANAGER_LOGIN_MSG_CODE"), int(phone))
    if redis.exists(key):
        code = int(redis.get(key))
    else:
        code = int(random.uniform(1000, 9999))
        redis.set(key, code)
        redis.expire(key, 15 * 60)
        redis.set(auth_count_key, 0)
        redis.expire(auth_count_key, 15 * 60)
    return code
Exemple #3
0
    async def get_app_data(self, uuid, app_uuid, app_info=None):
        key_result = app_uuid + "&&" + self.only_id + "&&result"
        key_status = app_uuid + "&&" + self.only_id + "&&status"

        if self.input_app == app_uuid or self.webhook_app == app_uuid or self.timer_app == app_uuid:
            if redis.exists(key_result) == 0:
                result_data = await self.execute(app_uuid=app_uuid)

                result = str(result_data["result"])
                redis.set(key_result, result, ex=60 * 1)
                redis.set(key_status, result_data["status"], ex=60 * 1)

                if self.input_app == app_uuid:
                    await self.add_execute_logs(uuid=uuid, app_uuid=app_uuid, app_name="用户输入",
                                                result=result, status=result_data["status"], html=result)
                elif self.webhook_app == app_uuid:
                    await self.add_execute_logs(uuid=uuid, app_uuid=app_uuid, app_name="WebHook",
                                                result=result, status=result_data["status"],
                                                html=result)
                elif self.timer_app == app_uuid:
                    await self.add_execute_logs(uuid=uuid, app_uuid=app_uuid, app_name="定时器",
                                                result=result, status=result_data["status"],
                                                html=result)

                return result_data["status"], result
            else:
                return int(redis.get(key_status).decode()), redis.get(key_result).decode()
        else:
            if redis.exists(key_result) == 0:
                result_data = await self.execute(app_uuid=app_uuid, app_dir=app_info["app_dir"],
                                                 data=app_info["data"])

                result = str(result_data["result"])
                redis.set(key_result, result, ex=60 * 1)
                redis.set(key_status, result_data["status"], ex=60 * 1)

                if str(result_data["html"]) == "":
                    html_data = result
                else:
                    html_data = result_data["html"]

                await self.add_execute_logs(
                    uuid=uuid,
                    app_uuid=app_uuid,
                    app_name=app_info["data"]["node_name"],
                    result=result,
                    status=result_data["status"],
                    html=html_data,
                    args=result_data["args"]
                )

                return result_data["status"], result
            else:
                return int(redis.get(key_status).decode()), redis.get(key_result).decode()
Exemple #4
0
    async def run(self, uuid):
        redis.incr("exec_sum")
        redis.incr(uuid + "&&exec_sum")

        workflow_info = Workflow.select(
            'uuid',
            'name',
            'remarks',
            'start_app',
            'end_app',
            'input_app',
            'webhook_app',
            'timer_app',
            'flow_json',
            'flow_data',
            'controller_data',
            'local_var_data',
            'status'
        ).where(
            "uuid", uuid
        ).first()

        if workflow_info:
            if str(workflow_info.status) == "1":
                return False

            self.workflow_name = workflow_info.name
            self.workflow_remarks = workflow_info.remarks
            self.start_app = workflow_info.start_app
            self.end_app = workflow_info.end_app
            self.input_app = workflow_info.input_app
            self.webhook_app = workflow_info.webhook_app
            self.timer_app = workflow_info.timer_app
            self.flow_json = json.loads(workflow_info.flow_json)
            self.flow_data = json.loads(workflow_info.flow_data)

            await self.make_var(workflow_info.local_var_data, workflow_info.controller_data)

            target_app = await self.find_start_app(edges=self.flow_json["edges"], start_app=self.start_app)

            await self.add_execute_logs(uuid=uuid, app_uuid=self.start_app, app_name="开始", result="剧本开始执行", status=0,
                                        html="<span>剧本开始执行</span>")

            is_while = True

            while is_while:
                try:
                    edge_name, source_app, next_app, is_switch, edge_action, edge_if_else = await self.find_next_app(
                        edges=self.flow_json["edges"],
                        next_app=target_app
                    )

                except Exception as e:
                    await self.add_execute_logs(uuid=uuid, app_uuid="", app_name="", result="当前剧本不具有可执行条件", status=1,
                                                html="<span>当前剧本不具有可执行条件</span>")
                    is_while = False
                    break

                key = target_app + "&&" + self.only_id + "&&sum"
                if redis.exists(key) == 1:
                    sum = redis.get(key)
                    redis.set(key, int(sum) + 1, ex=60 * 1)
                else:
                    redis.set(key, 1, ex=60 * 1)

                if self.input_app == source_app or self.webhook_app == source_app or self.timer_app == source_app:
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app)
                else:
                    source_info = self.flow_data[source_app]
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app,
                                                                        app_info=source_info)

                if is_status == 0:
                    if is_switch:
                        if str(edge_action) == "1":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if edge_if_else == if_else_result:
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "2":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result not in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if edge_if_else != if_else_result:
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "3":
                            is_arr = re.findall(r'{0}'.format(edge_if_else), if_else_result)
                            if len(is_arr) > 0:
                                target_app = next_app
                            else:
                                pass
                    else:
                        target_app = next_app
                else:
                    is_while = False

                if next_app == self.end_app:
                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")
                    await self.add_report()
                    is_while = False

                    if int(redis.decr("exec_sum")) < 0:
                        redis.set("exec_sum", "0")

                    if int(redis.decr(uuid + "&&exec_sum")) < 0:
                        redis.set(uuid + "&&exec_sum", "0")

                    break
Exemple #5
0
    async def execute(self, app_uuid, app_dir=None, data=None):

        args_data = copy.deepcopy(data)

        if app_dir:
            args_data["app_dir"] = app_dir

        args_data_json_x = json.dumps(args_data)

        if self.input_app == app_uuid or self.webhook_app == app_uuid:
            key = app_uuid + "&&" + self.only_id + "&&text"
            return {"status": 0, "result": redis.get(key).decode(), "args": args_data_json_x, "html": ""}

        if self.timer_app == app_uuid:
            return {"status": 0, "result": "定时器正在执行", "args": args_data_json_x, "html": ""}

        is_identification = await self.is_identification(app_dir=app_dir)

        if is_identification == False:
            return {"status": 1, "result": "请勿使用非法应用", "args": args_data_json_x, "html": ""}

        is_public_status, is_public = await self.is_public(app_dir=app_dir)

        if is_public_status == 0:
            return {"status": 1, "result": "请配置 is_public", "args": args_data_json_x, "html": ""}

        import_path = ""

        if platform.system() == 'Windows':
            import_path = 'apps.' + str(app_dir) + '.windows.run'
        elif platform.system() == 'Linux':
            import_path = 'apps.' + str(app_dir) + '.linux.run'
        elif platform.system() == "Darwin":
            import_path = 'apps.' + str(app_dir) + '.mac.run'

        try:
            data["app"] = __import__(import_path, fromlist=['*'])
        except Exception as e:
            return {"status": 1, "result": "请使用正确的应用", "args": args_data_json_x, "html": ""}

        args = ""

        for key in data:
            if key != "node_name" and key != "action" and key != "app" and key != "action_name" and key != "description" and key != "app_dir":
                args = args + "," + key

                var_status, text = await self.analysis_var(text=str(data[key]))

                redis_key = app_uuid + "&&" + self.only_id + "&&" + key
                redis.set(redis_key, text, ex=60 * 1)

                if var_status == 0:
                    data[key] = text
                    args_data[key] = text
                else:
                    return {"status": var_status, "result": text, "args": args_data_json_x, "html": ""}

        args_data_json = json.dumps(args_data)

        eval_action = "app.{action}({args})".format(action=data["action"], args=args[1:])

        try:
            result_data = await eval(eval_action, data)
        except TypeError as e:
            return {"status": 1, "result": "请勿使用非法应用", "args": args_data_json, "html": ""}

        if "status" not in result_data:
            return {"status": 2, "result": "APP 错误,请检测 status 返回字段", "args": args_data_json, "html": ""}

        if "result" not in result_data:
            return {"status": 2, "result": "APP 错误,请检测 result 返回字段", "args": args_data_json, "html": ""}

        if "html" not in result_data:
            html_data = ""
        else:
            html_data = result_data["html"]

        return {"status": result_data["status"], "result": str(result_data["result"]), "args": args_data_json,
                "html": html_data}
Exemple #6
0
    async def run(self, uuid):
        redis.incr("exec_sum")
        redis.incr(uuid + "&&exec_sum")

        workflow_info = Workflow.select(
            'uuid',
            'name',
            'remarks',
            'start_app',
            'end_app',
            'input_app',
            'webhook_app',
            'timer_app',
            'flow_json',
            'flow_data',
            'controller_data',
            'local_var_data',
            'status'
        ).where(
            "uuid", uuid
        ).first()

        if workflow_info:
            if str(workflow_info.status) == "1":
                return False

            self.workflow_name = workflow_info.name
            self.workflow_remarks = workflow_info.remarks
            self.start_app = workflow_info.start_app
            self.end_app = workflow_info.end_app
            self.input_app = workflow_info.input_app
            self.webhook_app = workflow_info.webhook_app
            self.timer_app = workflow_info.timer_app
            self.flow_json = json.loads(workflow_info.flow_json)
            self.flow_data = json.loads(workflow_info.flow_data)

            await self.make_var(workflow_info.local_var_data, workflow_info.controller_data)

            target_app = await self.find_start_app(edges=self.flow_json["edges"], start_app=self.start_app)

            await self.add_execute_logs(uuid=uuid, app_uuid=self.start_app, app_name="开始", result="剧本开始执行", status=0,
                                        html="<span>剧本开始执行</span>")
            is_while = True

            while is_while:
                try:
                    edge_name, source_app, next_app, is_switch, edge_action, edge_if_else = await self.find_next_app(
                        edges=self.flow_json["edges"],
                        next_app=target_app
                    )
                except Exception as e:
                    await self.add_execute_logs(uuid=uuid, app_uuid="", app_name="", result="当前剧本不具有可执行条件", status=1,
                                                html="<span>当前剧本不具有可执行条件</span>")

                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")

                    await self.decr_sum(uuid=uuid)
                    is_while = False
                    break

                key = target_app + "&&" + self.only_id + "&&sum"
                if redis.exists(key) == 1:
                    sum = redis.get(key)
                    redis.set(key, int(sum) + 1, ex=lose_time)
                else:
                    redis.set(key, 1, ex=lose_time)

                if self.input_app == source_app or self.webhook_app == source_app or self.timer_app == source_app:
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app)
                else:
                    source_info = self.flow_data[source_app]
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app,
                                                                        app_info=source_info)

                if str(is_status) == "0":
                    if is_switch:
                        if str(edge_action) == "1":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if str(edge_if_else) == str(if_else_result):
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "2":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result not in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if str(edge_if_else) != str(if_else_result):
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "3":
                            is_arr = re.findall(r'{0}'.format(edge_if_else), if_else_result)
                            if len(is_arr) > 0:
                                target_app = next_app
                            else:
                                pass
                        elif str(edge_action) == "4":
                            is_json_arr = re.findall(r'\{\w.*\}!=\w.*', edge_if_else)
                            is_json_arr_equal = re.findall(r'\{\w.*\}=\w.*', edge_if_else)

                            if len(is_json_arr) == 1:
                                is_equal = False
                                is_json_arr = is_json_arr[0].split("!=")
                                json_key = str(is_json_arr[0])
                                json_val = str(is_json_arr[1])
                            else:
                                is_equal = True
                                is_json_arr = is_json_arr_equal[0].split("=")
                                json_key = str(is_json_arr[0])
                                json_val = str(is_json_arr[1])

                            if len(is_json_arr) > 0:
                                edge_if_else_key = json_key.replace("{", "").replace("}", "")
                                edge_if_else_arr = edge_if_else_key.split(".")

                                is_json = self.is_json(json_text=if_else_result)

                                if is_json is False:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="非 JSON 格式变量",
                                        status=1,
                                        html="<span>非 JSON 格式变量</span>")

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break

                                try:
                                    if_else_result = json.loads(json.loads(if_else_result))
                                except:
                                    if_else_result = json.loads(if_else_result)

                                key_all = "if_else_result"
                                key_front = "['"
                                key_after = "']"
                                key_number_front = "["
                                key_number_after = "]"

                                for k in edge_if_else_arr:
                                    try:
                                        int(k)
                                        key_all += key_number_front + k + key_number_after
                                    except ValueError:
                                        k = str(k).replace("!!!", "")
                                        key_all += key_front + k + key_after
                                try:
                                    if_else_result = eval(key_all)
                                    if is_equal:
                                        if str(if_else_result) == json_val:
                                            target_app = next_app
                                        else:
                                            pass
                                    else:
                                        if str(if_else_result) != json_val:
                                            target_app = next_app
                                        else:
                                            pass
                                except IndexError:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="未找到 JSON Index : {key}".format(key=edge_if_else_key),
                                        status=1,
                                        html="<span>未找到 JSON Index : {key}</span>".format(key=edge_if_else_key))

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break
                                except KeyError:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="未找到 JSON KEY : {key}".format(key=edge_if_else_key),
                                        status=1,
                                        html="<span>未找到 JSON KEY : {key}</span>".format(key=edge_if_else_key))

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break
                                except TypeError:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="JSON 格式不存在 : {key}".format(key=edge_if_else_key),
                                        status=1,
                                        html="<span>JSON 格式不存在 : {key}</span>".format(key=edge_if_else_key))

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break
                    else:
                        target_app = next_app
                else:
                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")
                    await self.decr_sum(uuid=uuid)
                    is_while = False
                    break

                if next_app == self.end_app:
                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")
                    await self.add_report()
                    await self.decr_sum(uuid=uuid)
                    # redis.delete(*redis.keys(pattern='*{key}*'.format(key=self.only_id)))
                    is_while = False
                    break
Exemple #7
0
    async def decr_sum(self, uuid):
        if int(redis.decr("exec_sum")) < 0:
            redis.set("exec_sum", "0")

        if int(redis.decr(uuid + "&&exec_sum")) < 0:
            redis.set(uuid + "&&exec_sum", "0")