ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

well-known.ts 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import cors from 'cors'
  2. import express from 'express'
  3. import { join } from 'path'
  4. import { asyncMiddleware, buildRateLimiter, handleStaticError, webfingerValidator } from '@server/middlewares/index.js'
  5. import { root } from '@peertube/peertube-node-utils'
  6. import { CONFIG } from '../initializers/config.js'
  7. import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants.js'
  8. import { cacheRoute } from '../middlewares/cache/cache.js'
  9. const wellKnownRouter = express.Router()
  10. const wellKnownRateLimiter = buildRateLimiter({
  11. windowMs: CONFIG.RATES_LIMIT.WELL_KNOWN.WINDOW_MS,
  12. max: CONFIG.RATES_LIMIT.WELL_KNOWN.MAX
  13. })
  14. wellKnownRouter.use(cors())
  15. wellKnownRouter.get('/.well-known/webfinger',
  16. wellKnownRateLimiter,
  17. asyncMiddleware(webfingerValidator),
  18. webfingerController
  19. )
  20. wellKnownRouter.get('/.well-known/security.txt',
  21. wellKnownRateLimiter,
  22. cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT),
  23. (_, res: express.Response) => {
  24. res.type('text/plain')
  25. return res.send(CONFIG.INSTANCE.SECURITYTXT)
  26. }
  27. )
  28. // nodeinfo service
  29. wellKnownRouter.use('/.well-known/nodeinfo',
  30. wellKnownRateLimiter,
  31. cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
  32. (_, res: express.Response) => {
  33. return res.json({
  34. links: [
  35. {
  36. rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
  37. href: WEBSERVER.URL + '/nodeinfo/2.0.json'
  38. },
  39. {
  40. rel: 'https://www.w3.org/ns/activitystreams#Application',
  41. href: WEBSERVER.URL + '/accounts/peertube'
  42. }
  43. ]
  44. })
  45. }
  46. )
  47. // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
  48. wellKnownRouter.use('/.well-known/dnt-policy.txt',
  49. wellKnownRateLimiter,
  50. cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY),
  51. (_, res: express.Response) => {
  52. res.type('text/plain')
  53. return res.sendFile(join(root(), 'dist/core/static/dnt-policy/dnt-policy-1.0.txt'))
  54. }
  55. )
  56. // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
  57. wellKnownRouter.use('/.well-known/dnt/',
  58. wellKnownRateLimiter,
  59. (_, res: express.Response) => {
  60. res.json({ tracking: 'N' })
  61. }
  62. )
  63. wellKnownRouter.use('/.well-known/change-password',
  64. wellKnownRateLimiter,
  65. (_, res: express.Response) => {
  66. res.redirect('/my-account/settings')
  67. }
  68. )
  69. wellKnownRouter.use('/.well-known/host-meta',
  70. wellKnownRateLimiter,
  71. (_, res: express.Response) => {
  72. res.type('application/xml')
  73. const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
  74. '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
  75. ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
  76. '</XRD>'
  77. res.send(xml).end()
  78. }
  79. )
  80. wellKnownRouter.use('/.well-known/',
  81. wellKnownRateLimiter,
  82. cacheRoute(ROUTE_CACHE_LIFETIME.WELL_KNOWN),
  83. express.static(CONFIG.STORAGE.WELL_KNOWN_DIR, { fallthrough: false }),
  84. handleStaticError
  85. )
  86. // ---------------------------------------------------------------------------
  87. export {
  88. wellKnownRouter
  89. }
  90. // ---------------------------------------------------------------------------
  91. function webfingerController (req: express.Request, res: express.Response) {
  92. const actor = res.locals.actorUrl
  93. const json = {
  94. subject: req.query.resource,
  95. aliases: [ actor.url ],
  96. links: [
  97. {
  98. rel: 'self',
  99. type: 'application/activity+json',
  100. href: actor.url
  101. },
  102. {
  103. rel: 'http://ostatus.org/schema/1.0/subscribe',
  104. template: WEBSERVER.URL + '/remote-interaction?uri={uri}'
  105. }
  106. ]
  107. }
  108. return res.json(json)
  109. }