Sign.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace app\middleware;
  15. use Illuminate\Support\Arr;
  16. use Webman\MiddlewareInterface;
  17. use Webman\Http\Response;
  18. use Webman\Http\Request;
  19. /**
  20. * Class StaticFile
  21. * @package app\middleware
  22. */
  23. class Sign implements MiddlewareInterface
  24. {
  25. public function process(Request $request, callable $next): Response
  26. {
  27. $sign = $request->header('sign');
  28. if (empty($sign)) {
  29. return $this->transfer();
  30. }
  31. $transfersecret=$request->header('transfersecret');
  32. if(empty($transfersecret)){
  33. return $this->transfer();
  34. }
  35. $string_data = openssl_decrypt(base64_decode($transfersecret),'AES-128-ECB',getenv ("JWT_AESKEY"),OPENSSL_RAW_DATA);
  36. if(!$string_data){
  37. return $this->transfer();
  38. }
  39. $param_data = json_decode($string_data,true);
  40. if(!$param_data){
  41. return $this->transfer();
  42. }
  43. $param = $param_data;
  44. if (Arr::get($param, 'time', 0) < (time() - 15)) {
  45. return $this->transfer();
  46. }
  47. if ($sign != $this->sign($param_data)) {
  48. return $this->transfer();
  49. }
  50. /** @var Response $response */
  51. $response = $next($request);
  52. return $response;
  53. }
  54. public function transfer()
  55. {
  56. $arr = [
  57. 0 => 'https://www.baidu.com',
  58. 1 => 'https://weibo.com',
  59. 2 => 'https://www.douyin.com',
  60. 3 => 'https://www.jd.com',
  61. 4 => 'https://uland.taobao.com',
  62. 5 => 'https://news.qq.com',
  63. 6 => 'https://www.toutiao.com',
  64. 7 => 'https://www.kuaishou.com',
  65. 8 => 'https://www.sohu.com',
  66. ];
  67. return redirect($arr[mt_rand(0, 8)]);
  68. }
  69. public function sign($array)
  70. {
  71. ksort($array); //ASCII码排序
  72. $md5str = "";
  73. foreach ($array as $key => $val) {
  74. $md5str = $md5str . $key . "=" . $val . "&";
  75. }
  76. return md5($md5str . "key=" . getenv('JWT_PRIVATEKEY'));
  77. }
  78. }