1 import { createClient, RedisClientOptions, RedisModules } from 'redis'
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 USER_EMAIL_VERIFY_LIFETIME,
13 USER_PASSWORD_CREATE_LIFETIME,
14 USER_PASSWORD_RESET_LIFETIME,
17 } from '../initializers/constants'
19 // Only used for typings
20 // TODO: remove when https://github.com/microsoft/TypeScript/issues/37181 is fixed
21 const redisClientWrapperForType = () => createClient<{}>()
25 private static instance: Redis
26 private initialized = false
27 private connected = false
28 private client: ReturnType<typeof redisClientWrapperForType>
29 private prefix: string
31 private constructor () {
35 // Already initialized
36 if (this.initialized === true) return
37 this.initialized = true
39 this.client = createClient(Redis.getRedisClientOptions())
41 logger.info('Connecting to redis...')
45 logger.info('Connected to redis.')
49 logger.error('Cannot connect to redis', { err })
53 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
56 static getRedisClientOptions () {
57 let config: RedisClientOptions<RedisModules, {}> = {
59 connectTimeout: 20000 // Could be slow since node use sync call to compile PeerTube
63 if (CONFIG.REDIS.AUTH) {
64 config = { ...config, password: CONFIG.REDIS.AUTH }
67 if (CONFIG.REDIS.DB) {
68 config = { ...config, database: CONFIG.REDIS.DB }
71 if (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) {
72 config.socket = { ...config.socket, host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
74 config.socket = { ...config.socket, path: CONFIG.REDIS.SOCKET }
92 /* ************ Forgot password ************ */
94 async setResetPasswordVerificationString (userId: number) {
95 const generatedString = await generateRandomString(32)
97 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
99 return generatedString
102 async setCreatePasswordVerificationString (userId: number) {
103 const generatedString = await generateRandomString(32)
105 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
107 return generatedString
110 async removePasswordVerificationString (userId: number) {
111 return this.removeValue(this.generateResetPasswordKey(userId))
114 async getResetPasswordLink (userId: number) {
115 return this.getValue(this.generateResetPasswordKey(userId))
118 /* ************ Email verification ************ */
120 async setVerifyEmailVerificationString (userId: number) {
121 const generatedString = await generateRandomString(32)
123 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
125 return generatedString
128 async getVerifyEmailLink (userId: number) {
129 return this.getValue(this.generateVerifyEmailKey(userId))
132 /* ************ Contact form per IP ************ */
134 async setContactFormIp (ip: string) {
135 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
138 async doesContactFormIpExist (ip: string) {
139 return this.exists(this.generateContactFormKey(ip))
142 /* ************ Views per IP ************ */
144 setIPVideoView (ip: string, videoUUID: string) {
145 return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW)
148 setIPVideoViewer (ip: string, videoUUID: string) {
149 return this.setValue(this.generateIPViewerKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEWER)
152 async doesVideoIPViewExist (ip: string, videoUUID: string) {
153 return this.exists(this.generateIPViewKey(ip, videoUUID))
156 async doesVideoIPViewerExist (ip: string, videoUUID: string) {
157 return this.exists(this.generateIPViewerKey(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 /* ************ Resumable uploads final responses ************ */
254 setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) {
255 return this.setValue(
256 'resumable-upload-' + uploadId,
258 ? JSON.stringify(response)
260 RESUMABLE_UPLOAD_SESSION_LIFETIME
264 doesUploadSessionExist (uploadId: string) {
265 return this.exists('resumable-upload-' + uploadId)
268 async getUploadSession (uploadId: string) {
269 const value = await this.getValue('resumable-upload-' + uploadId)
276 deleteUploadSession (uploadId: string) {
277 return this.deleteKey('resumable-upload-' + uploadId)
280 /* ************ AP ressource unavailability ************ */
282 async addAPUnavailability (url: string) {
283 const key = this.generateAPUnavailabilityKey(url)
285 const value = await this.increment(key)
286 await this.setExpiration(key, AP_CLEANER.PERIOD * 2)
291 /* ************ Keys generation ************ */
293 private generateLocalVideoViewsKeys (videoId?: Number) {
294 return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` }
297 private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) {
298 const hour = exists(options.hour)
300 : new Date().getHours()
302 return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` }
305 private generateResetPasswordKey (userId: number) {
306 return 'reset-password-' + userId
309 private generateVerifyEmailKey (userId: number) {
310 return 'verify-email-' + userId
313 private generateIPViewKey (ip: string, videoUUID: string) {
314 return `views-${videoUUID}-${ip}`
317 private generateIPViewerKey (ip: string, videoUUID: string) {
318 return `viewer-${videoUUID}-${ip}`
321 private generateTrackerBlockIPKey (ip: string) {
322 return `tracker-block-ip-${ip}`
325 private generateContactFormKey (ip: string) {
326 return 'contact-form-' + ip
329 private generateAPUnavailabilityKey (url: string) {
330 return 'ap-unavailability-' + sha256(url)
333 /* ************ Redis helpers ************ */
335 private getValue (key: string) {
336 return this.client.get(this.prefix + key)
339 private getSet (key: string) {
340 return this.client.sMembers(this.prefix + key)
343 private addToSet (key: string, value: string) {
344 return this.client.sAdd(this.prefix + key, value)
347 private deleteFromSet (key: string, value: string) {
348 return this.client.sRem(this.prefix + key, value)
351 private deleteKey (key: string) {
352 return this.client.del(this.prefix + key)
355 private async setValue (key: string, value: string, expirationMilliseconds: number) {
356 const result = await this.client.set(this.prefix + key, value, { PX: expirationMilliseconds })
358 if (result !== 'OK') throw new Error('Redis set result is not OK.')
361 private removeValue (key: string) {
362 return this.client.del(this.prefix + key)
365 private increment (key: string) {
366 return this.client.incr(this.prefix + key)
369 private async exists (key: string) {
370 const result = await this.client.exists(this.prefix + key)
375 private setExpiration (key: string, ms: number) {
376 return this.client.expire(this.prefix + key, ms / 1000)
379 static get Instance () {
380 return this.instance || (this.instance = new this())
384 // ---------------------------------------------------------------------------