Skip to content

Commit

Permalink
修复 cfworker 遗漏 query 的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
EtherDream committed Jul 25, 2019
1 parent 92e9f89 commit 52bcc47
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions cf-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function fetchHandler(e) {
const req = e.request
const urlStr = req.url
const urlObj = new URL(urlStr)
const {pathname} = urlObj
const path = urlObj.href.substr(urlObj.origin.length)

if (urlObj.protocol === 'http:') {
urlObj.protocol = 'https:'
Expand All @@ -54,11 +54,11 @@ async function fetchHandler(e) {
})
}

if (pathname.startsWith('/http/')) {
return httpHandler(req, pathname)
if (path.startsWith('/http/')) {
return httpHandler(req, path.substr(6))
}

switch (pathname) {
switch (path) {
case '/http':
return makeRes('请更新 cfworker 到最新版本!')
case '/ws':
Expand All @@ -67,7 +67,7 @@ async function fetchHandler(e) {
return makeRes('it works')
default:
// static files
return fetch(ASSET_URL + pathname)
return fetch(ASSET_URL + path)
}
}

Expand Down Expand Up @@ -101,6 +101,9 @@ function httpHandler(req, pathname) {
// https://github.com/EtherDream/jsproxy/blob/master/lua/http-dec-req-hdr.lua
const refer = reqHdrNew.get('referer')
const query = refer.substr(refer.indexOf('?') + 1)
if (!query) {
return makeRes('missing params', 403)
}
const param = new URLSearchParams(query)

for (const [k, v] of Object.entries(param)) {
Expand All @@ -127,13 +130,8 @@ function httpHandler(req, pathname) {
reqHdrNew.delete('referer')
}

let targetUrlStr = pathname.substr('/http/'.length)

// cfworker 会把路径中的 `//` 合并成 `/`
const m = targetUrlStr.match(/^https?:(\/+)/)
if (m && m[1] !== '//') {
targetUrlStr = targetUrlStr.replace('/', '//')
}
let targetUrlStr = pathname.replace(/^(https?):\/+/, "$1://")

try {
var targetUrlObj = new URL(targetUrlStr)
Expand Down

0 comments on commit 52bcc47

Please sign in to comment.