vps交流

workersproxy 反代 成功 但是 登录不了


workersproxy 反代 成功 但是 登录不了
这个要怎么解决……

  1. // 替换成你想镜像的站点
  2. const upstream = ‘www.google.com’
  3. // 如果那个站点有专门的移动适配站点,否则保持和上面一致
  4. const upstream_mobile = ‘www.google.com’
  5. // 你希望禁止哪些国家访问
  6. const blocked_region = [‘RU’]
  7. // 禁止自访问
  8. const blocked_ip_address = [‘0.0.0.0’, ‘127.0.0.1’]
  9. // 替换成你想镜像的站点
  10. const replace_dict = {
  11.     ‘$upstream’: ‘$custom_domain’,
  12.     ‘//www.google.com’: ”
  13. }
  14. //以下内容都不用动
  15. addEventListener(‘fetch’, event => {
  16.     event.respondWith(fetchAndApply(event.request));
  17. })
  18. async function fetchAndApply(request) {
  19.     const region = request.headers.get(‘cf-ipcountry’).toUpperCase();
  20.     const ip_address = request.headers.get(‘cf-connecting-ip’);
  21.     const user_agent = request.headers.get(‘user-agent’);
  22.     let response = null;
  23.     let url = new URL(request.url);
  24.     let url_host = url.host;
  25.     if (url.protocol == ‘http:’) {
  26.         url.protocol = ‘https:’
  27.         response = Response.redirect(url.href);
  28.         return response;
  29.     }
  30.     if (await device_status(user_agent)) {
  31.         upstream_domain = upstream
  32.     } else {
  33.         upstream_domain = upstream_mobile
  34.     }
  35.     url.host = upstream_domain;
  36.     if (blocked_region.includes(region)) {
  37.         response = new Response(‘Access denied: WorkersProxy is not available in your region yet.’, {
  38.             status: 403
  39.         });
  40.     } else if(blocked_ip_address.includes(ip_address)){
  41.         response = new Response(‘Access denied: Your IP address is blocked by WorkersProxy.’, {
  42.             status: 403
  43.         });
  44.     } else{
  45.         let method = request.method;
  46.         let request_headers = request.headers;
  47.         let new_request_headers = new Headers(request_headers);
  48.         new_request_headers.set(‘Host’, upstream_domain);
  49.         new_request_headers.set(‘Referer’, url.href);
  50.         let original_response = await fetch(url.href, {
  51.             method: method,
  52.             headers: new_request_headers
  53.         })
  54.         let original_response_clone = original_response.clone();
  55.         let original_text = null;
  56.         let response_headers = original_response.headers;
  57.         let new_response_headers = new Headers(response_headers);
  58.         let status = original_response.status;
  59.         new_response_headers.set(‘access-control-allow-origin’, ‘*’);
  60.         new_response_headers.set(‘access-control-allow-credentials’, true);
  61.         new_response_headers.delete(‘content-security-policy’);
  62.         new_response_headers.delete(‘content-security-policy-report-only’);
  63.         new_response_headers.delete(‘clear-site-data’);
  64.         const content_type = new_response_headers.get(‘content-type’);
  65.         if (content_type.includes(‘text/html’) && content_type.includes(‘UTF-8’)) {
  66.             original_text = await replace_response_text(original_response_clone, upstream_domain, url_host);
  67.         } else {
  68.             original_text = original_response_clone.body
  69.         }
  70.         response = new Response(original_text, {
  71.             status,
  72.             headers: new_response_headers
  73.         })
  74.     }
  75.     return response;
  76. }
  77. async function replace_response_text(response, upstream_domain, host_name) {
  78.     let text = await response.text()
  79.     var i, j;
  80.     for (i in replace_dict) {
  81.         j = replace_dict[i]
  82.         if (i == ‘$upstream’) {
  83.             i = upstream_domain
  84.         } else if (i == ‘$custom_domain’) {
  85.             i = host_name
  86.         }
  87.         if (j == ‘$upstream’) {
  88.             j = upstream_domain
  89.         } else if (j == ‘$custom_domain’) {
  90.             j = host_name
  91.         }
  92.         let re = new RegExp(i, ‘g’)
  93.         text = text.replace(re, j);
  94.     }
  95.     return text;
  96. }
  97. async function device_status (user_agent_info) {
  98.     var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  99.     var flag = true;
  100.     for (var v = 0; v < agents.length; v++) { if (user_agent_info.indexOf(agents[v]) > 0) {
  101.             flag = false;
  102.             break;
  103.         }
  104.     }
  105.     return flag;
  106. }

复制代码

没处理 response.headers 里面的 set-cookie
workersproxy 反代 成功 但是 登录不了

whiler 发表于 2022-1-19 18:09
没处理 response.headers 里面的 set-cookie

要怎么修改啊

参考 replace_response_text 函数,修改 cookie 的 domain 参数

whiler 发表于 2022-1-19 18:46
参考 replace_response_text 函数,修改 cookie 的 domain 参数

大佬有没有现成的给抄抄 。搞不定=^_^

aiastia 发表于 2022-1-19 18:48
大佬有没有现成的给抄抄 。搞不定=^_^

没有

whiler 发表于 2022-1-19 18:52
没有

好吧 谢谢