GameEncrypt.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Game\Services;
  3. use App\Notification\TelegramBot;
  4. use App\Util;
  5. use Closure;
  6. use App\Game\Services\LZCompressor\LZString as LZ;
  7. use App\Game\Services\LZCompressor\LZUtil;
  8. use Symfony\Component\HttpFoundation\Response as SymfonyResponse; // 新增
  9. use Illuminate\Http\JsonResponse; // 可选,用于数组转 JSON 响应
  10. class GameEncrypt
  11. {
  12. /**
  13. * 验证签名 API接口
  14. * @param \Illuminate\Http\Request $request
  15. * @param Closure $next
  16. */
  17. public function handle($request, Closure $next)
  18. {
  19. $hasLz=$request->has('lz');
  20. $debug=RouteService::isTestSite()||$request->has('_db')&&($request->input('_db')=='g');
  21. //判定一下,如果路由是/game/registerNew 就输出日志
  22. if($hasLz) {
  23. if ($request->path() === 'game/registerNew') {
  24. // 输出日志逻辑
  25. Util::WriteLog('register_params', 'Register new user: ' . json_encode($request->all()));
  26. }
  27. try{
  28. $newarr = \GuzzleHttp\json_decode(self::decrypt($request->input('lz')),true);
  29. $_REQUEST=array_merge($_REQUEST,$newarr);
  30. $request->replace($newarr);
  31. }catch (\Exception $exception){
  32. try{
  33. $newarr = \GuzzleHttp\json_decode(utf8_encode(self::decrypt($request->input('lz'))),true);
  34. $_REQUEST=array_merge($_REQUEST,$newarr);
  35. $request->replace($newarr);
  36. }catch (\Exception $exception){
  37. TelegramBot::getDefault()->sendMsgWithEnv("Game LZ jsdecode:".$exception->getMessage());
  38. }
  39. }
  40. }
  41. $response = $next($request);
  42. // 如果是数组响应,统一转成 JsonResponse,避免后面 getContent() 报错 // 新增
  43. if (is_array($response)) { // 新增
  44. $response = response()->json($response); // 新增
  45. } // 新增
  46. // 如果不是一个 Symfony Response 对象,直接返回,不再做加密和加头 // 新增
  47. if (!$response instanceof SymfonyResponse) { // 新增
  48. return $response; // 新增
  49. } // 新增
  50. if($debug&&$hasLz){
  51. $this->header('req',json_encode($request->all()));
  52. }
  53. if($hasLz&&!RouteService::isTestSite()&&!$debug) {
  54. // 对返回内容进行加密处理
  55. $content = $response->getContent();
  56. $encryptedContent = self::encrypt($content);
  57. // 将加密后的内容设置回响应
  58. $response->setContent($encryptedContent);
  59. }
  60. $origin = $request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*';
  61. $this->header('Access-Control-Allow-Origin', $origin);
  62. $this->header('Access-Control-Allow-Headers', 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
  63. $this->header('Access-Control-Expose-Headers', '*');
  64. $this->header('Access-Control-Allow-Methods', 'GET, POST');
  65. $this->header('Access-Control-Allow-Credentials', 'true');
  66. return $response;
  67. }
  68. protected function header($key, $value)
  69. {
  70. header(sprintf('%s: %s', $key, $value));
  71. }
  72. public static function encrypt($str){
  73. try {
  74. $encStr = LZ::compressToBase64($str);
  75. }catch (\Exception $exception){
  76. TelegramBot::getDefault()->sendProgramNotify("Game LZ encrypt:", $exception->getTraceAsString());
  77. $encStr=$str;
  78. }
  79. return $encStr;
  80. }
  81. public static function decrypt($str){
  82. if (!isset($str)||!$str||empty($str)) {
  83. # code...
  84. return '';
  85. }
  86. $str=implode("+",explode(" ",$str));
  87. try {
  88. $destr=LZ::decompressFromBase64($str);
  89. }catch (\Exception $exception){
  90. Util::WriteLog('gamelz',$str);
  91. TelegramBot::getDefault()->sendProgramNotify("Game LZ decrypt:".$str);
  92. TelegramBot::getDefault()->sendProgramNotify("Game LZ decrypt:", $exception->getMessage().$exception->getTraceAsString());
  93. $destr=$str;
  94. }
  95. return $destr;
  96. }
  97. }