]>
Commit | Line | Data |
---|---|---|
564b9b55 | 1 | import IoRedis, { RedisOptions } from 'ioredis' |
e5d91a9b | 2 | import { exists } from '@server/helpers/custom-validators/misc' |
f1569117 | 3 | import { sha256 } from '@shared/extra-utils' |
ecb4e35f C |
4 | import { logger } from '../helpers/logger' |
5 | import { generateRandomString } from '../helpers/utils' | |
e5d91a9b | 6 | import { CONFIG } from '../initializers/config' |
a4101923 | 7 | import { |
f1569117 | 8 | AP_CLEANER, |
a4101923 | 9 | CONTACT_FORM_LIFETIME, |
e5d91a9b | 10 | RESUMABLE_UPLOAD_SESSION_LIFETIME, |
56f47830 | 11 | TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME, |
a4101923 | 12 | USER_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 |
19 | class 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 | |
d9eaee39 JM |
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 | ||
a1587156 | 139 | /* ************ Contact form per IP ************ */ |
a4101923 C |
140 | |
141 | async setContactFormIp (ip: string) { | |
142 | return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) | |
143 | } | |
144 | ||
0f6acda1 | 145 | async doesContactFormIpExist (ip: string) { |
a4101923 C |
146 | return this.exists(this.generateContactFormKey(ip)) |
147 | } | |
148 | ||
a1587156 | 149 | /* ************ Views per IP ************ */ |
6e46de09 | 150 | |
51353d9a C |
151 | setIPVideoView (ip: string, videoUUID: string) { |
152 | return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW) | |
153 | } | |
e4bf7856 | 154 | |
0f6acda1 | 155 | async doesVideoIPViewExist (ip: string, videoUUID: string) { |
51353d9a C |
156 | return this.exists(this.generateIPViewKey(ip, videoUUID)) |
157 | } | |
158 | ||
51353d9a | 159 | /* ************ Video views stats ************ */ |
6e46de09 | 160 | |
51353d9a C |
161 | addVideoViewStats (videoId: number) { |
162 | const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId }) | |
6b616860 C |
163 | |
164 | return Promise.all([ | |
51353d9a C |
165 | this.addToSet(setKey, videoId.toString()), |
166 | this.increment(videoKey) | |
6b616860 C |
167 | ]) |
168 | } | |
169 | ||
51353d9a C |
170 | async getVideoViewsStats (videoId: number, hour: number) { |
171 | const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour }) | |
6b616860 | 172 | |
51353d9a | 173 | const valueString = await this.getValue(videoKey) |
6040f87d C |
174 | const valueInt = parseInt(valueString, 10) |
175 | ||
176 | if (isNaN(valueInt)) { | |
51353d9a | 177 | logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString) |
6040f87d C |
178 | return undefined |
179 | } | |
180 | ||
181 | return valueInt | |
6b616860 C |
182 | } |
183 | ||
51353d9a C |
184 | async listVideosViewedForStats (hour: number) { |
185 | const { setKey } = this.generateVideoViewStatsKeys({ hour }) | |
6b616860 | 186 | |
51353d9a | 187 | const stringIds = await this.getSet(setKey) |
6b616860 C |
188 | return stringIds.map(s => parseInt(s, 10)) |
189 | } | |
190 | ||
51353d9a C |
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) | |
6b616860 C |
204 | |
205 | return Promise.all([ | |
51353d9a C |
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) | |
6b616860 C |
238 | ]) |
239 | } | |
240 | ||
b2111066 C |
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 | ||
276250f0 RK |
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 | ||
020d3d3d C |
304 | deleteUploadSession (uploadId: string) { |
305 | return this.deleteKey('resumable-upload-' + uploadId) | |
306 | } | |
307 | ||
7a4fd56c | 308 | /* ************ AP resource unavailability ************ */ |
f1569117 C |
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 | ||
a1587156 | 319 | /* ************ Keys generation ************ */ |
6e46de09 | 320 | |
b2111066 C |
321 | private generateLocalVideoViewsKeys (videoId: number): { setKey: string, videoKey: string } |
322 | private generateLocalVideoViewsKeys (): { setKey: string } | |
323 | private generateLocalVideoViewsKeys (videoId?: number) { | |
51353d9a | 324 | return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` } |
6b616860 C |
325 | } |
326 | ||
b2111066 C |
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 | ||
51353d9a C |
333 | private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) { |
334 | const hour = exists(options.hour) | |
335 | ? options.hour | |
336 | : new Date().getHours() | |
6b616860 | 337 | |
51353d9a | 338 | return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` } |
6b616860 C |
339 | } |
340 | ||
6e46de09 | 341 | private generateResetPasswordKey (userId: number) { |
b40f0575 C |
342 | return 'reset-password-' + userId |
343 | } | |
344 | ||
56f47830 C |
345 | private generateTwoFactorRequestKey (userId: number, token: string) { |
346 | return 'two-factor-request-' + userId + '-' + token | |
347 | } | |
348 | ||
6e46de09 | 349 | private generateVerifyEmailKey (userId: number) { |
d9eaee39 JM |
350 | return 'verify-email-' + userId |
351 | } | |
352 | ||
51353d9a | 353 | private generateIPViewKey (ip: string, videoUUID: string) { |
a4101923 C |
354 | return `views-${videoUUID}-${ip}` |
355 | } | |
356 | ||
357 | private generateContactFormKey (ip: string) { | |
358 | return 'contact-form-' + ip | |
b40f0575 C |
359 | } |
360 | ||
f1569117 C |
361 | private generateAPUnavailabilityKey (url: string) { |
362 | return 'ap-unavailability-' + sha256(url) | |
363 | } | |
364 | ||
a1587156 | 365 | /* ************ Redis helpers ************ */ |
b40f0575 | 366 | |
ecb4e35f | 367 | private getValue (key: string) { |
e5d91a9b | 368 | return this.client.get(this.prefix + key) |
ecb4e35f C |
369 | } |
370 | ||
6b616860 | 371 | private getSet (key: string) { |
564b9b55 | 372 | return this.client.smembers(this.prefix + key) |
6b616860 C |
373 | } |
374 | ||
375 | private addToSet (key: string, value: string) { | |
564b9b55 | 376 | return this.client.sadd(this.prefix + key, value) |
6b616860 C |
377 | } |
378 | ||
379 | private deleteFromSet (key: string, value: string) { | |
564b9b55 | 380 | return this.client.srem(this.prefix + key, value) |
6b616860 C |
381 | } |
382 | ||
383 | private deleteKey (key: string) { | |
e5d91a9b | 384 | return this.client.del(this.prefix + key) |
6e46de09 C |
385 | } |
386 | ||
b2111066 C |
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 | ||
56f47830 C |
394 | private setObject (key: string, value: { [ id: string ]: number | string }, expirationMilliseconds?: number) { |
395 | return this.setValue(key, JSON.stringify(value), expirationMilliseconds) | |
b2111066 C |
396 | } |
397 | ||
398 | private async setValue (key: string, value: string, expirationMilliseconds?: number) { | |
90dbc731 C |
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) | |
ecb4e35f | 402 | |
e5d91a9b | 403 | if (result !== 'OK') throw new Error('Redis set result is not OK.') |
ecb4e35f C |
404 | } |
405 | ||
e9c5f123 | 406 | private removeValue (key: string) { |
e5d91a9b | 407 | return this.client.del(this.prefix + key) |
4195cd2b C |
408 | } |
409 | ||
6b616860 | 410 | private increment (key: string) { |
e5d91a9b | 411 | return this.client.incr(this.prefix + key) |
6b616860 C |
412 | } |
413 | ||
c0d2eac3 C |
414 | private async exists (key: string) { |
415 | const result = await this.client.exists(this.prefix + key) | |
416 | ||
417 | return result !== 0 | |
b5c0e955 C |
418 | } |
419 | ||
f1569117 C |
420 | private setExpiration (key: string, ms: number) { |
421 | return this.client.expire(this.prefix + key, ms / 1000) | |
422 | } | |
423 | ||
ecb4e35f C |
424 | static get Instance () { |
425 | return this.instance || (this.instance = new this()) | |
426 | } | |
427 | } | |
428 | ||
429 | // --------------------------------------------------------------------------- | |
430 | ||
431 | export { | |
432 | Redis | |
433 | } |