|
@@ -26,47 +26,51 @@ const PREFLIGHT_INIT = {
|
|
|
*/
|
|
*/
|
|
|
function makeRes(message, status = 200, headers = {}) {
|
|
function makeRes(message, status = 200, headers = {}) {
|
|
|
headers['cache-control'] = 'no-cache'
|
|
headers['cache-control'] = 'no-cache'
|
|
|
|
|
+ headers['vary'] = '--url'
|
|
|
|
|
+ headers['access-control-allow-origin'] = '*'
|
|
|
return new Response(message, {status, headers})
|
|
return new Response(message, {status, headers})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
addEventListener('fetch', e => {
|
|
addEventListener('fetch', e => {
|
|
|
|
|
+ const ret = fetchHandler(e)
|
|
|
|
|
+ .catch(err => makeRes('cfworker error:' + err, 502))
|
|
|
|
|
+ e.respondWith(ret)
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+function fetchHandler(e) {
|
|
|
const req = e.request
|
|
const req = e.request
|
|
|
const urlStr = req.url
|
|
const urlStr = req.url
|
|
|
const urlObj = new URL(urlStr)
|
|
const urlObj = new URL(urlStr)
|
|
|
- let ret
|
|
|
|
|
|
|
|
|
|
- // HTTP 跳转到 HTTPS
|
|
|
|
|
if (urlObj.protocol === 'http:') {
|
|
if (urlObj.protocol === 'http:') {
|
|
|
urlObj.protocol = 'https:'
|
|
urlObj.protocol = 'https:'
|
|
|
- ret = makeRes('', 301, {
|
|
|
|
|
|
|
+ return makeRes('', 301, {
|
|
|
'strict-transport-security': 'max-age=99999999; includeSubDomains; preload',
|
|
'strict-transport-security': 'max-age=99999999; includeSubDomains; preload',
|
|
|
'location': urlObj.href,
|
|
'location': urlObj.href,
|
|
|
})
|
|
})
|
|
|
- e.respondWith(ret)
|
|
|
|
|
- return
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
switch (urlObj.pathname) {
|
|
switch (urlObj.pathname) {
|
|
|
case '/http':
|
|
case '/http':
|
|
|
- ret = handler(req)
|
|
|
|
|
- break
|
|
|
|
|
|
|
+ return httpHandler(req)
|
|
|
|
|
+ case '/ws':
|
|
|
|
|
+ return makeRes('not support', 400)
|
|
|
case '/works':
|
|
case '/works':
|
|
|
- ret = makeRes('it works')
|
|
|
|
|
- break
|
|
|
|
|
|
|
+ return makeRes('it works')
|
|
|
default:
|
|
default:
|
|
|
// static files
|
|
// static files
|
|
|
- ret = fetch(ASSET_URL + urlObj.pathname)
|
|
|
|
|
- break
|
|
|
|
|
|
|
+ return fetch(ASSET_URL + urlObj.pathname)
|
|
|
}
|
|
}
|
|
|
- e.respondWith(ret)
|
|
|
|
|
-})
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @param {Request} req
|
|
* @param {Request} req
|
|
|
*/
|
|
*/
|
|
|
-async function handler(req) {
|
|
|
|
|
|
|
+async function httpHandler(req) {
|
|
|
const reqHdrRaw = req.headers
|
|
const reqHdrRaw = req.headers
|
|
|
if (reqHdrRaw.has('x-jsproxy')) {
|
|
if (reqHdrRaw.has('x-jsproxy')) {
|
|
|
return Response.error()
|
|
return Response.error()
|
|
@@ -130,10 +134,15 @@ async function handler(req) {
|
|
|
if (!urlObj) {
|
|
if (!urlObj) {
|
|
|
return makeRes('missing url param', 403)
|
|
return makeRes('missing url param', 403)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /** @type {RequestInit} */
|
|
|
const reqInit = {
|
|
const reqInit = {
|
|
|
method: req.method,
|
|
method: req.method,
|
|
|
headers: reqHdrNew,
|
|
headers: reqHdrNew,
|
|
|
}
|
|
}
|
|
|
|
|
+ if (req.method === 'POST') {
|
|
|
|
|
+ reqInit.body = req.body
|
|
|
|
|
+ }
|
|
|
return proxy(urlObj, reqInit, acehOld, rawLen, 0)
|
|
return proxy(urlObj, reqInit, acehOld, rawLen, 0)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -197,7 +206,7 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
|
|
|
return proxy(urlObj, reqInit, acehOld, rawLen, retryTimes + 1)
|
|
return proxy(urlObj, reqInit, acehOld, rawLen, retryTimes + 1)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return makeRes(`error`, 400, {
|
|
|
|
|
|
|
+ return makeRes('error', 400, {
|
|
|
'--error': 'bad len:' + newLen
|
|
'--error': 'bad len:' + newLen
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|