当前位置:   article > 正文

使用cloudflare worker反代网站_反代技术 网站

反代技术 网站
//这个代码是用example1.com反代example2.com

const current = 'example1.com'
const origin = 'example2.com'
 addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// new URL object to play with,
// based on the one being requested.
// e.g. https://domain.com/blog/page
var url = new URL(request.url)
// set hostname to the place we're proxying requests from
url.hostname = origin
url.protocol = 'https:';
console.log(url.pathname);
// remove the first occurence of /blog
// so it requests / of the proxy domain
// pass the modified url back to the request,
let response = await fetch(url, request)

if (request.url.endsWith('.png') ||request.url.endsWith('.jpg')||request.url.endsWith('.css')||request.url.endsWith('.gif')) {
    return response
  }
let { readable, writable } = new TransformStream()
  streamBody(response.body, writable)
  return new Response(readable, response)
}

async function streamBody(readable, writable) {
  let reader = readable.getReader()
  let writer = writable.getWriter()
  const decoder = new TextDecoder('utf-8')
  const encoder = new TextEncoder('utf-8')
  
  let body = ''
  while (true) {
    const { done, value } = await reader.read()
    if (done) break

    body += decoder.decode(value)
  }

  body = body.replace(new RegExp(current,'g'), origin);
  await writer.write(encoder.encode(body))

  await writer.close()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/943218
推荐阅读
相关标签
  

闽ICP备14008679号