重构Cookie监控系统:添加Web管理后台、Vue3前端界面、Cookie Cloud参数支持、5秒倒计时自动刷新、专业UI配色
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
"""
|
||||
网站管理路由
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models.database import db, Website
|
||||
from models.database import Cookie as CookieModel
|
||||
import requests as http_requests
|
||||
from datetime import datetime
|
||||
|
||||
website_bp = Blueprint('websites', __name__)
|
||||
|
||||
|
||||
@website_bp.route('/check-network', methods=['GET'])
|
||||
def check_all_network():
|
||||
"""检测所有网站的网络可达性"""
|
||||
websites = Website.query.all()
|
||||
result = {}
|
||||
|
||||
for website in websites:
|
||||
try:
|
||||
resp = http_requests.get(website.url, timeout=5, allow_redirects=True)
|
||||
result[website.id] = {
|
||||
'status': resp.status_code,
|
||||
'reachable': resp.status_code < 400,
|
||||
'response_time': resp.elapsed.total_seconds() * 1000
|
||||
}
|
||||
except Exception:
|
||||
result[website.id] = {
|
||||
'status': 0,
|
||||
'reachable': False,
|
||||
'response_time': 0
|
||||
}
|
||||
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@website_bp.route('/check-login/<int:website_id>', methods=['POST'])
|
||||
def check_website_login(website_id):
|
||||
"""检查网站登录状态"""
|
||||
from DrissionPage import ChromiumPage, ChromiumOptions
|
||||
|
||||
website = Website.query.get_or_404(website_id)
|
||||
|
||||
try:
|
||||
# 获取网站的cookie
|
||||
cookies = CookieModel.query.filter_by(website_id=website_id).all()
|
||||
if not cookies:
|
||||
return jsonify({'success': False, 'message': '没有可用的cookie'})
|
||||
|
||||
# 初始化浏览器
|
||||
co = ChromiumOptions()
|
||||
co.set_browser_path(r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
|
||||
co.set_argument('--no-sandbox')
|
||||
co.set_argument('--disable-dev-shm-usage')
|
||||
browser = ChromiumPage(addr_or_opts=co)
|
||||
|
||||
# 导入cookie
|
||||
for cookie in cookies:
|
||||
cookie_dict = {
|
||||
'name': cookie.name,
|
||||
'value': cookie.value,
|
||||
'domain': cookie.domain,
|
||||
'path': cookie.path
|
||||
}
|
||||
browser.set.cookies(cookie_dict)
|
||||
|
||||
# 访问网站
|
||||
browser.get(website.url)
|
||||
browser.wait.doc_loaded(timeout=30)
|
||||
|
||||
import time
|
||||
time.sleep(2)
|
||||
browser.refresh()
|
||||
browser.wait.doc_loaded(timeout=30)
|
||||
time.sleep(2)
|
||||
|
||||
# 检查登录状态
|
||||
login_success = False
|
||||
check_selector = website.login_check_selector
|
||||
success_text = website.success_text
|
||||
|
||||
if check_selector:
|
||||
try:
|
||||
element = browser.ele(check_selector, timeout=10)
|
||||
if element:
|
||||
element_text = element.text or ""
|
||||
if not success_text:
|
||||
login_success = True
|
||||
elif success_text in element_text:
|
||||
login_success = True
|
||||
except:
|
||||
pass
|
||||
|
||||
if not login_success and success_text:
|
||||
try:
|
||||
page_text = browser.html or ""
|
||||
if success_text in page_text:
|
||||
login_success = True
|
||||
except:
|
||||
pass
|
||||
|
||||
# 如果登录成功,获取最新cookie
|
||||
if login_success:
|
||||
new_cookies = browser.cookies()
|
||||
# 更新cookie
|
||||
CookieModel.query.filter_by(website_id=website_id).delete()
|
||||
for c in new_cookies:
|
||||
new_cookie = CookieModel(
|
||||
website_id=website_id,
|
||||
name=c.get('name', ''),
|
||||
value=c.get('value', ''),
|
||||
domain=c.get('domain', ''),
|
||||
path=c.get('path', '/')
|
||||
)
|
||||
db.session.add(new_cookie)
|
||||
|
||||
# 保存检测结果
|
||||
from models.database import Detection
|
||||
detection = Detection(
|
||||
website_id=website_id,
|
||||
status=1,
|
||||
response_time=0,
|
||||
http_status=200,
|
||||
message='登录验证成功'
|
||||
)
|
||||
db.session.add(detection)
|
||||
db.session.commit()
|
||||
|
||||
result = {'success': True, 'message': '登录成功'}
|
||||
else:
|
||||
# 保存检测结果
|
||||
from models.database import Detection
|
||||
detection = Detection(
|
||||
website_id=website_id,
|
||||
status=0,
|
||||
response_time=0,
|
||||
http_status=200,
|
||||
message='登录验证失败'
|
||||
)
|
||||
db.session.add(detection)
|
||||
db.session.commit()
|
||||
|
||||
result = {'success': False, 'message': '登录失败'}
|
||||
|
||||
browser.quit()
|
||||
return jsonify(result)
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'message': f'检测异常: {str(e)}'})
|
||||
|
||||
|
||||
@website_bp.route('/', methods=['GET'])
|
||||
def get_websites():
|
||||
"""获取所有网站"""
|
||||
user_id = request.args.get('user_id')
|
||||
|
||||
if user_id:
|
||||
websites = Website.query.filter_by(user_id=user_id).all()
|
||||
else:
|
||||
websites = Website.query.all()
|
||||
|
||||
return jsonify([website.to_dict() for website in websites])
|
||||
|
||||
|
||||
@website_bp.route('/', methods=['POST'])
|
||||
def create_website():
|
||||
"""创建网站"""
|
||||
data = request.json
|
||||
|
||||
website = Website(
|
||||
user_id=data.get('user_id'),
|
||||
name=data.get('name'),
|
||||
url=data.get('url'),
|
||||
login_check_selector=data.get('login_check_selector'),
|
||||
success_text=data.get('success_text'),
|
||||
interval_minutes=data.get('interval_minutes', 30),
|
||||
status=data.get('status', 1)
|
||||
)
|
||||
|
||||
db.session.add(website)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify(website.to_dict()), 201
|
||||
|
||||
|
||||
@website_bp.route('/<int:website_id>', methods=['GET'])
|
||||
def get_website(website_id):
|
||||
"""获取单个网站"""
|
||||
website = Website.query.get_or_404(website_id)
|
||||
return jsonify(website.to_dict())
|
||||
|
||||
|
||||
@website_bp.route('/<int:website_id>', methods=['PUT'])
|
||||
def update_website(website_id):
|
||||
"""更新网站"""
|
||||
website = Website.query.get_or_404(website_id)
|
||||
data = request.json
|
||||
|
||||
website.name = data.get('name', website.name)
|
||||
website.url = data.get('url', website.url)
|
||||
website.login_check_selector = data.get('login_check_selector', website.login_check_selector)
|
||||
website.success_text = data.get('success_text', website.success_text)
|
||||
website.interval_minutes = data.get('interval_minutes', website.interval_minutes)
|
||||
website.status = data.get('status', website.status)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify(website.to_dict())
|
||||
|
||||
|
||||
@website_bp.route('/<int:website_id>', methods=['DELETE'])
|
||||
def delete_website(website_id):
|
||||
"""删除网站"""
|
||||
website = Website.query.get_or_404(website_id)
|
||||
|
||||
db.session.delete(website)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'message': '网站已删除'})
|
||||
|
||||
|
||||
# 为Website模型添加to_dict方法
|
||||
def website_to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'user_id': self.user_id,
|
||||
'name': self.name,
|
||||
'url': self.url,
|
||||
'login_check_selector': self.login_check_selector,
|
||||
'success_text': self.success_text,
|
||||
'interval_minutes': self.interval_minutes,
|
||||
'status': self.status,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
'updated_at': self.updated_at.isoformat() if self.updated_at else None
|
||||
}
|
||||
|
||||
Website.to_dict = website_to_dict
|
||||
Reference in New Issue
Block a user