]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/redis.ts
Correctly close RTMPS server too
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
... / ...
CommitLineData
1import express from 'express'
2import { createClient, RedisClient } from 'redis'
3import { logger } from '../helpers/logger'
4import { generateRandomString } from '../helpers/utils'
5import {
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 RESUMABLE_UPLOAD_SESSION_LIFETIME
14} from '../initializers/constants'
15import { CONFIG } from '../initializers/config'
16
17type CachedRoute = {
18 body: string
19 contentType?: string
20 statusCode?: string
21}
22
23class Redis {
24
25 private static instance: Redis
26 private initialized = false
27 private client: RedisClient
28 private prefix: string
29
30 private constructor () {
31 }
32
33 init () {
34 // Already initialized
35 if (this.initialized === true) return
36 this.initialized = true
37
38 this.client = createClient(Redis.getRedisClientOptions())
39
40 this.client.on('error', err => {
41 logger.error('Error in Redis client.', { err })
42 process.exit(-1)
43 })
44
45 if (CONFIG.REDIS.AUTH) {
46 this.client.auth(CONFIG.REDIS.AUTH)
47 }
48
49 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
50 }
51
52 static getRedisClientOptions () {
53 return Object.assign({},
54 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
55 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
56 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
57 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
58 : { path: CONFIG.REDIS.SOCKET }
59 )
60 }
61
62 getClient () {
63 return this.client
64 }
65
66 getPrefix () {
67 return this.prefix
68 }
69
70 /* ************ Forgot password ************ */
71
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
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
85 return generatedString
86 }
87
88 async removePasswordVerificationString (userId: number) {
89 return this.removeValue(this.generateResetPasswordKey(userId))
90 }
91
92 async getResetPasswordLink (userId: number) {
93 return this.getValue(this.generateResetPasswordKey(userId))
94 }
95
96 /* ************ Email verification ************ */
97
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
110 /* ************ Contact form per IP ************ */
111
112 async setContactFormIp (ip: string) {
113 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
114 }
115
116 async doesContactFormIpExist (ip: string) {
117 return this.exists(this.generateContactFormKey(ip))
118 }
119
120 /* ************ Views per IP ************ */
121
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)
128 }
129
130 async doesVideoIPViewExist (ip: string, videoUUID: string) {
131 return this.exists(this.generateViewKey(ip, videoUUID))
132 }
133
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
144 /* ************ API cache ************ */
145
146 async getCachedRoute (req: express.Request) {
147 const cached = await this.getObject(this.generateCachedRouteKey(req))
148
149 return cached as CachedRoute
150 }
151
152 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
153 const cached: CachedRoute = Object.assign(
154 {},
155 { body: body.toString() },
156 (contentType) ? { contentType } : null,
157 (statusCode) ? { statusCode: statusCode.toString() } : null
158 )
159
160 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
161 }
162
163 /* ************ Video views ************ */
164
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)
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
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
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
230 /* ************ Keys generation ************ */
231
232 generateCachedRouteKey (req: express.Request) {
233 return req.method + '-' + req.originalUrl
234 }
235
236 private generateVideosViewKey (hour?: number) {
237 if (!hour) hour = new Date().getHours()
238
239 return `videos-view-h${hour}`
240 }
241
242 private generateVideoViewKey (videoId: number, hour?: number) {
243 if (hour === undefined || hour === null) hour = new Date().getHours()
244
245 return `video-view-${videoId}-h${hour}`
246 }
247
248 private generateResetPasswordKey (userId: number) {
249 return 'reset-password-' + userId
250 }
251
252 private generateVerifyEmailKey (userId: number) {
253 return 'verify-email-' + userId
254 }
255
256 private generateViewKey (ip: string, videoUUID: string) {
257 return `views-${videoUUID}-${ip}`
258 }
259
260 private generateTrackerBlockIPKey (ip: string) {
261 return `tracker-block-ip-${ip}`
262 }
263
264 private generateContactFormKey (ip: string) {
265 return 'contact-form-' + ip
266 }
267
268 /* ************ Redis helpers ************ */
269
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
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) {
291 return new Promise<void>((res, rej) => {
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
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
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
319 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
320
321 return res()
322 })
323 })
324 }
325
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
336 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
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) {
353 return new Promise<{ [id: string]: string }>((res, rej) => {
354 this.client.hgetall(this.prefix + key, (err, value) => {
355 if (err) return rej(err)
356
357 return res(value)
358 })
359 })
360 }
361
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
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
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
392 static get Instance () {
393 return this.instance || (this.instance = new this())
394 }
395}
396
397// ---------------------------------------------------------------------------
398
399export {
400 Redis
401}