Skip to content

Commit

Permalink
Merge pull request jxxghp#929 from thsrite/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp authored Oct 20, 2023
2 parents c2dec7b + 0ce6e51 commit 556d858
Show file tree
Hide file tree
Showing 22 changed files with 388 additions and 74 deletions.
224 changes: 224 additions & 0 deletions app/plugins/autologin/__init__.py
Original file line number Diff line number Diff line change
@@ -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
59 changes: 56 additions & 3 deletions app/plugins/autosignin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class AutoSignIn(_PluginBase):
_clean: bool = False
_start_time: int = None
_end_time: int = None
_auto_cf: int = 0

def init_plugin(self, config: dict = None):
self.sites = SitesHelper()
Expand All @@ -91,6 +92,7 @@ def init_plugin(self, config: dict = None):
self._sign_sites = config.get("sign_sites") or []
self._login_sites = config.get("login_sites") or []
self._retry_keyword = config.get("retry_keyword")
self._auto_cf = config.get("auto_cf")
self._clean = config.get("clean")

# 过滤掉已删除的站点
Expand Down Expand Up @@ -206,6 +208,7 @@ def __update_config(self):
"sign_sites": self._sign_sites,
"login_sites": self._login_sites,
"retry_keyword": self._retry_keyword,
"auto_cf": self._auto_cf,
"clean": self._clean,
}
)
Expand Down Expand Up @@ -333,7 +336,7 @@ def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
'md': 3
},
'content': [
{
Expand All @@ -350,7 +353,7 @@ def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
'md': 3
},
'content': [
{
Expand All @@ -366,7 +369,7 @@ def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
'md': 3
},
'content': [
{
Expand All @@ -378,6 +381,23 @@ def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
}
}
]
},
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 3
},
'content': [
{
'component': 'VTextField',
'props': {
'model': 'auto_cf',
'label': '自动优选',
'placeholder': '0为不开启,命中重试关键词达到数量自动进行Cloudflare IP优选'
}
}
]
}
]
},
Expand Down Expand Up @@ -443,13 +463,33 @@ def get_form(self) -> Tuple[List[dict], Dict[str, Any]]:
]
}
]
},
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
},
'content': [
{
'component': 'VAlert',
'props': {
'text': '自动Cloudflare IP优选,0=不开启,命中重试关键词数量大于该数量时自动执行Cloudflare IP优选。(需要开启且则正确配置Cloudflare IP优选插件和自定义Hosts插件)'
}
}
]
}
]
}
]
}
], {
"enabled": False,
"notify": True,
"cron": "",
"auto_cf": 0,
"onlyonce": False,
"clean": False,
"queue_cnt": 5,
Expand Down Expand Up @@ -710,6 +750,15 @@ def __do(self, today: datetime, type: str, do_sites: list, event: Event = None):
elif '已签到' in s:
already_sign_msg.append(s)
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)

if not self._retry_keyword:
Expand All @@ -724,6 +773,10 @@ def __do(self, today: datetime, type: str, do_sites: list, event: Event = None):
"retry": retry_sites
})

# 自动Cloudflare IP优选
if self._auto_cf and self._auto_cf > 0 and retry_msg and len(retry_msg) > self._auto_cf:
EventManager().send_event(EventType.CloudFlareSpeedTest, {})

# 发送通知
if self._notify:
# 签到详细信息 登录成功、签到成功、已签到、仿真签到成功、失败--命中重试
Expand Down
4 changes: 2 additions & 2 deletions app/plugins/autosignin/sites/52pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def signin(self, site_info: dict) -> Tuple[bool, str]:
return False, '签到失败,请检查站点连通性'

if "login.php" in html_text:
logger.error(f"{site} 签到失败,Cookie失效")
return False, '签到失败,Cookie失效'
logger.error(f"{site} 签到失败,Cookie已失效")
return False, '签到失败,Cookie已失效'

sign_status = self.sign_in_result(html_res=html_text,
regexs=self._sign_regex)
Expand Down
4 changes: 2 additions & 2 deletions app/plugins/autosignin/sites/btschool.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def signin(self, site_info: CommentedMap) -> Tuple[bool, str]:
return False, '签到失败,请检查站点连通性'

if "login.php" in html_text:
logger.error(f"{site} 签到失败,Cookie失效")
return False, '签到失败,Cookie失效'
logger.error(f"{site} 签到失败,Cookie已失效")
return False, '签到失败,Cookie已失效'

# 已签到
if self._sign_text not in html_text:
Expand Down
4 changes: 2 additions & 2 deletions app/plugins/autosignin/sites/chdbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def signin(self, site_info: CommentedMap) -> Tuple[bool, str]:
return False, '签到失败,请检查站点连通性'

if "login.php" in html_text:
logger.error(f"{site} 签到失败,Cookie失效")
return False, '签到失败,Cookie失效'
logger.error(f"{site} 签到失败,Cookie已失效")
return False, '签到失败,Cookie已失效'

sign_status = self.sign_in_result(html_res=html_text,
regexs=self._sign_regex)
Expand Down
Loading

0 comments on commit 556d858

Please sign in to comment.