[update] Schedule

This commit is contained in:
Lkeme 2020-04-24 20:09:55 +08:00
parent d6b2792485
commit fa58953d93
18 changed files with 273 additions and 71 deletions

View File

@ -1,6 +1,22 @@
# Release Notes
# 本项目Log
## v0.3.0.200424 alpha (2020-04-24)
### Added
- 抽出独立小黑屋
- 提前引入BV2AV
-
### Changed
- 优化监控推送
- 优化登陆参数
- 同步黑屋提醒
### Fixed
- 修复监控输出
-
## v0.3.0.200421 alpha (2020-04-21)
### Added

View File

@ -19,6 +19,7 @@ use Socket\Raw\Factory;
class AloneTcpClient
{
use TimeLock;
private static $heart_lock = 0;
private static $client = null;
private static $server_addr = null;
@ -33,6 +34,7 @@ class AloneTcpClient
if (self::getLock() > time() || getenv('USE_ALONE_SERVER') == 'false') {
return;
}
self::setPauseStatus();
self::init();
self::heartBeat();
self::receive();

View File

@ -145,9 +145,13 @@ class AnchorRaffle extends BaseRaffle
$data = $result['source'];
$content = $result['content'];
$de_raw = json_decode($content, true);
// {"code":-403,"data":null,"message":"访问被拒绝","msg":"访问被拒绝"}
if (isset($de_raw['code']) && $de_raw['code'] == 0) {
Statistics::addSuccessList(self::ACTIVE_TITLE);
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": 参与抽奖成功~");
} elseif (isset($de_raw['msg']) && $de_raw['code'] == -403 && $de_raw['msg'] == '访问被拒绝') {
Log::debug("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}");
self::pauseLock();
} else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}");
}

View File

@ -12,7 +12,9 @@
namespace BiliHelper\Plugin;
use BiliHelper\Core\Log;
use BiliHelper\Core\Curl;abstract class BaseRaffle
use BiliHelper\Core\Curl;
abstract class BaseRaffle
{
const ACTIVE_TITLE = '';
const ACTIVE_SWITCH = '';
@ -30,6 +32,7 @@ use BiliHelper\Core\Curl;abstract class BaseRaffle
if (static::getLock() > time()) {
return;
}
static::setPauseStatus();
static::startLottery();
}
@ -172,6 +175,10 @@ use BiliHelper\Core\Curl;abstract class BaseRaffle
if (getenv(static::ACTIVE_SWITCH) == 'false') {
return false;
}
// 黑屋
if (static::getPauseStatus()) {
return false;
}
$current_rid = (int)$data['rid'];
// 去重
if (static::toRepeatLid($current_rid, false)) {

View File

@ -27,19 +27,19 @@ class DataTreating
return;
}
switch ($data['raffle_type']) {
case "storm":
case 'storm':
// 风暴
StormRaffle::pushToQueue($info);
break;
case "raffle":
case 'raffle':
// 礼物
GiftRaffle::pushToQueue($info);
break;
case "guard":
case 'guard':
// 舰长
GuardRaffle::pushToQueue($info);
break;
case "small_tv":
case 'small_tv':
// 电视
GiftRaffle::pushToQueue($info);
break;

View File

@ -23,6 +23,7 @@ class GiftHeart
if (self::getLock() > time()) {
return;
}
self::setPauseStatus();
if (self::giftHeart()) {
self::setLock(60 * 60);
return;
@ -44,6 +45,12 @@ class GiftHeart
$raw = Curl::get('app', $url, Sign::common($payload));
$de_raw = json_decode($raw, true);
// {"code":400,"msg":"访问被拒绝","message":"访问被拒绝","data":[]}
if (isset($de_raw['msg']) && $de_raw['code'] == 400 && $de_raw['msg'] == '访问被拒绝') {
self::pauseLock();
return false;
}
if ($de_raw['code'] == -403) {
Log::info($de_raw['msg']);
$payload = [

View File

@ -111,6 +111,7 @@ class GiftRaffle extends BaseRaffle
$data = $result['source'];
$content = $result['content'];
$de_raw = json_decode($content, true);
// { "code": -403, "data": null, "message": "访问被拒绝", "msg": "访问被拒绝", }
if (isset($de_raw['code']) && !$de_raw['code']) {
// 推送中奖信息
if ($de_raw['data']['award_name'] != '辣条' && $de_raw['data']['award_name'] != '') {
@ -119,6 +120,9 @@ class GiftRaffle extends BaseRaffle
}
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['award_name']}x{$de_raw['data']['award_num']}");
Statistics::addSuccessList(self::ACTIVE_TITLE);
} elseif (isset($de_raw['msg']) && $de_raw['code'] == -403 && $de_raw['msg'] == '访问被拒绝') {
Log::debug("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}");
self::pauseLock();
} else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}");
}

View File

@ -125,9 +125,13 @@ class GuardRaffle extends BaseRaffle
$data = $result['source'];
$content = $result['content'];
$de_raw = json_decode($content, true);
// {"code":-403,"data":null,"message":"访问被拒绝","msg":"访问被拒绝"}
if (isset($de_raw['code']) && $de_raw['code'] == 0) {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": " . $de_raw['data']['award_name'] . "x" . $de_raw['data']['award_num']);
Statistics::addSuccessList(self::ACTIVE_TITLE);
} elseif (isset($de_raw['msg']) && $de_raw['code'] == -403 && $de_raw['msg'] == '访问被拒绝') {
Log::debug("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}");
self::pauseLock();
} else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}");
}

View File

@ -152,8 +152,8 @@ class Login
$url = 'https://passport.snm0516.aisee.tv/api/tv/login';
$payload = [
'channel' => 'master',
'token' => '5598158bcd8511e2',
'guid' => 'XYEBAA3E54D502E37BD606F0589A356902FCF',
'token' => '5598158bcd8511e9',
'guid' => 'XYEBAA3E54D502E73BD606F0589A356902FCF',
'username' => $user,
'password' => base64_encode($crypt),
'captcha' => $captcha,
@ -201,7 +201,7 @@ class Login
Log::info('登录需要验证, 启动验证码登录!');
$url = 'https://passport.snm0516.aisee.tv/api/captcha';
$payload = [
'token' => '5598158bcd8511e2'
'token' => '5598158bcd8511e9'
];
$headers = [
'Accept' => 'application/json, text/plain, */*',

View File

@ -112,8 +112,8 @@ class Notice
break;
case 'banned':
$info = [
'title' => '账号封禁',
'content' => '[' . $now_time . ']' . ' 用户: ' . self::$uname . ' 账号被封禁: 程序开始睡眠,凌晨自动唤醒,距离唤醒还有' . self::$result . '小时',
'title' => '任务小黑屋',
'content' => '[' . $now_time . ']' . ' 用户: ' . self::$uname . ' 小黑屋: ' . self::$result,
];
break;
case 'error':

View File

@ -118,6 +118,9 @@ class PkRaffle extends BaseRaffle
if (isset($de_raw['code']) && $de_raw['code'] == 0) {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['award_text']}");
Statistics::addSuccessList(self::ACTIVE_TITLE);
} elseif (isset($de_raw['msg']) && $de_raw['code'] == -403 && $de_raw['msg'] == '访问被拒绝') {
Log::debug("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}");
self::pauseLock();
} else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}");
}

View File

@ -11,99 +11,141 @@
namespace BiliHelper\Plugin;
use BiliHelper\Core\Log;
use BiliHelper\Core\Curl;
use BiliHelper\Util\TimeLock;
class Schedule
{
use TimeLock;
// TODO 黑白名单|考虑添加到每个插件内部自动添加|优化RUN逻辑代码
private static $unlock_hour = 24;
private static $unlock_time = 0;
private static $fillable = ['Login', 'Schedule', 'Daily', 'Judge', 'MasterSite', 'GiftSend', 'Task', 'Silver2Coin', 'GroupSignIn', 'AwardRecord', 'Statistics'];
private static $guarded = ['Barrage', 'GiftHeart', 'Heart', 'Silver', 'MaterialObject', 'AloneTcpClient', 'ZoneTcpClient', 'StormRaffle', 'GuardRaffle', 'PkRaffle', 'GiftRaffle', 'AnchorRaffle'];
private static $unlock_timers = [];
private static $sleep_section = [];
// 日常类
private static $fillable = ['Login', 'Schedule', 'Daily', 'Judge', 'MasterSite', 'GiftSend', 'Task', 'Silver2Coin', 'GroupSignIn', 'AwardRecord', 'Statistics'];
// 任务类
private static $guarded_first = ['Barrage', 'GiftHeart', 'Heart', 'Silver', 'MaterialObject'];
// 监控类
private static $guarded_second = ['AloneTcpClient', 'ZoneTcpClient',];
// 抽奖类
private static $guarded_third = ['StormRaffle', 'GuardRaffle', 'PkRaffle', 'GiftRaffle', 'AnchorRaffle'];
public static function run()
{
if (self::getLock() > time()) {
return;
} else {
self::setLock(1 * 60);
}
// 封禁逻辑
if (self::$unlock_time < time()) {
if (!self::isRefuse()) {
self::setLock(5 * 60);
} else {
self::setLock(1 * 60);
return;
}
}
// 休眠逻辑
if (getenv('USE_SLEEP') != 'false' && self::$unlock_time < time() && self::$unlock_hour != date('H')) {
if (!self::isPause()) {
self::setLock(5 * 60);
} else {
self::setLock(1 * 60);
return;
}
};
self::isSleep();
self::isSpecialPause();
self::setLock(1 * 60);
}
/**
* @use 检查休眠
* @return bool
*/
private static function isPause(): bool
private static function isSleep()
{
self::$sleep_section = empty(self::$filter_type) ? explode(',', getenv('SLEEP_SECTION')) : self::$sleep_section;
if (in_array(date('H'), self::$sleep_section)) {
$unlock_time = 60 * 60;
self::stopProc($unlock_time);
Log::warning('进入自定义休眠时间范围,暂停非必要任务,自动开启!');
return true;
}
return false;
if (getenv('USE_SLEEP') != 'false' && self::$unlock_hour != date('H')) {
self::$sleep_section = empty(self::$sleep_section) ? explode(',', getenv('SLEEP_SECTION')) : self::$sleep_section;
if (!in_array(date('H'), self::$sleep_section)) {
return false;
};
self::handleBan('sleep');
};
return true;
}
/**
* @use 检查封禁
* @return bool
* @use 特殊暂停
*/
private static function isRefuse(): bool
private static function isSpecialPause()
{
$url = 'https://api.live.bilibili.com/mobile/freeSilverAward';
$payload = [];
$raw = Curl::get('app', $url, Sign::common($payload));
$de_raw = json_decode($raw, true);
if ($de_raw['msg'] == '访问被拒绝') {
$unlock_time = strtotime(date("Y-m-d", strtotime("+1 day", time()))) - time();
self::stopProc($unlock_time);
Log::warning('账号拒绝访问,暂停非必要任务,自动开启!');
// 推送被ban信息
Notice::push('banned', floor($unlock_time / 60 / 60));
return true;
foreach (self::$guarded_second as $classname) {
$status = call_user_func(array(__NAMESPACE__ . '\\' . $classname, 'getPauseStatus'));
if ($status) {
return true;
}
}
return false;
foreach (self::$guarded_third as $classname) {
$status = call_user_func(array(__NAMESPACE__ . '\\' . $classname, 'getPauseStatus'));
if (!$status) {
return false;
}
}
self::handleBan('special');
return true;
}
/**
* @use 处理禁令
* @param $action
* @param string $classname
*/
private static function handleBan($action, $classname = '')
{
switch ($action) {
// 休眠
case 'sleep':
foreach (self::$fillable as $classname) {
Log::info("插件 {$classname} 白名单,保持当前状态继续");
}
$unlock_time = 60 * 60;
self::$unlock_hour = date('H');
$classname_list = array_merge(self::$guarded_first, self::$guarded_second, self::$guarded_third);
self::stopProc($classname_list, $unlock_time, true);
Log::warning('进入自定义休眠时间范围,暂停非必要任务,自动开启!');
break;
// 暂停访问
case 'pause':
// 访问拒绝 统一时间 第二天0点
$unlock_time = strtotime(date("Y-m-d", strtotime("+1 day", time()))) - time();
self::stopProc([$classname], $unlock_time);
Log::warning("{$classname} 任务拒绝访问,暂停任务,自动开启!");
// 推送被ban信息
$time = floor($unlock_time / 60 / 60);
Notice::push('banned', "任务 {$classname} 暂停,{$time} 小时后自动恢复!");
break;
// 特殊类
case 'special':
// 访问拒绝 统一时间 第二天0点
$unlock_time = strtotime(date("Y-m-d", strtotime("+1 day", time()))) - time();
self::stopProc(self::$guarded_second, $unlock_time);
Log::warning("所有抽奖任务拒绝访问,暂停监控任务,自动开启!");
break;
default:
break;
}
}
/**
* @use 停止运行
* @param array $classname_list
* @param int $unlock_time
* @param bool $force
*/
private static function stopProc(int $unlock_time)
private static function stopProc(array $classname_list, int $unlock_time, bool $force = false)
{
self::$unlock_time = time() + $unlock_time;
self::$unlock_hour = date('H');
foreach (self::$fillable as $classname) {
Log::info("插件 {$classname} 白名单,保持当前状态继续");
}
foreach (self::$guarded as $classname) {
foreach ($classname_list as $classname) {
Log::info("插件 {$classname} 黑名单,锁定状态将于" . date("Y-m-d H:i", time() + $unlock_time) . "解除");
// 强制 无视小黑屋设定
if ($force) {
call_user_func(array(__NAMESPACE__ . '\\' . $classname, 'setPauseStatus'), false);
}
call_user_func(array(__NAMESPACE__ . '\\' . $classname, 'setLock'), $unlock_time + 3 * 60);
call_user_func(array(__NAMESPACE__ . '\\' . $classname, 'setPauseStatus'), true);
}
}
/**
* @use 触发封禁
* @param string $classname
*/
public static function triggerRefused(string $classname)
{
self::handleBan('pause', $classname);
}
}

View File

@ -17,6 +17,7 @@ use BiliHelper\Util\TimeLock;
class Silver
{
use TimeLock;
protected static $task = [];
public static function run()
@ -24,6 +25,7 @@ class Silver
if (self::getLock() > time()) {
return;
}
self::setPauseStatus();
if (empty(self::$task)) {
self::getSilverBox();
@ -81,6 +83,12 @@ class Silver
$data = Curl::get('app', $url, Sign::common($payload));
$data = json_decode($data, true);
// {"code":400,"msg":"访问被拒绝","message":"访问被拒绝","data":[]}
if (isset($data['msg']) && $data['code'] == 400 && $data['msg'] == '访问被拒绝') {
self::pauseLock();
return;
}
if ($data['code'] == -800) {
self::setLock(12 * 60 * 60);
Log::warning("领取宝箱失败,{$data['message']}!");

View File

@ -131,7 +131,8 @@ class StormRaffle extends BaseRaffle
break;
}
if (isset($de_raw['data']) && empty($de_raw['data'])) {
Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴在小黑屋'));
Log::debug(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴在小黑屋'));
self::pauseLock();
break;
}
if ($de_raw['msg'] == '你错过了奖励,下次要更快一点哦~') {

View File

@ -42,6 +42,7 @@ class ZoneTcpClient
if (self::getLock() > time() || getenv('USE_ZONE_SERVER') == 'false') {
return;
}
self::setPauseStatus();
self::init();
self::updateConnection();
self::heartBeat();
@ -353,8 +354,9 @@ class ZoneTcpClient
if (!isset(self::$raffle_list[$data['raffle_type']])) {
self::$raffle_list[$data['raffle_type']] = [];
}
$data['area_id'] = self::$area_id;
array_push(self::$raffle_list[$data['raffle_type']], $data);
// Log::info("获取到分区 #" . self::$area_id . "# {$data['source']}");
Log::info("监测到 @分区 {$data['area_id']} @房间 {$data['room_id']} @抽奖 {$data['raffle_title']}");
// print_r($data);
}
}

57
src/tool/BvToAv.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2020 ~ 2021
* Source: https://github.com/anhao/bv2av/
*/
namespace BiliHelper\Tool;
use BiliHelper\Core\Log;
class BvToAv
{
protected $tr = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
protected $xor = 177451812;
protected $add = 8728348608;
protected $s = [11, 10, 3, 8, 4, 6];
/**
* BV AV
*
* @param $bv
* @return int
*/
public function dec($bv)
{
$r = 0;
$tr = array_flip(str_split($this->tr));
for ($i = 0; $i < 6; $i++) {
$r += $tr[$bv[$this->s[$i]]] * (pow(58, $i));
}
return ($r - $this->add) ^ $this->xor;
}
/**
*
* AV BV
*
* @param $av
* @return string
*/
public function enc($av)
{
$tr = str_split($this->tr);
$bv = 'BV1 4 1 7 ';
$av = ($av ^ $this->xor) + $this->add;
for ($i = 0; $i < 6; $i++) {
$bv[$this->s[$i]] = $tr[floor($av / pow(58, $i) % 58)];
}
return $bv;
}
}

View File

@ -4,7 +4,9 @@
namespace BiliHelper\Tool;
use BiliHelper\Core\Log;
use BiliHelper\Core\Curl;class DumpMemory
use BiliHelper\Core\Curl;
class DumpMemory
{
public static function dd($title)
{

View File

@ -11,10 +11,12 @@
namespace BiliHelper\Util;
use Amp\Delayed;
use BiliHelper\Plugin\Schedule;
trait TimeLock
{
public static $lock = 0;
public static $pause_status = false;
/**
* @use 设置时间
@ -22,7 +24,9 @@ trait TimeLock
*/
public static function setLock(int $lock)
{
static::$lock = time() + $lock;
if (!static::getpauseStatus()) {
static::$lock = time() + $lock;
}
}
/**
@ -54,4 +58,43 @@ trait TimeLock
return strtotime('tomorrow') + ($hour * 60 * 60) - time();
}
/**
* @use 暂停
*/
public static function pauseLock()
{
// 备份几种获取方式 get_called_class()
// basename(str_replace('\\', '/', $class));
// substr(strrchr($class, "\\"), 1);
// substr($class, strrpos($class, '\\') + 1);
// array_pop(explode('\\', $class));
Schedule::triggerRefused((new \ReflectionClass(static::class))->getShortName());
}
/**
* @use 取消暂停
*/
public static function cancelPause()
{
static::$lock = false;
}
/**
* @use 暂停状态
* @return bool
*/
public static function getPauseStatus()
{
return static::$pause_status;
}
/**
* @use 设置状态
* @param bool $status
*/
public static function setPauseStatus(bool $status = false)
{
self::$pause_status = $status;
}
}