ニジカ投稿局 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.

plugins.ts 5.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import express from 'express'
  2. import { join } from 'path'
  3. import { getCompleteLocale, is18nLocale } from '@peertube/peertube-core-utils'
  4. import { HttpStatusCode, PluginType } from '@peertube/peertube-models'
  5. import { isProdInstance } from '@peertube/peertube-node-utils'
  6. import { logger } from '@server/helpers/logger.js'
  7. import { CONFIG } from '@server/initializers/config.js'
  8. import { optionalAuthenticate } from '@server/middlewares/auth.js'
  9. import { buildRateLimiter } from '@server/middlewares/index.js'
  10. import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants.js'
  11. import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager.js'
  12. import { getExternalAuthValidator, getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins.js'
  13. import { serveThemeCSSValidator } from '../middlewares/validators/themes.js'
  14. const sendFileOptions = {
  15. maxAge: '30 days',
  16. immutable: isProdInstance()
  17. }
  18. const pluginsRouter = express.Router()
  19. const pluginsRateLimiter = buildRateLimiter({
  20. windowMs: CONFIG.RATES_LIMIT.PLUGINS.WINDOW_MS,
  21. max: CONFIG.RATES_LIMIT.PLUGINS.MAX
  22. })
  23. pluginsRouter.get('/plugins/global.css',
  24. pluginsRateLimiter,
  25. servePluginGlobalCSS
  26. )
  27. pluginsRouter.get('/plugins/translations/:locale.json',
  28. pluginsRateLimiter,
  29. getPluginTranslations
  30. )
  31. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName',
  32. pluginsRateLimiter,
  33. getPluginValidator(PluginType.PLUGIN),
  34. getExternalAuthValidator,
  35. handleAuthInPlugin
  36. )
  37. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
  38. pluginsRateLimiter,
  39. getPluginValidator(PluginType.PLUGIN),
  40. pluginStaticDirectoryValidator,
  41. servePluginStaticDirectory
  42. )
  43. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
  44. pluginsRateLimiter,
  45. getPluginValidator(PluginType.PLUGIN),
  46. pluginStaticDirectoryValidator,
  47. servePluginClientScripts
  48. )
  49. pluginsRouter.use('/plugins/:pluginName/router',
  50. pluginsRateLimiter,
  51. getPluginValidator(PluginType.PLUGIN, false),
  52. optionalAuthenticate,
  53. servePluginCustomRoutes
  54. )
  55. pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
  56. pluginsRateLimiter,
  57. getPluginValidator(PluginType.PLUGIN),
  58. optionalAuthenticate,
  59. servePluginCustomRoutes
  60. )
  61. pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
  62. pluginsRateLimiter,
  63. getPluginValidator(PluginType.THEME),
  64. pluginStaticDirectoryValidator,
  65. servePluginStaticDirectory
  66. )
  67. pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
  68. pluginsRateLimiter,
  69. getPluginValidator(PluginType.THEME),
  70. pluginStaticDirectoryValidator,
  71. servePluginClientScripts
  72. )
  73. pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
  74. pluginsRateLimiter,
  75. serveThemeCSSValidator,
  76. serveThemeCSSDirectory
  77. )
  78. // ---------------------------------------------------------------------------
  79. export {
  80. pluginsRouter
  81. }
  82. // ---------------------------------------------------------------------------
  83. function servePluginGlobalCSS (req: express.Request, res: express.Response) {
  84. // Only cache requests that have a ?hash=... query param
  85. const globalCSSOptions = req.query.hash
  86. ? sendFileOptions
  87. : {}
  88. return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
  89. }
  90. function getPluginTranslations (req: express.Request, res: express.Response) {
  91. const locale = req.params.locale
  92. if (is18nLocale(locale)) {
  93. const completeLocale = getCompleteLocale(locale)
  94. const json = PluginManager.Instance.getTranslations(completeLocale)
  95. return res.json(json)
  96. }
  97. return res.status(HttpStatusCode.NOT_FOUND_404).end()
  98. }
  99. function servePluginStaticDirectory (req: express.Request, res: express.Response) {
  100. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  101. const staticEndpoint = req.params.staticEndpoint
  102. const [ directory, ...file ] = staticEndpoint.split('/')
  103. const staticPath = plugin.staticDirs[directory]
  104. if (!staticPath) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  105. const filepath = file.join('/')
  106. return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
  107. }
  108. function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
  109. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  110. const router = PluginManager.Instance.getRouter(plugin.npmName)
  111. if (!router) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  112. return router(req, res, next)
  113. }
  114. function servePluginClientScripts (req: express.Request, res: express.Response) {
  115. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  116. const staticEndpoint = req.params.staticEndpoint
  117. const file = plugin.clientScripts[staticEndpoint]
  118. if (!file) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  119. return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
  120. }
  121. function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
  122. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  123. const staticEndpoint = req.params.staticEndpoint
  124. if (plugin.css.includes(staticEndpoint) === false) {
  125. return res.status(HttpStatusCode.NOT_FOUND_404).end()
  126. }
  127. return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
  128. }
  129. function handleAuthInPlugin (req: express.Request, res: express.Response) {
  130. const authOptions = res.locals.externalAuth
  131. try {
  132. logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName)
  133. authOptions.onAuthRequest(req, res)
  134. } catch (err) {
  135. logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err })
  136. }
  137. }