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

390 lines
12 KiB

  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, sequelizeTypescript } 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. if (req.originalUrl.startsWith('/plugins/')) {
  181. req.rawBody = buf
  182. }
  183. }
  184. }))
  185. // W3C DNT Tracking Status
  186. app.use(advertiseDoNotTrack)
  187. // ----------- Open Telemetry -----------
  188. OpenTelemetryMetrics.Instance.init(app)
  189. // ----------- Views, routes and static files -----------
  190. app.use('/api/' + API_VERSION, apiRouter)
  191. // Services (oembed...)
  192. app.use('/services', servicesRouter)
  193. app.use('/', activityPubRouter)
  194. app.use('/', feedsRouter)
  195. app.use('/', trackerRouter)
  196. app.use('/', sitemapRouter)
  197. // Static files
  198. app.use('/', staticRouter)
  199. app.use('/', wellKnownRouter)
  200. app.use('/', miscRouter)
  201. app.use('/', downloadRouter)
  202. app.use('/', lazyStaticRouter)
  203. app.use('/', objectStorageProxyRouter)
  204. // Cookies for plugins and HTML
  205. app.use(cookieParser())
  206. // Plugins & themes
  207. app.use('/', pluginsRouter)
  208. // Client files, last valid routes!
  209. const cliOptions = cli.opts<{ client: boolean, plugins: boolean }>()
  210. if (cliOptions.client) app.use('/', clientsRouter)
  211. // ----------- Errors -----------
  212. // Catch unmatched routes
  213. app.use((_req, res: express.Response) => {
  214. res.status(HttpStatusCode.NOT_FOUND_404).end()
  215. })
  216. // Catch thrown errors
  217. app.use((err, _req, res: express.Response, _next) => {
  218. // Format error to be logged
  219. let error = 'Unknown error.'
  220. if (err) {
  221. error = err.stack || err.message || err
  222. }
  223. // Handling Sequelize error traces
  224. const sql = err?.parent ? err.parent.sql : undefined
  225. // Help us to debug SequelizeConnectionAcquireTimeoutError errors
  226. const activeRequests = err?.name === 'SequelizeConnectionAcquireTimeoutError' && typeof (process as any)._getActiveRequests !== 'function'
  227. ? (process as any)._getActiveRequests()
  228. : undefined
  229. logger.error('Error in controller.', { err: error, sql, activeRequests })
  230. return res.fail({
  231. status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
  232. message: err.message,
  233. type: err.name
  234. })
  235. })
  236. const { server, trackerServer } = createWebsocketTrackerServer(app)
  237. // ----------- Run -----------
  238. async function startApplication () {
  239. const port = CONFIG.LISTEN.PORT
  240. const hostname = CONFIG.LISTEN.HOSTNAME
  241. await installApplication()
  242. // Check activity pub urls are valid
  243. checkActivityPubUrls()
  244. .catch(err => {
  245. logger.error('Error in ActivityPub URLs checker.', { err })
  246. process.exit(-1)
  247. })
  248. checkFFmpegVersion()
  249. .catch(err => logger.error('Cannot check ffmpeg version', { err }))
  250. Redis.Instance.init()
  251. Emailer.Instance.init()
  252. await Promise.all([
  253. Emailer.Instance.checkConnection(),
  254. JobQueue.Instance.init(),
  255. ServerConfigManager.Instance.init()
  256. ])
  257. // Enable Schedulers
  258. ActorFollowScheduler.Instance.enable()
  259. UpdateVideosScheduler.Instance.enable()
  260. YoutubeDlUpdateScheduler.Instance.enable()
  261. VideosRedundancyScheduler.Instance.enable()
  262. RemoveOldHistoryScheduler.Instance.enable()
  263. RemoveOldViewsScheduler.Instance.enable()
  264. PluginsCheckScheduler.Instance.enable()
  265. PeerTubeVersionCheckScheduler.Instance.enable()
  266. AutoFollowIndexInstances.Instance.enable()
  267. RemoveDanglingResumableUploadsScheduler.Instance.enable()
  268. VideoChannelSyncLatestScheduler.Instance.enable()
  269. VideoViewsBufferScheduler.Instance.enable()
  270. GeoIPUpdateScheduler.Instance.enable()
  271. RunnerJobWatchDogScheduler.Instance.enable()
  272. RemoveExpiredUserExportsScheduler.Instance.enable()
  273. OpenTelemetryMetrics.Instance.registerMetrics({ trackerServer })
  274. PluginManager.Instance.init(server)
  275. // Before PeerTubeSocket init
  276. PluginManager.Instance.registerWebSocketRouter()
  277. PeerTubeSocket.Instance.init(server)
  278. VideoViewsManager.Instance.init()
  279. updateStreamingPlaylistsInfohashesIfNeeded()
  280. .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
  281. LiveManager.Instance.init()
  282. if (CONFIG.LIVE.ENABLED) await LiveManager.Instance.run()
  283. // Make server listening
  284. server.listen(port, hostname, async () => {
  285. if (cliOptions.plugins) {
  286. try {
  287. await PluginManager.Instance.rebuildNativePluginsIfNeeded()
  288. await PluginManager.Instance.registerPluginsAndThemes()
  289. } catch (err) {
  290. logger.error('Cannot register plugins and themes.', { err })
  291. }
  292. }
  293. ApplicationModel.updateNodeVersions()
  294. .catch(err => logger.error('Cannot update node versions.', { err }))
  295. JobQueue.Instance.start()
  296. .catch(err => {
  297. logger.error('Cannot start job queue.', { err })
  298. process.exit(-1)
  299. })
  300. logger.info('HTTP server listening on %s:%d', hostname, port)
  301. logger.info('Web server: %s', WEBSERVER.URL)
  302. Hooks.runAction('action:application.listening')
  303. if (cliOptions['benchmarkStartup']) process.exit(0)
  304. })
  305. process.on('exit', () => {
  306. sequelizeTypescript.close()
  307. .catch(err => logger.error('Cannot close database connection.', { err }))
  308. JobQueue.Instance.terminate()
  309. .catch(err => logger.error('Cannot terminate job queue.', { err }))
  310. })
  311. process.on('SIGINT', () => process.exit(0))
  312. }