helpers.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. /**
  3. * 助手函数
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2019/3/3
  7. * @time 16:34
  8. */
  9. use GuzzleHttp\Client;
  10. use GuzzleHttp\Cookie\CookieJar;
  11. use GuzzleHttp\Cookie\SetCookie;
  12. use Luolongfei\App\Console\GlobalValue;
  13. use Luolongfei\App\Console\MigrateEnvFile;
  14. use Luolongfei\App\Console\Upgrade;
  15. use Luolongfei\App\Constants\CommonConst;
  16. use Luolongfei\App\Exceptions\LlfException;
  17. use Luolongfei\Libs\Argv;
  18. use Luolongfei\Libs\Config;
  19. use Luolongfei\Libs\Env;
  20. use Luolongfei\Libs\Lang;
  21. use Luolongfei\Libs\Log;
  22. use Luolongfei\Libs\PhpColor;
  23. if (!function_exists('config')) {
  24. /**
  25. * 获取配置
  26. *
  27. * @param string $key 键,支持点式访问
  28. * @param string $default 默认值
  29. *
  30. * @return array|mixed
  31. */
  32. function config($key = '', $default = null)
  33. {
  34. return Config::getInstance()->get($key, $default);
  35. }
  36. }
  37. if (!function_exists('lang')) {
  38. /**
  39. * 读取语言包
  40. *
  41. * @param string $key 键,支持点式访问
  42. *
  43. * @return array|mixed|null
  44. */
  45. function lang($key = '')
  46. {
  47. return Lang::getInstance()->get($key);
  48. }
  49. }
  50. if (!function_exists('system_log')) {
  51. /**
  52. * 写日志
  53. *
  54. * @param $content
  55. * @param array $response
  56. * @param string $fileName
  57. * @description 受支持的着色标签
  58. * 'reset', 'bold', 'dark', 'italic', 'underline', 'blink', 'reverse', 'concealed', 'default', 'black', 'red',
  59. * 'green', 'yellow', 'blue', 'magenta', 'cyan', 'light_gray', 'dark_gray', 'light_red', 'light_green',
  60. * 'light_yellow', 'light_blue', 'light_magenta', 'light_cyan', 'white', 'bg_default', 'bg_black', 'bg_red',
  61. * 'bg_green', 'bg_yellow', 'bg_blue', 'bg_magenta', 'bg_cyan', 'bg_light_gray', 'bg_dark_gray', 'bg_light_red',
  62. * 'bg_light_green','bg_light_yellow', 'bg_light_blue', 'bg_light_magenta', 'bg_light_cyan', 'bg_white'
  63. *
  64. * system_log('<light_magenta>颜色 light_magenta</light_magenta>');
  65. */
  66. function system_log($content, array $response = [], $fileName = '')
  67. {
  68. try {
  69. $msg = sprintf(
  70. "[%s] %s %s\n",
  71. date('Y-m-d H:i:s'),
  72. is_string($content) ? $content : json_encode($content),
  73. $response ? json_encode($response, JSON_UNESCAPED_UNICODE) : '');
  74. // 过滤敏感信息
  75. if ((int)env('MOSAIC_SENSITIVE_INFO') === 1) {
  76. // 在 php 7.3 之前,连字符“-”在中括号中随便放,但在之后,只能放在开头或结尾或者转义后才能随便放
  77. $msg = preg_replace_callback('/(?P<secret>[\w.-]{1,3}?)(?=@[\w.-]+)/ui', function ($m) {
  78. return str_ireplace($m['secret'], str_repeat('*', strlen($m['secret'])), $m['secret']);
  79. }, $msg);
  80. }
  81. // 尝试为消息着色
  82. $c = PhpColor::getInstance()->getColorInstance();
  83. echo $c($msg)->colorize();
  84. // 干掉着色标签
  85. $msg = strip_tags($msg); // 不完整或者破损标签将导致更多的数据被删除
  86. // 写入日志文件
  87. if (is_writable(ROOT_PATH)) {
  88. $path = sprintf('%s/logs/%s/', ROOT_PATH, date('Y-m'));
  89. $file = $path . ($fileName ?: date('d')) . '.log';
  90. if (!is_dir($path)) {
  91. mkdir($path, 0766, true); // 0766 赋予所有用户读写
  92. chmod($path, 0766); // 显式 chmod 确保权限设置正确
  93. }
  94. $handle = fopen($file, 'a'); // 追加而非覆盖
  95. if ($handle !== false) {
  96. if (!filesize($file)) {
  97. chmod($file, 0666);
  98. }
  99. fwrite($handle, $msg);
  100. fclose($handle);
  101. }
  102. }
  103. flush();
  104. } catch (\Exception $e) {
  105. // do nothing
  106. }
  107. }
  108. }
  109. if (!function_exists('is_locked')) {
  110. /**
  111. * 检查任务是否已被锁定
  112. *
  113. * @param string $taskName
  114. * @param bool $always 是否被永久锁定
  115. *
  116. * @return bool
  117. * @throws Exception
  118. */
  119. function is_locked($taskName = '', $always = false)
  120. {
  121. try {
  122. $lock = sprintf(
  123. '%s/num_limit/%s/%s.lock',
  124. APP_PATH,
  125. $always ? 'always' : date('Y-m-d'),
  126. $taskName
  127. );
  128. return file_exists($lock);
  129. } catch (\Exception $e) {
  130. system_log(sprintf('检查任务%s是否锁定时出错,错误原因:%s', $taskName, $e->getMessage()));
  131. }
  132. return false;
  133. }
  134. }
  135. if (!function_exists('lock_task')) {
  136. /**
  137. * 锁定任务
  138. *
  139. * 防止重复执行
  140. *
  141. * @param string $taskName
  142. * @param bool $always 是否永久锁定
  143. *
  144. * @return bool
  145. */
  146. function lock_task($taskName = '', $always = false)
  147. {
  148. try {
  149. $lock = sprintf(
  150. '%s/num_limit/%s/%s.lock',
  151. APP_PATH,
  152. $always ? 'always' : date('Y-m-d'),
  153. $taskName
  154. );
  155. $path = dirname($lock);
  156. if (!is_dir($path)) {
  157. mkdir($path, 0777, true);
  158. chmod($path, 0777);
  159. }
  160. if (file_exists($lock)) {
  161. return true;
  162. }
  163. $handle = fopen($lock, 'a'); // 追加而非覆盖
  164. if ($handle === false) {
  165. throw new \RuntimeException(sprintf('Unable to open lock file: %s', $lock));
  166. }
  167. if (!filesize($lock)) {
  168. chmod($lock, 0666);
  169. }
  170. fwrite($handle, sprintf(
  171. "Locked at %s.\n",
  172. date('Y-m-d H:i:s')
  173. )
  174. );
  175. fclose($handle);
  176. Log::info(sprintf('%s已被锁定,此任务%s已不会再执行,请知悉', $taskName, $always ? '' : '今天内'));
  177. } catch (\Exception $e) {
  178. system_log(sprintf('创建锁定任务文件%s时出错,错误原因:%s', $lock, $e->getMessage()));
  179. return false;
  180. }
  181. return true;
  182. }
  183. }
  184. if (!function_exists('env')) {
  185. /**
  186. * 获取环境变量值
  187. *
  188. * @param string $key
  189. * @param string $default 默认值
  190. *
  191. * @return array | bool | false | null | string
  192. */
  193. function env($key = '', $default = null)
  194. {
  195. return Env::getInstance()->get($key, $default);
  196. }
  197. }
  198. if (!function_exists('get_argv')) {
  199. /**
  200. * 获取命令行传参
  201. *
  202. * @param string $name
  203. * @param string $default 默认值
  204. *
  205. * @return mixed|string
  206. */
  207. function get_argv(string $name, string $default = '')
  208. {
  209. return Argv::getInstance()->get($name, $default);
  210. }
  211. }
  212. if (!function_exists('system_check')) {
  213. /**
  214. * 检查环境是否满足要求
  215. *
  216. * @throws LlfException
  217. */
  218. function system_check()
  219. {
  220. // 当前依赖组合要求 PHP 8.1 及以上版本
  221. if (version_compare(PHP_VERSION, '8.1.0') < 0) {
  222. throw new LlfException(34520006, ['8.1', PHP_VERSION]);
  223. }
  224. // 特殊环境无需检查这几项
  225. if (IS_SCF || !is_writable(ROOT_PATH) || (int)env('IS_KOYEB') === 1 || (int)env('IS_HEROKU') === 1) {
  226. system_log(lang('100009'));
  227. } else {
  228. if (!function_exists('putenv')) {
  229. throw new LlfException(34520005);
  230. }
  231. $envFile = ROOT_PATH . '/.env';
  232. if (!file_exists($envFile)) {
  233. throw new LlfException(copy(ROOT_PATH . '/.env.example', $envFile) ? 34520007 : 34520008);
  234. }
  235. // 检查当前 .env 文件版本是否过低,过低自动升级
  236. MigrateEnvFile::getInstance()->handle();
  237. }
  238. // 是否有新版可用
  239. if (config('new_version_detection')) {
  240. Upgrade::getInstance()->handle();
  241. } else {
  242. system_log(lang('100012'));
  243. }
  244. if (!extension_loaded('curl')) {
  245. throw new LlfException(34520010);
  246. }
  247. }
  248. }
  249. if (!function_exists('get_local_num')) {
  250. /**
  251. * 获取当地数字
  252. *
  253. * @param string|int $num
  254. *
  255. * @return string
  256. */
  257. function get_local_num($num)
  258. {
  259. $num = (string)$num;
  260. if (is_chinese()) {
  261. return $num;
  262. }
  263. // 英文数字规则
  264. $lastDigit = substr($num, -1);
  265. switch ($lastDigit) {
  266. case '1':
  267. return $num . 'st';
  268. case '2':
  269. return $num . 'nd';
  270. case '3':
  271. return $num . 'rd';
  272. default:
  273. return $num . 'th';
  274. }
  275. }
  276. }
  277. if (!function_exists('is_chinese')) {
  278. /**
  279. * 判断当前语言环境
  280. *
  281. * @return bool
  282. */
  283. function is_chinese()
  284. {
  285. return config('custom_language', 'zh') === 'zh';
  286. }
  287. }
  288. if (!function_exists('get_ip_info')) {
  289. /**
  290. * 获取 ip 信息
  291. *
  292. * @return string
  293. */
  294. function get_ip_info()
  295. {
  296. return \Luolongfei\Libs\IP::getInstance()->get();
  297. }
  298. }
  299. if (!function_exists('get_random_user_agent')) {
  300. /**
  301. * 获取随机 user-agent
  302. *
  303. * @return string
  304. */
  305. function get_random_user_agent()
  306. {
  307. $chromeVersions = [
  308. '121.0.0.0',
  309. ];
  310. return $chromeVersions[array_rand($chromeVersions)];
  311. }
  312. }
  313. if (!function_exists('autoRetry')) {
  314. /**
  315. * 自动重试
  316. *
  317. * @param $func
  318. * @param int $maxRetryCount
  319. * @param array $params
  320. *
  321. * @return mixed|void
  322. * @throws Exception
  323. */
  324. function autoRetry($func, $maxRetryCount = 3, $params = [])
  325. {
  326. $retryCount = 0;
  327. while (true) {
  328. try {
  329. return call_user_func_array($func, $params);
  330. } catch (\Exception $e) {
  331. $retryCount++;
  332. if ($retryCount > $maxRetryCount) {
  333. throw $e;
  334. }
  335. $sleepTime = getSleepTime($retryCount, 2, 10);
  336. if (stripos($e->getMessage(), '405') !== false) {
  337. system_log(\lang('100141'));
  338. sleep(9);
  339. // aws waf token 失效,将重新获取新的 token
  340. $handleInvalidToken = false;
  341. foreach ($params as &$param) {
  342. if ($param instanceof CookieJar) {
  343. $handleInvalidToken = true;
  344. $sleepTime = 1;
  345. delGlobalValue(CommonConst::AWS_WAF_TOKEN);
  346. $param->setCookie(buildAwsWafCookie(getAwsWafToken()));
  347. break;
  348. }
  349. }
  350. system_log($handleInvalidToken ? \lang('exception_msg.34520019') : sprintf(lang('exception_msg.34520015'), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
  351. continue;
  352. } else {
  353. system_log(sprintf(lang('exception_msg.34520016'), $e->getMessage(), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
  354. }
  355. sleep($sleepTime);
  356. }
  357. }
  358. }
  359. }
  360. if (!function_exists('buildAwsWafCookie')) {
  361. /**
  362. * 构建 aws waf cookie
  363. *
  364. * @param string $awsWafToken
  365. *
  366. * @return SetCookie
  367. */
  368. function buildAwsWafCookie(string $awsWafToken)
  369. {
  370. $cookie = new SetCookie();
  371. $cookie->setName('aws-waf-token');
  372. $cookie->setValue($awsWafToken);
  373. $cookie->setDomain('.my.freenom.com');
  374. return $cookie;
  375. }
  376. }
  377. if (!function_exists('getSleepTime')) {
  378. /**
  379. * 获取睡眠秒数
  380. *
  381. * @param int $i
  382. * @param int $magRatio
  383. * @param int $minSleepTime
  384. *
  385. * @return int
  386. */
  387. function getSleepTime($i, $magRatio = 4, $minSleepTime = 20)
  388. {
  389. $sleepTime = $i * $magRatio;
  390. if ($sleepTime < $minSleepTime) { // 最小休眠 $minSleepTime 秒
  391. return $minSleepTime;
  392. }
  393. return $sleepTime;
  394. }
  395. }
  396. if (!function_exists('getAwsWafToken')) {
  397. /**
  398. * 获取 aws waf token
  399. *
  400. * @return string
  401. * @throws LlfException
  402. * @throws \GuzzleHttp\Exception\GuzzleException
  403. */
  404. function getAwsWafToken()
  405. {
  406. // 优先从全局变量中获取
  407. $AWS_WAF_TOKEN = getGlobalValue(CommonConst::AWS_WAF_TOKEN);
  408. if ($AWS_WAF_TOKEN !== null) {
  409. return $AWS_WAF_TOKEN;
  410. }
  411. $client = new Client([
  412. 'headers' => [
  413. 'Accept' => 'application/json',
  414. 'Authorization' => \env('FF_SECRET_KEY', '')
  415. ],
  416. 'timeout' => 32,
  417. ]);
  418. // 调用开源的接口获取
  419. $USE_OPEN_SOURCE_WAF_SOLVER_API = (int)\env('USE_OPEN_SOURCE_WAF_SOLVER_API', 1);
  420. if ($USE_OPEN_SOURCE_WAF_SOLVER_API === 1) {
  421. $OPEN_SOURCE_WAF_SOLVER_URL = \env('OPEN_SOURCE_WAF_SOLVER_URL');
  422. if (!$OPEN_SOURCE_WAF_SOLVER_URL) {
  423. throw new LlfException(34520020);
  424. }
  425. $OPEN_SOURCE_WAF_SOLVER_URL = rtrim($OPEN_SOURCE_WAF_SOLVER_URL, '/');
  426. $startTime = time();
  427. $maxWaitSeconds = 300;
  428. $n = 0;
  429. while (true) {
  430. try {
  431. if (time() - $startTime >= $maxWaitSeconds) {
  432. break;
  433. }
  434. $r = $client->get($OPEN_SOURCE_WAF_SOLVER_URL);
  435. $body = json_decode($r->getBody()->getContents(), true);
  436. if (!isset($body['status']) || $body['status'] !== 'OK') {
  437. throw new \Exception(isset($body['msg']) ? $body['msg'] : json_encode($body));
  438. }
  439. $awsWafToken = $body['data']['token'];
  440. setGlobalValue(CommonConst::AWS_WAF_TOKEN, $awsWafToken);
  441. system_log(sprintf(lang('100139'), $awsWafToken));
  442. return $awsWafToken;
  443. } catch (\Exception $e) {
  444. system_log('<red>getAwsWafToken error:</red> ' . $e->getMessage());
  445. }
  446. $n++;
  447. sleep($n > 5 ? 60 : 10); // 前 5 次每次休眠 10 秒,之后每次休眠 60 秒
  448. }
  449. throw new LlfException(34520021, $maxWaitSeconds);
  450. }
  451. // 使用自建接口获取
  452. $AWS_WAF_SOLVER_URL = \env('AWS_WAF_SOLVER_URL');
  453. if (!$AWS_WAF_SOLVER_URL) {
  454. throw new LlfException(34520017);
  455. }
  456. $AWS_WAF_SOLVER_URL = rtrim($AWS_WAF_SOLVER_URL, '/');
  457. $i = 0;
  458. do {
  459. try {
  460. // 获取任务 ID
  461. $r = $client->get($AWS_WAF_SOLVER_URL);
  462. $body = json_decode($r->getBody()->getContents(), true);
  463. if (!isset($body['status']) || $body['status'] !== 'OK') {
  464. // 一般情况下走不到这个分支
  465. if (isset($body['msg']) && $body['msg'] === 'A task is already running') {
  466. sleep(180);
  467. }
  468. throw new \Exception(isset($body['msg']) ? $body['msg'] : json_encode($body));
  469. }
  470. // 已获取任务 ID,等待任务完成
  471. $taskId = $body['data']['task_id'];
  472. $startTime = time();
  473. while (true) {
  474. // 最多等 10 分钟
  475. if (time() - $startTime >= 600) {
  476. break;
  477. }
  478. $r = $client->get(sprintf('%s/%s', $AWS_WAF_SOLVER_URL, $taskId));
  479. $body = json_decode($r->getBody()->getContents(), true);
  480. if (!isset($body['status']) || $body['status'] !== 'OK') {
  481. throw new \Exception(isset($body['msg']) ? $body['msg'] : json_encode($body));
  482. }
  483. $taskStatus = $body['data']['task_status'];
  484. if ($taskStatus !== 'done') { // 任务进行中,继续等待
  485. sleep(2);
  486. continue;
  487. }
  488. if (!isset($body['data']['result']) || $body['data']['result'] === '') {
  489. throw new \Exception('no result');
  490. }
  491. $awsWafToken = $body['data']['result'] ?? '';
  492. setGlobalValue(CommonConst::AWS_WAF_TOKEN, $awsWafToken);
  493. system_log(sprintf(lang('100139'), $awsWafToken));
  494. return $awsWafToken;
  495. }
  496. } catch (\Exception $e) {
  497. system_log('<red>getAwsWafToken error:</red> ' . $e->getMessage());
  498. }
  499. sleep(1);
  500. $i++;
  501. } while ($i <= 10);
  502. throw new LlfException('34520018');
  503. }
  504. }
  505. if (!function_exists('getGlobalValue')) {
  506. /**
  507. * 获取全局变量
  508. *
  509. * @param string $name
  510. *
  511. * @return string|null
  512. */
  513. function getGlobalValue(string $name, ?string $default = null)
  514. {
  515. return GlobalValue::getInstance()->get($name, $default);
  516. }
  517. }
  518. if (!function_exists('setGlobalValue')) {
  519. /**
  520. * 设置全局变量
  521. *
  522. * @param string $name
  523. * @param string $value
  524. *
  525. * @return void
  526. */
  527. function setGlobalValue(string $name, string $value)
  528. {
  529. GlobalValue::getInstance()->set($name, $value);
  530. }
  531. }
  532. if (!function_exists('hasGlobalValue')) {
  533. /**
  534. * 是否存在全局变量
  535. *
  536. * @param string $name
  537. *
  538. * @return bool
  539. */
  540. function hasGlobalValue(string $name)
  541. {
  542. return GlobalValue::getInstance()->has($name);
  543. }
  544. }
  545. if (!function_exists('delGlobalValue')) {
  546. /**
  547. * 删除全局变量
  548. *
  549. * @param string $name
  550. *
  551. * @return void
  552. */
  553. function delGlobalValue(string $name)
  554. {
  555. GlobalValue::getInstance()->del($name);
  556. }
  557. }
  558. if (!function_exists('needAwsWafToken')) {
  559. /**
  560. * @return bool
  561. */
  562. function needAwsWafToken()
  563. {
  564. try {
  565. $client = new Client([
  566. 'headers' => [
  567. 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  568. 'Accept-Encoding' => 'gzip, deflate, br',
  569. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
  570. ],
  571. 'timeout' => 4.2011,
  572. CURLOPT_FOLLOWLOCATION => true,
  573. CURLOPT_AUTOREFERER => true,
  574. 'verify' => config('verify_ssl'),
  575. 'proxy' => config('freenom_proxy'),
  576. ]);
  577. $res = $client->get('https://my.freenom.com/clientarea.php');
  578. return $res->getStatusCode() != 200;
  579. } catch (\Exception $e) {
  580. return stripos($e->getMessage(), '405') !== false;
  581. }
  582. }
  583. }