Federated video streaming platform using ActivityPub and P2P in the web browser with Angular. https://joinpeertube.org/
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.
 
 
 
 
 
 

311 lines
9.3 KiB

  1. import { registerTSPaths } from './server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { isTestInstance } from './server/helpers/core-utils'
  4. if (isTestInstance()) {
  5. require('source-map-support').install()
  6. }
  7. // ----------- Node modules -----------
  8. import * as bodyParser from 'body-parser'
  9. import * as express from 'express'
  10. import * as morgan from 'morgan'
  11. import * as cors from 'cors'
  12. import * as cookieParser from 'cookie-parser'
  13. import * as helmet from 'helmet'
  14. import * as useragent from 'useragent'
  15. import * as anonymize from 'ip-anonymize'
  16. import * as cli from 'commander'
  17. process.title = 'peertube'
  18. // Create our main app
  19. const app = express().disable("x-powered-by")
  20. // ----------- Core checker -----------
  21. import { checkMissedConfig, checkFFmpeg, checkNodeVersion } from './server/initializers/checker-before-init'
  22. // Do not use barrels because we don't want to load all modules here (we need to initialize database first)
  23. import { CONFIG } from './server/initializers/config'
  24. import { API_VERSION, FILES_CACHE, WEBSERVER, loadLanguages } from './server/initializers/constants'
  25. import { logger } from './server/helpers/logger'
  26. const missed = checkMissedConfig()
  27. if (missed.length !== 0) {
  28. logger.error('Your configuration files miss keys: ' + missed)
  29. process.exit(-1)
  30. }
  31. checkFFmpeg(CONFIG)
  32. .catch(err => {
  33. logger.error('Error in ffmpeg check.', { err })
  34. process.exit(-1)
  35. })
  36. checkNodeVersion()
  37. import { checkConfig, checkActivityPubUrls, checkFFmpegVersion } from './server/initializers/checker-after-init'
  38. const errorMessage = checkConfig()
  39. if (errorMessage !== null) {
  40. throw new Error(errorMessage)
  41. }
  42. // Trust our proxy (IP forwarding...)
  43. app.set('trust proxy', CONFIG.TRUST_PROXY)
  44. // Security middleware
  45. import { baseCSP } from './server/middlewares/csp'
  46. if (CONFIG.CSP.ENABLED) {
  47. app.use(baseCSP)
  48. }
  49. if (CONFIG.SECURITY.FRAMEGUARD.ENABLED) {
  50. app.use(helmet.frameguard({
  51. action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts
  52. }))
  53. }
  54. // ----------- Database -----------
  55. // Initialize database and models
  56. import { initDatabaseModels, checkDatabaseConnectionOrDie } from './server/initializers/database'
  57. checkDatabaseConnectionOrDie()
  58. import { migrate } from './server/initializers/migrator'
  59. migrate()
  60. .then(() => initDatabaseModels(false))
  61. .then(() => startApplication())
  62. .catch(err => {
  63. logger.error('Cannot start application.', { err })
  64. process.exit(-1)
  65. })
  66. // ----------- Initialize -----------
  67. loadLanguages()
  68. // ----------- PeerTube modules -----------
  69. import { installApplication } from './server/initializers/installer'
  70. import { Emailer } from './server/lib/emailer'
  71. import { JobQueue } from './server/lib/job-queue'
  72. import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache'
  73. import {
  74. activityPubRouter,
  75. apiRouter,
  76. clientsRouter,
  77. feedsRouter,
  78. staticRouter,
  79. lazyStaticRouter,
  80. servicesRouter,
  81. liveRouter,
  82. pluginsRouter,
  83. webfingerRouter,
  84. trackerRouter,
  85. createWebsocketTrackerServer,
  86. botsRouter,
  87. downloadRouter
  88. } from './server/controllers'
  89. import { advertiseDoNotTrack } from './server/middlewares/dnt'
  90. import { Redis } from './server/lib/redis'
  91. import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler'
  92. import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler'
  93. import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
  94. import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
  95. import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler'
  96. import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler'
  97. import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler'
  98. import { AutoFollowIndexInstances } from './server/lib/schedulers/auto-follow-index-instances'
  99. import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto'
  100. import { PeerTubeSocket } from './server/lib/peertube-socket'
  101. import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls'
  102. import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler'
  103. import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler'
  104. import { Hooks } from './server/lib/plugins/hooks'
  105. import { PluginManager } from './server/lib/plugins/plugin-manager'
  106. import { LiveManager } from './server/lib/live-manager'
  107. import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes'
  108. import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
  109. // ----------- Command line -----------
  110. cli
  111. .option('--no-client', 'Start PeerTube without client interface')
  112. .option('--no-plugins', 'Start PeerTube without plugins/themes enabled')
  113. .parse(process.argv)
  114. // ----------- App -----------
  115. // Enable CORS for develop
  116. if (isTestInstance()) {
  117. app.use(cors({
  118. origin: '*',
  119. exposedHeaders: 'Retry-After',
  120. credentials: true
  121. }))
  122. }
  123. // For the logger
  124. morgan.token('remote-addr', (req: express.Request) => {
  125. if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
  126. return anonymize(req.ip, 16, 16)
  127. }
  128. return req.ip
  129. })
  130. morgan.token('user-agent', (req: express.Request) => {
  131. if (req.get('DNT') === '1') {
  132. return useragent.parse(req.get('user-agent')).family
  133. }
  134. return req.get('user-agent')
  135. })
  136. app.use(morgan('combined', {
  137. stream: {
  138. write: (str: string) => logger.info(str, { tags: [ 'http' ] })
  139. },
  140. skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
  141. }))
  142. // For body requests
  143. app.use(bodyParser.urlencoded({ extended: false }))
  144. app.use(bodyParser.json({
  145. type: [ 'application/json', 'application/*+json' ],
  146. limit: '500kb',
  147. verify: (req: express.Request, _, buf: Buffer) => {
  148. const valid = isHTTPSignatureDigestValid(buf, req)
  149. if (valid !== true) throw new Error('Invalid digest')
  150. }
  151. }))
  152. // Cookies
  153. app.use(cookieParser())
  154. // W3C DNT Tracking Status
  155. app.use(advertiseDoNotTrack)
  156. // ----------- Views, routes and static files -----------
  157. // API
  158. const apiRoute = '/api/' + API_VERSION
  159. app.use(apiRoute, apiRouter)
  160. // Services (oembed...)
  161. app.use('/services', servicesRouter)
  162. // Live streaming
  163. app.use('/live', liveRouter)
  164. // Plugins & themes
  165. app.use('/', pluginsRouter)
  166. app.use('/', activityPubRouter)
  167. app.use('/', feedsRouter)
  168. app.use('/', webfingerRouter)
  169. app.use('/', trackerRouter)
  170. app.use('/', botsRouter)
  171. // Static files
  172. app.use('/', staticRouter)
  173. app.use('/', downloadRouter)
  174. app.use('/', lazyStaticRouter)
  175. // Client files, last valid routes!
  176. const cliOptions = cli.opts()
  177. if (cliOptions.client) app.use('/', clientsRouter)
  178. // ----------- Errors -----------
  179. // Catch 404 and forward to error handler
  180. app.use(function (req, res, next) {
  181. const err = new Error('Not Found')
  182. err['status'] = HttpStatusCode.NOT_FOUND_404
  183. next(err)
  184. })
  185. app.use(function (err, req, res, next) {
  186. let error = 'Unknown error.'
  187. if (err) {
  188. error = err.stack || err.message || err
  189. }
  190. // Sequelize error
  191. const sql = err.parent ? err.parent.sql : undefined
  192. logger.error('Error in controller.', { err: error, sql })
  193. return res.status(err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
  194. })
  195. const server = createWebsocketTrackerServer(app)
  196. // ----------- Run -----------
  197. async function startApplication () {
  198. const port = CONFIG.LISTEN.PORT
  199. const hostname = CONFIG.LISTEN.HOSTNAME
  200. await installApplication()
  201. // Check activity pub urls are valid
  202. checkActivityPubUrls()
  203. .catch(err => {
  204. logger.error('Error in ActivityPub URLs checker.', { err })
  205. process.exit(-1)
  206. })
  207. checkFFmpegVersion()
  208. .catch(err => logger.error('Cannot check ffmpeg version', { err }))
  209. // Email initialization
  210. Emailer.Instance.init()
  211. await Promise.all([
  212. Emailer.Instance.checkConnection(),
  213. JobQueue.Instance.init()
  214. ])
  215. // Caches initializations
  216. VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE)
  217. VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE)
  218. VideosTorrentCache.Instance.init(CONFIG.CACHE.TORRENTS.SIZE, FILES_CACHE.TORRENTS.MAX_AGE)
  219. // Enable Schedulers
  220. ActorFollowScheduler.Instance.enable()
  221. RemoveOldJobsScheduler.Instance.enable()
  222. UpdateVideosScheduler.Instance.enable()
  223. YoutubeDlUpdateScheduler.Instance.enable()
  224. VideosRedundancyScheduler.Instance.enable()
  225. RemoveOldHistoryScheduler.Instance.enable()
  226. RemoveOldViewsScheduler.Instance.enable()
  227. PluginsCheckScheduler.Instance.enable()
  228. PeerTubeVersionCheckScheduler.Instance.enable()
  229. AutoFollowIndexInstances.Instance.enable()
  230. // Redis initialization
  231. Redis.Instance.init()
  232. PeerTubeSocket.Instance.init(server)
  233. updateStreamingPlaylistsInfohashesIfNeeded()
  234. .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
  235. if (cliOptions.plugins) await PluginManager.Instance.registerPluginsAndThemes()
  236. LiveManager.Instance.init()
  237. if (CONFIG.LIVE.ENABLED) LiveManager.Instance.run()
  238. // Make server listening
  239. server.listen(port, hostname, () => {
  240. logger.info('HTTP server listening on %s:%d', hostname, port)
  241. logger.info('Web server: %s', WEBSERVER.URL)
  242. Hooks.runAction('action:application.listening')
  243. })
  244. process.on('exit', () => {
  245. JobQueue.Instance.terminate()
  246. })
  247. process.on('SIGINT', () => process.exit(0))
  248. }