| import json |
| import os |
|
|
| import requests |
| from flask import request |
|
|
| import public |
|
|
|
|
| class main: |
| |
| def get_operation_log(self): |
| try: |
| logs = json.loads(public.readFile("data/activity_task_logs.json")) |
| except: |
| logs = {} |
| return logs |
|
|
| |
| def write_operation_log(self, task_id, log): |
| logs = self.get_operation_log() |
| if str(task_id) not in logs.keys(): |
| logs[str(task_id)] = [] |
| logs[str(task_id)].append(log) |
| public.writeFile("data/activity_task_logs.json", json.dumps(logs)) |
| return True |
|
|
| |
| def get_task_info(self, get): |
| task_info = {} |
| if not get: |
| |
| try: |
| task_info = json.loads(public.readFile("data/activity_task_info.json")) |
| except: |
| task_info = {} |
| |
| if not task_info: |
| from sslModel import certModel |
| task_info = certModel.main().new_ssl_proxy_request({"url": "/api/v1/ssl/activity/get_task_list","1":1}) |
| if task_info: |
| public.writeFile("data/activity_task_info.json", json.dumps(task_info)) |
| logs = self.get_operation_log() |
| for task in task_info.get("tasks", []): |
| if "rule" not in task: |
| continue |
| if "progress" not in task["rule"]: |
| continue |
| current = len(logs[str(task["id"])] if str(task["id"]) in logs else []) |
| task["rule"]["progress"]["current"] = current if current < task["rule"]["success"]["value"] and not task["success"] else task["rule"]["success"]["value"] |
| task["rule"]["progress"]["last_time"] = logs[str(task["id"])][-1]["time"] if str(task["id"]) in logs and len(logs[str(task["id"])]) > 0 else 0 |
|
|
| return public.returnMsg(True, task_info) |
|
|
| |
| def is_url_match_task(self, req_url, urls, match_type): |
| if req_url[-1] == "?": |
| req_url = req_url[:-1] |
| for i in range(len(urls)): |
| if urls[i][-1] == "?": |
| urls[i] = urls[i][:-1] |
| |
| if "/monitor/" in req_url and len(req_url.split("/")) > 3: |
| return False |
|
|
| |
| if match_type == "exact": |
| if req_url in urls: |
| return True |
| |
| elif match_type == "fuzzy": |
| for url in urls: |
| if url in req_url: |
| return True |
| return False |
|
|
| |
| def update_task_progress(self, task): |
| if task["rule"].get("params", False): |
| |
| for key in task["rule"]["params"].keys(): |
| req_value = request.form.get(key, None) |
| if not req_value: |
| try: |
| req_value = request.json.get(key, None) |
| except: |
| req_value = None |
| if req_value != task["rule"]["params"][key] and task["rule"]["params"][key] != "*": |
| return task |
|
|
| success_rule = task["rule"]["success"] |
| if success_rule["type"] == "count": |
| import time |
| current_time = int(time.time()) |
| reset = success_rule["reset"] |
| last_time = task["rule"]["progress"]["last_time"] |
| |
| if current_time - last_time < reset: |
| return task |
| |
| |
| log = { |
| "time": current_time, |
| "task_id": task["id"], |
| "path": request.full_path, |
| "message": "任务进度更新,当前进度:{}/{}".format( |
| task["rule"]["progress"]["current"] + 1, |
| success_rule["value"] |
| ) |
| } |
| self.write_operation_log(task["id"], log) |
|
|
| |
| if task["rule"]["progress"]["current"] + 1 >= success_rule["value"]: |
| task["success"] = True |
| self.submit_completed_task(task["id"]) |
| return task |
|
|
| |
| def check_task_status(self, response): |
| |
| if not os.path.exists("/www/server/panel/data/userInfo.json"): |
| return |
| |
| if os.path.exists("/www/server/panel/data/activity_inviter_id.pl"): |
| inviter_id = public.readFile("/www/server/panel/data/activity_inviter_id.pl") |
| if inviter_id: |
| from sslModel import certModel |
| certModel.main().new_ssl_proxy_request( |
| {"url": "/api/v1/ssl/activity/user_pull_new", "inviter_id": inviter_id}) |
| |
| os.rename("/www/server/panel/data/activity_inviter_id.pl", "/www/server/panel/data/activity_inviter_id") |
|
|
| task_info = self.get_task_info(None)['msg'] |
| try: |
| res = response.get_json() |
| except: |
| return |
| if not task_info: |
| return |
| |
| if task_info["success"]: |
| return |
| |
| req_url = request.full_path |
| is_matched = False |
| for task in task_info["tasks"]: |
| if not task["rule"]: |
| continue |
| if task["success"]: |
| continue |
| if isinstance(res, dict) and "status" in res and not res["status"]: |
| continue |
| urls = task["rule"]["urls"] |
| match_type = task["rule"]["match_type"] |
| if not self.is_url_match_task(req_url, urls, match_type): |
| continue |
| is_matched = True |
|
|
| |
| |
| task = self.update_task_progress(task) |
|
|
| |
| if not is_matched: |
| return task_info |
|
|
| |
| all_success = True |
| for task in task_info["tasks"]: |
| if not task["success"]: |
| all_success = False |
| break |
| task_info["success"] = all_success |
| |
| public.writeFile("data/activity_task_info.json", json.dumps(task_info)) |
| return task_info |
|
|
| |
| def submit_completed_task(self, task_id): |
| try: |
| from sslModel import certModel |
| res = certModel.main().new_ssl_proxy_request({"task_id": task_id, "url": "/api/v1/ssl/activity/user_complete_task"}) |
| public.print_log(res) |
| return res |
| except Exception as e: |
| public.print_log("提交已完成任务失败: {}".format(e)) |
| return public.returnMsg(False, "提交已完成任务失败!") |
|
|
|
|