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