feat: Add core mail service and proxy pool modules, and integrate them into the bot.
This commit is contained in:
47
bot.py
47
bot.py
@@ -265,15 +265,54 @@ async def cmd_proxytest(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text("❌ 代理池为空(proxy.txt 不存在或无有效代理)")
|
||||
return
|
||||
|
||||
total = pp.count
|
||||
status_msg = await update.message.reply_text(
|
||||
f"🔍 正在测试 {pp.count} 个代理...(可能需要一些时间)"
|
||||
f"🔍 正在测试 {total} 个代理...\n"
|
||||
f"{_progress_bar(0, total)}",
|
||||
parse_mode="HTML",
|
||||
)
|
||||
|
||||
# 在线程中执行测试避免阻塞事件循环
|
||||
loop = asyncio.get_event_loop()
|
||||
results = await loop.run_in_executor(None, pp.test_all)
|
||||
|
||||
# 构建结果报告
|
||||
# 在后台线程中运行测试,通过回调更新进度
|
||||
def _run_test():
|
||||
ok_count = 0
|
||||
fail_count = 0
|
||||
last_update_time = 0
|
||||
|
||||
def on_progress(current, total, result):
|
||||
nonlocal ok_count, fail_count, last_update_time
|
||||
if result["ok"]:
|
||||
ok_count += 1
|
||||
else:
|
||||
fail_count += 1
|
||||
|
||||
# 限制更新频率:最少 2 秒更新一次,或者最后一个时强制更新
|
||||
now = time.time()
|
||||
is_last = (current == total)
|
||||
if not is_last and (now - last_update_time) < 2:
|
||||
return
|
||||
last_update_time = now
|
||||
|
||||
icon = "✅" if result["ok"] else "❌"
|
||||
latency = f"{result['latency_ms']}ms" if result.get('latency_ms', -1) > 0 else "-"
|
||||
|
||||
text = (
|
||||
f"🔍 <b>代理测试中...</b>\n"
|
||||
f"{_progress_bar(current, total)}\n\n"
|
||||
f"✅ 通过: {ok_count} ❌ 失败: {fail_count}\n\n"
|
||||
f"最新: {icon} <code>{result['proxy']}</code> {latency}"
|
||||
)
|
||||
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
_edit_or_send(status_msg, text), loop
|
||||
)
|
||||
|
||||
return pp.test_all(progress_callback=on_progress)
|
||||
|
||||
results = await loop.run_in_executor(None, _run_test)
|
||||
|
||||
# 构建最终结果报告
|
||||
ok_count = sum(1 for r in results if r["ok"])
|
||||
fail_count = len(results) - ok_count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user