CheckGooglePlayStore.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Notification\TelegramBot;
  4. use App\Util;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Redis;
  7. /**
  8. * 定时检测 Google Play 上架包是否仍可访问(大致对应「未下架」)。
  9. *
  10. * 规则与 iOS 侧类似:
  11. * - online : HTTP 200 且页面含正常 Play 详情特征
  12. * - delisted : HTTP 404,或 200 但为 Play「Not Found」错误页
  13. * - unknown : 超时、429、5xx、页面过短无法判断等
  14. *
  15. * 连续 delisted / unknown 达阈值才告警;恢复 online 会清计数与已通知标记。
  16. */
  17. class CheckGooglePlayStore extends Command
  18. {
  19. protected $signature = 'google:check-play-store';
  20. protected $description = '检测 Google Play 包是否疑似下架,告警时 Telegram sendMsgAndImportant';
  21. /** 展示名 => Play 详情页完整 URL(须含 id= 包名) */
  22. protected array $apps = [
  23. 'google-AFVegas-Nights-228' => 'https://play.google.com/store/apps/details?id=com.oipjuwenkjhde.kjhbnmuiejnd',
  24. 'google-PrimeAF-Gaming-229' => 'https://play.google.com/store/apps/details?id=com.rewubsbdqkk.koeesowemli',
  25. 'google-SpinAura-Play-233' => 'https://play.google.com/store/apps/details?id=com.ohnyuwiyobt.fyghdsghfy'
  26. // 'google-test111' => 'https://play.google.com/store/apps/details?id=com.rewubsbdqkk.123',
  27. ];
  28. protected int $delistedConfirmTimes = 3;
  29. protected int $unknownConfirmTimes = 3;
  30. protected int $counterExpireSeconds = 3600;
  31. protected int $httpMaxAttempts = 3;
  32. protected int $sleepMinMicroseconds = 200000;
  33. protected int $sleepMaxMicroseconds = 500000;
  34. public function handle()
  35. {
  36. if ($this->apps === []) {
  37. $this->warn('未配置 Google Play 检测列表(CheckGooglePlayStore::$apps),跳过。');
  38. return 0;
  39. }
  40. $env = env('APP_ENV', 'local');
  41. $results = [];
  42. $delistedNotified = [];
  43. $unknownNotified = [];
  44. foreach ($this->apps as $name => $url) {
  45. $result = $this->checkSingleApp($name, $url);
  46. $results[] = $result;
  47. Util::WriteLog(
  48. 'google_play_check',
  49. sprintf(
  50. '[%s] %s | %s | %s',
  51. strtoupper($result['status']),
  52. $name,
  53. $url,
  54. $result['reason']
  55. )
  56. );
  57. $pkg = $result['package'];
  58. if ($result['status'] === 'online') {
  59. $this->clearCounters($pkg);
  60. } elseif ($result['status'] === 'delisted') {
  61. $count = $this->increaseCounter($this->getDelistedCounterKey($pkg));
  62. $this->clearCounter($this->getUnknownCounterKey($pkg));
  63. if ($count >= $this->delistedConfirmTimes) {
  64. if (!$this->hasAlreadyNotified('delisted', $pkg)) {
  65. $delistedNotified[] = [
  66. 'name' => $name,
  67. 'url' => $url,
  68. 'reason' => $result['reason'],
  69. 'count' => $count,
  70. ];
  71. $this->markNotified('delisted', $pkg);
  72. }
  73. }
  74. } elseif ($result['status'] === 'unknown') {
  75. $count = $this->increaseCounter($this->getUnknownCounterKey($pkg));
  76. $this->clearCounter($this->getDelistedCounterKey($pkg));
  77. if ($count >= $this->unknownConfirmTimes) {
  78. if (!$this->hasAlreadyNotified('unknown', $pkg)) {
  79. $unknownNotified[] = [
  80. 'name' => $name,
  81. 'url' => $url,
  82. 'reason' => $result['reason'],
  83. 'count' => $count,
  84. ];
  85. $this->markNotified('unknown', $pkg);
  86. }
  87. }
  88. }
  89. usleep(random_int($this->sleepMinMicroseconds, $this->sleepMaxMicroseconds));
  90. }
  91. if (!empty($delistedNotified)) {
  92. $this->notifyDelisted($env, $delistedNotified);
  93. $this->warn('已发送下架告警: ' . count($delistedNotified) . ' 个');
  94. }
  95. if (!empty($unknownNotified)) {
  96. $this->notifyUnknown($env, $unknownNotified);
  97. $this->warn('已发送检测异常告警: ' . count($unknownNotified) . ' 个');
  98. }
  99. foreach ($results as $item) {
  100. $this->line(sprintf(
  101. '[%s] %s - %s',
  102. strtoupper($item['status']),
  103. $item['name'],
  104. $item['reason']
  105. ));
  106. }
  107. if (empty($delistedNotified) && empty($unknownNotified)) {
  108. $this->info('本次检测完成,无需告警。');
  109. }
  110. return empty($delistedNotified) ? 0 : 1;
  111. }
  112. /**
  113. * @return array{name:string,url:string,package:string,status:string,reason:string}
  114. */
  115. protected function checkSingleApp(string $name, string $playUrl): array
  116. {
  117. $parsed = $this->parsePlayStoreUrl($playUrl);
  118. if ($parsed === null) {
  119. return [
  120. 'name' => $name,
  121. 'url' => $playUrl,
  122. 'package' => '_badurl_' . md5($playUrl),
  123. 'status' => 'unknown',
  124. 'reason' => '无法从 URL 解析包名 id=',
  125. ];
  126. }
  127. $package = $parsed['package'];
  128. $requestUrl = $this->buildDetailsUrl($package);
  129. $resp = $this->httpGetWithRetry($requestUrl, $this->httpMaxAttempts);
  130. if (!empty($resp['error'])) {
  131. return [
  132. 'name' => $name,
  133. 'url' => $playUrl,
  134. 'package' => $package,
  135. 'status' => 'unknown',
  136. 'reason' => '请求失败: ' . $resp['error'],
  137. ];
  138. }
  139. $code = (int) ($resp['code'] ?? 0);
  140. $body = (string) ($resp['body'] ?? '');
  141. if (in_array($code, [403, 429, 500, 502, 503, 504], true)) {
  142. return [
  143. 'name' => $name,
  144. 'url' => $playUrl,
  145. 'package' => $package,
  146. 'status' => 'unknown',
  147. 'reason' => "HTTP {$code}",
  148. ];
  149. }
  150. if ($code === 404) {
  151. return [
  152. 'name' => $name,
  153. 'url' => $playUrl,
  154. 'package' => $package,
  155. 'status' => 'delisted',
  156. 'reason' => 'Play 返回 HTTP 404(包不存在或已下架)',
  157. ];
  158. }
  159. if ($code !== 200) {
  160. return [
  161. 'name' => $name,
  162. 'url' => $playUrl,
  163. 'package' => $package,
  164. 'status' => 'unknown',
  165. 'reason' => "HTTP {$code}",
  166. ];
  167. }
  168. if ($this->isPlayNotFoundPage($body)) {
  169. return [
  170. 'name' => $name,
  171. 'url' => $playUrl,
  172. 'package' => $package,
  173. 'status' => 'delisted',
  174. 'reason' => 'Play 错误页(Not Found 等)',
  175. ];
  176. }
  177. if ($this->isLikelyNormalDetailPage($body)) {
  178. $this->clearNotifiedFlags($package);
  179. return [
  180. 'name' => $name,
  181. 'url' => $playUrl,
  182. 'package' => $package,
  183. 'status' => 'online',
  184. 'reason' => '详情页正常(含 Play 前端特征)',
  185. ];
  186. }
  187. $len = strlen($body);
  188. if ($len < 15000) {
  189. return [
  190. 'name' => $name,
  191. 'url' => $playUrl,
  192. 'package' => $package,
  193. 'status' => 'unknown',
  194. 'reason' => "HTTP 200 但正文过短({$len} bytes),无法判断",
  195. ];
  196. }
  197. return [
  198. 'name' => $name,
  199. 'url' => $playUrl,
  200. 'package' => $package,
  201. 'status' => 'unknown',
  202. 'reason' => 'HTTP 200 但未识别为正常详情页(页面结构可能变更)',
  203. ];
  204. }
  205. protected function isPlayNotFoundPage(string $body): bool
  206. {
  207. if (stripos($body, '<title>Not Found</title>') !== false) {
  208. return true;
  209. }
  210. if (preg_match('/requested\s+URL\s+was\s+not\s+found/i', $body)) {
  211. return true;
  212. }
  213. return false;
  214. }
  215. protected function isLikelyNormalDetailPage(string $body): bool
  216. {
  217. if ($this->isPlayNotFoundPage($body)) {
  218. return false;
  219. }
  220. if (strlen($body) < 30000) {
  221. return false;
  222. }
  223. // 当前 Play Web 详情页常见内嵌数据;404 短页不含
  224. if (strpos($body, 'WIZ_global_data') === false) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. /**
  230. * 从详情链接解析包名。
  231. */
  232. protected function parsePlayStoreUrl(string $url): ?array
  233. {
  234. $parts = parse_url($url);
  235. if (empty($parts['host']) || stripos($parts['host'], 'play.google.com') === false) {
  236. return null;
  237. }
  238. $query = $parts['query'] ?? '';
  239. if ($query === '') {
  240. return null;
  241. }
  242. parse_str($query, $q);
  243. if (empty($q['id']) || !is_string($q['id'])) {
  244. return null;
  245. }
  246. $id = $q['id'];
  247. if (!preg_match('/^[a-zA-Z][a-zA-Z0-9._]*$/', $id)) {
  248. return null;
  249. }
  250. return ['package' => $id];
  251. }
  252. protected function buildDetailsUrl(string $package): string
  253. {
  254. return 'https://play.google.com/store/apps/details?id=' . rawurlencode($package) . '&hl=en&gl=US';
  255. }
  256. /**
  257. * @return array{code:int,body:string,error:string}
  258. */
  259. protected function httpGetWithRetry(string $url, int $maxAttempts = 3): array
  260. {
  261. $attempt = 0;
  262. $last = [
  263. 'code' => 0,
  264. 'body' => '',
  265. 'error' => '',
  266. ];
  267. while ($attempt < $maxAttempts) {
  268. $attempt++;
  269. $ch = curl_init($url);
  270. curl_setopt_array($ch, [
  271. CURLOPT_RETURNTRANSFER => true,
  272. CURLOPT_FOLLOWLOCATION => true,
  273. CURLOPT_TIMEOUT => 45,
  274. CURLOPT_CONNECTTIMEOUT => 15,
  275. CURLOPT_SSL_VERIFYPEER => true,
  276. CURLOPT_HTTPHEADER => [
  277. 'Accept: text/html,application/xhtml+xml;q=0.9,*/*;q=0.8',
  278. 'Accept-Language: en-US,en;q=0.9',
  279. 'Cache-Control: no-cache',
  280. ],
  281. CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
  282. ]);
  283. $body = curl_exec($ch);
  284. $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  285. $error = curl_error($ch);
  286. curl_close($ch);
  287. $last = [
  288. 'code' => $code,
  289. 'body' => is_string($body) ? $body : '',
  290. 'error' => $error ?: '',
  291. ];
  292. if ($last['error'] === '' && ($code === 200 || $code === 404)) {
  293. return $last;
  294. }
  295. $shouldRetry = $last['error'] !== '' || in_array($code, [429, 500, 502, 503, 504], true);
  296. if (!$shouldRetry || $attempt >= $maxAttempts) {
  297. return $last;
  298. }
  299. $sleepSeconds = $attempt === 1 ? 2 : ($attempt === 2 ? 5 : 8);
  300. sleep($sleepSeconds);
  301. }
  302. return $last;
  303. }
  304. protected function getDelistedCounterKey(string $package): string
  305. {
  306. return 'google:play:delisted:' . $package;
  307. }
  308. protected function getUnknownCounterKey(string $package): string
  309. {
  310. return 'google:play:unknown:' . $package;
  311. }
  312. protected function getDelistedNotifiedKey(string $package): string
  313. {
  314. return 'google:play:notified:delisted:' . $package;
  315. }
  316. protected function getUnknownNotifiedKey(string $package): string
  317. {
  318. return 'google:play:notified:unknown:' . $package;
  319. }
  320. protected function increaseCounter(string $key): int
  321. {
  322. $count = (int) Redis::incr($key);
  323. Redis::expire($key, $this->counterExpireSeconds);
  324. return $count;
  325. }
  326. protected function clearCounters(string $package): void
  327. {
  328. $this->clearCounter($this->getDelistedCounterKey($package));
  329. $this->clearCounter($this->getUnknownCounterKey($package));
  330. }
  331. protected function clearCounter(string $key): void
  332. {
  333. try {
  334. Redis::del($key);
  335. } catch (\Throwable $e) {
  336. Util::WriteLog('google_play_check', 'Redis del error: ' . $e->getMessage());
  337. }
  338. }
  339. protected function hasAlreadyNotified(string $type, string $package): bool
  340. {
  341. try {
  342. $key = $type === 'delisted'
  343. ? $this->getDelistedNotifiedKey($package)
  344. : $this->getUnknownNotifiedKey($package);
  345. return (bool) Redis::exists($key);
  346. } catch (\Throwable $e) {
  347. Util::WriteLog('google_play_check', 'Redis exists error: ' . $e->getMessage());
  348. return false;
  349. }
  350. }
  351. protected function markNotified(string $type, string $package): void
  352. {
  353. try {
  354. $key = $type === 'delisted'
  355. ? $this->getDelistedNotifiedKey($package)
  356. : $this->getUnknownNotifiedKey($package);
  357. Redis::setex($key, $this->counterExpireSeconds, 1);
  358. } catch (\Throwable $e) {
  359. Util::WriteLog('google_play_check', 'Redis setex error: ' . $e->getMessage());
  360. }
  361. }
  362. protected function clearNotifiedFlags(string $package): void
  363. {
  364. try {
  365. Redis::del(
  366. $this->getDelistedNotifiedKey($package),
  367. $this->getUnknownNotifiedKey($package)
  368. );
  369. } catch (\Throwable $e) {
  370. Util::WriteLog('google_play_check', 'Redis clear notified flags error: ' . $e->getMessage());
  371. }
  372. }
  373. protected function notifyDelisted(string $env, array $items): void
  374. {
  375. $lines = ["[{$env}] Google Play 下架/不可访问检测告警"];
  376. foreach ($items as $item) {
  377. $lines[] = '• ' . $item['name'];
  378. $lines[] = ' 原因: ' . $item['reason'];
  379. $lines[] = ' 连续次数: ' . $item['count'];
  380. $lines[] = ' 链接: ' . $item['url'];
  381. }
  382. $this->sendTelegramMessage(implode("\n", $lines));
  383. }
  384. protected function notifyUnknown(string $env, array $items): void
  385. {
  386. $lines = ["[{$env}] Google Play 检测异常告警"];
  387. foreach ($items as $item) {
  388. $lines[] = '• ' . $item['name'];
  389. $lines[] = ' 原因: ' . $item['reason'];
  390. $lines[] = ' 连续次数: ' . $item['count'];
  391. $lines[] = ' 链接: ' . $item['url'];
  392. }
  393. $this->sendTelegramMessage(implode("\n", $lines));
  394. }
  395. protected function sendTelegramMessage(string $message): void
  396. {
  397. try {
  398. TelegramBot::getDefault()->sendMsgAndImportant($message);
  399. } catch (\Throwable $e) {
  400. Util::WriteLog('google_play_check', 'Telegram send error: ' . $e->getMessage());
  401. $this->error('Telegram 发送失败: ' . $e->getMessage());
  402. }
  403. }
  404. }