feat 站点自动登录插件
This commit is contained in:
parent
f0593996a1
commit
b3733ed9ed
224
app/plugins/autologin/__init__.py
Normal file
224
app/plugins/autologin/__init__.py
Normal file
@ -0,0 +1,224 @@
|
|||||||
|
from typing import Any, List, Dict, Tuple
|
||||||
|
|
||||||
|
from app.chain.site import SiteChain
|
||||||
|
from app.core.event import eventmanager
|
||||||
|
from app.db.site_oper import SiteOper
|
||||||
|
from app.log import logger
|
||||||
|
from app.plugins import _PluginBase
|
||||||
|
from app.schemas.types import EventType, NotificationType
|
||||||
|
from app.utils.string import StringUtils
|
||||||
|
|
||||||
|
|
||||||
|
class AutoLogin(_PluginBase):
|
||||||
|
# 插件名称
|
||||||
|
plugin_name = "自动登录"
|
||||||
|
# 插件描述
|
||||||
|
plugin_desc = "配置站点用户名和密码、Cookie过期自动登录刷新Cookie和Ua。"
|
||||||
|
# 插件图标
|
||||||
|
plugin_icon = "login.png"
|
||||||
|
# 主题色
|
||||||
|
plugin_color = "#99b3ff"
|
||||||
|
# 插件版本
|
||||||
|
plugin_version = "1.0"
|
||||||
|
# 插件作者
|
||||||
|
plugin_author = "thsrite"
|
||||||
|
# 作者主页
|
||||||
|
author_url = "https://github.com/thsrite"
|
||||||
|
# 插件配置项ID前缀
|
||||||
|
plugin_config_prefix = "autologin_"
|
||||||
|
# 加载顺序
|
||||||
|
plugin_order = 2
|
||||||
|
# 可使用的用户级别
|
||||||
|
auth_level = 2
|
||||||
|
|
||||||
|
# 私有属性
|
||||||
|
siteoper: SiteOper = None
|
||||||
|
|
||||||
|
# 配置属性
|
||||||
|
_enabled: bool = False
|
||||||
|
_notify: bool = False
|
||||||
|
"""
|
||||||
|
格式
|
||||||
|
站点domain|用户名|用户密码
|
||||||
|
"""
|
||||||
|
_siteconf: list = []
|
||||||
|
|
||||||
|
def init_plugin(self, config: dict = None):
|
||||||
|
self.siteoper = SiteOper()
|
||||||
|
# 配置
|
||||||
|
if config:
|
||||||
|
self._enabled = config.get("enabled")
|
||||||
|
self._notify = config.get("notify")
|
||||||
|
self._siteconf = str(config.get("siteconf")).split('\n')
|
||||||
|
|
||||||
|
def get_state(self) -> bool:
|
||||||
|
return self._enabled
|
||||||
|
|
||||||
|
@eventmanager.register(EventType.SiteLogin)
|
||||||
|
def site_login(self, event):
|
||||||
|
"""
|
||||||
|
开始站点登录
|
||||||
|
"""
|
||||||
|
if not self.get_state():
|
||||||
|
return
|
||||||
|
|
||||||
|
# 站点id
|
||||||
|
site_id = event.event_data.get("site_id")
|
||||||
|
if not site_id:
|
||||||
|
logger.error(f"未获取到site_id")
|
||||||
|
return
|
||||||
|
|
||||||
|
site = self.siteoper.get(site_id)
|
||||||
|
if not site:
|
||||||
|
logger.error(f"未获取到site_id {site_id} 对应的站点数据")
|
||||||
|
return
|
||||||
|
|
||||||
|
site_name = site.name
|
||||||
|
logger.info(f"开始尝试登录站点 {site_name}")
|
||||||
|
siteurl, siteuser, sitepwd = None, None, None
|
||||||
|
# 判断site是否已配置用户名密码
|
||||||
|
for site_conf in self._siteconf:
|
||||||
|
if not site_conf:
|
||||||
|
continue
|
||||||
|
site_confs = str(site_conf).split("|")
|
||||||
|
if len(site_confs) == 3:
|
||||||
|
siteurl = site_confs[0]
|
||||||
|
siteuser = site_confs[1]
|
||||||
|
sitepwd = site_confs[2]
|
||||||
|
else:
|
||||||
|
logger.error(f"{site_conf}配置有误,已跳过")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 判断是否是目标域名
|
||||||
|
if str(siteurl) in StringUtils.get_url_domain(site.url):
|
||||||
|
# 找到目标域名配置,跳出循环
|
||||||
|
break
|
||||||
|
|
||||||
|
# 开始登录更新cookie和ua
|
||||||
|
if siteurl and siteuser and sitepwd:
|
||||||
|
state, messages = SiteChain().update_cookie(site_info=site,
|
||||||
|
username=siteuser,
|
||||||
|
password=sitepwd)
|
||||||
|
if state:
|
||||||
|
logger.info(f"站点{site_name}自动更新Cookie和Ua成功")
|
||||||
|
else:
|
||||||
|
logger.error(f"站点{site_name}自动更新Cookie和Ua失败")
|
||||||
|
|
||||||
|
if self._notify:
|
||||||
|
self.post_message(mtype=NotificationType.SiteMessage,
|
||||||
|
title=f"站点 {site_name} 自动更新Cookie和Ua{'成功' if state else '失败'}")
|
||||||
|
else:
|
||||||
|
logger.error(f"未获取到站点{site_name}配置,已跳过")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_command() -> List[Dict[str, Any]]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_api(self) -> List[Dict[str, Any]]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
拼装插件配置页面,需要返回两块数据:1、页面配置;2、数据结构
|
||||||
|
"""
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
'component': 'VForm',
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VRow',
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VCol',
|
||||||
|
'props': {
|
||||||
|
'cols': 12,
|
||||||
|
'md': 6
|
||||||
|
},
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VSwitch',
|
||||||
|
'props': {
|
||||||
|
'model': 'enabled',
|
||||||
|
'label': '启用插件',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'component': 'VCol',
|
||||||
|
'props': {
|
||||||
|
'cols': 12,
|
||||||
|
'md': 6
|
||||||
|
},
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VSwitch',
|
||||||
|
'props': {
|
||||||
|
'model': 'notify',
|
||||||
|
'label': '开启通知',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'component': 'VRow',
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VCol',
|
||||||
|
'props': {
|
||||||
|
'cols': 12
|
||||||
|
},
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VTextarea',
|
||||||
|
'props': {
|
||||||
|
'model': 'siteconf',
|
||||||
|
'label': '站点配置',
|
||||||
|
'rows': 5,
|
||||||
|
'placeholder': '每一行一个站点,配置方式:\n'
|
||||||
|
'域名domain|用户名|用户密码\n'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'component': 'VRow',
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VCol',
|
||||||
|
'props': {
|
||||||
|
'cols': 12,
|
||||||
|
},
|
||||||
|
'content': [
|
||||||
|
{
|
||||||
|
'component': 'VAlert',
|
||||||
|
'props': {
|
||||||
|
'text': '站点签到提示Cookie过期时自动触发。'
|
||||||
|
'不支持开启两步认证的站点。'
|
||||||
|
'不是所有站点都支持,失败请手动更新。'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
], {
|
||||||
|
"enabled": False,
|
||||||
|
"notify": False,
|
||||||
|
"siteconf": ""
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_page(self) -> List[dict]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def stop_service(self):
|
||||||
|
"""
|
||||||
|
退出插件
|
||||||
|
"""
|
||||||
|
pass
|
@ -750,6 +750,15 @@ class AutoSignIn(_PluginBase):
|
|||||||
elif '已签到' in s:
|
elif '已签到' in s:
|
||||||
already_sign_msg.append(s)
|
already_sign_msg.append(s)
|
||||||
else:
|
else:
|
||||||
|
if 'Cookie已失效' in s and site_id:
|
||||||
|
# 触发自动登录插件登录
|
||||||
|
autologin = self.get_config("AutoLogin")
|
||||||
|
if autologin and autologin.get("enabled") and autologin.get("siteconf"):
|
||||||
|
logger.info(f"触发站点 {site_name} 自动登录更新Cookie和Ua")
|
||||||
|
self.eventmanager.send_event(EventType.SiteLogin,
|
||||||
|
{
|
||||||
|
"site_id": site_id
|
||||||
|
})
|
||||||
failed_msg.append(s)
|
failed_msg.append(s)
|
||||||
|
|
||||||
if not self._retry_keyword:
|
if not self._retry_keyword:
|
||||||
|
@ -59,8 +59,8 @@ class Pt52(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
regexs=self._sign_regex)
|
regexs=self._sign_regex)
|
||||||
|
@ -51,8 +51,8 @@ class BTSchool(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
# 已签到
|
# 已签到
|
||||||
if self._sign_text not in html_text:
|
if self._sign_text not in html_text:
|
||||||
|
@ -60,8 +60,8 @@ class CHDBits(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
regexs=self._sign_regex)
|
regexs=self._sign_regex)
|
||||||
|
@ -49,8 +49,8 @@ class HaiDan(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
regexs=self._succeed_regex)
|
regexs=self._succeed_regex)
|
||||||
|
@ -53,8 +53,8 @@ class Hares(_ISiteSigninHandler):
|
|||||||
return False, '模拟访问失败,请检查站点连通性'
|
return False, '模拟访问失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 模拟访问失败,Cookie失效")
|
logger.error(f"{site} 模拟访问失败,Cookie已失效")
|
||||||
return False, '模拟访问失败,Cookie失效'
|
return False, '模拟访问失败,Cookie已失效'
|
||||||
|
|
||||||
# if self._sign_text in html_res.text:
|
# if self._sign_text in html_res.text:
|
||||||
# logger.info(f"今日已签到")
|
# logger.info(f"今日已签到")
|
||||||
|
@ -53,8 +53,8 @@ class HD4fans(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
# 判断是否已签到
|
# 判断是否已签到
|
||||||
if self._repeat_text in html_text:
|
if self._repeat_text in html_text:
|
||||||
|
@ -54,8 +54,8 @@ class HDArea(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_res.text:
|
if "login.php" in html_res.text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
# 判断是否已签到
|
# 判断是否已签到
|
||||||
# '已连续签到278天,此次签到您获得了100魔力值奖励!'
|
# '已连续签到278天,此次签到您获得了100魔力值奖励!'
|
||||||
|
@ -52,8 +52,8 @@ class HDChina(_ISiteSigninHandler):
|
|||||||
cookie += sub_str + ";"
|
cookie += sub_str + ";"
|
||||||
|
|
||||||
if "hdchina=" not in cookie:
|
if "hdchina=" not in cookie:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
site_cookie = cookie
|
site_cookie = cookie
|
||||||
# 获取页面html
|
# 获取页面html
|
||||||
|
@ -51,8 +51,8 @@ class HDCity(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login" in html_text:
|
if "login" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
# 判断是否已签到
|
# 判断是否已签到
|
||||||
# '已连续签到278天,此次签到您获得了100魔力值奖励!'
|
# '已连续签到278天,此次签到您获得了100魔力值奖励!'
|
||||||
|
@ -54,8 +54,8 @@ class HDSky(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
regexs=self._sign_regex)
|
regexs=self._sign_regex)
|
||||||
|
@ -53,8 +53,8 @@ class HDUpt(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
regexs=self._sign_regex)
|
regexs=self._sign_regex)
|
||||||
|
@ -55,8 +55,8 @@ class Opencd(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
if self._repeat_text in html_text:
|
if self._repeat_text in html_text:
|
||||||
logger.info(f"{site} 今日已签到")
|
logger.info(f"{site} 今日已签到")
|
||||||
|
@ -43,8 +43,12 @@ class PTerClub(_ISiteSigninHandler):
|
|||||||
proxy=proxy,
|
proxy=proxy,
|
||||||
render=render)
|
render=render)
|
||||||
if not html_text:
|
if not html_text:
|
||||||
logger.error(f"{site} 签到失败,签到接口请求失败")
|
logger.error(f"{site} 签到失败,请检查站点连通性")
|
||||||
return False, '签到失败,请检查cookie是否失效'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
|
if "login.php" in html_text:
|
||||||
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
|
return False, '签到失败,Cookie已失效'
|
||||||
try:
|
try:
|
||||||
sign_dict = json.loads(html_text)
|
sign_dict = json.loads(html_text)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -75,8 +75,8 @@ class Tjupt(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
regexs=self._sign_regex)
|
regexs=self._sign_regex)
|
||||||
|
@ -56,8 +56,8 @@ class TTG(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
# 判断是否已签到
|
# 判断是否已签到
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
|
@ -68,8 +68,8 @@ class U2(_ISiteSigninHandler):
|
|||||||
return False, '签到失败,请检查站点连通性'
|
return False, '签到失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 签到失败,Cookie失效")
|
logger.error(f"{site} 签到失败,Cookie已失效")
|
||||||
return False, '签到失败,Cookie失效'
|
return False, '签到失败,Cookie已失效'
|
||||||
|
|
||||||
# 判断是否已签到
|
# 判断是否已签到
|
||||||
sign_status = self.sign_in_result(html_res=html_text,
|
sign_status = self.sign_in_result(html_res=html_text,
|
||||||
|
@ -50,8 +50,8 @@ class ZhuQue(_ISiteSigninHandler):
|
|||||||
return False, '模拟登录失败,请检查站点连通性'
|
return False, '模拟登录失败,请检查站点连通性'
|
||||||
|
|
||||||
if "login.php" in html_text:
|
if "login.php" in html_text:
|
||||||
logger.error(f"{site} 模拟登录失败,Cookie失效")
|
logger.error(f"{site} 模拟登录失败,Cookie已失效")
|
||||||
return False, '模拟登录失败,Cookie失效'
|
return False, '模拟登录失败,Cookie已失效'
|
||||||
|
|
||||||
html = etree.HTML(html_text)
|
html = etree.HTML(html_text)
|
||||||
|
|
||||||
|
@ -48,7 +48,8 @@ class EventType(Enum):
|
|||||||
DirectorySync = "directory.sync"
|
DirectorySync = "directory.sync"
|
||||||
# Cloudflare IP优选
|
# Cloudflare IP优选
|
||||||
CloudFlareSpeedTest = "cloudflare.speedtest"
|
CloudFlareSpeedTest = "cloudflare.speedtest"
|
||||||
|
# 站点自动登录更新Cookie、Ua
|
||||||
|
SiteLogin = "site.login"
|
||||||
|
|
||||||
# 系统配置Key字典
|
# 系统配置Key字典
|
||||||
class SystemConfigKey(Enum):
|
class SystemConfigKey(Enum):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user