diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2022-07-02 15:12:11 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-02 15:12:11 +0900 |
| commit | eccc90c843f63b2dc08d8fbf80e4f54a601e477d (patch) | |
| tree | a26e23b56d711806862bdfaafeb9b826d25465a8 /packages/backend/src/server/api/define.ts | |
| parent | refactor(client): refactoring (diff) | |
| download | sharkey-eccc90c843f63b2dc08d8fbf80e4f54a601e477d.tar.gz sharkey-eccc90c843f63b2dc08d8fbf80e4f54a601e477d.tar.bz2 sharkey-eccc90c843f63b2dc08d8fbf80e4f54a601e477d.zip | |
feat: Log user ips (#8872)
* wip
* store ip and headers
* Update admin-file.vue
* require admin for view ip/headers
* IP (recent) 消した
* admin必須
* opt in
* clean ips periodically
* respect logging setting in drive/files/create
Diffstat (limited to 'packages/backend/src/server/api/define.ts')
| -rw-r--r-- | packages/backend/src/server/api/define.ts | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/packages/backend/src/server/api/define.ts b/packages/backend/src/server/api/define.ts index 47dcb44ea8..c1b56b8a83 100644 --- a/packages/backend/src/server/api/define.ts +++ b/packages/backend/src/server/api/define.ts @@ -1,16 +1,16 @@ import * as fs from 'node:fs'; import Ajv from 'ajv'; import { CacheableLocalUser, ILocalUser } from '@/models/entities/user.js'; -import { IEndpointMeta } from './endpoints.js'; -import { ApiError } from './error.js'; import { Schema, SchemaType } from '@/misc/schema.js'; import { AccessToken } from '@/models/entities/access-token.js'; +import { IEndpointMeta } from './endpoints.js'; +import { ApiError } from './error.js'; export type Response = Record<string, any> | void; // TODO: paramsの型をT['params']のスキーマ定義から推論する type executor<T extends IEndpointMeta, Ps extends Schema> = - (params: SchemaType<Ps>, user: T['requireCredential'] extends true ? CacheableLocalUser : CacheableLocalUser | null, token: AccessToken | null, file?: any, cleanup?: () => any) => + (params: SchemaType<Ps>, user: T['requireCredential'] extends true ? CacheableLocalUser : CacheableLocalUser | null, token: AccessToken | null, file?: any, cleanup?: () => any, ip?: string | null, headers?: Record<string, string> | null) => Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>; const ajv = new Ajv({ @@ -20,23 +20,27 @@ const ajv = new Ajv({ ajv.addFormat('misskey:id', /^[a-zA-Z0-9]+$/); export default function <T extends IEndpointMeta, Ps extends Schema>(meta: T, paramDef: Ps, cb: executor<T, Ps>) - : (params: any, user: T['requireCredential'] extends true ? CacheableLocalUser : CacheableLocalUser | null, token: AccessToken | null, file?: any) => Promise<any> { + : (params: any, user: T['requireCredential'] extends true ? CacheableLocalUser : CacheableLocalUser | null, token: AccessToken | null, file?: any, ip?: string | null, headers?: Record<string, string> | null) => Promise<any> { const validate = ajv.compile(paramDef); - return (params: any, user: T['requireCredential'] extends true ? CacheableLocalUser : CacheableLocalUser | null, token: AccessToken | null, file?: any) => { - function cleanup() { - fs.unlink(file.path, () => {}); - } + return (params: any, user: T['requireCredential'] extends true ? CacheableLocalUser : CacheableLocalUser | null, token: AccessToken | null, file?: any, ip?: string | null, headers?: Record<string, string> | null) => { + let cleanup: undefined | (() => void) = undefined; + + if (meta.requireFile) { + cleanup = () => { + fs.unlink(file.path, () => {}); + }; - if (meta.requireFile && file == null) return Promise.reject(new ApiError({ - message: 'File required.', - code: 'FILE_REQUIRED', - id: '4267801e-70d1-416a-b011-4ee502885d8b', - })); + if (file == null) return Promise.reject(new ApiError({ + message: 'File required.', + code: 'FILE_REQUIRED', + id: '4267801e-70d1-416a-b011-4ee502885d8b', + })); + } const valid = validate(params); if (!valid) { - if (file) cleanup(); + if (file) cleanup!(); const errors = validate.errors!; const err = new ApiError({ @@ -50,6 +54,6 @@ export default function <T extends IEndpointMeta, Ps extends Schema>(meta: T, pa return Promise.reject(err); } - return cb(params as SchemaType<Ps>, user, token, file, cleanup); + return cb(params as SchemaType<Ps>, user, token, file, cleanup, ip, headers); }; } |