GameEncrypt.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class GameEncrypt
  9. {
  10. /**
  11. * 验证签名 API接口
  12. * @param \Illuminate\Http\Request $request
  13. * @param Closure $next
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. $hasLz=$request->has('lz');
  18. $debug=$request->has('_db')&&($request->input('_db')=='g');
  19. if($hasLz) {
  20. try{
  21. $newarr = \GuzzleHttp\json_decode(self::decrypt($request->input('lz')),true);
  22. $_REQUEST=array_merge($_REQUEST,$newarr);
  23. $request->replace($newarr);
  24. }catch (\Exception $exception){
  25. try{
  26. $newarr = \GuzzleHttp\json_decode(utf8_encode(self::decrypt($request->input('lz'))),true);
  27. $_REQUEST=array_merge($_REQUEST,$newarr);
  28. $request->replace($newarr);
  29. }catch (\Exception $exception){
  30. TelegramBot::getDefault()->sendMsgWithEnv("Game LZ jsdecode:".$exception->getMessage());
  31. }
  32. }
  33. }
  34. $response = $next($request);
  35. if($debug&&$hasLz&&is_array($response)){
  36. $response['req']=$request->all();
  37. }
  38. $origin = $request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*';
  39. if($hasLz&&!RouteService::isTestSite()&&!$debug) {
  40. // 对返回内容进行加密处理
  41. $content = $response->getContent();
  42. $encryptedContent = self::encrypt($content);
  43. // 将加密后的内容设置回响应
  44. $response->setContent($encryptedContent);
  45. }
  46. $this->header('Access-Control-Allow-Origin', $origin);
  47. $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');
  48. $this->header('Access-Control-Expose-Headers', '*');
  49. $this->header('Access-Control-Allow-Methods', 'GET, POST');
  50. $this->header('Access-Control-Allow-Credentials', 'true');
  51. return $response;
  52. }
  53. protected function header($key, $value)
  54. {
  55. header(sprintf('%s: %s', $key, $value));
  56. }
  57. public static function encrypt($str){
  58. try {
  59. $encStr = LZ::compressToBase64($str);
  60. }catch (\Exception $exception){
  61. TelegramBot::getDefault()->sendProgramNotify("Game LZ encrypt:", $exception->getTraceAsString());
  62. $encStr=$str;
  63. }
  64. return $encStr;
  65. }
  66. public static function decrypt($str){
  67. if (!isset($str)||!$str||empty($str)) {
  68. # code...
  69. return '';
  70. }
  71. $str=implode("+",explode(" ",$str));
  72. try {
  73. $destr=LZ::decompressFromBase64($str);
  74. }catch (\Exception $exception){
  75. Util::WriteLog('gamelz',$str);
  76. TelegramBot::getDefault()->sendProgramNotify("Game LZ decrypt:".$str);
  77. TelegramBot::getDefault()->sendProgramNotify("Game LZ decrypt:", $exception->getMessage().$exception->getTraceAsString());
  78. $destr=$str;
  79. }
  80. return $destr;
  81. }
  82. }