]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/redis.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
CommitLineData
564b9b55 1import IoRedis, { RedisOptions } from 'ioredis'
e5d91a9b 2import { exists } from '@server/helpers/custom-validators/misc'
f1569117 3import { sha256 } from '@shared/extra-utils'
ecb4e35f
C
4import { logger } from '../helpers/logger'
5import { generateRandomString } from '../helpers/utils'
e5d91a9b 6import { CONFIG } from '../initializers/config'
a4101923 7import {
f1569117 8 AP_CLEANER,
a4101923 9 CONTACT_FORM_LIFETIME,
e5d91a9b 10 RESUMABLE_UPLOAD_SESSION_LIFETIME,
56f47830 11 TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME,
e364e31e 12 EMAIL_VERIFY_LIFETIME,
45f1bd72 13 USER_PASSWORD_CREATE_LIFETIME,
e5d91a9b 14 USER_PASSWORD_RESET_LIFETIME,
e4bf7856 15 VIEW_LIFETIME,
e5d91a9b 16 WEBSERVER
74dc3bca 17} from '../initializers/constants'
4195cd2b 18
ecb4e35f
C
19class Redis {
20
21 private static instance: Redis
22 private initialized = false
e5d91a9b 23 private connected = false
564b9b55 24 private client: IoRedis
ecb4e35f
C
25 private prefix: string
26
a1587156
C
27 private constructor () {
28 }
ecb4e35f
C
29
30 init () {
31 // Already initialized
32 if (this.initialized === true) return
33 this.initialized = true
34
8f5a1f36
C
35 logger.info('Connecting to redis...')
36
564b9b55 37 this.client = new IoRedis(Redis.getRedisClientOptions('', { enableAutoPipelining: true }))
38 this.client.on('error', err => logger.error('Redis failed to connect', { err }))
39 this.client.on('connect', () => {
40 logger.info('Connected to redis.')
41
42 this.connected = true
43 })
44 this.client.on('reconnecting', (ms) => {
45 logger.error(`Reconnecting to redis in ${ms}.`)
46 })
47 this.client.on('close', () => {
48 logger.error('Connection to redis has closed.')
49 this.connected = false
50 })
51
52 this.client.on('end', () => {
53 logger.error('Connection to redis has closed and no more reconnects will be done.')
54 })
e5d91a9b 55
6dd9de95 56 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
ecb4e35f
C
57 }
58
564b9b55 59 static getRedisClientOptions (connectionName?: string, options: RedisOptions = {}): RedisOptions {
60 return {
61 connectionName: [ 'PeerTube', connectionName ].join(''),
62 connectTimeout: 20000, // Could be slow since node use sync call to compile PeerTube
63 password: CONFIG.REDIS.AUTH,
64 db: CONFIG.REDIS.DB,
65 host: CONFIG.REDIS.HOSTNAME,
66 port: CONFIG.REDIS.PORT,
67 path: CONFIG.REDIS.SOCKET,
68 showFriendlyErrorStack: true,
69 ...options
8f5a1f36 70 }
19f7b248
RK
71 }
72
47f6409b
C
73 getClient () {
74 return this.client
75 }
76
77 getPrefix () {
78 return this.prefix
79 }
80
e5d91a9b
C
81 isConnected () {
82 return this.connected
83 }
84
a1587156 85 /* ************ Forgot password ************ */
6e46de09 86
ecb4e35f
C
87 async setResetPasswordVerificationString (userId: number) {
88 const generatedString = await generateRandomString(32)
89
90 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
91
45f1bd72
JL
92 return generatedString
93 }
94
95 async setCreatePasswordVerificationString (userId: number) {
96 const generatedString = await generateRandomString(32)
97
98 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
99
ecb4e35f
C
100 return generatedString
101 }
102
e9c5f123
C
103 async removePasswordVerificationString (userId: number) {
104 return this.removeValue(this.generateResetPasswordKey(userId))
105 }
106
56f47830 107 async getResetPasswordVerificationString (userId: number) {
ecb4e35f
C
108 return this.getValue(this.generateResetPasswordKey(userId))
109 }
110
56f47830
C
111 /* ************ Two factor auth request ************ */
112
113 async setTwoFactorRequest (userId: number, otpSecret: string) {
114 const requestToken = await generateRandomString(32)
115
116 await this.setValue(this.generateTwoFactorRequestKey(userId, requestToken), otpSecret, TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME)
117
118 return requestToken
119 }
120
121 async getTwoFactorRequestToken (userId: number, requestToken: string) {
122 return this.getValue(this.generateTwoFactorRequestKey(userId, requestToken))
123 }
124
a1587156 125 /* ************ Email verification ************ */
6e46de09 126
e364e31e 127 async setUserVerifyEmailVerificationString (userId: number) {
d9eaee39
JM
128 const generatedString = await generateRandomString(32)
129
e364e31e 130 await this.setValue(this.generateUserVerifyEmailKey(userId), generatedString, EMAIL_VERIFY_LIFETIME)
d9eaee39
JM
131
132 return generatedString
133 }
134
e364e31e
C
135 async getUserVerifyEmailLink (userId: number) {
136 return this.getValue(this.generateUserVerifyEmailKey(userId))
137 }
138
139 async setRegistrationVerifyEmailVerificationString (registrationId: number) {
140 const generatedString = await generateRandomString(32)
141
142 await this.setValue(this.generateRegistrationVerifyEmailKey(registrationId), generatedString, EMAIL_VERIFY_LIFETIME)
143
144 return generatedString
145 }
146
147 async getRegistrationVerifyEmailLink (registrationId: number) {
148 return this.getValue(this.generateRegistrationVerifyEmailKey(registrationId))
d9eaee39
JM
149 }
150
a1587156 151 /* ************ Contact form per IP ************ */
a4101923
C
152
153 async setContactFormIp (ip: string) {
154 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
155 }
156
0f6acda1 157 async doesContactFormIpExist (ip: string) {
a4101923
C
158 return this.exists(this.generateContactFormKey(ip))
159 }
160
a1587156 161 /* ************ Views per IP ************ */
6e46de09 162
51353d9a
C
163 setIPVideoView (ip: string, videoUUID: string) {
164 return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW)
165 }
e4bf7856 166
0f6acda1 167 async doesVideoIPViewExist (ip: string, videoUUID: string) {
51353d9a
C
168 return this.exists(this.generateIPViewKey(ip, videoUUID))
169 }
170
51353d9a 171 /* ************ Video views stats ************ */
6e46de09 172
51353d9a
C
173 addVideoViewStats (videoId: number) {
174 const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId })
6b616860
C
175
176 return Promise.all([
51353d9a
C
177 this.addToSet(setKey, videoId.toString()),
178 this.increment(videoKey)
6b616860
C
179 ])
180 }
181
51353d9a
C
182 async getVideoViewsStats (videoId: number, hour: number) {
183 const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
6b616860 184
51353d9a 185 const valueString = await this.getValue(videoKey)
6040f87d
C
186 const valueInt = parseInt(valueString, 10)
187
188 if (isNaN(valueInt)) {
51353d9a 189 logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
6040f87d
C
190 return undefined
191 }
192
193 return valueInt
6b616860
C
194 }
195
51353d9a
C
196 async listVideosViewedForStats (hour: number) {
197 const { setKey } = this.generateVideoViewStatsKeys({ hour })
6b616860 198
51353d9a 199 const stringIds = await this.getSet(setKey)
6b616860
C
200 return stringIds.map(s => parseInt(s, 10))
201 }
202
51353d9a
C
203 deleteVideoViewsStats (videoId: number, hour: number) {
204 const { setKey, videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
205
206 return Promise.all([
207 this.deleteFromSet(setKey, videoId.toString()),
208 this.deleteKey(videoKey)
209 ])
210 }
211
212 /* ************ Local video views buffer ************ */
213
214 addLocalVideoView (videoId: number) {
215 const { videoKey, setKey } = this.generateLocalVideoViewsKeys(videoId)
6b616860
C
216
217 return Promise.all([
51353d9a
C
218 this.addToSet(setKey, videoId.toString()),
219 this.increment(videoKey)
220 ])
221 }
222
223 async getLocalVideoViews (videoId: number) {
224 const { videoKey } = this.generateLocalVideoViewsKeys(videoId)
225
226 const valueString = await this.getValue(videoKey)
227 const valueInt = parseInt(valueString, 10)
228
229 if (isNaN(valueInt)) {
230 logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString)
231 return undefined
232 }
233
234 return valueInt
235 }
236
237 async listLocalVideosViewed () {
238 const { setKey } = this.generateLocalVideoViewsKeys()
239
240 const stringIds = await this.getSet(setKey)
241 return stringIds.map(s => parseInt(s, 10))
242 }
243
244 deleteLocalVideoViews (videoId: number) {
245 const { setKey, videoKey } = this.generateLocalVideoViewsKeys(videoId)
246
247 return Promise.all([
248 this.deleteFromSet(setKey, videoId.toString()),
249 this.deleteKey(videoKey)
6b616860
C
250 ])
251 }
252
b2111066
C
253 /* ************ Video viewers stats ************ */
254
255 getLocalVideoViewer (options: {
256 key?: string
257 // Or
258 ip?: string
259 videoId?: number
260 }) {
261 if (options.key) return this.getObject(options.key)
262
263 const { viewerKey } = this.generateLocalVideoViewerKeys(options.ip, options.videoId)
264
265 return this.getObject(viewerKey)
266 }
267
268 setLocalVideoViewer (ip: string, videoId: number, object: any) {
269 const { setKey, viewerKey } = this.generateLocalVideoViewerKeys(ip, videoId)
270
271 return Promise.all([
272 this.addToSet(setKey, viewerKey),
273 this.setObject(viewerKey, object)
274 ])
275 }
276
277 listLocalVideoViewerKeys () {
278 const { setKey } = this.generateLocalVideoViewerKeys()
279
280 return this.getSet(setKey)
281 }
282
283 deleteLocalVideoViewersKeys (key: string) {
284 const { setKey } = this.generateLocalVideoViewerKeys()
285
286 return Promise.all([
287 this.deleteFromSet(setKey, key),
288 this.deleteKey(key)
289 ])
290 }
291
276250f0
RK
292 /* ************ Resumable uploads final responses ************ */
293
294 setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) {
295 return this.setValue(
296 'resumable-upload-' + uploadId,
297 response
298 ? JSON.stringify(response)
299 : '',
300 RESUMABLE_UPLOAD_SESSION_LIFETIME
301 )
302 }
303
304 doesUploadSessionExist (uploadId: string) {
305 return this.exists('resumable-upload-' + uploadId)
306 }
307
308 async getUploadSession (uploadId: string) {
309 const value = await this.getValue('resumable-upload-' + uploadId)
310
311 return value
312 ? JSON.parse(value)
313 : ''
314 }
315
020d3d3d
C
316 deleteUploadSession (uploadId: string) {
317 return this.deleteKey('resumable-upload-' + uploadId)
318 }
319
7a4fd56c 320 /* ************ AP resource unavailability ************ */
f1569117
C
321
322 async addAPUnavailability (url: string) {
323 const key = this.generateAPUnavailabilityKey(url)
324
325 const value = await this.increment(key)
326 await this.setExpiration(key, AP_CLEANER.PERIOD * 2)
327
328 return value
329 }
330
a1587156 331 /* ************ Keys generation ************ */
6e46de09 332
b2111066
C
333 private generateLocalVideoViewsKeys (videoId: number): { setKey: string, videoKey: string }
334 private generateLocalVideoViewsKeys (): { setKey: string }
335 private generateLocalVideoViewsKeys (videoId?: number) {
51353d9a 336 return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` }
6b616860
C
337 }
338
b2111066
C
339 private generateLocalVideoViewerKeys (ip: string, videoId: number): { setKey: string, viewerKey: string }
340 private generateLocalVideoViewerKeys (): { setKey: string }
341 private generateLocalVideoViewerKeys (ip?: string, videoId?: number) {
342 return { setKey: `local-video-viewer-stats-keys`, viewerKey: `local-video-viewer-stats-${ip}-${videoId}` }
343 }
344
51353d9a
C
345 private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) {
346 const hour = exists(options.hour)
347 ? options.hour
348 : new Date().getHours()
6b616860 349
51353d9a 350 return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` }
6b616860
C
351 }
352
6e46de09 353 private generateResetPasswordKey (userId: number) {
b40f0575
C
354 return 'reset-password-' + userId
355 }
356
56f47830
C
357 private generateTwoFactorRequestKey (userId: number, token: string) {
358 return 'two-factor-request-' + userId + '-' + token
359 }
360
e364e31e
C
361 private generateUserVerifyEmailKey (userId: number) {
362 return 'verify-email-user-' + userId
363 }
364
365 private generateRegistrationVerifyEmailKey (registrationId: number) {
366 return 'verify-email-registration-' + registrationId
d9eaee39
JM
367 }
368
51353d9a 369 private generateIPViewKey (ip: string, videoUUID: string) {
a4101923
C
370 return `views-${videoUUID}-${ip}`
371 }
372
373 private generateContactFormKey (ip: string) {
374 return 'contact-form-' + ip
b40f0575
C
375 }
376
f1569117
C
377 private generateAPUnavailabilityKey (url: string) {
378 return 'ap-unavailability-' + sha256(url)
379 }
380
a1587156 381 /* ************ Redis helpers ************ */
b40f0575 382
ecb4e35f 383 private getValue (key: string) {
e5d91a9b 384 return this.client.get(this.prefix + key)
ecb4e35f
C
385 }
386
6b616860 387 private getSet (key: string) {
564b9b55 388 return this.client.smembers(this.prefix + key)
6b616860
C
389 }
390
391 private addToSet (key: string, value: string) {
564b9b55 392 return this.client.sadd(this.prefix + key, value)
6b616860
C
393 }
394
395 private deleteFromSet (key: string, value: string) {
564b9b55 396 return this.client.srem(this.prefix + key, value)
6b616860
C
397 }
398
399 private deleteKey (key: string) {
e5d91a9b 400 return this.client.del(this.prefix + key)
6e46de09
C
401 }
402
b2111066
C
403 private async getObject (key: string) {
404 const value = await this.getValue(key)
405 if (!value) return null
406
407 return JSON.parse(value)
408 }
409
56f47830
C
410 private setObject (key: string, value: { [ id: string ]: number | string }, expirationMilliseconds?: number) {
411 return this.setValue(key, JSON.stringify(value), expirationMilliseconds)
b2111066
C
412 }
413
414 private async setValue (key: string, value: string, expirationMilliseconds?: number) {
90dbc731
C
415 const result = expirationMilliseconds !== undefined
416 ? await this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds)
417 : await this.client.set(this.prefix + key, value)
ecb4e35f 418
e5d91a9b 419 if (result !== 'OK') throw new Error('Redis set result is not OK.')
ecb4e35f
C
420 }
421
e9c5f123 422 private removeValue (key: string) {
e5d91a9b 423 return this.client.del(this.prefix + key)
4195cd2b
C
424 }
425
6b616860 426 private increment (key: string) {
e5d91a9b 427 return this.client.incr(this.prefix + key)
6b616860
C
428 }
429
c0d2eac3
C
430 private async exists (key: string) {
431 const result = await this.client.exists(this.prefix + key)
432
433 return result !== 0
b5c0e955
C
434 }
435
f1569117
C
436 private setExpiration (key: string, ms: number) {
437 return this.client.expire(this.prefix + key, ms / 1000)
438 }
439
ecb4e35f
C
440 static get Instance () {
441 return this.instance || (this.instance = new this())
442 }
443}
444
445// ---------------------------------------------------------------------------
446
447export {
448 Redis
449}