GameEncrypt.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if($hasLz) {
  22. try{
  23. $newarr = \GuzzleHttp\json_decode(self::decrypt($request->input('lz')),true);
  24. $_REQUEST=array_merge($_REQUEST,$newarr);
  25. $request->replace($newarr);
  26. }catch (\Exception $exception){
  27. try{
  28. $newarr = \GuzzleHttp\json_decode(utf8_encode(self::decrypt($request->input('lz'))),true);
  29. $_REQUEST=array_merge($_REQUEST,$newarr);
  30. $request->replace($newarr);
  31. }catch (\Exception $exception){
  32. TelegramBot::getDefault()->sendMsgWithEnv("Game LZ jsdecode:".$exception->getMessage());
  33. }
  34. }
  35. }
  36. $response = $next($request);
  37. // 如果是数组响应,统一转成 JsonResponse,避免后面 getContent() 报错 // 新增
  38. if (is_array($response)) { // 新增
  39. $response = response()->json($response); // 新增
  40. } // 新增
  41. // 如果不是一个 Symfony Response 对象,直接返回,不再做加密和加头 // 新增
  42. if (!$response instanceof SymfonyResponse) { // 新增
  43. return $response; // 新增
  44. } // 新增
  45. if($debug&&$hasLz){
  46. $this->header('req',json_encode($request->all()));
  47. }
  48. if($hasLz&&!RouteService::isTestSite()&&!$debug) {
  49. // 对返回内容进行加密处理
  50. $content = $response->getContent();
  51. $encryptedContent = self::encrypt($content);
  52. // 将加密后的内容设置回响应
  53. $response->setContent($encryptedContent);
  54. }
  55. $origin = $request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*';
  56. $this->header('Access-Control-Allow-Origin', $origin);
  57. $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');
  58. $this->header('Access-Control-Expose-Headers', '*');
  59. $this->header('Access-Control-Allow-Methods', 'GET, POST');
  60. $this->header('Access-Control-Allow-Credentials', 'true');
  61. return $response;
  62. }
  63. protected function header($key, $value)
  64. {
  65. header(sprintf('%s: %s', $key, $value));
  66. }
  67. public static function encrypt($str){
  68. try {
  69. $encStr = LZ::compressToBase64($str);
  70. }catch (\Exception $exception){
  71. TelegramBot::getDefault()->sendProgramNotify("Game LZ encrypt:", $exception->getTraceAsString());
  72. $encStr=$str;
  73. }
  74. return $encStr;
  75. }
  76. public static function decrypt($str){
  77. if (!isset($str)||!$str||empty($str)) {
  78. # code...
  79. return '';
  80. }
  81. $str=implode("+",explode(" ",$str));
  82. try {
  83. $destr=LZ::decompressFromBase64($str);
  84. }catch (\Exception $exception){
  85. Util::WriteLog('gamelz',$str);
  86. TelegramBot::getDefault()->sendProgramNotify("Game LZ decrypt:".$str);
  87. TelegramBot::getDefault()->sendProgramNotify("Game LZ decrypt:", $exception->getMessage().$exception->getTraceAsString());
  88. $destr=$str;
  89. }
  90. return $destr;
  91. }
  92. }