This commit is contained in:
zjcqoo 2019-05-04 21:47:08 +08:00
parent c101c28eab
commit df245e5f85
6 changed files with 148 additions and 4 deletions

View File

@ -9,10 +9,11 @@ https://zjcqoo.github.io/-----https://www.google.com
# 安装
新建一个名为 `jsproxy` 用户,在其主目录安装 nginx
新建一个名为 `jsproxy:nobody` 用户,在其主目录安装 nginx
```bash
useradd jsproxy -g nobody
groupadd nobody
useradd jsproxy -g nobody --create-home
su jsproxy
cd ~

0
cf-worker/README.md Normal file
View File

130
cf-worker/index.js Normal file
View File

@ -0,0 +1,130 @@
/**
* jsproxy cfworker api
*
* @update: 2019-05-03
* @author: EtherDream
* @see: https://github.com/EtherDream/jsproxy/
*/
const pairs = Object.entries
addEventListener('fetch', e => {
const ret = handler(e.request)
.catch(err => new Response(err))
e.respondWith(ret)
})
const PREFLIGHT_INIT = {
status: 204,
headers: new Headers({
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
'access-control-allow-headers': '--url,--referer,--cookie,--origin,--ext,--aceh,--ver,--type,--mode,accept,accept-charset,accept-encoding,accept-language,accept-datetime,authorization,cache-control,content-length,content-type,date,if-match,if-modified-since,if-none-match,if-range,if-unmodified-since,max-forwards,pragma,range,te,upgrade,upgrade-insecure-requests,x-requested-with,chrome-proxy',
'access-control-max-age': '1728000',
}),
}
/**
* @param {Request} req
*/
async function handler(req) {
const reqHdrRaw = req.headers
// preflight
if (reqHdrRaw.has('access-control-request-headers')) {
return new Response(null, PREFLIGHT_INIT)
}
let url = ''
let extHdrs = null
let acehOld = false
const reqHdrNew = new Headers(reqHdrRaw)
for (const [k, v] of reqHdrRaw.entries()) {
if (!k.startsWith('--')) {
continue
}
const k2 = k.substr(2)
switch (k2) {
case 'url':
url = v
break
case 'aceh':
acehOld = true
break
case 'mode':
case 'type':
break
case 'ext':
extHdrs = JSON.parse(v)
break
default:
reqHdrNew.set(k2, v)
break
}
}
if (extHdrs) {
for (const [k, v] of pairs(extHdrs)) {
reqHdrNew.set(k, v)
}
}
// proxy
const res = await fetch(url, {
method: req.method,
headers: reqHdrNew,
})
// header filter
const resHdrOld = res.headers
const resHdrNew = new Headers(resHdrOld)
let expose = '*'
let vary = '--url'
for (const [k, v] of resHdrOld.entries()) {
if (k === 'access-control-allow-origin' ||
k === 'access-control-expose-headers' ||
k === 'location' ||
k === 'set-cookie'
) {
const x = '--' + k
resHdrNew.set(x, v)
if (acehOld) {
expose = expose + ',' + x
}
resHdrNew.delete(k)
}
else if (k === 'vary') {
vary = vary + ',' + v
}
else if (acehOld &&
k !== 'cache-control' &&
k !== 'content-language' &&
k !== 'content-type' &&
k !== 'expires' &&
k !== 'last-modified' &&
k !== 'pragma'
) {
expose = expose + ',' + k
}
}
if (acehOld) {
expose = expose + ',--s'
resHdrNew.set('--t', '1')
}
resHdrNew.set('access-control-expose-headers', expose)
resHdrNew.set('access-control-allow-origin', '*')
resHdrNew.set('vary', vary)
resHdrNew.set('--s', res.status)
return new Response(res.body, {
status: 200,
headers: resHdrNew,
})
}

View File

@ -21,12 +21,14 @@ for k, v in pairs(hdrs) do
elseif k == 'mode' then
ngx.var._mode = v
elseif k == 'aceh' then
ngx.ctx._aceh = 1
ngx.ctx._acehOld = true
elseif k == 'ext' then
extHdrs = require('cjson').decode(v)
else
if k == 'referer' then
ngx.var._ref = v
else if k == 'cookie' then
ngx.ctx._hasCookie = true
end
ngx.req.set_header(k, v)
end

View File

@ -4,11 +4,20 @@
-- aceh = HTTP 返回头的 access-control-expose-headers 字段
--
if
ngx.req.get_method() == 'GET' and
not ngx.ctx._hasCookie
then
end
-- 无论浏览器是否支持aceh 始终包含 *
local expose = '*'
-- 该值为 true 表示浏览器不支持 aceh: *,需返回详细的头部列表
local detail = (ngx.ctx._aceh == 1)
local detail = ngx.ctx._acehOld
-- 由于接口路径固定,为避免被缓存,以请求头的 --url 值区分缓存
local vary = '--url'

View File

@ -3,6 +3,8 @@ curl -O https://openresty.org/download/openresty-1.15.8.1rc1.tar.gz
tar zxvf openresty-*
cd openresty-*
export PATH=$PATH:/sbin
./configure \
--with-http_v2_module \
--with-http_ssl_module \