File size: 1,039 Bytes
0c86b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const TARGET_HOST = 'copilot.github1s.tk'

const handlers = {
  async fetch(request, env = {}) {
    const uri = new URL(request.url)
    console.log('uri', uri.toString())
    if (uri.protocol === 'http:' && !/^[0-9.:]+$/.test(TARGET_HOST)) {
      uri.protocol = 'https:';
    }
    uri.host = TARGET_HOST
    uri.port = TARGET_HOST.split(':')[1] || ''
    const headersObj = {}
    for (const [key, value] of request.headers.entries()) {
      if (key.startsWith('cf-') || key.startsWith('x-') || ['connection', 'origin', 'referer', 'host', 'authority', 'link'].includes(key)) continue
      headersObj[key] = value
    }
    const headers = new Headers(headersObj)
    headers.set('Host', uri.host)
    // console.log(headers)
    const response = await fetch(uri.toString(), {
      headers,
      method: request.method,
      redirect: request.redirect,
      body: request.body,
    })
    return response
  },
}

export default handlers

addEventListener("fetch", (event) => {
  event.respondWith(handlers.fetch(event.request))
})