1 import * as express from 'express'
2 import { createClient, RedisClient } from 'redis'
3 import { logger } from '../helpers/logger'
4 import { generateRandomString } from '../helpers/utils'
7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME,
9 USER_PASSWORD_CREATE_LIFETIME,
13 } from '../initializers/constants'
14 import { CONFIG } from '../initializers/config'
24 private static instance: Redis
25 private initialized = false
26 private client: RedisClient
27 private prefix: string
29 private constructor () {
33 // Already initialized
34 if (this.initialized === true) return
35 this.initialized = true
37 this.client = createClient(Redis.getRedisClientOptions())
39 this.client.on('error', err => {
40 logger.error('Error in Redis client.', { err })
44 if (CONFIG.REDIS.AUTH) {
45 this.client.auth(CONFIG.REDIS.AUTH)
48 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
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 }
69 /* ************ Forgot password ************ */
71 async setResetPasswordVerificationString (userId: number) {
72 const generatedString = await generateRandomString(32)
74 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
76 return generatedString
79 async setCreatePasswordVerificationString (userId: number) {
80 const generatedString = await generateRandomString(32)
82 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
84 return generatedString
87 async getResetPasswordLink (userId: number) {
88 return this.getValue(this.generateResetPasswordKey(userId))
91 /* ************ Email verification ************ */
93 async setVerifyEmailVerificationString (userId: number) {
94 const generatedString = await generateRandomString(32)
96 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
98 return generatedString
101 async getVerifyEmailLink (userId: number) {
102 return this.getValue(this.generateVerifyEmailKey(userId))
105 /* ************ Contact form per IP ************ */
107 async setContactFormIp (ip: string) {
108 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
111 async doesContactFormIpExist (ip: string) {
112 return this.exists(this.generateContactFormKey(ip))
115 /* ************ Views per IP ************ */
117 setIPVideoView (ip: string, videoUUID: string) {
118 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
121 async doesVideoIPViewExist (ip: string, videoUUID: string) {
122 return this.exists(this.generateViewKey(ip, videoUUID))
125 /* ************ Tracker IP block ************ */
127 setTrackerBlockIP (ip: string) {
128 return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
131 async doesTrackerBlockIPExist (ip: string) {
132 return this.exists(this.generateTrackerBlockIPKey(ip))
135 /* ************ API cache ************ */
137 async getCachedRoute (req: express.Request) {
138 const cached = await this.getObject(this.generateCachedRouteKey(req))
140 return cached as CachedRoute
143 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
144 const cached: CachedRoute = Object.assign(
146 { body: body.toString() },
147 (contentType) ? { contentType } : null,
148 (statusCode) ? { statusCode: statusCode.toString() } : null
151 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
154 /* ************ Video views ************ */
156 addVideoView (videoId: number) {
157 const keyIncr = this.generateVideoViewKey(videoId)
158 const keySet = this.generateVideosViewKey()
161 this.addToSet(keySet, videoId.toString()),
162 this.increment(keyIncr)
166 async getVideoViews (videoId: number, hour: number) {
167 const key = this.generateVideoViewKey(videoId, hour)
169 const valueString = await this.getValue(key)
170 const valueInt = parseInt(valueString, 10)
172 if (isNaN(valueInt)) {
173 logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
180 async getVideosIdViewed (hour: number) {
181 const key = this.generateVideosViewKey(hour)
183 const stringIds = await this.getSet(key)
184 return stringIds.map(s => parseInt(s, 10))
187 deleteVideoViews (videoId: number, hour: number) {
188 const keySet = this.generateVideosViewKey(hour)
189 const keyIncr = this.generateVideoViewKey(videoId, hour)
192 this.deleteFromSet(keySet, videoId.toString()),
193 this.deleteKey(keyIncr)
197 /* ************ Keys generation ************ */
199 generateCachedRouteKey (req: express.Request) {
200 return req.method + '-' + req.originalUrl
203 private generateVideosViewKey (hour?: number) {
204 if (!hour) hour = new Date().getHours()
206 return `videos-view-h${hour}`
209 private generateVideoViewKey (videoId: number, hour?: number) {
210 if (!hour) hour = new Date().getHours()
212 return `video-view-${videoId}-h${hour}`
215 private generateResetPasswordKey (userId: number) {
216 return 'reset-password-' + userId
219 private generateVerifyEmailKey (userId: number) {
220 return 'verify-email-' + userId
223 private generateViewKey (ip: string, videoUUID: string) {
224 return `views-${videoUUID}-${ip}`
227 private generateTrackerBlockIPKey (ip: string) {
228 return `tracker-block-ip-${ip}`
231 private generateContactFormKey (ip: string) {
232 return 'contact-form-' + ip
235 /* ************ Redis helpers ************ */
237 private getValue (key: string) {
238 return new Promise<string>((res, rej) => {
239 this.client.get(this.prefix + key, (err, value) => {
240 if (err) return rej(err)
247 private getSet (key: string) {
248 return new Promise<string[]>((res, rej) => {
249 this.client.smembers(this.prefix + key, (err, value) => {
250 if (err) return rej(err)
257 private addToSet (key: string, value: string) {
258 return new Promise<string[]>((res, rej) => {
259 this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
263 private deleteFromSet (key: string, value: string) {
264 return new Promise<void>((res, rej) => {
265 this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
269 private deleteKey (key: string) {
270 return new Promise<void>((res, rej) => {
271 this.client.del(this.prefix + key, err => err ? rej(err) : res())
275 private deleteFieldInHash (key: string, field: string) {
276 return new Promise<void>((res, rej) => {
277 this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res())
281 private setValue (key: string, value: string, expirationMilliseconds: number) {
282 return new Promise<void>((res, rej) => {
283 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
284 if (err) return rej(err)
286 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
293 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
294 return new Promise<void>((res, rej) => {
295 this.client.hmset(this.prefix + key, obj, (err, ok) => {
296 if (err) return rej(err)
297 if (!ok) return rej(new Error('Redis mset result is not OK.'))
299 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
300 if (err) return rej(err)
301 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
309 private getObject (key: string) {
310 return new Promise<{ [id: string]: string }>((res, rej) => {
311 this.client.hgetall(this.prefix + key, (err, value) => {
312 if (err) return rej(err)
319 private setValueInHash (key: string, field: string, value: string) {
320 return new Promise<void>((res, rej) => {
321 this.client.hset(this.prefix + key, field, value, (err) => {
322 if (err) return rej(err)
329 private increment (key: string) {
330 return new Promise<number>((res, rej) => {
331 this.client.incr(this.prefix + key, (err, value) => {
332 if (err) return rej(err)
339 private exists (key: string) {
340 return new Promise<boolean>((res, rej) => {
341 this.client.exists(this.prefix + key, (err, existsNumber) => {
342 if (err) return rej(err)
344 return res(existsNumber === 1)
349 static get Instance () {
350 return this.instance || (this.instance = new this())
354 // ---------------------------------------------------------------------------