Skip to content

Commit 8bb7735

Browse files
committed
更新 v0.1.0
1 parent 6cc0059 commit 8bb7735

File tree

12 files changed

+229
-187
lines changed

12 files changed

+229
-187
lines changed

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11

22
# 更新
33

4-
* 2019-06-22 [cfworker 无服务器版](cf-worker) 发布,长期使用演示服务的请使用该版本。
5-
6-
* 2019-06-11 前端脚本调整,首页可离线访问(如果长时间加载中,尝试多刷新几次或者隐身模式访问)
7-
8-
* 2019-05-30 更新 cfworker,对 ytb 视频进行了优化(推荐选 1080p+,不会增加服务器压力)
4+
* 2019-07-24 [v0.1.0](tree/0.1.0) 发布,主要修复了缓存失效的问题。网络接口和之前版本不兼容,请及时更新服务端和 cfworker。
95

10-
* 2019-05-29 nginx 增加静态资源服务,可同时支持代理接口和首页访问
11-
12-
* 2019-05-27 增加 nio.io、sslip.io 后备域名,减少申请失败的几率
13-
14-
* 2019-05-26 安装时自动申请证书(使用 xip.io 域名),安装后即可预览
6+
* 2019-06-22 [cfworker 无服务器版](cf-worker) 发布,长期使用演示服务的请使用该版本。
157

168
[查看更多](changelogs)
179

1810

1911
# 安装
2012

2113
```bash
14+
<<<<<<< HEAD
2215
curl https://raw.githubusercontent.com/EtherDream/jsproxy/master/i.sh | bash
16+
=======
17+
curl https://raw.githubusercontent.com/EtherDream/jsproxy/0.1.0/i.sh | bash
18+
>>>>>>> dev
2319
```
2420

2521
* 自动安装目前只支持 Linux x64,并且需要 root 权限

api.conf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ location = /error {
1515
more_set_headers
1616
'access-control-allow-origin: *'
1717
'access-control-expose-headers: gateway-err--'
18-
'gateway-err--: {"msg": "$arg_msg", "addr": "$upstream_addr"}'
18+
'gateway-err--: {"msg": "$arg_msg", "addr": "$upstream_addr", "url": "$arg_url"}'
1919
;
2020
return 204;
2121
}
@@ -27,15 +27,20 @@ location = /preflight {
2727
more_set_headers
2828
'access-control-allow-origin: *'
2929
'access-control-allow-methods: GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS'
30-
'access-control-allow-headers: --raw-info,--level,--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,purpose'
3130
'access-control-max-age: 1728000'
3231
;
3332
return 204;
3433
}
3534

35+
# 该接口已作废
36+
location = /http {
37+
access_log off;
38+
more_set_headers 'access-control-allow-origin: *';
39+
return 200 "该接口已作废,请更新前端脚本";
40+
}
3641

3742
# HTTP(S) Proxy
38-
location = /http {
43+
location /http/ {
3944
# see ./allowed-sites.conf
4045
if ($_origin_id = '') {
4146
rewrite ^ /error?msg=ORIGIN_NOT_ALLOWED;
@@ -47,7 +52,7 @@ location = /http {
4752
proxy_set_header Connection $http_connection;
4853

4954

50-
if ($http_access_control_request_headers) {
55+
if ($http_access_control_request_methods) {
5156
rewrite ^ /preflight;
5257
}
5358

cf-worker/.eslintrc.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"env": {
33
"browser": true,
4-
"es6": true
4+
"es6": true,
5+
"serviceworker": true
56
},
67
"extends": "eslint:recommended",
78
"globals": {
@@ -13,10 +14,8 @@
1314
"sourceType": "module"
1415
},
1516
"rules": {
16-
"no-console": "warn",
1717
"no-empty": "warn",
1818
"no-unused-vars": "warn",
19-
"no-debugger": "warn",
2019
"no-constant-condition": "warn"
2120
}
2221
}

cf-worker/README.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,10 @@
2323

2424
如果不够用,可注册多个 Worker,在 `conf.js` 中配置多线路负载均衡。或者升级到 $5 的高级版本,每月可用 1000 万次请求(超出部分 $0.5/百万次请求)。
2525

26-
如果远不够用,建议和服务器组合使用。因为 cfworker 是按请求次数计费的,所以小文件更适合通过服务器代理,大文件走 cfworker 才合算。可参考下面的 `加速功能`
27-
2826

2927
# 修改配置
3028

31-
默认情况下,静态资源从 `https://zjcqoo.github.io` 反向代理,可通过代码中 `ASSET_URL` 配置,从而可使用自定义的 `conf.js` 配置。
32-
33-
34-
# 加速功能
35-
36-
如果你已有服务器,也可通过 CloudFlare Worker 分担大文件的代理。
37-
38-
前端修改:`conf.js``cfworker` 节点 `lines` 配置。
39-
40-
后端修改:`lua/http-enc-res-hdr.lua`[114-116 行](https://github.com/EtherDream/jsproxy/blob/master/lua/http-enc-res-hdr.lua#L114-L116) 注释打开,重启服务生效。
41-
42-
可在 [84行](https://github.com/EtherDream/jsproxy/blob/master/lua/http-enc-res-hdr.lua#L84) 处修改大于多少字节的静态资源走加速。
43-
44-
该功能目前还在实验中,有问题或者更好的思路可交流。
45-
46-
(推荐下行流量免费且不限速的服务器,可节省大量费用)
29+
默认情况下,静态资源从 `https://etherdream.github.io/jsproxy` 反向代理,可通过代码中 `ASSET_URL` 配置,从而可使用自定义的 `conf.js` 配置。
4730

4831

4932
# 存在问题

cf-worker/index.js

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,60 @@
33
/**
44
* static files (404.html, sw.js, conf.js)
55
*/
6-
const ASSET_URL = 'https://zjcqoo.github.io'
6+
const ASSET_URL = 'https://etherdream.github.io/jsproxy'
77

8-
const JS_VER = 8
8+
const JS_VER = 10
99
const MAX_RETRY = 1
1010

11-
11+
/** @type {RequestInit} */
1212
const PREFLIGHT_INIT = {
1313
status: 204,
1414
headers: new Headers({
1515
'access-control-allow-origin': '*',
1616
'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
17-
'access-control-allow-headers': '--raw-info,--level,--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,purpose',
1817
'access-control-max-age': '1728000',
1918
}),
2019
}
2120

2221
/**
23-
* @param {string} message
22+
* @param {any} body
2423
* @param {number} status
25-
* @param {any} headers
24+
* @param {Object<string, string>} headers
2625
*/
27-
function makeRes(message, status = 200, headers = {}) {
28-
headers['cache-control'] = 'no-cache'
29-
headers['vary'] = '--url'
26+
function makeRes(body, status = 200, headers = {}) {
27+
headers['--ver'] = JS_VER
3028
headers['access-control-allow-origin'] = '*'
31-
return new Response(message, {status, headers})
29+
return new Response(body, {status, headers})
30+
}
31+
32+
33+
/**
34+
* @param {string} urlStr
35+
*/
36+
function newUrl(urlStr) {
37+
try {
38+
return new URL(urlStr)
39+
} catch (err) {
40+
return null
41+
}
3242
}
3343

3444

3545
addEventListener('fetch', e => {
3646
const ret = fetchHandler(e)
37-
.catch(err => makeRes('cfworker error:' + err, 502))
47+
.catch(err => makeRes('cfworker error:\n' + err.stack, 502))
3848
e.respondWith(ret)
3949
})
4050

4151

42-
function fetchHandler(e) {
52+
/**
53+
* @param {FetchEvent} e
54+
*/
55+
async function fetchHandler(e) {
4356
const req = e.request
4457
const urlStr = req.url
4558
const urlObj = new URL(urlStr)
59+
const path = urlObj.href.substr(urlObj.origin.length)
4660

4761
if (urlObj.protocol === 'http:') {
4862
urlObj.protocol = 'https:'
@@ -52,25 +66,29 @@ function fetchHandler(e) {
5266
})
5367
}
5468

55-
switch (urlObj.pathname) {
69+
if (path.startsWith('/http/')) {
70+
return httpHandler(req, path.substr(6))
71+
}
72+
73+
switch (path) {
5674
case '/http':
57-
return httpHandler(req)
75+
return makeRes('请更新 cfworker 到最新版本!')
5876
case '/ws':
5977
return makeRes('not support', 400)
6078
case '/works':
6179
return makeRes('it works')
6280
default:
6381
// static files
64-
return fetch(ASSET_URL + urlObj.pathname)
82+
return fetch(ASSET_URL + path)
6583
}
6684
}
6785

6886

69-
7087
/**
7188
* @param {Request} req
89+
* @param {string} pathname
7290
*/
73-
async function httpHandler(req) {
91+
function httpHandler(req, pathname) {
7492
const reqHdrRaw = req.headers
7593
if (reqHdrRaw.has('x-jsproxy')) {
7694
return Response.error()
@@ -83,8 +101,6 @@ async function httpHandler(req) {
83101
return new Response(null, PREFLIGHT_INIT)
84102
}
85103

86-
let urlObj = null
87-
let extHdrs = null
88104
let acehOld = false
89105
let rawSvr = ''
90106
let rawLen = ''
@@ -93,52 +109,51 @@ async function httpHandler(req) {
93109
const reqHdrNew = new Headers(reqHdrRaw)
94110
reqHdrNew.set('x-jsproxy', '1')
95111

96-
for (const [k, v] of reqHdrRaw.entries()) {
97-
if (!k.startsWith('--')) {
98-
continue
99-
}
100-
reqHdrNew.delete(k)
101-
102-
const k2 = k.substr(2)
103-
switch (k2) {
104-
case 'url':
105-
urlObj = new URL(v)
106-
break
107-
case 'aceh':
108-
acehOld = true
109-
break
110-
case 'raw-info':
111-
[rawSvr, rawLen, rawEtag] = v.split('|')
112-
break
113-
case 'level':
114-
case 'mode':
115-
case 'type':
116-
break
117-
case 'ext':
118-
extHdrs = JSON.parse(v)
119-
break
120-
default:
112+
// 此处逻辑和 http-dec-req-hdr.lua 大致相同
113+
// https://github.com/EtherDream/jsproxy/blob/master/lua/http-dec-req-hdr.lua
114+
const refer = reqHdrNew.get('referer')
115+
const query = refer.substr(refer.indexOf('?') + 1)
116+
if (!query) {
117+
return makeRes('missing params', 403)
118+
}
119+
const param = new URLSearchParams(query)
120+
121+
for (const [k, v] of Object.entries(param)) {
122+
if (k.substr(0, 2) === '--') {
123+
// 系统信息
124+
switch (k.substr(2)) {
125+
case 'aceh':
126+
acehOld = true
127+
break
128+
case 'raw-info':
129+
[rawSvr, rawLen, rawEtag] = v.split('|')
130+
break
131+
}
132+
} else {
133+
// 还原 HTTP 请求头
121134
if (v) {
122-
reqHdrNew.set(k2, v)
135+
reqHdrNew.set(k, v)
123136
} else {
124-
reqHdrNew.delete(k2)
137+
reqHdrNew.delete(k)
125138
}
126-
break
127139
}
128140
}
129-
if (extHdrs) {
130-
for (const [k, v] of Object.entries(extHdrs)) {
131-
reqHdrNew.set(k, v)
132-
}
141+
if (!param.has('referer')) {
142+
reqHdrNew.delete('referer')
133143
}
144+
145+
// cfworker 会把路径中的 `//` 合并成 `/`
146+
const urlStr = pathname.replace(/^(https?):\/+/, '$1://')
147+
const urlObj = newUrl(urlStr)
134148
if (!urlObj) {
135-
return makeRes('missing url param', 403)
149+
return makeRes('invalid proxy url: ' + urlStr, 403)
136150
}
137151

138152
/** @type {RequestInit} */
139153
const reqInit = {
140154
method: req.method,
141155
headers: reqHdrNew,
156+
redirect: 'manual',
142157
}
143158
if (req.method === 'POST') {
144159
reqInit.body = req.body
@@ -159,7 +174,6 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
159174
const resHdrNew = new Headers(resHdrOld)
160175

161176
let expose = '*'
162-
let vary = '--url'
163177

164178
for (const [k, v] of resHdrOld.entries()) {
165179
if (k === 'access-control-allow-origin' ||
@@ -174,9 +188,6 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
174188
}
175189
resHdrNew.delete(k)
176190
}
177-
else if (k === 'vary') {
178-
vary = vary + ',' + v
179-
}
180191
else if (acehOld &&
181192
k !== 'cache-control' &&
182193
k !== 'content-language' &&
@@ -206,8 +217,9 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
206217
return proxy(urlObj, reqInit, acehOld, rawLen, retryTimes + 1)
207218
}
208219
}
209-
return makeRes('error', 400, {
210-
'--error': 'bad len:' + newLen
220+
return makeRes(res.body, 400, {
221+
'--error': `bad len: ${newLen}, except: ${rawLen}`,
222+
'access-control-expose-headers': '--error',
211223
})
212224
}
213225

@@ -220,12 +232,12 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
220232

221233
resHdrNew.set('access-control-expose-headers', expose)
222234
resHdrNew.set('access-control-allow-origin', '*')
223-
resHdrNew.set('vary', vary)
224235
resHdrNew.set('--s', status)
225236
resHdrNew.set('--ver', JS_VER)
226237

227238
resHdrNew.delete('content-security-policy')
228239
resHdrNew.delete('content-security-policy-report-only')
240+
resHdrNew.delete('clear-site-data')
229241

230242
if (status === 301 ||
231243
status === 302 ||

0 commit comments

Comments
 (0)