X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server.ts;h=bfc7ee1450867bfdddf45d54f18ade4ff5f92fd8;hb=171efc48e67498406feb6d7873b3482b41505515;hp=7aaf1e553626049518e46cdec1e84ec51f5454e0;hpb=2539932e16129992a2c0889b4ff527c265a8e2c7;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server.ts b/server.ts index 7aaf1e553..bfc7ee145 100644 --- a/server.ts +++ b/server.ts @@ -7,7 +7,6 @@ if (isTestInstance()) { } // ----------- Node modules ----------- -import * as bodyParser from 'body-parser' import * as express from 'express' import * as morgan from 'morgan' import * as cors from 'cors' @@ -15,7 +14,7 @@ import * as cookieParser from 'cookie-parser' import * as helmet from 'helmet' import * as useragent from 'useragent' import * as anonymize from 'ip-anonymize' -import * as cli from 'commander' +import { program as cli } from 'commander' process.title = 'peertube' @@ -107,6 +106,7 @@ import { downloadRouter } from './server/controllers' import { advertiseDoNotTrack } from './server/middlewares/dnt' +import { apiFailMiddleware } from './server/middlewares/error' import { Redis } from './server/lib/redis' import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' @@ -124,8 +124,8 @@ import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-sch import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler' import { Hooks } from './server/lib/plugins/hooks' import { PluginManager } from './server/lib/plugins/plugin-manager' -import { LiveManager } from './server/lib/live-manager' -import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' +import { LiveManager } from './server/lib/live' +import { HttpStatusCode } from './shared/models/http/http-error-codes' import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' import { ServerConfigManager } from '@server/lib/server-config-manager' @@ -164,19 +164,28 @@ morgan.token('user-agent', (req: express.Request) => { }) app.use(morgan('combined', { stream: { - write: (str: string) => logger.info(str, { tags: [ 'http' ] }) + write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] }) }, skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' })) +// Add .fail() helper to response +app.use(apiFailMiddleware) + // For body requests -app.use(bodyParser.urlencoded({ extended: false })) -app.use(bodyParser.json({ +app.use(express.urlencoded({ extended: false })) +app.use(express.json({ type: [ 'application/json', 'application/*+json' ], limit: '500kb', - verify: (req: express.Request, _, buf: Buffer) => { + verify: (req: express.Request, res: express.Response, buf: Buffer) => { const valid = isHTTPSignatureDigestValid(buf, req) - if (valid !== true) throw new Error('Invalid digest') + + if (valid !== true) { + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Invalid digest' + }) + } } })) @@ -218,24 +227,27 @@ if (cliOptions.client) app.use('/', clientsRouter) // ----------- Errors ----------- -// Catch 404 and forward to error handler -app.use(function (req, res, next) { - const err = new Error('Not Found') - err['status'] = HttpStatusCode.NOT_FOUND_404 - next(err) +// Catch unmatched routes +app.use((req, res: express.Response) => { + res.status(HttpStatusCode.NOT_FOUND_404).end() }) -app.use(function (err, req, res, next) { +// Catch thrown errors +app.use((err, req, res: express.Response, next) => { + // Format error to be logged let error = 'Unknown error.' if (err) { error = err.stack || err.message || err } - - // Sequelize error + // Handling Sequelize error traces const sql = err.parent ? err.parent.sql : undefined - logger.error('Error in controller.', { err: error, sql }) - return res.status(err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() + + return res.fail({ + status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, + message: err.message, + type: err.name + }) }) const server = createWebsocketTrackerServer(app) @@ -293,13 +305,19 @@ async function startApplication () { updateStreamingPlaylistsInfohashesIfNeeded() .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err })) - if (cliOptions.plugins) await PluginManager.Instance.registerPluginsAndThemes() - LiveManager.Instance.init() if (CONFIG.LIVE.ENABLED) LiveManager.Instance.run() // Make server listening - server.listen(port, hostname, () => { + server.listen(port, hostname, async () => { + if (cliOptions.plugins) { + try { + await PluginManager.Instance.registerPluginsAndThemes() + } catch (err) { + logger.error('Cannot register plugins and themes.', { err }) + } + } + logger.info('HTTP server listening on %s:%d', hostname, port) logger.info('Web server: %s', WEBSERVER.URL)