]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/redis.ts
Don't need to use redis to block tracker ips
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
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'
7 import {
8 AP_CLEANER,
9 CONTACT_FORM_LIFETIME,
10 RESUMABLE_UPLOAD_SESSION_LIFETIME,
11 TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME,
12 USER_EMAIL_VERIFY_LIFETIME,
13 USER_PASSWORD_CREATE_LIFETIME,
14 USER_PASSWORD_RESET_LIFETIME,
15 VIEW_LIFETIME,
16 WEBSERVER
17 } from '../initializers/constants'
18
19 class Redis {
20
21 private static instance: Redis
22 private initialized = false
23 private connected = false
24 private client: IoRedis
25 private prefix: string
26
27 private constructor () {
28 }
29
30 init () {
31 // Already initialized
32 if (this.initialized === true) return
33 this.initialized = true
34
35 logger.info('Connecting to redis...')
36
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 })
55
56 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
57 }
58
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
70 }
71 }
72
73 getClient () {
74 return this.client
75 }
76
77 getPrefix () {
78 return this.prefix
79 }
80
81 isConnected () {
82 return this.connected
83 }
84
85 /* ************ Forgot password ************ */
86
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
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
100 return generatedString
101 }
102
103 async removePasswordVerificationString (userId: number) {
104 return this.removeValue(this.generateResetPasswordKey(userId))
105 }
106
107 async getResetPasswordVerificationString (userId: number) {
108 return this.getValue(this.generateResetPasswordKey(userId))
109 }
110
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
125 /* ************ Email verification ************ */
126
127 async setVerifyEmailVerificationString (userId: number) {
128 const generatedString = await generateRandomString(32)
129
130 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
131
132 return generatedString
133 }
134
135 async getVerifyEmailLink (userId: number) {
136 return this.getValue(this.generateVerifyEmailKey(userId))
137 }
138
139 /* ************ Contact form per IP ************ */
140
141 async setContactFormIp (ip: string) {
142 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
143 }
144
145 async doesContactFormIpExist (ip: string) {
146 return this.exists(this.generateContactFormKey(ip))
147 }
148
149 /* ************ Views per IP ************ */
150
151 setIPVideoView (ip: string, videoUUID: string) {
152 return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW)
153 }
154
155 async doesVideoIPViewExist (ip: string, videoUUID: string) {
156 return this.exists(this.generateIPViewKey(ip, videoUUID))
157 }
158
159 /* ************ Video views stats ************ */
160
161 addVideoViewStats (videoId: number) {
162 const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId })
163
164 return Promise.all([
165 this.addToSet(setKey, videoId.toString()),
166 this.increment(videoKey)
167 ])
168 }
169
170 async getVideoViewsStats (videoId: number, hour: number) {
171 const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
172
173 const valueString = await this.getValue(videoKey)
174 const valueInt = parseInt(valueString, 10)
175
176 if (isNaN(valueInt)) {
177 logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
178 return undefined
179 }
180
181 return valueInt
182 }
183
184 async listVideosViewedForStats (hour: number) {
185 const { setKey } = this.generateVideoViewStatsKeys({ hour })
186
187 const stringIds = await this.getSet(setKey)
188 return stringIds.map(s => parseInt(s, 10))
189 }
190
191 deleteVideoViewsStats (videoId: number, hour: number) {
192 const { setKey, videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
193
194 return Promise.all([
195 this.deleteFromSet(setKey, videoId.toString()),
196 this.deleteKey(videoKey)
197 ])
198 }
199
200 /* ************ Local video views buffer ************ */
201
202 addLocalVideoView (videoId: number) {
203 const { videoKey, setKey } = this.generateLocalVideoViewsKeys(videoId)
204
205 return Promise.all([
206 this.addToSet(setKey, videoId.toString()),
207 this.increment(videoKey)
208 ])
209 }
210
211 async getLocalVideoViews (videoId: number) {
212 const { videoKey } = this.generateLocalVideoViewsKeys(videoId)
213
214 const valueString = await this.getValue(videoKey)
215 const valueInt = parseInt(valueString, 10)
216
217 if (isNaN(valueInt)) {
218 logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString)
219 return undefined
220 }
221
222 return valueInt
223 }
224
225 async listLocalVideosViewed () {
226 const { setKey } = this.generateLocalVideoViewsKeys()
227
228 const stringIds = await this.getSet(setKey)
229 return stringIds.map(s => parseInt(s, 10))
230 }
231
232 deleteLocalVideoViews (videoId: number) {
233 const { setKey, videoKey } = this.generateLocalVideoViewsKeys(videoId)
234
235 return Promise.all([
236 this.deleteFromSet(setKey, videoId.toString()),
237 this.deleteKey(videoKey)
238 ])
239 }
240
241 /* ************ Video viewers stats ************ */
242
243 getLocalVideoViewer (options: {
244 key?: string
245 // Or
246 ip?: string
247 videoId?: number
248 }) {
249 if (options.key) return this.getObject(options.key)
250
251 const { viewerKey } = this.generateLocalVideoViewerKeys(options.ip, options.videoId)
252
253 return this.getObject(viewerKey)
254 }
255
256 setLocalVideoViewer (ip: string, videoId: number, object: any) {
257 const { setKey, viewerKey } = this.generateLocalVideoViewerKeys(ip, videoId)
258
259 return Promise.all([
260 this.addToSet(setKey, viewerKey),
261 this.setObject(viewerKey, object)
262 ])
263 }
264
265 listLocalVideoViewerKeys () {
266 const { setKey } = this.generateLocalVideoViewerKeys()
267
268 return this.getSet(setKey)
269 }
270
271 deleteLocalVideoViewersKeys (key: string) {
272 const { setKey } = this.generateLocalVideoViewerKeys()
273
274 return Promise.all([
275 this.deleteFromSet(setKey, key),
276 this.deleteKey(key)
277 ])
278 }
279
280 /* ************ Resumable uploads final responses ************ */
281
282 setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) {
283 return this.setValue(
284 'resumable-upload-' + uploadId,
285 response
286 ? JSON.stringify(response)
287 : '',
288 RESUMABLE_UPLOAD_SESSION_LIFETIME
289 )
290 }
291
292 doesUploadSessionExist (uploadId: string) {
293 return this.exists('resumable-upload-' + uploadId)
294 }
295
296 async getUploadSession (uploadId: string) {
297 const value = await this.getValue('resumable-upload-' + uploadId)
298
299 return value
300 ? JSON.parse(value)
301 : ''
302 }
303
304 deleteUploadSession (uploadId: string) {
305 return this.deleteKey('resumable-upload-' + uploadId)
306 }
307
308 /* ************ AP resource unavailability ************ */
309
310 async addAPUnavailability (url: string) {
311 const key = this.generateAPUnavailabilityKey(url)
312
313 const value = await this.increment(key)
314 await this.setExpiration(key, AP_CLEANER.PERIOD * 2)
315
316 return value
317 }
318
319 /* ************ Keys generation ************ */
320
321 private generateLocalVideoViewsKeys (videoId: number): { setKey: string, videoKey: string }
322 private generateLocalVideoViewsKeys (): { setKey: string }
323 private generateLocalVideoViewsKeys (videoId?: number) {
324 return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` }
325 }
326
327 private generateLocalVideoViewerKeys (ip: string, videoId: number): { setKey: string, viewerKey: string }
328 private generateLocalVideoViewerKeys (): { setKey: string }
329 private generateLocalVideoViewerKeys (ip?: string, videoId?: number) {
330 return { setKey: `local-video-viewer-stats-keys`, viewerKey: `local-video-viewer-stats-${ip}-${videoId}` }
331 }
332
333 private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) {
334 const hour = exists(options.hour)
335 ? options.hour
336 : new Date().getHours()
337
338 return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` }
339 }
340
341 private generateResetPasswordKey (userId: number) {
342 return 'reset-password-' + userId
343 }
344
345 private generateTwoFactorRequestKey (userId: number, token: string) {
346 return 'two-factor-request-' + userId + '-' + token
347 }
348
349 private generateVerifyEmailKey (userId: number) {
350 return 'verify-email-' + userId
351 }
352
353 private generateIPViewKey (ip: string, videoUUID: string) {
354 return `views-${videoUUID}-${ip}`
355 }
356
357 private generateContactFormKey (ip: string) {
358 return 'contact-form-' + ip
359 }
360
361 private generateAPUnavailabilityKey (url: string) {
362 return 'ap-unavailability-' + sha256(url)
363 }
364
365 /* ************ Redis helpers ************ */
366
367 private getValue (key: string) {
368 return this.client.get(this.prefix + key)
369 }
370
371 private getSet (key: string) {
372 return this.client.smembers(this.prefix + key)
373 }
374
375 private addToSet (key: string, value: string) {
376 return this.client.sadd(this.prefix + key, value)
377 }
378
379 private deleteFromSet (key: string, value: string) {
380 return this.client.srem(this.prefix + key, value)
381 }
382
383 private deleteKey (key: string) {
384 return this.client.del(this.prefix + key)
385 }
386
387 private async getObject (key: string) {
388 const value = await this.getValue(key)
389 if (!value) return null
390
391 return JSON.parse(value)
392 }
393
394 private setObject (key: string, value: { [ id: string ]: number | string }, expirationMilliseconds?: number) {
395 return this.setValue(key, JSON.stringify(value), expirationMilliseconds)
396 }
397
398 private async setValue (key: string, value: string, expirationMilliseconds?: number) {
399 const result = expirationMilliseconds !== undefined
400 ? await this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds)
401 : await this.client.set(this.prefix + key, value)
402
403 if (result !== 'OK') throw new Error('Redis set result is not OK.')
404 }
405
406 private removeValue (key: string) {
407 return this.client.del(this.prefix + key)
408 }
409
410 private increment (key: string) {
411 return this.client.incr(this.prefix + key)
412 }
413
414 private async exists (key: string) {
415 const result = await this.client.exists(this.prefix + key)
416
417 return result !== 0
418 }
419
420 private setExpiration (key: string, ms: number) {
421 return this.client.expire(this.prefix + key, ms / 1000)
422 }
423
424 static get Instance () {
425 return this.instance || (this.instance = new this())
426 }
427 }
428
429 // ---------------------------------------------------------------------------
430
431 export {
432 Redis
433 }