server.ts 7.4 KB

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