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