app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const http = require('http')
  2. const fsPromises = require('fs').promises
  3. const path = require('path')
  4. const crypto = require('crypto')
  5. const sleep = require('util').promisify(setTimeout)
  6. const argvIndexOfFile = process.argv.indexOf(__filename)
  7. let PORT = parseInt(process.argv[argvIndexOfFile + 1])
  8. if (isNaN(PORT)) {
  9. PORT = 8000
  10. }
  11. let DATA_INSTANT = 0
  12. let SLEEP_TIME = 200
  13. let CACHE_MAX_AGE = 0
  14. let USE_WHITELIST = 0
  15. function handleCookies(req) {
  16. const cookies = req.headers.cookie
  17. if (!cookies) {
  18. return
  19. }
  20. cookies.split('; ').map((cookie) => {
  21. const [key, value] = cookie.split('=')
  22. if (key != 'instantpage_test') {
  23. return
  24. }
  25. const cookieValueSplit = value.split(',').map((param) => parseInt(param))
  26. DATA_INSTANT = cookieValueSplit[0]
  27. SLEEP_TIME = cookieValueSplit[1]
  28. CACHE_MAX_AGE = cookieValueSplit[2]
  29. USE_WHITELIST = cookieValueSplit[3]
  30. })
  31. }
  32. function sha384(data) {
  33. const hash = crypto.createHash('sha384')
  34. hash.update(data)
  35. return hash.digest('base64')
  36. }
  37. async function requestListener(req, res) {
  38. let headers = {
  39. 'Content-Type': 'text/html',
  40. }
  41. let pathString = req.url.substr(1)
  42. let page = parseInt(pathString)
  43. if (pathString == '') {
  44. page = 1
  45. }
  46. let content = ''
  47. const jsContent = await fsPromises.readFile(path.resolve(__dirname, '../instantpage.js'))
  48. const jsHash = sha384(jsContent)
  49. if (pathString == 'instantpage.js') {
  50. headers['Content-Type'] = 'text/javascript'
  51. content += jsContent
  52. }
  53. else if (!isNaN(page)) {
  54. handleCookies(req)
  55. await sleep(SLEEP_TIME)
  56. if (CACHE_MAX_AGE) {
  57. headers['Cache-Control'] = `max-age=${CACHE_MAX_AGE}`
  58. }
  59. content += await fsPromises.readFile(path.resolve(__dirname, 'header.html'))
  60. if (!DATA_INSTANT) {
  61. content = content.replace('<body>', '<body data-instant-allow-query-string data-instant-allow-external-links>')
  62. }
  63. if (USE_WHITELIST) {
  64. content = content.replace('<body', '<body data-instant-whitelist')
  65. }
  66. dataInstantAttribute = DATA_INSTANT || USE_WHITELIST ? `data-instant` : ``
  67. content = content.replace(':checked_aqsael', DATA_INSTANT ? 'checked' : '')
  68. content = content.replace(':checked_whitelist', USE_WHITELIST ? 'checked' : '')
  69. content = content.replace(':value_sleep', `value="${SLEEP_TIME}"`)
  70. content = content.replace(':value_cacheAge', `value="${CACHE_MAX_AGE}"`)
  71. content += `<h1>Page ${page}</h1>`
  72. for (let i = 1; i <= 3; i++) {
  73. if (page != i) {
  74. content += `<a href="/${i}?${Math.random()}" ${dataInstantAttribute}><span>Page ${i}</span></a>`
  75. }
  76. }
  77. content += `<a href="/${page}?${Math.random()}" target="_blank" ${dataInstantAttribute}><span>Opens in a new tab</span></a>`
  78. content += `<a href="/${page}?${Math.random()}#anchor" ${dataInstantAttribute}><span>Other page anchor</span></a>`
  79. content += `<a href="${req.url}#anchor" id="anchor"><span>Same-page anchor</span></a>`
  80. content += `<a href="/${page}?${Math.random()}" data-no-instant><span>Manually blacklisted link</span></a>`
  81. content += `<a href="/${page}?${Math.random()}"><span>Non-whitelisted link</span></a>`
  82. content += `<a href="https://www.google.com/" ${dataInstantAttribute}><span>External link</span></a>`
  83. content += `<a><span>&lt;a&gt; without <code>href</code></span></a>`
  84. content += `<a href="file:///C:/"><span>file: link</span></a>`
  85. let footer = await fsPromises.readFile(path.resolve(__dirname, 'footer.html'))
  86. footer = footer.toString().replace('__HASH__', jsHash)
  87. content += footer
  88. }
  89. res.writeHead(200, headers)
  90. res.write(content)
  91. res.end()
  92. }
  93. http.createServer(requestListener).listen(PORT)
  94. console.log(`-> Running on http://127.0.0.1:${PORT}/`)