]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
ecb4e35f C |
2 | import { createClient, RedisClient } from 'redis' |
3 | import { logger } from '../helpers/logger' | |
4 | import { generateRandomString } from '../helpers/utils' | |
a4101923 | 5 | import { |
a4101923 C |
6 | CONTACT_FORM_LIFETIME, |
7 | USER_EMAIL_VERIFY_LIFETIME, | |
8 | USER_PASSWORD_RESET_LIFETIME, | |
45f1bd72 | 9 | USER_PASSWORD_CREATE_LIFETIME, |
e4bf7856 | 10 | VIEW_LIFETIME, |
db48de85 | 11 | WEBSERVER, |
276250f0 RK |
12 | TRACKER_RATE_LIMITS, |
13 | RESUMABLE_UPLOAD_SESSION_LIFETIME | |
74dc3bca | 14 | } from '../initializers/constants' |
6dd9de95 | 15 | import { CONFIG } from '../initializers/config' |
4195cd2b C |
16 | |
17 | type CachedRoute = { | |
a1587156 | 18 | body: string |
2cebd797 C |
19 | contentType?: string |
20 | statusCode?: string | |
4195cd2b | 21 | } |
ecb4e35f C |
22 | |
23 | class Redis { | |
24 | ||
25 | private static instance: Redis | |
26 | private initialized = false | |
27 | private client: RedisClient | |
28 | private prefix: string | |
29 | ||
a1587156 C |
30 | private constructor () { |
31 | } | |
ecb4e35f C |
32 | |
33 | init () { | |
34 | // Already initialized | |
35 | if (this.initialized === true) return | |
36 | this.initialized = true | |
37 | ||
47f6409b | 38 | this.client = createClient(Redis.getRedisClientOptions()) |
ecb4e35f C |
39 | |
40 | this.client.on('error', err => { | |
d5b7d911 | 41 | logger.error('Error in Redis client.', { err }) |
ecb4e35f C |
42 | process.exit(-1) |
43 | }) | |
44 | ||
45 | if (CONFIG.REDIS.AUTH) { | |
46 | this.client.auth(CONFIG.REDIS.AUTH) | |
47 | } | |
48 | ||
6dd9de95 | 49 | this.prefix = 'redis-' + WEBSERVER.HOST + '-' |
ecb4e35f C |
50 | } |
51 | ||
47f6409b | 52 | static getRedisClientOptions () { |
19f7b248 RK |
53 | return Object.assign({}, |
54 | (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, | |
55 | (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, | |
a1587156 C |
56 | (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) |
57 | ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } | |
58 | : { path: CONFIG.REDIS.SOCKET } | |
19f7b248 RK |
59 | ) |
60 | } | |
61 | ||
47f6409b C |
62 | getClient () { |
63 | return this.client | |
64 | } | |
65 | ||
66 | getPrefix () { | |
67 | return this.prefix | |
68 | } | |
69 | ||
a1587156 | 70 | /* ************ Forgot password ************ */ |
6e46de09 | 71 | |
ecb4e35f C |
72 | async setResetPasswordVerificationString (userId: number) { |
73 | const generatedString = await generateRandomString(32) | |
74 | ||
75 | await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME) | |
76 | ||
45f1bd72 JL |
77 | return generatedString |
78 | } | |
79 | ||
80 | async setCreatePasswordVerificationString (userId: number) { | |
81 | const generatedString = await generateRandomString(32) | |
82 | ||
83 | await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME) | |
84 | ||
ecb4e35f C |
85 | return generatedString |
86 | } | |
87 | ||
e9c5f123 C |
88 | async removePasswordVerificationString (userId: number) { |
89 | return this.removeValue(this.generateResetPasswordKey(userId)) | |
90 | } | |
91 | ||
ecb4e35f C |
92 | async getResetPasswordLink (userId: number) { |
93 | return this.getValue(this.generateResetPasswordKey(userId)) | |
94 | } | |
95 | ||
a1587156 | 96 | /* ************ Email verification ************ */ |
6e46de09 | 97 | |
d9eaee39 JM |
98 | async setVerifyEmailVerificationString (userId: number) { |
99 | const generatedString = await generateRandomString(32) | |
100 | ||
101 | await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME) | |
102 | ||
103 | return generatedString | |
104 | } | |
105 | ||
106 | async getVerifyEmailLink (userId: number) { | |
107 | return this.getValue(this.generateVerifyEmailKey(userId)) | |
108 | } | |
109 | ||
a1587156 | 110 | /* ************ Contact form per IP ************ */ |
a4101923 C |
111 | |
112 | async setContactFormIp (ip: string) { | |
113 | return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) | |
114 | } | |
115 | ||
0f6acda1 | 116 | async doesContactFormIpExist (ip: string) { |
a4101923 C |
117 | return this.exists(this.generateContactFormKey(ip)) |
118 | } | |
119 | ||
a1587156 | 120 | /* ************ Views per IP ************ */ |
6e46de09 | 121 | |
e4bf7856 C |
122 | setIPVideoView (ip: string, videoUUID: string, isLive: boolean) { |
123 | const lifetime = isLive | |
124 | ? VIEW_LIFETIME.LIVE | |
125 | : VIEW_LIFETIME.VIDEO | |
126 | ||
127 | return this.setValue(this.generateViewKey(ip, videoUUID), '1', lifetime) | |
b5c0e955 C |
128 | } |
129 | ||
0f6acda1 | 130 | async doesVideoIPViewExist (ip: string, videoUUID: string) { |
6e46de09 | 131 | return this.exists(this.generateViewKey(ip, videoUUID)) |
b5c0e955 C |
132 | } |
133 | ||
db48de85 C |
134 | /* ************ Tracker IP block ************ */ |
135 | ||
136 | setTrackerBlockIP (ip: string) { | |
137 | return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME) | |
138 | } | |
139 | ||
140 | async doesTrackerBlockIPExist (ip: string) { | |
141 | return this.exists(this.generateTrackerBlockIPKey(ip)) | |
142 | } | |
143 | ||
a1587156 | 144 | /* ************ API cache ************ */ |
6e46de09 | 145 | |
4195cd2b | 146 | async getCachedRoute (req: express.Request) { |
6e46de09 | 147 | const cached = await this.getObject(this.generateCachedRouteKey(req)) |
4195cd2b C |
148 | |
149 | return cached as CachedRoute | |
150 | } | |
151 | ||
fd4484f1 | 152 | setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { |
a1587156 C |
153 | const cached: CachedRoute = Object.assign( |
154 | {}, | |
155 | { body: body.toString() }, | |
156 | (contentType) ? { contentType } : null, | |
157 | (statusCode) ? { statusCode: statusCode.toString() } : null | |
c1e791ba | 158 | ) |
4195cd2b | 159 | |
6e46de09 | 160 | return this.setObject(this.generateCachedRouteKey(req), cached, lifetime) |
4195cd2b C |
161 | } |
162 | ||
a1587156 | 163 | /* ************ Video views ************ */ |
6e46de09 | 164 | |
6b616860 C |
165 | addVideoView (videoId: number) { |
166 | const keyIncr = this.generateVideoViewKey(videoId) | |
167 | const keySet = this.generateVideosViewKey() | |
168 | ||
169 | return Promise.all([ | |
170 | this.addToSet(keySet, videoId.toString()), | |
171 | this.increment(keyIncr) | |
172 | ]) | |
173 | } | |
174 | ||
175 | async getVideoViews (videoId: number, hour: number) { | |
176 | const key = this.generateVideoViewKey(videoId, hour) | |
177 | ||
178 | const valueString = await this.getValue(key) | |
6040f87d C |
179 | const valueInt = parseInt(valueString, 10) |
180 | ||
181 | if (isNaN(valueInt)) { | |
182 | logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString) | |
183 | return undefined | |
184 | } | |
185 | ||
186 | return valueInt | |
6b616860 C |
187 | } |
188 | ||
189 | async getVideosIdViewed (hour: number) { | |
190 | const key = this.generateVideosViewKey(hour) | |
191 | ||
192 | const stringIds = await this.getSet(key) | |
193 | return stringIds.map(s => parseInt(s, 10)) | |
194 | } | |
195 | ||
196 | deleteVideoViews (videoId: number, hour: number) { | |
197 | const keySet = this.generateVideosViewKey(hour) | |
198 | const keyIncr = this.generateVideoViewKey(videoId, hour) | |
199 | ||
200 | return Promise.all([ | |
201 | this.deleteFromSet(keySet, videoId.toString()), | |
202 | this.deleteKey(keyIncr) | |
203 | ]) | |
204 | } | |
205 | ||
276250f0 RK |
206 | /* ************ Resumable uploads final responses ************ */ |
207 | ||
208 | setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) { | |
209 | return this.setValue( | |
210 | 'resumable-upload-' + uploadId, | |
211 | response | |
212 | ? JSON.stringify(response) | |
213 | : '', | |
214 | RESUMABLE_UPLOAD_SESSION_LIFETIME | |
215 | ) | |
216 | } | |
217 | ||
218 | doesUploadSessionExist (uploadId: string) { | |
219 | return this.exists('resumable-upload-' + uploadId) | |
220 | } | |
221 | ||
222 | async getUploadSession (uploadId: string) { | |
223 | const value = await this.getValue('resumable-upload-' + uploadId) | |
224 | ||
225 | return value | |
226 | ? JSON.parse(value) | |
227 | : '' | |
228 | } | |
229 | ||
a1587156 | 230 | /* ************ Keys generation ************ */ |
6e46de09 C |
231 | |
232 | generateCachedRouteKey (req: express.Request) { | |
233 | return req.method + '-' + req.originalUrl | |
234 | } | |
235 | ||
236 | private generateVideosViewKey (hour?: number) { | |
6b616860 C |
237 | if (!hour) hour = new Date().getHours() |
238 | ||
239 | return `videos-view-h${hour}` | |
240 | } | |
241 | ||
6e46de09 | 242 | private generateVideoViewKey (videoId: number, hour?: number) { |
cc49be3e | 243 | if (hour === undefined || hour === null) hour = new Date().getHours() |
6b616860 C |
244 | |
245 | return `video-view-${videoId}-h${hour}` | |
246 | } | |
247 | ||
6e46de09 | 248 | private generateResetPasswordKey (userId: number) { |
b40f0575 C |
249 | return 'reset-password-' + userId |
250 | } | |
251 | ||
6e46de09 | 252 | private generateVerifyEmailKey (userId: number) { |
d9eaee39 JM |
253 | return 'verify-email-' + userId |
254 | } | |
255 | ||
6e46de09 | 256 | private generateViewKey (ip: string, videoUUID: string) { |
a4101923 C |
257 | return `views-${videoUUID}-${ip}` |
258 | } | |
259 | ||
db48de85 C |
260 | private generateTrackerBlockIPKey (ip: string) { |
261 | return `tracker-block-ip-${ip}` | |
262 | } | |
263 | ||
a4101923 C |
264 | private generateContactFormKey (ip: string) { |
265 | return 'contact-form-' + ip | |
b40f0575 C |
266 | } |
267 | ||
a1587156 | 268 | /* ************ Redis helpers ************ */ |
b40f0575 | 269 | |
ecb4e35f C |
270 | private getValue (key: string) { |
271 | return new Promise<string>((res, rej) => { | |
272 | this.client.get(this.prefix + key, (err, value) => { | |
273 | if (err) return rej(err) | |
274 | ||
275 | return res(value) | |
276 | }) | |
277 | }) | |
278 | } | |
279 | ||
6b616860 C |
280 | private getSet (key: string) { |
281 | return new Promise<string[]>((res, rej) => { | |
282 | this.client.smembers(this.prefix + key, (err, value) => { | |
283 | if (err) return rej(err) | |
284 | ||
285 | return res(value) | |
286 | }) | |
287 | }) | |
288 | } | |
289 | ||
290 | private addToSet (key: string, value: string) { | |
ba5a8d89 | 291 | return new Promise<void>((res, rej) => { |
6b616860 C |
292 | this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res()) |
293 | }) | |
294 | } | |
295 | ||
296 | private deleteFromSet (key: string, value: string) { | |
297 | return new Promise<void>((res, rej) => { | |
298 | this.client.srem(this.prefix + key, value, err => err ? rej(err) : res()) | |
299 | }) | |
300 | } | |
301 | ||
302 | private deleteKey (key: string) { | |
303 | return new Promise<void>((res, rej) => { | |
304 | this.client.del(this.prefix + key, err => err ? rej(err) : res()) | |
305 | }) | |
306 | } | |
307 | ||
6e46de09 C |
308 | private deleteFieldInHash (key: string, field: string) { |
309 | return new Promise<void>((res, rej) => { | |
310 | this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res()) | |
311 | }) | |
312 | } | |
313 | ||
ecb4e35f C |
314 | private setValue (key: string, value: string, expirationMilliseconds: number) { |
315 | return new Promise<void>((res, rej) => { | |
316 | this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { | |
317 | if (err) return rej(err) | |
318 | ||
4195cd2b | 319 | if (ok !== 'OK') return rej(new Error('Redis set result is not OK.')) |
ecb4e35f C |
320 | |
321 | return res() | |
322 | }) | |
323 | }) | |
324 | } | |
325 | ||
e9c5f123 C |
326 | private removeValue (key: string) { |
327 | return new Promise<void>((res, rej) => { | |
328 | this.client.del(this.prefix + key, err => { | |
329 | if (err) return rej(err) | |
330 | ||
331 | return res() | |
332 | }) | |
333 | }) | |
334 | } | |
335 | ||
a1587156 | 336 | private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) { |
4195cd2b C |
337 | return new Promise<void>((res, rej) => { |
338 | this.client.hmset(this.prefix + key, obj, (err, ok) => { | |
339 | if (err) return rej(err) | |
340 | if (!ok) return rej(new Error('Redis mset result is not OK.')) | |
341 | ||
342 | this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => { | |
343 | if (err) return rej(err) | |
344 | if (!ok) return rej(new Error('Redis expiration result is not OK.')) | |
345 | ||
346 | return res() | |
347 | }) | |
348 | }) | |
349 | }) | |
350 | } | |
351 | ||
352 | private getObject (key: string) { | |
a1587156 | 353 | return new Promise<{ [id: string]: string }>((res, rej) => { |
4195cd2b C |
354 | this.client.hgetall(this.prefix + key, (err, value) => { |
355 | if (err) return rej(err) | |
356 | ||
357 | return res(value) | |
358 | }) | |
359 | }) | |
360 | } | |
361 | ||
6e46de09 C |
362 | private setValueInHash (key: string, field: string, value: string) { |
363 | return new Promise<void>((res, rej) => { | |
364 | this.client.hset(this.prefix + key, field, value, (err) => { | |
365 | if (err) return rej(err) | |
366 | ||
367 | return res() | |
368 | }) | |
369 | }) | |
370 | } | |
371 | ||
6b616860 C |
372 | private increment (key: string) { |
373 | return new Promise<number>((res, rej) => { | |
374 | this.client.incr(this.prefix + key, (err, value) => { | |
375 | if (err) return rej(err) | |
376 | ||
377 | return res(value) | |
378 | }) | |
379 | }) | |
380 | } | |
381 | ||
b5c0e955 C |
382 | private exists (key: string) { |
383 | return new Promise<boolean>((res, rej) => { | |
384 | this.client.exists(this.prefix + key, (err, existsNumber) => { | |
385 | if (err) return rej(err) | |
386 | ||
387 | return res(existsNumber === 1) | |
388 | }) | |
389 | }) | |
390 | } | |
391 | ||
ecb4e35f C |
392 | static get Instance () { |
393 | return this.instance || (this.instance = new this()) | |
394 | } | |
395 | } | |
396 | ||
397 | // --------------------------------------------------------------------------- | |
398 | ||
399 | export { | |
400 | Redis | |
401 | } |