server.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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()
  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 } 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. app.use(helmet({
  49. frameguard: {
  50. action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts
  51. },
  52. hsts: false
  53. }))
  54. }
  55. // ----------- Database -----------
  56. // Initialize database and models
  57. import { initDatabaseModels } from './server/initializers/database'
  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'
  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. pluginsRouter,
  82. webfingerRouter,
  83. trackerRouter,
  84. createWebsocketTrackerServer, botsRouter
  85. } from './server/controllers'
  86. import { advertiseDoNotTrack } from './server/middlewares/dnt'
  87. import { Redis } from './server/lib/redis'
  88. import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler'
  89. import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler'
  90. import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
  91. import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
  92. import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler'
  93. import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler'
  94. import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler'
  95. import { AutoFollowIndexInstances } from './server/lib/schedulers/auto-follow-index-instances'
  96. import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto'
  97. import { PeerTubeSocket } from './server/lib/peertube-socket'
  98. import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls'
  99. import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler'
  100. import { Hooks } from './server/lib/plugins/hooks'
  101. import { PluginManager } from './server/lib/plugins/plugin-manager'
  102. // ----------- Command line -----------
  103. cli
  104. .option('--no-client', 'Start PeerTube without client interface')
  105. .option('--no-plugins', 'Start PeerTube without plugins/themes enabled')
  106. .parse(process.argv)
  107. // ----------- App -----------
  108. // Enable CORS for develop
  109. if (isTestInstance()) {
  110. app.use(cors({
  111. origin: '*',
  112. exposedHeaders: 'Retry-After',
  113. credentials: true
  114. }))
  115. }
  116. // For the logger
  117. morgan.token('remote-addr', req => {
  118. if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
  119. return anonymize(req.ip, 16, 16)
  120. }
  121. return req.ip
  122. })
  123. morgan.token('user-agent', req => {
  124. if (req.get('DNT') === '1') {
  125. return useragent.parse(req.get('user-agent')).family
  126. }
  127. return req.get('user-agent')
  128. })
  129. app.use(morgan('combined', {
  130. stream: { write: logger.info.bind(logger) }
  131. }))
  132. // For body requests
  133. app.use(bodyParser.urlencoded({ extended: false }))
  134. app.use(bodyParser.json({
  135. type: [ 'application/json', 'application/*+json' ],
  136. limit: '500kb',
  137. verify: (req: express.Request, _, buf: Buffer) => {
  138. const valid = isHTTPSignatureDigestValid(buf, req)
  139. if (valid !== true) throw new Error('Invalid digest')
  140. }
  141. }))
  142. // Cookies
  143. app.use(cookieParser())
  144. // W3C DNT Tracking Status
  145. app.use(advertiseDoNotTrack)
  146. // ----------- Views, routes and static files -----------
  147. // API
  148. const apiRoute = '/api/' + API_VERSION
  149. app.use(apiRoute, apiRouter)
  150. // Services (oembed...)
  151. app.use('/services', servicesRouter)
  152. // Plugins & themes
  153. app.use('/', pluginsRouter)
  154. app.use('/', activityPubRouter)
  155. app.use('/', feedsRouter)
  156. app.use('/', webfingerRouter)
  157. app.use('/', trackerRouter)
  158. app.use('/', botsRouter)
  159. // Static files
  160. app.use('/', staticRouter)
  161. app.use('/', lazyStaticRouter)
  162. // Client files, last valid routes!
  163. if (cli.client) app.use('/', clientsRouter)
  164. // ----------- Errors -----------
  165. // Catch 404 and forward to error handler
  166. app.use(function (req, res, next) {
  167. const err = new Error('Not Found')
  168. err['status'] = 404
  169. next(err)
  170. })
  171. app.use(function (err, req, res, next) {
  172. let error = 'Unknown error.'
  173. if (err) {
  174. error = err.stack || err.message || err
  175. }
  176. // Sequelize error
  177. const sql = err.parent ? err.parent.sql : undefined
  178. logger.error('Error in controller.', { err: error, sql })
  179. return res.status(err.status || 500).end()
  180. })
  181. const server = createWebsocketTrackerServer(app)
  182. // ----------- Run -----------
  183. async function startApplication () {
  184. const port = CONFIG.LISTEN.PORT
  185. const hostname = CONFIG.LISTEN.HOSTNAME
  186. await installApplication()
  187. // Check activity pub urls are valid
  188. checkActivityPubUrls()
  189. .catch(err => {
  190. logger.error('Error in ActivityPub URLs checker.', { err })
  191. process.exit(-1)
  192. })
  193. // Email initialization
  194. Emailer.Instance.init()
  195. await Promise.all([
  196. Emailer.Instance.checkConnectionOrDie(),
  197. JobQueue.Instance.init()
  198. ])
  199. // Caches initializations
  200. VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE)
  201. VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE)
  202. // Enable Schedulers
  203. ActorFollowScheduler.Instance.enable()
  204. RemoveOldJobsScheduler.Instance.enable()
  205. UpdateVideosScheduler.Instance.enable()
  206. YoutubeDlUpdateScheduler.Instance.enable()
  207. VideosRedundancyScheduler.Instance.enable()
  208. RemoveOldHistoryScheduler.Instance.enable()
  209. RemoveOldViewsScheduler.Instance.enable()
  210. PluginsCheckScheduler.Instance.enable()
  211. AutoFollowIndexInstances.Instance.enable()
  212. // Redis initialization
  213. Redis.Instance.init()
  214. PeerTubeSocket.Instance.init(server)
  215. updateStreamingPlaylistsInfohashesIfNeeded()
  216. .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
  217. if (cli.plugins) await PluginManager.Instance.registerPluginsAndThemes()
  218. // Make server listening
  219. server.listen(port, hostname, () => {
  220. logger.info('Server listening on %s:%d', hostname, port)
  221. logger.info('Web server: %s', WEBSERVER.URL)
  222. Hooks.runAction('action:application.listening')
  223. })
  224. process.on('exit', () => {
  225. JobQueue.Instance.terminate()
  226. })
  227. process.on('SIGINT', () => process.exit(0))
  228. }