server.ts 8.3 KB

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