index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. 'use strict'
  2. /**
  3. * static files (404.html, sw.js, conf.js)
  4. */
  5. const ASSET_URL = 'https://zjcqoo.github.io'
  6. const JS_VER = 10
  7. const MAX_RETRY = 1
  8. /** @type {RequestInit} */
  9. const PREFLIGHT_INIT = {
  10. status: 204,
  11. headers: new Headers({
  12. 'access-control-allow-origin': '*',
  13. 'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
  14. 'access-control-max-age': '1728000',
  15. }),
  16. }
  17. /**
  18. * @param {any} body
  19. * @param {number} status
  20. * @param {Object<string, string>} headers
  21. */
  22. function makeRes(body, status = 200, headers = {}) {
  23. headers['--ver'] = JS_VER
  24. headers['access-control-allow-origin'] = '*'
  25. return new Response(body, {status, headers})
  26. }
  27. addEventListener('fetch', e => {
  28. const ret = fetchHandler(e)
  29. .catch(err => makeRes('cfworker error:\n' + err.stack, 502))
  30. e.respondWith(ret)
  31. })
  32. /**
  33. * @param {FetchEvent} e
  34. */
  35. async function fetchHandler(e) {
  36. const req = e.request
  37. const urlStr = req.url
  38. const urlObj = new URL(urlStr)
  39. const {pathname} = urlObj
  40. if (urlObj.protocol === 'http:') {
  41. urlObj.protocol = 'https:'
  42. return makeRes('', 301, {
  43. 'strict-transport-security': 'max-age=99999999; includeSubDomains; preload',
  44. 'location': urlObj.href,
  45. })
  46. }
  47. if (pathname.startsWith('/http/')) {
  48. return httpHandler(req, pathname)
  49. }
  50. switch (pathname) {
  51. case '/http':
  52. return makeRes('请更新 cfworker 到最新版本!')
  53. case '/ws':
  54. return makeRes('not support', 400)
  55. case '/works':
  56. return makeRes('it works')
  57. default:
  58. // static files
  59. return fetch(ASSET_URL + pathname)
  60. }
  61. }
  62. /**
  63. * @param {Request} req
  64. * @param {string} pathname
  65. */
  66. function httpHandler(req, pathname) {
  67. const reqHdrRaw = req.headers
  68. if (reqHdrRaw.has('x-jsproxy')) {
  69. return Response.error()
  70. }
  71. // preflight
  72. if (req.method === 'OPTIONS' &&
  73. reqHdrRaw.has('access-control-request-headers')
  74. ) {
  75. return new Response(null, PREFLIGHT_INIT)
  76. }
  77. let acehOld = false
  78. let rawSvr = ''
  79. let rawLen = ''
  80. let rawEtag = ''
  81. const reqHdrNew = new Headers(reqHdrRaw)
  82. reqHdrNew.set('x-jsproxy', '1')
  83. // 此处逻辑和 http-dec-req-hdr.lua 大致相同
  84. // https://github.com/EtherDream/jsproxy/blob/master/lua/http-dec-req-hdr.lua
  85. const refer = reqHdrNew.get('referer')
  86. const query = refer.substr(refer.indexOf('?') + 1)
  87. const param = new URLSearchParams(query)
  88. for (const [k, v] of Object.entries(param)) {
  89. if (k.substr(0, 2) === '--') {
  90. // 系统信息
  91. switch (k.substr(2)) {
  92. case 'aceh':
  93. acehOld = true
  94. break
  95. case 'raw-info':
  96. [rawSvr, rawLen, rawEtag] = v.split('|')
  97. break
  98. }
  99. } else {
  100. // 还原 HTTP 请求头
  101. if (v) {
  102. reqHdrNew.set(k, v)
  103. } else {
  104. reqHdrNew.delete(k)
  105. }
  106. }
  107. }
  108. if (!param.has('referer')) {
  109. reqHdrNew.delete('referer')
  110. }
  111. let targetUrlStr = pathname.substr('/http/'.length)
  112. // cfworker 会把路径中的 `//` 合并成 `/`
  113. const m = targetUrlStr.match(/^https?:(\/+)/)
  114. if (m && m[1] !== '//') {
  115. targetUrlStr = targetUrlStr.replace('/', '//')
  116. }
  117. try {
  118. var targetUrlObj = new URL(targetUrlStr)
  119. } catch (err) {
  120. return makeRes('invalid url: ' + targetUrlStr, 403)
  121. }
  122. /** @type {RequestInit} */
  123. const reqInit = {
  124. method: req.method,
  125. headers: reqHdrNew,
  126. redirect: 'manual',
  127. }
  128. if (req.method === 'POST') {
  129. reqInit.body = req.body
  130. }
  131. return proxy(targetUrlObj, reqInit, acehOld, rawLen, 0)
  132. }
  133. /**
  134. *
  135. * @param {URL} urlObj
  136. * @param {RequestInit} reqInit
  137. * @param {number} retryTimes
  138. */
  139. async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
  140. const res = await fetch(urlObj.href, reqInit)
  141. const resHdrOld = res.headers
  142. const resHdrNew = new Headers(resHdrOld)
  143. let expose = '*'
  144. for (const [k, v] of resHdrOld.entries()) {
  145. if (k === 'access-control-allow-origin' ||
  146. k === 'access-control-expose-headers' ||
  147. k === 'location' ||
  148. k === 'set-cookie'
  149. ) {
  150. const x = '--' + k
  151. resHdrNew.set(x, v)
  152. if (acehOld) {
  153. expose = expose + ',' + x
  154. }
  155. resHdrNew.delete(k)
  156. }
  157. else if (acehOld &&
  158. k !== 'cache-control' &&
  159. k !== 'content-language' &&
  160. k !== 'content-type' &&
  161. k !== 'expires' &&
  162. k !== 'last-modified' &&
  163. k !== 'pragma'
  164. ) {
  165. expose = expose + ',' + k
  166. }
  167. }
  168. if (acehOld) {
  169. expose = expose + ',--s'
  170. resHdrNew.set('--t', '1')
  171. }
  172. // verify
  173. if (rawLen) {
  174. const newLen = resHdrOld.get('content-length') || ''
  175. const badLen = (rawLen !== newLen)
  176. if (badLen) {
  177. if (retryTimes < MAX_RETRY) {
  178. urlObj = await parseYtVideoRedir(urlObj, newLen, res)
  179. if (urlObj) {
  180. return proxy(urlObj, reqInit, acehOld, rawLen, retryTimes + 1)
  181. }
  182. }
  183. return makeRes(res.body, 400, {
  184. '--error': `bad len: ${newLen}, except: ${rawLen}`,
  185. 'access-control-expose-headers': '--error',
  186. })
  187. }
  188. if (retryTimes > 1) {
  189. resHdrNew.set('--retry', retryTimes)
  190. }
  191. }
  192. let status = res.status
  193. resHdrNew.set('access-control-expose-headers', expose)
  194. resHdrNew.set('access-control-allow-origin', '*')
  195. resHdrNew.set('--s', status)
  196. resHdrNew.set('--ver', JS_VER)
  197. resHdrNew.delete('content-security-policy')
  198. resHdrNew.delete('content-security-policy-report-only')
  199. resHdrNew.delete('clear-site-data')
  200. if (status === 301 ||
  201. status === 302 ||
  202. status === 303 ||
  203. status === 307 ||
  204. status === 308
  205. ) {
  206. status = status + 10
  207. }
  208. return new Response(res.body, {
  209. status,
  210. headers: resHdrNew,
  211. })
  212. }
  213. /**
  214. * @param {URL} urlObj
  215. */
  216. function isYtUrl(urlObj) {
  217. return (
  218. urlObj.host.endsWith('.googlevideo.com') &&
  219. urlObj.pathname.startsWith('/videoplayback')
  220. )
  221. }
  222. /**
  223. * @param {URL} urlObj
  224. * @param {number} newLen
  225. * @param {Response} res
  226. */
  227. async function parseYtVideoRedir(urlObj, newLen, res) {
  228. if (newLen > 2000) {
  229. return null
  230. }
  231. if (!isYtUrl(urlObj)) {
  232. return null
  233. }
  234. try {
  235. const data = await res.text()
  236. urlObj = new URL(data)
  237. } catch (err) {
  238. return null
  239. }
  240. if (!isYtUrl(urlObj)) {
  241. return null
  242. }
  243. return urlObj
  244. }