From 83c1c131c9f0b68e2744e862d50f68128c8d879e Mon Sep 17 00:00:00 2001 From: xaoyaoo Date: Fri, 5 Jan 2024 16:45:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pywxdump/__init__.py | 2 +- pywxdump/api/api.py | 6 +++--- pywxdump/cli.py | 50 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pywxdump/__init__.py b/pywxdump/__init__.py index 41e4e71..3cc3c7d 100644 --- a/pywxdump/__init__.py +++ b/pywxdump/__init__.py @@ -22,4 +22,4 @@ except: VERSION_LIST = {} VERSION_LIST_PATH = None -__version__ = "2.4.6" +__version__ = "2.4.7" diff --git a/pywxdump/api/api.py b/pywxdump/api/api.py index cddd25b..19f7a51 100644 --- a/pywxdump/api/api.py +++ b/pywxdump/api/api.py @@ -11,13 +11,13 @@ import os from flask import Flask, request, render_template, g, Blueprint, send_file from pywxdump import analyzer, read_img_dat, read_audio from pywxdump.api.rjson import ReJson, RqJson + # app = Flask(__name__, static_folder='../ui/web/dist', static_url_path='/') -api = Blueprint('api', __name__, template_folder='templates') +api = Blueprint('api', __name__, template_folder='../ui/web') api.debug = False - @api.route('/api/init', methods=["GET", 'POST']) def init(): """ @@ -182,7 +182,7 @@ def get_audio(): video_data = f"data:audio/wav;base64,{video_base64}" return ReJson(0, video_data) + @api.route('/') def index(): return render_template('index.html') - diff --git a/pywxdump/cli.py b/pywxdump/cli.py index 895e37f..760ca9e 100644 --- a/pywxdump/cli.py +++ b/pywxdump/cli.py @@ -203,12 +203,29 @@ class MainShowChatRecords(): print("[-] 输入数据库路径不存在") return - app = Flask(__name__, template_folder='./ui/web', static_folder='./ui/web/assets/', static_url_path='/assets/') - app.logger.setLevel(logging.ERROR) - from flask_cors import CORS from pywxdump.api import api + + # if getattr(sys, 'frozen', False): + # # The application is run as a bundled executable (PyInstaller) + # base_dir = sys._MEIPASS + # else: + # # The application is run as a script + # base_dir = os.path.abspath(os.path.dirname(__file__)) + + # template_folder = os.path.join(base_dir, 'ui/web') + # static_folder = os.path.join(base_dir, 'ui/web/assets/') + + app = Flask(__name__, template_folder='./ui/web', static_folder='./ui/web/assets/', static_url_path='/assets/') + + # app.template_folder = template_folder + # app.static_folder = static_folder + # app.static_url_path = '/assets/' + + app.logger.setLevel(logging.ERROR) + CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) # 允许所有域名跨域 + @app.before_request def before_request(): g.msg_path = args.msg_path @@ -235,9 +252,25 @@ class MainShowChatRecords(): print("Unsupported platform, can't open browser automatically.") except Exception as e: pass + import socket + def is_port_in_use(host, port): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + try: + s.bind((host, port)) + except socket.error: + return True + return False - print("[+] 请使用浏览器访问 http://127.0.0.1:5000/ 查看聊天记录") - app.run(host='0.0.0.0', port=5000, debug=False) + # 检查端口是否被占用 + host = '0.0.0.0' + port = 5000 + if is_port_in_use(host, port): + print(f"Port {port} is already in use. Choose a different port.") + input("Press Enter to exit...") + else: + time.sleep(1) + print("[+] 请使用浏览器访问 http://127.0.0.1:5000/ 查看聊天记录") + app.run(host=host, port=port, debug=False) class MainExportChatRecords(): @@ -368,9 +401,10 @@ class MainAll(): print( f'[+] success "{os.path.relpath(ret1[0], os.path.commonprefix(wxdbpaths))}" -> "{os.path.relpath(ret1[1], os.getcwd())}"') out_dbs.append(ret1[1]) - print("-" * 32) - print( - "[-] " + f"警告:共 {len(errors)} 个文件未解密(可能原因:非当前登录用户数据库;非加密数据库),详见{out_path}下‘未解密.txt’;") + if len(errors) > 0: + print("-" * 32) + print( + "[-] " + f"警告:共 {len(errors)} 个文件未解密(可能原因:非当前登录用户数据库;非加密数据库),详见{out_path}下‘未解密.txt’;") # print("; ".join([f'"{wxdbpaths[i]}"' for i in errors])) with open(os.path.join(out_path, "未解密.txt"), "w", encoding="utf-8") as f: f.write("\n".join([f'{i}' for i in errors]))