diff options
author | Chocobozzz <me@florianbigard.com> | 2022-07-15 15:30:14 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-07-18 11:37:18 +0200 |
commit | 42b40636991b97fe818007fab19091764fc5db73 (patch) | |
tree | db431787c06ce898d22e91ff771f795219274fc6 /server/controllers/api | |
parent | 654d4ede7fa4d0faa71e49bcfab6b65a686397b2 (diff) | |
download | PeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.gz PeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.zst PeerTube-42b40636991b97fe818007fab19091764fc5db73.zip |
Add ability for client to create server logs
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/server/logs.ts | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/server/controllers/api/server/logs.ts b/server/controllers/api/server/logs.ts index 8aa4b7190..ed0aa6e8e 100644 --- a/server/controllers/api/server/logs.ts +++ b/server/controllers/api/server/logs.ts | |||
@@ -3,15 +3,29 @@ import { readdir, readFile } from 'fs-extra' | |||
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { isArray } from '@server/helpers/custom-validators/misc' | 4 | import { isArray } from '@server/helpers/custom-validators/misc' |
5 | import { logger, mtimeSortFilesDesc } from '@server/helpers/logger' | 5 | import { logger, mtimeSortFilesDesc } from '@server/helpers/logger' |
6 | import { LogLevel } from '../../../../shared/models/server/log-level.type' | 6 | import { pick } from '@shared/core-utils' |
7 | import { ClientLogCreate, HttpStatusCode } from '@shared/models' | ||
8 | import { ServerLogLevel } from '../../../../shared/models/server/server-log-level.type' | ||
7 | import { UserRight } from '../../../../shared/models/users' | 9 | import { UserRight } from '../../../../shared/models/users' |
8 | import { CONFIG } from '../../../initializers/config' | 10 | import { CONFIG } from '../../../initializers/config' |
9 | import { AUDIT_LOG_FILENAME, LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants' | 11 | import { AUDIT_LOG_FILENAME, LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants' |
10 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' | 12 | import { asyncMiddleware, authenticate, buildRateLimiter, ensureUserHasRight, optionalAuthenticate } from '../../../middlewares' |
11 | import { getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs' | 13 | import { createClientLogValidator, getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs' |
14 | |||
15 | const createClientLogRateLimiter = buildRateLimiter({ | ||
16 | windowMs: CONFIG.RATES_LIMIT.RECEIVE_CLIENT_LOG.WINDOW_MS, | ||
17 | max: CONFIG.RATES_LIMIT.RECEIVE_CLIENT_LOG.MAX | ||
18 | }) | ||
12 | 19 | ||
13 | const logsRouter = express.Router() | 20 | const logsRouter = express.Router() |
14 | 21 | ||
22 | logsRouter.post('/logs/client', | ||
23 | createClientLogRateLimiter, | ||
24 | optionalAuthenticate, | ||
25 | createClientLogValidator, | ||
26 | createClientLog | ||
27 | ) | ||
28 | |||
15 | logsRouter.get('/logs', | 29 | logsRouter.get('/logs', |
16 | authenticate, | 30 | authenticate, |
17 | ensureUserHasRight(UserRight.MANAGE_LOGS), | 31 | ensureUserHasRight(UserRight.MANAGE_LOGS), |
@@ -34,6 +48,21 @@ export { | |||
34 | 48 | ||
35 | // --------------------------------------------------------------------------- | 49 | // --------------------------------------------------------------------------- |
36 | 50 | ||
51 | function createClientLog (req: express.Request, res: express.Response) { | ||
52 | const logInfo = req.body as ClientLogCreate | ||
53 | |||
54 | const meta = { | ||
55 | tags: [ 'client' ], | ||
56 | username: res.locals.oauth?.token?.User?.username, | ||
57 | |||
58 | ...pick(logInfo, [ 'userAgent', 'stackTrace', 'meta', 'url' ]) | ||
59 | } | ||
60 | |||
61 | logger.log(logInfo.level, `Client log: ${logInfo.message}`, meta) | ||
62 | |||
63 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
64 | } | ||
65 | |||
37 | const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME) | 66 | const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME) |
38 | async function getAuditLogs (req: express.Request, res: express.Response) { | 67 | async function getAuditLogs (req: express.Request, res: express.Response) { |
39 | const output = await generateOutput({ | 68 | const output = await generateOutput({ |
@@ -63,7 +92,7 @@ async function generateOutput (options: { | |||
63 | startDateQuery: string | 92 | startDateQuery: string |
64 | endDateQuery?: string | 93 | endDateQuery?: string |
65 | 94 | ||
66 | level: LogLevel | 95 | level: ServerLogLevel |
67 | nameFilter: RegExp | 96 | nameFilter: RegExp |
68 | tagsOneOf?: string[] | 97 | tagsOneOf?: string[] |
69 | }) { | 98 | }) { |
@@ -104,7 +133,7 @@ async function getOutputFromFile (options: { | |||
104 | path: string | 133 | path: string |
105 | startDate: Date | 134 | startDate: Date |
106 | endDate: Date | 135 | endDate: Date |
107 | level: LogLevel | 136 | level: ServerLogLevel |
108 | currentSize: number | 137 | currentSize: number |
109 | tagsOneOf: Set<string> | 138 | tagsOneOf: Set<string> |
110 | }) { | 139 | }) { |
@@ -116,7 +145,7 @@ async function getOutputFromFile (options: { | |||
116 | 145 | ||
117 | let logTime: number | 146 | let logTime: number |
118 | 147 | ||
119 | const logsLevel: { [ id in LogLevel ]: number } = { | 148 | const logsLevel: { [ id in ServerLogLevel ]: number } = { |
120 | audit: -1, | 149 | audit: -1, |
121 | debug: 0, | 150 | debug: 0, |
122 | info: 1, | 151 | info: 1, |