Skip to content

Commit

Permalink
小调整
Browse files Browse the repository at this point in the history
  • Loading branch information
EtherDream committed Jul 25, 2019
1 parent 885a847 commit 8c8fc8a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions cf-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ function makeRes(body, status = 200, headers = {}) {
}


/**
* @param {string} urlStr
*/
function newUrl(urlStr) {
try {
return new URL(urlStr)
} catch (err) {
return null
}
}


addEventListener('fetch', e => {
const ret = fetchHandler(e)
.catch(err => makeRes('cfworker error:\n' + err.stack, 502))
Expand Down Expand Up @@ -131,11 +143,10 @@ function httpHandler(req, pathname) {
}

// cfworker 会把路径中的 `//` 合并成 `/`
const targetUrlStr = pathname.replace(/^(https?):\/+/, '$1://')
try {
var targetUrlObj = new URL(targetUrlStr)
} catch (err) {
return makeRes('invalid url: ' + targetUrlStr, 403)
const urlStr = pathname.replace(/^(https?):\/+/, '$1://')
const urlObj = newUrl(urlStr)
if (!urlObj) {
return makeRes('invalid proxy url: ' + urlStr, 403)
}

/** @type {RequestInit} */
Expand All @@ -147,7 +158,7 @@ function httpHandler(req, pathname) {
if (req.method === 'POST') {
reqInit.body = req.body
}
return proxy(targetUrlObj, reqInit, acehOld, rawLen, 0)
return proxy(urlObj, reqInit, acehOld, rawLen, 0)
}


Expand Down

0 comments on commit 8c8fc8a

Please sign in to comment.