1 import IoRedis, { RedisOptions } from 'ioredis'
2 import { exists } from '@server/helpers/custom-validators/misc'
3 import { sha256 } from '@shared/extra-utils'
4 import { logger } from '../helpers/logger'
5 import { generateRandomString } from '../helpers/utils'
6 import { CONFIG } from '../initializers/config'
10 RESUMABLE_UPLOAD_SESSION_LIFETIME,
12 TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME,
13 USER_EMAIL_VERIFY_LIFETIME,
14 USER_PASSWORD_CREATE_LIFETIME,
15 USER_PASSWORD_RESET_LIFETIME,
18 } from '../initializers/constants'
22 private static instance: Redis
23 private initialized = false
24 private connected = false
25 private client: IoRedis
26 private prefix: string
28 private constructor () {
32 // Already initialized
33 if (this.initialized === true) return
34 this.initialized = true
36 logger.info('Connecting to redis...')
38 this.client = new IoRedis(Redis.getRedisClientOptions('', { enableAutoPipelining: true }))
39 this.client.on('error', err => logger.error('Redis failed to connect', { err }))
40 this.client.on('connect', () => {
41 logger.info('Connected to redis.')
45 this.client.on('reconnecting', (ms) => {
46 logger.error(`Reconnecting to redis in ${ms}.`)
48 this.client.on('close', () => {
49 logger.error('Connection to redis has closed.')
50 this.connected = false
53 this.client.on('end', () => {
54 logger.error('Connection to redis has closed and no more reconnects will be done.')
57 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
60 static getRedisClientOptions (connectionName?: string, options: RedisOptions = {}): RedisOptions {
62 connectionName: [ 'PeerTube', connectionName ].join(''),
63 connectTimeout: 20000, // Could be slow since node use sync call to compile PeerTube
64 password: CONFIG.REDIS.AUTH,
66 host: CONFIG.REDIS.HOSTNAME,
67 port: CONFIG.REDIS.PORT,
68 path: CONFIG.REDIS.SOCKET,
69 showFriendlyErrorStack: true,
86 /* ************ Forgot password ************ */
88 async setResetPasswordVerificationString (userId: number) {
89 const generatedString = await generateRandomString(32)
91 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
93 return generatedString
96 async setCreatePasswordVerificationString (userId: number) {
97 const generatedString = await generateRandomString(32)
99 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
101 return generatedString
104 async removePasswordVerificationString (userId: number) {
105 return this.removeValue(this.generateResetPasswordKey(userId))
108 async getResetPasswordVerificationString (userId: number) {
109 return this.getValue(this.generateResetPasswordKey(userId))
112 /* ************ Two factor auth request ************ */
114 async setTwoFactorRequest (userId: number, otpSecret: string) {
115 const requestToken = await generateRandomString(32)
117 await this.setValue(this.generateTwoFactorRequestKey(userId, requestToken), otpSecret, TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME)
122 async getTwoFactorRequestToken (userId: number, requestToken: string) {
123 return this.getValue(this.generateTwoFactorRequestKey(userId, requestToken))
126 /* ************ Email verification ************ */
128 async setVerifyEmailVerificationString (userId: number) {
129 const generatedString = await generateRandomString(32)
131 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
133 return generatedString
136 async getVerifyEmailLink (userId: number) {
137 return this.getValue(this.generateVerifyEmailKey(userId))
140 /* ************ Contact form per IP ************ */
142 async setContactFormIp (ip: string) {
143 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
146 async doesContactFormIpExist (ip: string) {
147 return this.exists(this.generateContactFormKey(ip))
150 /* ************ Views per IP ************ */
152 setIPVideoView (ip: string, videoUUID: string) {
153 return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW)
156 async doesVideoIPViewExist (ip: string, videoUUID: string) {
157 return this.exists(this.generateIPViewKey(ip, videoUUID))
160 /* ************ Tracker IP block ************ */
162 setTrackerBlockIP (ip: string) {
163 return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
166 async doesTrackerBlockIPExist (ip: string) {
167 return this.exists(this.generateTrackerBlockIPKey(ip))
170 /* ************ Video views stats ************ */
172 addVideoViewStats (videoId: number) {
173 const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId })
176 this.addToSet(setKey, videoId.toString()),
177 this.increment(videoKey)
181 async getVideoViewsStats (videoId: number, hour: number) {
182 const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
184 const valueString = await this.getValue(videoKey)
185 const valueInt = parseInt(valueString, 10)
187 if (isNaN(valueInt)) {
188 logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
195 async listVideosViewedForStats (hour: number) {
196 const { setKey } = this.generateVideoViewStatsKeys({ hour })
198 const stringIds = await this.getSet(setKey)
199 return stringIds.map(s => parseInt(s, 10))
202 deleteVideoViewsStats (videoId: number, hour: number) {
203 const { setKey, videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
206 this.deleteFromSet(setKey, videoId.toString()),
207 this.deleteKey(videoKey)
211 /* ************ Local video views buffer ************ */
213 addLocalVideoView (videoId: number) {
214 const { videoKey, setKey } = this.generateLocalVideoViewsKeys(videoId)
217 this.addToSet(setKey, videoId.toString()),
218 this.increment(videoKey)
222 async getLocalVideoViews (videoId: number) {
223 const { videoKey } = this.generateLocalVideoViewsKeys(videoId)
225 const valueString = await this.getValue(videoKey)
226 const valueInt = parseInt(valueString, 10)
228 if (isNaN(valueInt)) {
229 logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString)
236 async listLocalVideosViewed () {
237 const { setKey } = this.generateLocalVideoViewsKeys()
239 const stringIds = await this.getSet(setKey)
240 return stringIds.map(s => parseInt(s, 10))
243 deleteLocalVideoViews (videoId: number) {
244 const { setKey, videoKey } = this.generateLocalVideoViewsKeys(videoId)
247 this.deleteFromSet(setKey, videoId.toString()),
248 this.deleteKey(videoKey)
252 /* ************ Video viewers stats ************ */
254 getLocalVideoViewer (options: {
260 if (options.key) return this.getObject(options.key)
262 const { viewerKey } = this.generateLocalVideoViewerKeys(options.ip, options.videoId)
264 return this.getObject(viewerKey)
267 setLocalVideoViewer (ip: string, videoId: number, object: any) {
268 const { setKey, viewerKey } = this.generateLocalVideoViewerKeys(ip, videoId)
271 this.addToSet(setKey, viewerKey),
272 this.setObject(viewerKey, object)
276 listLocalVideoViewerKeys () {
277 const { setKey } = this.generateLocalVideoViewerKeys()
279 return this.getSet(setKey)
282 deleteLocalVideoViewersKeys (key: string) {
283 const { setKey } = this.generateLocalVideoViewerKeys()
286 this.deleteFromSet(setKey, key),
291 /* ************ Resumable uploads final responses ************ */
293 setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) {
294 return this.setValue(
295 'resumable-upload-' + uploadId,
297 ? JSON.stringify(response)
299 RESUMABLE_UPLOAD_SESSION_LIFETIME
303 doesUploadSessionExist (uploadId: string) {
304 return this.exists('resumable-upload-' + uploadId)
307 async getUploadSession (uploadId: string) {
308 const value = await this.getValue('resumable-upload-' + uploadId)
315 deleteUploadSession (uploadId: string) {
316 return this.deleteKey('resumable-upload-' + uploadId)
319 /* ************ AP resource unavailability ************ */
321 async addAPUnavailability (url: string) {
322 const key = this.generateAPUnavailabilityKey(url)
324 const value = await this.increment(key)
325 await this.setExpiration(key, AP_CLEANER.PERIOD * 2)
330 /* ************ Keys generation ************ */
332 private generateLocalVideoViewsKeys (videoId: number): { setKey: string, videoKey: string }
333 private generateLocalVideoViewsKeys (): { setKey: string }
334 private generateLocalVideoViewsKeys (videoId?: number) {
335 return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` }
338 private generateLocalVideoViewerKeys (ip: string, videoId: number): { setKey: string, viewerKey: string }
339 private generateLocalVideoViewerKeys (): { setKey: string }
340 private generateLocalVideoViewerKeys (ip?: string, videoId?: number) {
341 return { setKey: `local-video-viewer-stats-keys`, viewerKey: `local-video-viewer-stats-${ip}-${videoId}` }
344 private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) {
345 const hour = exists(options.hour)
347 : new Date().getHours()
349 return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` }
352 private generateResetPasswordKey (userId: number) {
353 return 'reset-password-' + userId
356 private generateTwoFactorRequestKey (userId: number, token: string) {
357 return 'two-factor-request-' + userId + '-' + token
360 private generateVerifyEmailKey (userId: number) {
361 return 'verify-email-' + userId
364 private generateIPViewKey (ip: string, videoUUID: string) {
365 return `views-${videoUUID}-${ip}`
368 private generateTrackerBlockIPKey (ip: string) {
369 return `tracker-block-ip-${ip}`
372 private generateContactFormKey (ip: string) {
373 return 'contact-form-' + ip
376 private generateAPUnavailabilityKey (url: string) {
377 return 'ap-unavailability-' + sha256(url)
380 /* ************ Redis helpers ************ */
382 private getValue (key: string) {
383 return this.client.get(this.prefix + key)
386 private getSet (key: string) {
387 return this.client.smembers(this.prefix + key)
390 private addToSet (key: string, value: string) {
391 return this.client.sadd(this.prefix + key, value)
394 private deleteFromSet (key: string, value: string) {
395 return this.client.srem(this.prefix + key, value)
398 private deleteKey (key: string) {
399 return this.client.del(this.prefix + key)
402 private async getObject (key: string) {
403 const value = await this.getValue(key)
404 if (!value) return null
406 return JSON.parse(value)
409 private setObject (key: string, value: { [ id: string ]: number | string }, expirationMilliseconds?: number) {
410 return this.setValue(key, JSON.stringify(value), expirationMilliseconds)
413 private async setValue (key: string, value: string, expirationMilliseconds?: number) {
414 const result = expirationMilliseconds !== undefined
415 ? await this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds)
416 : await this.client.set(this.prefix + key, value)
418 if (result !== 'OK') throw new Error('Redis set result is not OK.')
421 private removeValue (key: string) {
422 return this.client.del(this.prefix + key)
425 private increment (key: string) {
426 return this.client.incr(this.prefix + key)
429 private async exists (key: string) {
430 const result = await this.client.exists(this.prefix + key)
435 private setExpiration (key: string, ms: number) {
436 return this.client.expire(this.prefix + key, ms / 1000)
439 static get Instance () {
440 return this.instance || (this.instance = new this())
444 // ---------------------------------------------------------------------------