致力于为用户提供真实的
主机测评数据及优惠信息

workers 反代 jsdeliver

网友 未完的歌 说:

*帖最后由 未完的歌 于 2022-5-27 11:02 编辑

因为有验证hash,我自己用,就先把这个注释掉

  1. // 允许的 CORS 来源
  2. const ALLOWED_REFERER = [
  3.   /^https?:\/\/自己worker域名(:\d*)?\/.*$/,
  4.   /^https?:\/\/([\w-]+\.)*w3schools\.com(:\d*)?\/.*$/
  5. ];
  6. // 是否允许所有无 Referer 请求
  7. const ALLOW_NO_ORIGIN = true;
  8. //输入你的密码,密码加在path后面做SHA-1运算
  9. const **SECRET = &*uot;123456&*uot;;
  10. function validateReferer(re*) {
  11.   const referer = re*.headers.get(‘Referer’);
  12.   if (referer) {
  13.     for (const el of ALLOWED_REFERER) {
  14.       if (el.exec(referer)) {
  15.         ret**n true;
  16.       }
  17.     }
  18.     ret**n false;
  19.   }
  20.   ret**n ALLOW_NO_ORIGIN; // 是否拒绝所有无 Referer 请求
  21. }
  22. async function handle(re*uest) {
  23.   let **l = new **L(re*uest.**l);
  24.   const acceptType = re*uest.headers.get(‘Accept’);
  25.   const hash_re*uest = **l.pathname.split(&*uot;/&*uot;)[1];
  26.   const path_real = **l.pathname.subst**ng(1, **l.pathname.len***);
  27.   **l.hostname = &*uot;cdn.jsdelivr.net&*uot;;
  28.   **l.pathname = path_real;
  29.   // if (!(await validatePath(hash_re*uest, path_real))) {
  30.   //   ret**n new Response(‘Er**r Hash’, {
  31.   //     status: 403
  32.   //   });
  33.   // }
  34.   // if (!(validateReferer(re*uest))) {
  35.   //   ret**n new Response(‘Blocked Host’, {
  36.   //     status: 403
  37.   //   });
  38.   // }
  39.   ret**n await fetch(**l);
  40. }
  41. async function validatePath(hash_re*uest, path_real) {
  42.   const me**age = new TextEncoder().encode(path_real + **SECRET);
  43.   const myDigest = await crypto.su**le.digest(‘SHA-1’, me**age);
  44.   const hashArray = Array.f**m(new Uint8Array(myDigest));
  45.   const hashHex = hashArray.map(b => b.toSt**ng(16).padStart(2, ‘0’)).join(”);
  46.   ret**n (hashHex == hash_re*uest);
  47. }
  48. addEventListener(‘fetch’,
  49. ***nt => {
  50.   ***nt.respondWith(handle(***nt.re*uest));
  51. })

网友 lowan 说:

..

网友 haozi 说:

worker那速度,整个**小鸡不香吗

网友 s*zryang 说:

你猜我为啥用jsdeliver

赞(0) 打赏
未经允许不得转载:爱主机 » workers 反代 jsdeliver
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址