helpers.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 (!filesize($lock)) {
  165. chmod($lock, 0666);
  166. }
  167. fwrite($handle, sprintf(
  168. "Locked at %s.\n",
  169. date('Y-m-d H:i:s')
  170. )
  171. );
  172. fclose($handle);
  173. Log::info(sprintf('%s已被锁定,此任务%s已不会再执行,请知悉', $taskName, $always ? '' : '今天内'));
  174. } catch (\Exception $e) {
  175. system_log(sprintf('创建锁定任务文件%s时出错,错误原因:%s', $lock, $e->getMessage()));
  176. return false;
  177. }
  178. return true;
  179. }
  180. }
  181. if (!function_exists('env')) {
  182. /**
  183. * 获取环境变量值
  184. *
  185. * @param string $key
  186. * @param string $default 默认值
  187. *
  188. * @return array | bool | false | null | string
  189. */
  190. function env($key = '', $default = null)
  191. {
  192. return Env::getInstance()->get($key, $default);
  193. }
  194. }
  195. if (!function_exists('get_argv')) {
  196. /**
  197. * 获取命令行传参
  198. *
  199. * @param string $name
  200. * @param string $default 默认值
  201. *
  202. * @return mixed|string
  203. */
  204. function get_argv(string $name, string $default = '')
  205. {
  206. return Argv::getInstance()->get($name, $default);
  207. }
  208. }
  209. if (!function_exists('system_check')) {
  210. /**
  211. * 检查环境是否满足要求
  212. *
  213. * @throws LlfException
  214. */
  215. function system_check()
  216. {
  217. // 由于各种云函数目前支持的最大的 PHP 版本为 7.2,故此处暂时不强制要求升级 PHP 7.3 以获得更好的兼容性
  218. if (version_compare(PHP_VERSION, '7.2.0') < 0) {
  219. throw new LlfException(34520006, ['7.3', PHP_VERSION]);
  220. }
  221. // 特殊环境无需检查这几项
  222. if (IS_SCF || !is_writable(ROOT_PATH) || (int)env('IS_KOYEB') === 1 || (int)env('IS_HEROKU') === 1) {
  223. system_log(lang('100009'));
  224. } else {
  225. if (!function_exists('putenv')) {
  226. throw new LlfException(34520005);
  227. }
  228. $envFile = ROOT_PATH . '/.env';
  229. if (!file_exists($envFile)) {
  230. throw new LlfException(copy(ROOT_PATH . '/.env.example', $envFile) ? 34520007 : 34520008);
  231. }
  232. // 检查当前 .env 文件版本是否过低,过低自动升级
  233. MigrateEnvFile::getInstance()->handle();
  234. }
  235. // 是否有新版可用
  236. if (config('new_version_detection')) {
  237. Upgrade::getInstance()->handle();
  238. } else {
  239. system_log(lang('100012'));
  240. }
  241. if (!extension_loaded('curl')) {
  242. throw new LlfException(34520010);
  243. }
  244. }
  245. }
  246. if (!function_exists('get_local_num')) {
  247. /**
  248. * 获取当地数字
  249. *
  250. * @param string|int $num
  251. *
  252. * @return string
  253. */
  254. function get_local_num($num)
  255. {
  256. $num = (string)$num;
  257. if (is_chinese()) {
  258. return $num;
  259. }
  260. // 英文数字规则
  261. $lastDigit = substr($num, -1);
  262. switch ($lastDigit) {
  263. case '1':
  264. return $num . 'st';
  265. case '2':
  266. return $num . 'nd';
  267. case '3':
  268. return $num . 'rd';
  269. default:
  270. return $num . 'th';
  271. }
  272. }
  273. }
  274. if (!function_exists('is_chinese')) {
  275. /**
  276. * 判断当前语言环境
  277. *
  278. * @return bool
  279. */
  280. function is_chinese()
  281. {
  282. return config('custom_language', 'zh') === 'zh';
  283. }
  284. }
  285. if (!function_exists('get_ip_info')) {
  286. /**
  287. * 获取 ip 信息
  288. *
  289. * @return string
  290. */
  291. function get_ip_info()
  292. {
  293. return \Luolongfei\Libs\IP::getInstance()->get();
  294. }
  295. }
  296. if (!function_exists('get_random_user_agent')) {
  297. /**
  298. * 获取随机 user-agent
  299. *
  300. * @return string
  301. */
  302. function get_random_user_agent()
  303. {
  304. $chromeVersions = [
  305. '121.0.0.0',
  306. ];
  307. return $chromeVersions[array_rand($chromeVersions)];
  308. }
  309. }
  310. if (!function_exists('autoRetry')) {
  311. /**
  312. * 自动重试
  313. *
  314. * @param $func
  315. * @param int $maxRetryCount
  316. * @param array $params
  317. *
  318. * @return mixed|void
  319. * @throws Exception
  320. */
  321. function autoRetry($func, $maxRetryCount = 3, $params = [])
  322. {
  323. $retryCount = 0;
  324. while (true) {
  325. try {
  326. return call_user_func_array($func, $params);
  327. } catch (\Exception $e) {
  328. $retryCount++;
  329. if ($retryCount > $maxRetryCount) {
  330. throw $e;
  331. }
  332. $sleepTime = getSleepTime($retryCount, 2, 10);
  333. if (stripos($e->getMessage(), '405') !== false) {
  334. system_log(\lang('100141'));
  335. sleep(9);
  336. // aws waf token 失效,将重新获取新的 token
  337. $handleInvalidToken = false;
  338. foreach ($params as &$param) {
  339. if ($param instanceof CookieJar) {
  340. $handleInvalidToken = true;
  341. $sleepTime = 1;
  342. delGlobalValue(CommonConst::AWS_WAF_TOKEN);
  343. $param->setCookie(buildAwsWafCookie(getAwsWafToken()));
  344. break;
  345. }
  346. }
  347. system_log($handleInvalidToken ? \lang('exception_msg.34520019') : sprintf(lang('exception_msg.34520015'), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
  348. continue;
  349. } else {
  350. system_log(sprintf(lang('exception_msg.34520016'), $e->getMessage(), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
  351. }
  352. sleep($sleepTime);
  353. }
  354. }
  355. }
  356. }
  357. if (!function_exists('buildAwsWafCookie')) {
  358. /**
  359. * 构建 aws waf cookie
  360. *
  361. * @param string $awsWafToken
  362. *
  363. * @return SetCookie
  364. */
  365. function buildAwsWafCookie(string $awsWafToken)
  366. {
  367. $cookie = new SetCookie();
  368. $cookie->setName('aws-waf-token');
  369. $cookie->setValue($awsWafToken);
  370. $cookie->setDomain('.my.freenom.com');
  371. return $cookie;
  372. }
  373. }
  374. if (!function_exists('getSleepTime')) {
  375. /**
  376. * 获取睡眠秒数
  377. *
  378. * @param int $i
  379. * @param int $magRatio
  380. * @param int $minSleepTime
  381. *
  382. * @return int
  383. */
  384. function getSleepTime($i, $magRatio = 4, $minSleepTime = 20)
  385. {
  386. $sleepTime = $i * $magRatio;
  387. if ($sleepTime < $minSleepTime) { // 最小休眠 $minSleepTime 秒
  388. return $minSleepTime;
  389. }
  390. return $sleepTime;
  391. }
  392. }
  393. if (!function_exists('getAwsWafToken')) {
  394. /**
  395. * 获取 aws waf token
  396. *
  397. * @return string
  398. * @throws LlfException
  399. * @throws \GuzzleHttp\Exception\GuzzleException
  400. */
  401. function getAwsWafToken()
  402. {
  403. // 优先从全局变量中获取
  404. $AWS_WAF_TOKEN = getGlobalValue(CommonConst::AWS_WAF_TOKEN);
  405. if ($AWS_WAF_TOKEN !== null) {
  406. return $AWS_WAF_TOKEN;
  407. }
  408. $client = new Client([
  409. 'headers' => [
  410. 'Accept' => 'application/json',
  411. 'Authorization' => \env('FF_SECRET_KEY', '')
  412. ],
  413. 'timeout' => 32,
  414. ]);
  415. // 调用开源的接口获取
  416. $USE_OPEN_SOURCE_WAF_SOLVER_API = (int)\env('USE_OPEN_SOURCE_WAF_SOLVER_API', 1);
  417. if ($USE_OPEN_SOURCE_WAF_SOLVER_API === 1) {
  418. $OPEN_SOURCE_WAF_SOLVER_URL = \env('OPEN_SOURCE_WAF_SOLVER_URL');
  419. if (!$OPEN_SOURCE_WAF_SOLVER_URL) {
  420. throw new LlfException(34520020);
  421. }
  422. $OPEN_SOURCE_WAF_SOLVER_URL = rtrim($OPEN_SOURCE_WAF_SOLVER_URL, '/');
  423. $startTime = time();
  424. $maxWaitSeconds = 300;
  425. $n = 0;
  426. while (true) {
  427. try {
  428. if (time() - $startTime >= $maxWaitSeconds) {
  429. break;
  430. }
  431. $r = $client->get($OPEN_SOURCE_WAF_SOLVER_URL);
  432. $body = json_decode($r->getBody()->getContents(), true);
  433. if (!isset($body['status']) || $body['status'] !== 'OK') {
  434. throw new \Exception(isset($body['msg']) ? $body['msg'] : json_encode($body));
  435. }
  436. $awsWafToken = $body['data']['token'];
  437. setGlobalValue(CommonConst::AWS_WAF_TOKEN, $awsWafToken);
  438. system_log(sprintf(lang('100139'), $awsWafToken));
  439. return $awsWafToken;
  440. } catch (\Exception $e) {
  441. system_log('<red>getAwsWafToken error:</red> ' . $e->getMessage());
  442. }
  443. $n++;
  444. sleep($n > 5 ? 60 : 10); // 前 5 次每次休眠 10 秒,之后每次休眠 60 秒
  445. }
  446. throw new LlfException(34520021, $maxWaitSeconds);
  447. }
  448. // 使用自建接口获取
  449. $AWS_WAF_SOLVER_URL = \env('AWS_WAF_SOLVER_URL');
  450. if (!$AWS_WAF_SOLVER_URL) {
  451. throw new LlfException(34520017);
  452. }
  453. $AWS_WAF_SOLVER_URL = rtrim($AWS_WAF_SOLVER_URL, '/');
  454. $i = 0;
  455. do {
  456. try {
  457. // 获取任务 ID
  458. $r = $client->get($AWS_WAF_SOLVER_URL);
  459. $body = json_decode($r->getBody()->getContents(), true);
  460. if (!isset($body['status']) || $body['status'] !== 'OK') {
  461. // 一般情况下走不到这个分支
  462. if (isset($body['msg']) && $body['msg'] === 'A task is already running') {
  463. sleep(180);
  464. }
  465. throw new \Exception(isset($body['msg']) ? $body['msg'] : json_encode($body));
  466. }
  467. // 已获取任务 ID,等待任务完成
  468. $taskId = $body['data']['task_id'];
  469. $startTime = time();
  470. while (true) {
  471. // 最多等 10 分钟
  472. if (time() - $startTime >= 600) {
  473. break;
  474. }
  475. $r = $client->get(sprintf('%s/%s', $AWS_WAF_SOLVER_URL, $taskId));
  476. $body = json_decode($r->getBody()->getContents(), true);
  477. if (!isset($body['status']) || $body['status'] !== 'OK') {
  478. throw new \Exception(isset($body['msg']) ? $body['msg'] : json_encode($body));
  479. }
  480. $taskStatus = $body['data']['task_status'];
  481. if ($taskStatus !== 'done') { // 任务进行中,继续等待
  482. sleep(2);
  483. continue;
  484. }
  485. if (!isset($body['data']['result']) || $body['data']['result'] === '') {
  486. throw new \Exception('no result');
  487. }
  488. $awsWafToken = $body['data']['result'] ?? '';
  489. setGlobalValue(CommonConst::AWS_WAF_TOKEN, $awsWafToken);
  490. system_log(sprintf(lang('100139'), $awsWafToken));
  491. return $awsWafToken;
  492. }
  493. } catch (\Exception $e) {
  494. system_log('<red>getAwsWafToken error:</red> ' . $e->getMessage());
  495. }
  496. sleep(1);
  497. $i++;
  498. } while ($i <= 10);
  499. throw new LlfException('34520018');
  500. }
  501. }
  502. if (!function_exists('getGlobalValue')) {
  503. /**
  504. * 获取全局变量
  505. *
  506. * @param string $name
  507. *
  508. * @return string|null
  509. */
  510. function getGlobalValue(string $name, ?string $default = null)
  511. {
  512. return GlobalValue::getInstance()->get($name, $default);
  513. }
  514. }
  515. if (!function_exists('setGlobalValue')) {
  516. /**
  517. * 设置全局变量
  518. *
  519. * @param string $name
  520. * @param string $value
  521. *
  522. * @return void
  523. */
  524. function setGlobalValue(string $name, string $value)
  525. {
  526. GlobalValue::getInstance()->set($name, $value);
  527. }
  528. }
  529. if (!function_exists('hasGlobalValue')) {
  530. /**
  531. * 是否存在全局变量
  532. *
  533. * @param string $name
  534. *
  535. * @return bool
  536. */
  537. function hasGlobalValue(string $name)
  538. {
  539. return GlobalValue::getInstance()->has($name);
  540. }
  541. }
  542. if (!function_exists('delGlobalValue')) {
  543. /**
  544. * 删除全局变量
  545. *
  546. * @param string $name
  547. *
  548. * @return void
  549. */
  550. function delGlobalValue(string $name)
  551. {
  552. GlobalValue::getInstance()->del($name);
  553. }
  554. }
  555. if (!function_exists('needAwsWafToken')) {
  556. /**
  557. * @return bool
  558. */
  559. function needAwsWafToken()
  560. {
  561. try {
  562. $client = new Client([
  563. 'headers' => [
  564. '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',
  565. 'Accept-Encoding' => 'gzip, deflate, br',
  566. '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',
  567. ],
  568. 'timeout' => 4.2011,
  569. CURLOPT_FOLLOWLOCATION => true,
  570. CURLOPT_AUTOREFERER => true,
  571. 'verify' => config('verify_ssl'),
  572. 'proxy' => config('freenom_proxy'),
  573. ]);
  574. $res = $client->get('https://my.freenom.com/clientarea.php');
  575. return $res->getStatusCode() != 200;
  576. } catch (\Exception $e) {
  577. return stripos($e->getMessage(), '405') !== false;
  578. }
  579. }
  580. }