diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b86abf..9fd67c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/plugin/AloneTcpClient.php b/src/plugin/AloneTcpClient.php index 9197353..76f623f 100644 --- a/src/plugin/AloneTcpClient.php +++ b/src/plugin/AloneTcpClient.php @@ -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(); diff --git a/src/plugin/AnchorRaffle.php b/src/plugin/AnchorRaffle.php index d235c40..4d97690 100644 --- a/src/plugin/AnchorRaffle.php +++ b/src/plugin/AnchorRaffle.php @@ -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']}"); } diff --git a/src/plugin/BaseRaffle.php b/src/plugin/BaseRaffle.php index 6856bf5..8a92e59 100644 --- a/src/plugin/BaseRaffle.php +++ b/src/plugin/BaseRaffle.php @@ -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)) { diff --git a/src/plugin/DataTreating.php b/src/plugin/DataTreating.php index cb40bc4..df78789 100644 --- a/src/plugin/DataTreating.php +++ b/src/plugin/DataTreating.php @@ -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; diff --git a/src/plugin/GiftHeart.php b/src/plugin/GiftHeart.php index e73da01..fb818cb 100644 --- a/src/plugin/GiftHeart.php +++ b/src/plugin/GiftHeart.php @@ -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 = [ diff --git a/src/plugin/GiftRaffle.php b/src/plugin/GiftRaffle.php index d321635..6bd97ad 100644 --- a/src/plugin/GiftRaffle.php +++ b/src/plugin/GiftRaffle.php @@ -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']}"); } diff --git a/src/plugin/GuardRaffle.php b/src/plugin/GuardRaffle.php index bb1b254..f42877c 100644 --- a/src/plugin/GuardRaffle.php +++ b/src/plugin/GuardRaffle.php @@ -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']}"); } diff --git a/src/plugin/Login.php b/src/plugin/Login.php index 5b8f2fb..2c20141 100644 --- a/src/plugin/Login.php +++ b/src/plugin/Login.php @@ -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, */*', diff --git a/src/plugin/Notice.php b/src/plugin/Notice.php index 08a031f..a804e4e 100644 --- a/src/plugin/Notice.php +++ b/src/plugin/Notice.php @@ -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': diff --git a/src/plugin/PkRaffle.php b/src/plugin/PkRaffle.php index c7ffe85..ca09e6e 100644 --- a/src/plugin/PkRaffle.php +++ b/src/plugin/PkRaffle.php @@ -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']}"); } diff --git a/src/plugin/Schedule.php b/src/plugin/Schedule.php index fd73270..0c56586 100644 --- a/src/plugin/Schedule.php +++ b/src/plugin/Schedule.php @@ -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); + } } \ No newline at end of file diff --git a/src/plugin/Silver.php b/src/plugin/Silver.php index f777807..c49e5a9 100644 --- a/src/plugin/Silver.php +++ b/src/plugin/Silver.php @@ -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']}!"); diff --git a/src/plugin/StormRaffle.php b/src/plugin/StormRaffle.php index ea6bcd9..165dc58 100644 --- a/src/plugin/StormRaffle.php +++ b/src/plugin/StormRaffle.php @@ -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'] == '你错过了奖励,下次要更快一点哦~') { diff --git a/src/plugin/ZoneTcpClient.php b/src/plugin/ZoneTcpClient.php index 837c516..1cf35d9 100644 --- a/src/plugin/ZoneTcpClient.php +++ b/src/plugin/ZoneTcpClient.php @@ -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); } } diff --git a/src/tool/BvToAv.php b/src/tool/BvToAv.php new file mode 100644 index 0000000..e7c61d8 --- /dev/null +++ b/src/tool/BvToAv.php @@ -0,0 +1,57 @@ +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; + } +} \ No newline at end of file diff --git a/src/tool/DumpMemory.php b/src/tool/DumpMemory.php index 39f5266..bc223b4 100644 --- a/src/tool/DumpMemory.php +++ b/src/tool/DumpMemory.php @@ -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) { diff --git a/src/util/TimeLock.php b/src/util/TimeLock.php index 22877ea..635d465 100644 --- a/src/util/TimeLock.php +++ b/src/util/TimeLock.php @@ -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; + } + }