server.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import { registerOpentelemetryTracing } from '@server/lib/opentelemetry/tracing.js'
  2. await registerOpentelemetryTracing()
  3. process.title = 'peertube'
  4. // ----------- Core checker -----------
  5. import { checkMissedConfig, checkFFmpeg, checkNodeVersion } from './core/initializers/checker-before-init.js'
  6. // Do not use barrels because we don't want to load all modules here (we need to initialize database first)
  7. import { CONFIG } from './core/initializers/config.js'
  8. import { API_VERSION, WEBSERVER, loadLanguages } from './core/initializers/constants.js'
  9. import { logger } from './core/helpers/logger.js'
  10. const missed = checkMissedConfig()
  11. if (missed.length !== 0) {
  12. logger.error('Your configuration files miss keys: ' + missed)
  13. process.exit(-1)
  14. }
  15. checkFFmpeg(CONFIG)
  16. .catch(err => {
  17. logger.error('Error in ffmpeg check.', { err })
  18. process.exit(-1)
  19. })
  20. try {
  21. checkNodeVersion()
  22. } catch (err) {
  23. logger.error('Error in NodeJS check.', { err })
  24. process.exit(-1)
  25. }
  26. import { checkConfig, checkActivityPubUrls, checkFFmpegVersion } from './core/initializers/checker-after-init.js'
  27. try {
  28. checkConfig()
  29. } catch (err) {
  30. logger.error('Config error.', { err })
  31. process.exit(-1)
  32. }
  33. // ----------- Database -----------
  34. // Initialize database and models
  35. import { initDatabaseModels, checkDatabaseConnectionOrDie } from './core/initializers/database.js'
  36. checkDatabaseConnectionOrDie()
  37. import { migrate } from './core/initializers/migrator.js'
  38. migrate()
  39. .then(() => initDatabaseModels(false))
  40. .then(() => startApplication())
  41. .catch(err => {
  42. logger.error('Cannot start application.', { err })
  43. process.exit(-1)
  44. })
  45. // ----------- Initialize -----------
  46. loadLanguages()
  47. .catch(err => logger.error('Cannot load languages', { err }))
  48. // Express configuration
  49. import express from 'express'
  50. import morgan, { token } from 'morgan'
  51. import cors from 'cors'
  52. import cookieParser from 'cookie-parser'
  53. import { frameguard } from 'helmet'
  54. import { parse } from 'useragent'
  55. import anonymize from 'ip-anonymize'
  56. import { program as cli } from 'commander'
  57. const app = express().disable('x-powered-by')
  58. // Trust our proxy (IP forwarding...)
  59. app.set('trust proxy', CONFIG.TRUST_PROXY)
  60. app.use((_req, res, next) => {
  61. // OpenTelemetry
  62. res.locals.requestStart = Date.now()
  63. if (CONFIG.SECURITY.POWERED_BY_HEADER.ENABLED === true) {
  64. res.setHeader('x-powered-by', 'PeerTube')
  65. }
  66. return next()
  67. })
  68. // Security middleware
  69. import { baseCSP } from './core/middlewares/csp.js'
  70. if (CONFIG.CSP.ENABLED) {
  71. app.use(baseCSP)
  72. }
  73. if (CONFIG.SECURITY.FRAMEGUARD.ENABLED) {
  74. app.use(frameguard({
  75. action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts
  76. }))
  77. }
  78. // ----------- PeerTube modules -----------
  79. import { installApplication } from './core/initializers/installer.js'
  80. import { Emailer } from './core/lib/emailer.js'
  81. import { JobQueue } from './core/lib/job-queue/index.js'
  82. import {
  83. activityPubRouter,
  84. apiRouter,
  85. miscRouter,
  86. clientsRouter,
  87. feedsRouter,
  88. staticRouter,
  89. wellKnownRouter,
  90. lazyStaticRouter,
  91. servicesRouter,
  92. objectStorageProxyRouter,
  93. pluginsRouter,
  94. trackerRouter,
  95. createWebsocketTrackerServer,
  96. sitemapRouter,
  97. downloadRouter
  98. } from './core/controllers/index.js'
  99. import { advertiseDoNotTrack } from './core/middlewares/dnt.js'
  100. import { apiFailMiddleware } from './core/middlewares/error.js'
  101. import { Redis } from './core/lib/redis.js'
  102. import { ActorFollowScheduler } from './core/lib/schedulers/actor-follow-scheduler.js'
  103. import { RemoveOldViewsScheduler } from './core/lib/schedulers/remove-old-views-scheduler.js'
  104. import { UpdateVideosScheduler } from './core/lib/schedulers/update-videos-scheduler.js'
  105. import { YoutubeDlUpdateScheduler } from './core/lib/schedulers/youtube-dl-update-scheduler.js'
  106. import { VideosRedundancyScheduler } from './core/lib/schedulers/videos-redundancy-scheduler.js'
  107. import { RemoveOldHistoryScheduler } from './core/lib/schedulers/remove-old-history-scheduler.js'
  108. import { AutoFollowIndexInstances } from './core/lib/schedulers/auto-follow-index-instances.js'
  109. import { RemoveDanglingResumableUploadsScheduler } from './core/lib/schedulers/remove-dangling-resumable-uploads-scheduler.js'
  110. import { VideoViewsBufferScheduler } from './core/lib/schedulers/video-views-buffer-scheduler.js'
  111. import { GeoIPUpdateScheduler } from './core/lib/schedulers/geo-ip-update-scheduler.js'
  112. import { RunnerJobWatchDogScheduler } from './core/lib/schedulers/runner-job-watch-dog-scheduler.js'
  113. import { isHTTPSignatureDigestValid } from './core/helpers/peertube-crypto.js'
  114. import { PeerTubeSocket } from './core/lib/peertube-socket.js'
  115. import { updateStreamingPlaylistsInfohashesIfNeeded } from './core/lib/hls.js'
  116. import { PluginsCheckScheduler } from './core/lib/schedulers/plugins-check-scheduler.js'
  117. import { PeerTubeVersionCheckScheduler } from './core/lib/schedulers/peertube-version-check-scheduler.js'
  118. import { Hooks } from './core/lib/plugins/hooks.js'
  119. import { PluginManager } from './core/lib/plugins/plugin-manager.js'
  120. import { LiveManager } from './core/lib/live/index.js'
  121. import { HttpStatusCode } from '@peertube/peertube-models'
  122. import { ServerConfigManager } from '@server/lib/server-config-manager.js'
  123. import { VideoViewsManager } from '@server/lib/views/video-views-manager.js'
  124. import { isTestOrDevInstance } from '@peertube/peertube-node-utils'
  125. import { OpenTelemetryMetrics } from '@server/lib/opentelemetry/metrics.js'
  126. import { ApplicationModel } from '@server/models/application/application.js'
  127. import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler.js'
  128. import { RemoveExpiredUserExportsScheduler } from '@server/lib/schedulers/remove-expired-user-exports-scheduler.js'
  129. // ----------- Command line -----------
  130. cli
  131. .option('--no-client', 'Start PeerTube without client interface')
  132. .option('--no-plugins', 'Start PeerTube without plugins/themes enabled')
  133. .option('--benchmark-startup', 'Automatically stop server when initialized')
  134. .parse(process.argv)
  135. // ----------- App -----------
  136. // Enable CORS for develop
  137. if (isTestOrDevInstance()) {
  138. app.use(cors({
  139. origin: '*',
  140. exposedHeaders: 'Retry-After',
  141. credentials: true
  142. }))
  143. }
  144. // HTTP logging
  145. if (CONFIG.LOG.LOG_HTTP_REQUESTS) {
  146. token('remote-addr', (req: express.Request) => {
  147. if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
  148. return anonymize(req.ip, 16, 16)
  149. }
  150. return req.ip
  151. })
  152. token('user-agent', (req: express.Request) => {
  153. if (req.get('DNT') === '1') {
  154. return parse(req.get('user-agent')).family
  155. }
  156. return req.get('user-agent')
  157. })
  158. app.use(morgan('combined', {
  159. stream: {
  160. write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] })
  161. },
  162. skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
  163. }))
  164. }
  165. // Add .fail() helper to response
  166. app.use(apiFailMiddleware)
  167. // For body requests
  168. app.use(express.urlencoded({ extended: false }))
  169. app.use(express.json({
  170. type: [ 'application/json', 'application/*+json' ],
  171. limit: '500kb',
  172. verify: (req: express.Request, res: express.Response, buf: Buffer) => {
  173. const valid = isHTTPSignatureDigestValid(buf, req)
  174. if (valid !== true) {
  175. res.fail({
  176. status: HttpStatusCode.FORBIDDEN_403,
  177. message: 'Invalid digest'
  178. })
  179. }
  180. }
  181. }))
  182. // W3C DNT Tracking Status
  183. app.use(advertiseDoNotTrack)
  184. // ----------- Open Telemetry -----------
  185. OpenTelemetryMetrics.Instance.init(app)
  186. // ----------- Views, routes and static files -----------
  187. app.use('/api/' + API_VERSION, apiRouter)
  188. // Services (oembed...)
  189. app.use('/services', servicesRouter)
  190. app.use('/', activityPubRouter)
  191. app.use('/', feedsRouter)
  192. app.use('/', trackerRouter)
  193. app.use('/', sitemapRouter)
  194. // Static files
  195. app.use('/', staticRouter)
  196. app.use('/', wellKnownRouter)
  197. app.use('/', miscRouter)
  198. app.use('/', downloadRouter)
  199. app.use('/', lazyStaticRouter)
  200. app.use('/', objectStorageProxyRouter)
  201. // Cookies for plugins and HTML
  202. app.use(cookieParser())
  203. // Plugins & themes
  204. app.use('/', pluginsRouter)
  205. // Client files, last valid routes!
  206. const cliOptions = cli.opts<{ client: boolean, plugins: boolean }>()
  207. if (cliOptions.client) app.use('/', clientsRouter)
  208. // ----------- Errors -----------
  209. // Catch unmatched routes
  210. app.use((_req, res: express.Response) => {
  211. res.status(HttpStatusCode.NOT_FOUND_404).end()
  212. })
  213. // Catch thrown errors
  214. app.use((err, _req, res: express.Response, _next) => {
  215. // Format error to be logged
  216. let error = 'Unknown error.'
  217. if (err) {
  218. error = err.stack || err.message || err
  219. }
  220. // Handling Sequelize error traces
  221. const sql = err?.parent ? err.parent.sql : undefined
  222. // Help us to debug SequelizeConnectionAcquireTimeoutError errors
  223. const activeRequests = err?.name === 'SequelizeConnectionAcquireTimeoutError' && typeof (process as any)._getActiveRequests !== 'function'
  224. ? (process as any)._getActiveRequests()
  225. : undefined
  226. logger.error('Error in controller.', { err: error, sql, activeRequests })
  227. return res.fail({
  228. status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
  229. message: err.message,
  230. type: err.name
  231. })
  232. })
  233. const { server, trackerServer } = createWebsocketTrackerServer(app)
  234. // ----------- Run -----------
  235. async function startApplication () {
  236. const port = CONFIG.LISTEN.PORT
  237. const hostname = CONFIG.LISTEN.HOSTNAME
  238. await installApplication()
  239. // Check activity pub urls are valid
  240. checkActivityPubUrls()
  241. .catch(err => {
  242. logger.error('Error in ActivityPub URLs checker.', { err })
  243. process.exit(-1)
  244. })
  245. checkFFmpegVersion()
  246. .catch(err => logger.error('Cannot check ffmpeg version', { err }))
  247. Redis.Instance.init()
  248. Emailer.Instance.init()
  249. await Promise.all([
  250. Emailer.Instance.checkConnection(),
  251. JobQueue.Instance.init(),
  252. ServerConfigManager.Instance.init()
  253. ])
  254. // Enable Schedulers
  255. ActorFollowScheduler.Instance.enable()
  256. UpdateVideosScheduler.Instance.enable()
  257. YoutubeDlUpdateScheduler.Instance.enable()
  258. VideosRedundancyScheduler.Instance.enable()
  259. RemoveOldHistoryScheduler.Instance.enable()
  260. RemoveOldViewsScheduler.Instance.enable()
  261. PluginsCheckScheduler.Instance.enable()
  262. PeerTubeVersionCheckScheduler.Instance.enable()
  263. AutoFollowIndexInstances.Instance.enable()
  264. RemoveDanglingResumableUploadsScheduler.Instance.enable()
  265. VideoChannelSyncLatestScheduler.Instance.enable()
  266. VideoViewsBufferScheduler.Instance.enable()
  267. GeoIPUpdateScheduler.Instance.enable()
  268. RunnerJobWatchDogScheduler.Instance.enable()
  269. RemoveExpiredUserExportsScheduler.Instance.enable()
  270. OpenTelemetryMetrics.Instance.registerMetrics({ trackerServer })
  271. PluginManager.Instance.init(server)
  272. // Before PeerTubeSocket init
  273. PluginManager.Instance.registerWebSocketRouter()
  274. PeerTubeSocket.Instance.init(server)
  275. VideoViewsManager.Instance.init()
  276. updateStreamingPlaylistsInfohashesIfNeeded()
  277. .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
  278. LiveManager.Instance.init()
  279. if (CONFIG.LIVE.ENABLED) await LiveManager.Instance.run()
  280. // Make server listening
  281. server.listen(port, hostname, async () => {
  282. if (cliOptions.plugins) {
  283. try {
  284. await PluginManager.Instance.rebuildNativePluginsIfNeeded()
  285. await PluginManager.Instance.registerPluginsAndThemes()
  286. } catch (err) {
  287. logger.error('Cannot register plugins and themes.', { err })
  288. }
  289. }
  290. ApplicationModel.updateNodeVersions()
  291. .catch(err => logger.error('Cannot update node versions.', { err }))
  292. JobQueue.Instance.start()
  293. .catch(err => {
  294. logger.error('Cannot start job queue.', { err })
  295. process.exit(-1)
  296. })
  297. logger.info('HTTP server listening on %s:%d', hostname, port)
  298. logger.info('Web server: %s', WEBSERVER.URL)
  299. Hooks.runAction('action:application.listening')
  300. if (cliOptions['benchmarkStartup']) process.exit(0)
  301. })
  302. process.on('exit', () => {
  303. JobQueue.Instance.terminate()
  304. .catch(err => logger.error('Cannot terminate job queue.', { err }))
  305. })
  306. process.on('SIGINT', () => process.exit(0))
  307. }