2
1

server.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 } 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, checkDatabaseConnectionOrDie } from './server/initializers/database'
  58. checkDatabaseConnectionOrDie()
  59. import { migrate } from './server/initializers/migrator'
  60. migrate()
  61. .then(() => initDatabaseModels(false))
  62. .then(() => startApplication())
  63. .catch(err => {
  64. logger.error('Cannot start application.', { err })
  65. process.exit(-1)
  66. })
  67. // ----------- Initialize -----------
  68. loadLanguages()
  69. // ----------- PeerTube modules -----------
  70. import { installApplication } from './server/initializers/installer'
  71. import { Emailer } from './server/lib/emailer'
  72. import { JobQueue } from './server/lib/job-queue'
  73. import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache'
  74. import {
  75. activityPubRouter,
  76. apiRouter,
  77. clientsRouter,
  78. feedsRouter,
  79. staticRouter,
  80. lazyStaticRouter,
  81. servicesRouter,
  82. liveRouter,
  83. pluginsRouter,
  84. webfingerRouter,
  85. trackerRouter,
  86. createWebsocketTrackerServer,
  87. botsRouter
  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 { Hooks } from './server/lib/plugins/hooks'
  104. import { PluginManager } from './server/lib/plugins/plugin-manager'
  105. import { LiveManager } from './server/lib/live-manager'
  106. import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes'
  107. // ----------- Command line -----------
  108. cli
  109. .option('--no-client', 'Start PeerTube without client interface')
  110. .option('--no-plugins', 'Start PeerTube without plugins/themes enabled')
  111. .parse(process.argv)
  112. // ----------- App -----------
  113. // Enable CORS for develop
  114. if (isTestInstance()) {
  115. app.use(cors({
  116. origin: '*',
  117. exposedHeaders: 'Retry-After',
  118. credentials: true
  119. }))
  120. }
  121. // For the logger
  122. morgan.token('remote-addr', (req: express.Request) => {
  123. if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
  124. return anonymize(req.ip, 16, 16)
  125. }
  126. return req.ip
  127. })
  128. morgan.token('user-agent', (req: express.Request) => {
  129. if (req.get('DNT') === '1') {
  130. return useragent.parse(req.get('user-agent')).family
  131. }
  132. return req.get('user-agent')
  133. })
  134. app.use(morgan('combined', {
  135. stream: { write: logger.info.bind(logger) },
  136. skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
  137. }))
  138. // For body requests
  139. app.use(bodyParser.urlencoded({ extended: false }))
  140. app.use(bodyParser.json({
  141. type: [ 'application/json', 'application/*+json' ],
  142. limit: '500kb',
  143. verify: (req: express.Request, _, buf: Buffer) => {
  144. const valid = isHTTPSignatureDigestValid(buf, req)
  145. if (valid !== true) throw new Error('Invalid digest')
  146. }
  147. }))
  148. // Cookies
  149. app.use(cookieParser())
  150. // W3C DNT Tracking Status
  151. app.use(advertiseDoNotTrack)
  152. // ----------- Views, routes and static files -----------
  153. // API
  154. const apiRoute = '/api/' + API_VERSION
  155. app.use(apiRoute, apiRouter)
  156. // Services (oembed...)
  157. app.use('/services', servicesRouter)
  158. // Live streaming
  159. app.use('/live', liveRouter)
  160. // Plugins & themes
  161. app.use('/', pluginsRouter)
  162. app.use('/', activityPubRouter)
  163. app.use('/', feedsRouter)
  164. app.use('/', webfingerRouter)
  165. app.use('/', trackerRouter)
  166. app.use('/', botsRouter)
  167. // Static files
  168. app.use('/', staticRouter)
  169. app.use('/', lazyStaticRouter)
  170. // Client files, last valid routes!
  171. if (cli.client) app.use('/', clientsRouter)
  172. // ----------- Errors -----------
  173. // Catch 404 and forward to error handler
  174. app.use(function (req, res, next) {
  175. const err = new Error('Not Found')
  176. err['status'] = HttpStatusCode.NOT_FOUND_404
  177. next(err)
  178. })
  179. app.use(function (err, req, res, next) {
  180. let error = 'Unknown error.'
  181. if (err) {
  182. error = err.stack || err.message || err
  183. }
  184. // Sequelize error
  185. const sql = err.parent ? err.parent.sql : undefined
  186. logger.error('Error in controller.', { err: error, sql })
  187. return res.status(err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
  188. })
  189. const server = createWebsocketTrackerServer(app)
  190. // ----------- Run -----------
  191. async function startApplication () {
  192. const port = CONFIG.LISTEN.PORT
  193. const hostname = CONFIG.LISTEN.HOSTNAME
  194. await installApplication()
  195. // Check activity pub urls are valid
  196. checkActivityPubUrls()
  197. .catch(err => {
  198. logger.error('Error in ActivityPub URLs checker.', { err })
  199. process.exit(-1)
  200. })
  201. // Email initialization
  202. Emailer.Instance.init()
  203. await Promise.all([
  204. Emailer.Instance.checkConnection(),
  205. JobQueue.Instance.init()
  206. ])
  207. // Caches initializations
  208. VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE)
  209. VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE)
  210. // Enable Schedulers
  211. ActorFollowScheduler.Instance.enable()
  212. RemoveOldJobsScheduler.Instance.enable()
  213. UpdateVideosScheduler.Instance.enable()
  214. YoutubeDlUpdateScheduler.Instance.enable()
  215. VideosRedundancyScheduler.Instance.enable()
  216. RemoveOldHistoryScheduler.Instance.enable()
  217. RemoveOldViewsScheduler.Instance.enable()
  218. PluginsCheckScheduler.Instance.enable()
  219. AutoFollowIndexInstances.Instance.enable()
  220. // Redis initialization
  221. Redis.Instance.init()
  222. PeerTubeSocket.Instance.init(server)
  223. updateStreamingPlaylistsInfohashesIfNeeded()
  224. .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
  225. if (cli.plugins) await PluginManager.Instance.registerPluginsAndThemes()
  226. LiveManager.Instance.init()
  227. if (CONFIG.LIVE.ENABLED) LiveManager.Instance.run()
  228. // Make server listening
  229. server.listen(port, hostname, () => {
  230. logger.info('HTTP server listening on %s:%d', hostname, port)
  231. logger.info('Web server: %s', WEBSERVER.URL)
  232. Hooks.runAction('action:application.listening')
  233. })
  234. process.on('exit', () => {
  235. JobQueue.Instance.terminate()
  236. })
  237. process.on('SIGINT', () => process.exit(0))
  238. }