ed / main.py
BG5's picture
Create main.py
79bbd99 verified
import atexit
import subprocess
from DrissionPage._configs.chromium_options import ChromiumOptions
from DrissionPage import Chromium
from DrissionPage._pages.web_page import WebPage
from urllib.parse import urlparse, parse_qs
from mitmproxy import http
from mitmproxy.tools.main import mitmdump
import os
import httpx
import json
try:
import time
except:
pass
def get_info():
# 获取当前代码运行的目录
current_directory = os.getcwd()
# 定义相对路径
relative_file_path = "2.html"
# 构建绝对路径
absolute_file_path = os.path.join(current_directory, relative_file_path)
print(absolute_file_path)
# 定义代理服务器地址
proxy_server = "http://127.0.0.1:12346"
co = ChromiumOptions().set_paths(browser_path=r"microsoft-edge")
co.incognito(on_off=True) # 无痕隐身模式打开的话。不会记住你的网站账号密码
co.headless(on_off=True)
co.set_local_port("12369")
# co.auto_port(on_off=True)
co.set_proxy(proxy_server)
# co.headless(on_off=True) # 或者使用 co.set_argument('--headless=new')
co.set_argument("--disable-gpu") # 禁用GPU加速
co.set_argument('--no-sandbox') # 禁用沙箱提高速度
co.set_argument('--disable-dev-shm-usage')
co.set_argument('--ignore-certificate-errors')
# co.set_argument('2>/dev/null')
co.set_user_agent(
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTM, like Gecko) chrome/124.0.0.8 safari/537.36')
try:
browser2 = Chromium(chromium_options=co)
time.sleep(2)
browser2.get(absolute_file_path, retry=3, interval=2, timeout=15)
return browser2
except Exception as e:
print(f"打开主页失败,请检查网络连接,{e}")
return None
print(f"启动成功...")
def open_browser():
# 获取当前代码运行的目录
current_directory = os.getcwd()
# 定义相对路径
relative_file_path = "2.html"
# 构建绝对路径
absolute_file_path = os.path.join(current_directory, relative_file_path)
print(absolute_file_path)
# 定义代理服务器地址
proxy_server = "http://127.0.0.1:12346"
# 定义命令和路径
command = [
r"microsoft-edge",
# "--new-window",
"--headless",
"--no-sandbox",
"--disable-gpu",
"--remote-debugging-port=9222",
"--disable-dev-shm-usage",
"--ignore-certificate-errors",
"2>/dev/null",
f"--proxy-server={proxy_server}",
f"file:///{absolute_file_path}"
]
# 启动 Microsoft Edge 浏览器并打开本地文件
try:
process = subprocess.Popen(command)
except Exception as e:
# print("Failed to start browser:", e)
process = None
return process
class ListenComment:
def __init__(self):
pass
# 定义一个函数,用于处理每一个响应
def request(self, flow: http.HTTPFlow) -> None:
if 'speech.platform.bing.com' in flow.request.pretty_url:
try:
flow.kill()
except Exception as e:
print(e)
# 解析 URL
url = flow.request.pretty_url
parsed_url = urlparse(url)
# 获取当前的UTC时间戳
utc_time = time.time()
# 将UTC时间戳转换为北京时间(东8区)的时间戳
beijing_time = utc_time + 8 * 3600 # 加上8小时的时间差
# 将北京时间的时间戳转换为结构化时间
beijing_struct_time = time.localtime(beijing_time)
t = time.strftime('%Y-%m-%d %H:%M:%S', beijing_struct_time)
# 获取查询参数
query_params = parse_qs(parsed_url.query)
try:
ConnectionId = query_params['ConnectionId'][0]
# return
except KeyError:
pass
# 打印查询参数
print(t, ' ', url)
# print(query_params)
# 获取Sec-MS-GEC和Sec-MS-GEC-Version
try:
Sec_MS_GEC = query_params['Sec-MS-GEC'][0]
Sec_MS_GEC_Version = query_params['Sec-MS-GEC-Version'][0]
print(f"{t} Sec-MS-GEC: {Sec_MS_GEC}\nSec-MS-GEC-Version: {Sec_MS_GEC_Version}")
data = {
"Sec-MS-GEC": Sec_MS_GEC,
"Sec-MS-GEC-Version": Sec_MS_GEC_Version,
"last_update": int(time.time()),
"last_update_format(UTC +8)": t
}
body = {"key": "edge",
"value": json.dumps(data), # 将字典转换为 JSON 字符串
}
url = 'https://edge-sec.myaitool.top/lsjafadslhhi'
with httpx.Client() as client:
response = client.post(url, json=body)
print(response.text)
return data
except Exception as e:
print(f'cuowu: {e}')
pass
# 遍历查询参数
# for key, value in query_params.items():
# if key == 'Sec-MS-GEC':
# print(f"{key}: {value}")
# print(f"Query parameters for {url}: {query_params}")
addons = [ListenComment()]
if __name__ == '__main__':
# set_proxy()
# atexit.register(close_proxy) '--verbose',
# browser = open_browser()
b = get_info()
# atexit.register(b.quit)
mitmdump(['-s', __file__, '-p 12346', '-q', ])