-
Notifications
You must be signed in to change notification settings - Fork 155
/
sw.js
461 lines (382 loc) · 9.61 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import * as path from './path.js'
import * as route from './route.js'
import * as urlx from './urlx.js'
import * as util from './util.js'
import * as cookie from './cookie.js'
import * as network from './network.js'
import * as MSG from './msg.js'
import * as jsfilter from './jsfilter.js'
import * as inject from './inject.js'
let gConf
const MAX_REDIR = 5
/** @type {ServiceWorkerGlobalScope} */
// @ts-ignore
const global = self
const clients = global.clients
/**
* @param {*} target
* @param {number} cmd
* @param {*=} val
*/
function sendMsg(target, cmd, val) {
if (target) {
target.postMessage([cmd, val])
} else {
console.warn('invalid target', cmd, val)
}
}
// 也可以用 clientId 关联,但兼容性不高
let pageCounter = 0
/** @type {Map<number, [Function, number]>} */
const pageWaitMap = new Map()
function genPageId() {
return ++pageCounter
}
/**
* @param {number} pageId
*/
function pageWait(pageId) {
return new Promise(cb => {
// 设置最大等待时间
// 有些页面不会执行 JS(例如查看源文件),导致永久等待
const timer = setTimeout(_ => {
pageWaitMap.delete(pageId)
cb(false)
}, 2000)
pageWaitMap.set(pageId, [cb, timer])
})
}
/**
* @param {number} id
* @param {boolean} isDone
*/
function pageNotify(id, isDone) {
const arr = pageWaitMap.get(id)
if (!arr) {
console.warn('[jsproxy] unknown page id:', id)
return
}
const [cb, timer] = arr
if (isDone) {
pageWaitMap.delete(id)
cb(true)
} else {
// 页面已开始初始化,关闭定时器
clearTimeout(timer)
}
}
function makeErrRes(desc) {
return new Response(desc, {
status: 500
})
}
/**
* @param {Response} res
* @param {ResponseInit} resOpt
* @param {URL} urlObj
*/
function processHtml(res, resOpt, urlObj) {
const reader = res.body.getReader()
let injected = false
const stream = new ReadableStream({
async pull(controller) {
if (!injected) {
injected = true
// 注入页面顶部的代码
const pageId = genPageId()
const buf = inject.getHtmlCode(urlObj, pageId)
controller.enqueue(buf)
// 留一些时间给页面做异步初始化
const done = await pageWait(pageId)
if (!done) {
console.warn('[jsproxy] page wait timeout. id: %d url: %s',
pageId, urlObj.href)
}
}
const r = await reader.read()
if (r.done) {
controller.close()
} else {
controller.enqueue(r.value)
}
}
})
return new Response(stream, resOpt)
}
/**
* @param {ArrayBuffer} buf
* @param {string} charset
*/
function processJs(buf, charset) {
const u8 = new Uint8Array(buf)
const ret = jsfilter.parseBin(u8, charset) || u8
return util.concatBufs([inject.getWorkerCode(), ret])
}
/**
* @param {*} cmd
* @param {*} msg
* @param {string=} srcId
*/
async function sendMsgToPages(cmd, msg, srcId) {
// 通知页面更新 Cookie
const pages = await clients.matchAll({type: 'window'})
for (const page of pages) {
if (page.frameType !== 'top-level') {
continue
}
if (srcId && page.id === srcId) {
continue
}
sendMsg(page, cmd, msg)
}
}
/** @type Map<string, string> */
const idUrlMap = new Map()
/**
* @param {string} id
*/
async function getUrlByClientId(id) {
const client = await clients.get(id)
if (!client) {
return
}
const urlStr = urlx.decUrlStrAbs(client.url)
idUrlMap.set(id, urlStr)
return urlStr
}
/**
* @param {Request} req
* @param {URL} urlObj
* @param {URL} cliUrlObj
* @param {number} redirNum
* @returns {Promise<Response>}
*/
async function forward(req, urlObj, cliUrlObj, redirNum) {
const {
res, status, headers, cookies
} = await network.launch(req, urlObj, cliUrlObj)
if (cookies) {
sendMsgToPages(MSG.SW_COOKIE_PUSH, cookies)
}
const resOpt = {status, headers}
// 空响应
// https://fetch.spec.whatwg.org/#statuses
if (status === 101 ||
status === 204 ||
status === 205 ||
status === 304
) {
return new Response(null, resOpt)
}
// 处理重定向
if (status === 301 ||
status === 302 ||
status === 303 ||
status === 307 ||
status === 308
) {
const locStr = headers.get('location')
const locObj = locStr && urlx.newUrl(locStr, urlObj)
if (locObj) {
// 跟随模式,返回最终数据
if (req.redirect === 'follow') {
if (++redirNum === MAX_REDIR) {
return makeErrRes('too many redirects')
}
return forward(req, locObj, cliUrlObj, redirNum)
}
// 不跟随模式(例如页面跳转),返回 30X 状态
headers.set('location', urlx.encUrlObj(locObj))
}
// firefox, safari 保留内容会提示页面损坏
return new Response(null, resOpt)
}
//
// 提取 mime 和 charset(不存在则为 undefined)
// 可能存在多个段,并且值可能包含引号。例如:
// content-type: text/html; ...; charset="gbk"
//
const ctVal = headers.get('content-type') || ''
const [, mime, charset] = ctVal
.toLocaleLowerCase()
.match(/([^;]*)(?:.*?charset=['"]?([^'"]+))?/)
const type = req.destination
if (type === 'script' ||
type === 'worker' ||
type === 'sharedworker'
) {
const buf = await res.arrayBuffer()
const ret = processJs(buf, charset)
headers.set('content-type', 'text/javascript')
return new Response(ret, resOpt)
}
if (req.mode === 'navigate' && mime === 'text/html') {
return processHtml(res, resOpt, urlObj)
}
return new Response(res.body, resOpt)
}
async function proxy(e, urlObj) {
// 使用 e.resultingClientId 有问题
const id = e.clientId
let cliUrlStr
if (id) {
cliUrlStr = idUrlMap.get(id) || await getUrlByClientId(id)
}
if (!cliUrlStr) {
cliUrlStr = urlObj.href
}
const cliUrlObj = new URL(cliUrlStr)
try {
return await forward(e.request, urlObj, cliUrlObj, 0)
} catch (err) {
console.error(err)
return makeErrRes(err.stack)
}
}
/**
* @param {FetchEvent} e
*/
async function onFetch(e) {
if (!gConf) {
gConf = await initConf()
}
const req = e.request
const urlStr = urlx.delHash(req.url)
// 首页(例如 https://zjcqoo.github.io/index.html)
// 配置(例如 https://zjcqoo.github.io/conf.js)
if (urlStr === path.ROOT ||
urlStr === path.HOME ||
urlStr === path.CONF
) {
return fetch(urlStr)
}
// 注入页面的脚本(例如 https://zjcqoo.github.io/helper.js)
if (urlStr === path.HELPER) {
return fetch(self['__FILE__'])
}
// 静态资源(例如 https://zjcqoo.github.io/assets/ico/google.png)
if (urlStr.startsWith(path.ASSETS)) {
const filePath = urlStr.substr(path.ASSETS.length)
return fetch(gConf.assets_cdn + filePath)
}
const targetUrlStr = urlx.decUrlStrAbs(urlStr)
const targetUrlObj = urlx.newUrl(targetUrlStr)
if (targetUrlObj) {
return proxy(e, targetUrlObj)
}
return makeErrRes('invalid url: ' + targetUrlStr)
}
function updateConf(conf) {
route.setConf(conf)
network.setConf(conf)
}
async function fetchConf() {
const res = await fetch('conf.js')
const txt = await res.text()
let ret
self['jsproxy_config'] = function(v) {
ret = v
}
Function(txt)()
return ret
}
async function loadConf() {
const cache = await caches.open("sys")
const req = new Request("/conf.json")
const res = await cache.match(req)
if (res) {
return res.json()
}
}
async function saveConf(conf) {
const json = JSON.stringify(conf)
const cache = await caches.open("sys")
const req = new Request("/conf.json")
const res = new Response(json);
return cache.put(req, res)
}
let initing = false
let initingQueue = []
async function initConf() {
if (initing) {
return new Promise(f => {
initingQueue.push(f)
})
}
initing = true
let conf
try {
conf = await loadConf()
} catch (err) {
console.warn('loadConf fail:', err)
}
if (!conf) {
conf = await fetchConf()
console.log('fetchConf:', conf)
await saveConf()
}
updateConf(conf)
network.loadManifest()
initing = false
initingQueue.forEach(f => f(conf))
return conf
}
global.addEventListener('fetch', e => {
e.respondWith(onFetch(e))
})
global.addEventListener('message', e => {
// console.log('sw msg:', e.data)
const [cmd, val] = e.data
const src = e.source
switch (cmd) {
case MSG.PAGE_COOKIE_PUSH:
cookie.set(val)
// @ts-ignore
sendMsgToPages(MSG.SW_COOKIE_PUSH, [val], src.id)
break
case MSG.PAGE_INFO_PULL:
// console.log('SW MSG.COOKIE_PULL:', src.id)
sendMsg(src, MSG.SW_INFO_PUSH, {
cookies: cookie.getNonHttpOnlyItems(),
conf: gConf,
})
break
case MSG.PAGE_INIT_BEG:
// console.log('SW MSG.PAGE_INIT_BEG:', val)
pageNotify(val, false)
break
case MSG.PAGE_INIT_END:
// console.log('SW MSG.PAGE_INIT_END:', val)
pageNotify(val, true)
break
case MSG.PAGE_CONF_GET:
if (gConf) {
sendMsg(src, MSG.SW_CONF_RETURN, gConf)
} else {
initConf().then(conf => {
gConf = conf
sendMsg(src, MSG.SW_CONF_RETURN, gConf)
})
}
break
case MSG.PAGE_CONF_SET:
gConf = val
saveConf(gConf)
updateConf(gConf)
sendMsgToPages(MSG.SW_CONF_CHANGE, gConf)
break
case MSG.PAGE_READY_CHECK:
sendMsg(src, MSG.SW_READY)
break
}
})
global.addEventListener('install', e => {
console.log('oninstall:', e)
e.waitUntil(global.skipWaiting())
})
global.addEventListener('activate', e => {
console.log('onactivate:', e)
sendMsgToPages(MSG.SW_READY, 1)
e.waitUntil(clients.claim())
})
console.log('[jsproxy] sw inited')