X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server.ts;h=5e1f790645873d0f2a23ccfa0bd6c7f34eee544d;hb=fa12eacc014aae8094d108634371640f2695bf9f;hp=7aaf1e553626049518e46cdec1e84ec51f5454e0;hpb=1c5e49e75284100b7b1fc8b4e73c8ba53fe22e89;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server.ts b/server.ts index 7aaf1e553..5e1f79064 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,7 +124,7 @@ 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 { LiveManager } from './server/lib/live' import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' import { ServerConfigManager } from '@server/lib/server-config-manager' @@ -169,14 +169,23 @@ app.use(morgan('combined', { 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)