diff --git a/pywxdump/__init__.py b/pywxdump/__init__.py index 19b7a12..f6f5b2c 100644 --- a/pywxdump/__init__.py +++ b/pywxdump/__init__.py @@ -11,6 +11,7 @@ from .analyzer.db_parsing import read_img_dat, read_emoji, decompress_CompressCo parse_xml_string, read_BytesExtra from .analyzer import export_csv from .ui import app_show_chat, get_user_list, export +from .server import start_falsk import os, json diff --git a/pywxdump/cli.py b/pywxdump/cli.py index ea83e08..24fa0eb 100644 --- a/pywxdump/cli.py +++ b/pywxdump/cli.py @@ -390,103 +390,6 @@ class MainAll(): MainShowChatRecords().run(args) -def start_falsk(merge_path="", msg_path="", micro_path="", media_path="", wx_path="", key="", my_wxid="", port=5000, - online=False, debug=False): - """ - 启动flask - :param merge_path: 合并后的数据库路径 - :param msg_path: MSG.db 的路径 - :param micro_path: MicroMsg.db 的路径 - :param media_path: MediaMSG.db 的路径 - :param wx_path: 微信文件夹的路径(用于显示图片) - :param key: 密钥 - :param my_wxid: 微信账号(本人微信id) - :param port: 端口号 - :param online: 是否在线查看(局域网查看) - :param debug: 是否开启debug模式 - :return: - """ - tmp_path = os.path.join(os.getcwd(), "wxdump_tmp") # 临时文件夹,用于存放图片等 - if not os.path.exists(tmp_path): - os.makedirs(tmp_path) - print(f"[+] 创建临时文件夹:{tmp_path}") - - session_file = os.path.join(tmp_path, "session") # 用于存放各种基础信息 - - from flask import Flask, g - from flask_cors import CORS - from pywxdump.api import api, read_session, save_session - import logging - - if merge_path: - msg_path = merge_path - micro_path = merge_path - media_path = merge_path - - # 检查端口是否被占用 - if online: - host = '0.0.0.0' - else: - host = "127.0.0.1" - - app = Flask(__name__, template_folder='./ui/web', static_folder='./ui/web/assets/', static_url_path='/assets/') - - # 设置超时时间为 1000 秒 - app.config['TIMEOUT'] = 1000 - app.secret_key = 'secret_key' - - app.logger.setLevel(logging.ERROR) - - CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) # 允许所有域名跨域 - - @app.before_request - def before_request(): - - g.tmp_path = tmp_path # 临时文件夹,用于存放图片等 - g.sf = session_file # 用于存放各种基础信息 - - if msg_path: save_session(session_file, "msg_path", msg_path) - if micro_path: save_session(session_file, "micro_path", micro_path) - if media_path: save_session(session_file, "media_path", media_path) - if wx_path: save_session(session_file, "wx_path", wx_path) - if key: save_session(session_file, "key", key) - if my_wxid: save_session(session_file, "my_wxid", my_wxid) - save_session(session_file, "test", my_wxid) - - app.register_blueprint(api) - - try: - # 自动打开浏览器 - url = f"http://127.0.0.1:{port}/" - # 根据操作系统使用不同的命令打开默认浏览器 - if sys.platform.startswith('darwin'): # macOS - subprocess.call(['open', url]) - elif sys.platform.startswith('win'): # Windows - subprocess.call(['start', url], shell=True) - elif sys.platform.startswith('linux'): # Linux - subprocess.call(['xdg-open', url]) - else: - print("Unsupported platform, can't open browser automatically.") - except Exception as e: - pass - - def is_port_in_use(host, port): - import socket - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - try: - s.bind((host, port)) - except socket.error: - return True - return False - - 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=debug) - class MainUi(): def init_parses(self, parser): diff --git a/pywxdump/server.py b/pywxdump/server.py index ae67758..4951072 100644 --- a/pywxdump/server.py +++ b/pywxdump/server.py @@ -5,31 +5,117 @@ # Author: xaoyaoo # Date: 2024/01/04 # ------------------------------------------------------------------------------- -import logging - -from flask import Flask,g -from flask_cors import CORS -from pywxdump.api.api import api - -app = Flask(__name__, template_folder='./ui/web', static_folder='./ui/web/assets/', static_url_path='/assets/') -app.logger.setLevel(logging.ERROR) +import os +import subprocess +import sys +import time -CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) # 允许所有域名跨域 +def start_falsk(merge_path="", msg_path="", micro_path="", media_path="", wx_path="", key="", my_wxid="", port=5000, + online=False, debug=False): + """ + 启动flask + :param merge_path: 合并后的数据库路径 + :param msg_path: MSG.db 的路径 + :param micro_path: MicroMsg.db 的路径 + :param media_path: MediaMSG.db 的路径 + :param wx_path: 微信文件夹的路径(用于显示图片) + :param key: 密钥 + :param my_wxid: 微信账号(本人微信id) + :param port: 端口号 + :param online: 是否在线查看(局域网查看) + :param debug: 是否开启debug模式 + :return: + """ + tmp_path = os.path.join(os.getcwd(), "wxdump_tmp") # 临时文件夹,用于存放图片等 + if not os.path.exists(tmp_path): + os.makedirs(tmp_path) + print(f"[+] 创建临时文件夹:{tmp_path}") -@app.before_request -def before_request(): - path = r"****.db" - g.msg_path = path - g.micro_path = path - g.media_path = path - g.wx_path = r"*****" - g.my_wxid = "******" - g.tmp_path = "dist" # 临时文件夹,用于存放图片等 - g.user_list = [] + session_file = os.path.join(tmp_path, "session") # 用于存放各种基础信息 + + from flask import Flask, g + from flask_cors import CORS + from pywxdump.api import api, read_session, save_session + import logging + + if merge_path: + msg_path = merge_path + micro_path = merge_path + media_path = merge_path + + # 检查端口是否被占用 + if online: + host = '0.0.0.0' + else: + host = "127.0.0.1" + + app = Flask(__name__, template_folder='./ui/web', static_folder='./ui/web/assets/', static_url_path='/assets/') + + # 设置超时时间为 1000 秒 + app.config['TIMEOUT'] = 1000 + app.secret_key = 'secret_key' + + app.logger.setLevel(logging.ERROR) + + CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) # 允许所有域名跨域 + + @app.before_request + def before_request(): + + g.tmp_path = tmp_path # 临时文件夹,用于存放图片等 + g.sf = session_file # 用于存放各种基础信息 + + if msg_path: save_session(session_file, "msg_path", msg_path) + if micro_path: save_session(session_file, "micro_path", micro_path) + if media_path: save_session(session_file, "media_path", media_path) + if wx_path: save_session(session_file, "wx_path", wx_path) + if key: save_session(session_file, "key", key) + if my_wxid: save_session(session_file, "my_wxid", my_wxid) + save_session(session_file, "test", my_wxid) + + app.register_blueprint(api) + + try: + # 自动打开浏览器 + url = f"http://127.0.0.1:{port}/" + # 根据操作系统使用不同的命令打开默认浏览器 + if sys.platform.startswith('darwin'): # macOS + subprocess.call(['open', url]) + elif sys.platform.startswith('win'): # Windows + subprocess.call(['start', url], shell=True) + elif sys.platform.startswith('linux'): # Linux + subprocess.call(['xdg-open', url]) + else: + print("Unsupported platform, can't open browser automatically.") + except Exception as e: + pass + + def is_port_in_use(host, port): + import socket + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + try: + s.bind((host, port)) + except socket.error: + return True + return False + + 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=debug) -app.register_blueprint(api) +if __name__ == '__main__': + msg_path = r"****.db" + micro_path = r"****.db" + media_path = r"****.db" -print("[+] 请使用浏览器访问 http://127.0.0.1:5000/ 查看聊天记录") -app.run(host='0.0.0.0', port=5000, debug=True) \ No newline at end of file + wx_path = r"****" + my_wxid = "****" + + start_falsk(msg_path=msg_path, micro_path=micro_path, media_path=media_path, wx_path=wx_path, my_wxid=my_wxid, + port=5000, online=False, debug=False)