]>
Commit | Line | Data |
---|---|---|
e5d91a9b C |
1 | import { createClient } from 'redis' |
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 C |
10 | RESUMABLE_UPLOAD_SESSION_LIFETIME, |
11 | TRACKER_RATE_LIMITS, | |
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 | |
e5d91a9b C |
19 | // Only used for typings |
20 | const redisClientWrapperForType = () => createClient<{}>() | |
ecb4e35f C |
21 | |
22 | class Redis { | |
23 | ||
24 | private static instance: Redis | |
25 | private initialized = false | |
e5d91a9b C |
26 | private connected = false |
27 | private client: ReturnType<typeof redisClientWrapperForType> | |
ecb4e35f C |
28 | private prefix: string |
29 | ||
a1587156 C |
30 | private constructor () { |
31 | } | |
ecb4e35f C |
32 | |
33 | init () { | |
34 | // Already initialized | |
35 | if (this.initialized === true) return | |
36 | this.initialized = true | |
37 | ||
47f6409b | 38 | this.client = createClient(Redis.getRedisClientOptions()) |
ecb4e35f | 39 | |
e5d91a9b C |
40 | this.client.connect() |
41 | .then(() => { this.connected = true }) | |
42 | .catch(err => { | |
43 | logger.error('Cannot connect to redis', { err }) | |
44 | process.exit(-1) | |
45 | }) | |
46 | ||
ecb4e35f | 47 | this.client.on('error', err => { |
d5b7d911 | 48 | logger.error('Error in Redis client.', { err }) |
ecb4e35f C |
49 | process.exit(-1) |
50 | }) | |
51 | ||
6dd9de95 | 52 | this.prefix = 'redis-' + WEBSERVER.HOST + '-' |
ecb4e35f C |
53 | } |
54 | ||
47f6409b | 55 | static getRedisClientOptions () { |
19f7b248 | 56 | return Object.assign({}, |
e5d91a9b | 57 | CONFIG.REDIS.AUTH ? { password: CONFIG.REDIS.AUTH } : {}, |
19f7b248 | 58 | (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, |
a1587156 C |
59 | (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) |
60 | ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } | |
61 | : { path: CONFIG.REDIS.SOCKET } | |
19f7b248 RK |
62 | ) |
63 | } | |
64 | ||
47f6409b C |
65 | getClient () { |
66 | return this.client | |
67 | } | |
68 | ||
69 | getPrefix () { | |
70 | return this.prefix | |
71 | } | |
72 | ||
e5d91a9b C |
73 | isConnected () { |
74 | return this.connected | |
75 | } | |
76 | ||
a1587156 | 77 | /* ************ Forgot password ************ */ |
6e46de09 | 78 | |
ecb4e35f C |
79 | async setResetPasswordVerificationString (userId: number) { |
80 | const generatedString = await generateRandomString(32) | |
81 | ||
82 | await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME) | |
83 | ||
45f1bd72 JL |
84 | return generatedString |
85 | } | |
86 | ||
87 | async setCreatePasswordVerificationString (userId: number) { | |
88 | const generatedString = await generateRandomString(32) | |
89 | ||
90 | await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME) | |
91 | ||
ecb4e35f C |
92 | return generatedString |
93 | } | |
94 | ||
e9c5f123 C |
95 | async removePasswordVerificationString (userId: number) { |
96 | return this.removeValue(this.generateResetPasswordKey(userId)) | |
97 | } | |
98 | ||
ecb4e35f C |
99 | async getResetPasswordLink (userId: number) { |
100 | return this.getValue(this.generateResetPasswordKey(userId)) | |
101 | } | |
102 | ||
a1587156 | 103 | /* ************ Email verification ************ */ |
6e46de09 | 104 | |
d9eaee39 JM |
105 | async setVerifyEmailVerificationString (userId: number) { |
106 | const generatedString = await generateRandomString(32) | |
107 | ||
108 | await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME) | |
109 | ||
110 | return generatedString | |
111 | } | |
112 | ||
113 | async getVerifyEmailLink (userId: number) { | |
114 | return this.getValue(this.generateVerifyEmailKey(userId)) | |
115 | } | |
116 | ||
a1587156 | 117 | /* ************ Contact form per IP ************ */ |
a4101923 C |
118 | |
119 | async setContactFormIp (ip: string) { | |
120 | return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) | |
121 | } | |
122 | ||
0f6acda1 | 123 | async doesContactFormIpExist (ip: string) { |
a4101923 C |
124 | return this.exists(this.generateContactFormKey(ip)) |
125 | } | |
126 | ||
a1587156 | 127 | /* ************ Views per IP ************ */ |
6e46de09 | 128 | |
51353d9a C |
129 | setIPVideoView (ip: string, videoUUID: string) { |
130 | return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW) | |
131 | } | |
e4bf7856 | 132 | |
51353d9a C |
133 | setIPVideoViewer (ip: string, videoUUID: string) { |
134 | return this.setValue(this.generateIPViewerKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEWER) | |
b5c0e955 C |
135 | } |
136 | ||
0f6acda1 | 137 | async doesVideoIPViewExist (ip: string, videoUUID: string) { |
51353d9a C |
138 | return this.exists(this.generateIPViewKey(ip, videoUUID)) |
139 | } | |
140 | ||
141 | async doesVideoIPViewerExist (ip: string, videoUUID: string) { | |
142 | return this.exists(this.generateIPViewerKey(ip, videoUUID)) | |
b5c0e955 C |
143 | } |
144 | ||
db48de85 C |
145 | /* ************ Tracker IP block ************ */ |
146 | ||
147 | setTrackerBlockIP (ip: string) { | |
148 | return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME) | |
149 | } | |
150 | ||
151 | async doesTrackerBlockIPExist (ip: string) { | |
152 | return this.exists(this.generateTrackerBlockIPKey(ip)) | |
153 | } | |
154 | ||
51353d9a | 155 | /* ************ Video views stats ************ */ |
6e46de09 | 156 | |
51353d9a C |
157 | addVideoViewStats (videoId: number) { |
158 | const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId }) | |
6b616860 C |
159 | |
160 | return Promise.all([ | |
51353d9a C |
161 | this.addToSet(setKey, videoId.toString()), |
162 | this.increment(videoKey) | |
6b616860 C |
163 | ]) |
164 | } | |
165 | ||
51353d9a C |
166 | async getVideoViewsStats (videoId: number, hour: number) { |
167 | const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour }) | |
6b616860 | 168 | |
51353d9a | 169 | const valueString = await this.getValue(videoKey) |
6040f87d C |
170 | const valueInt = parseInt(valueString, 10) |
171 | ||
172 | if (isNaN(valueInt)) { | |
51353d9a | 173 | logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString) |
6040f87d C |
174 | return undefined |
175 | } | |
176 | ||
177 | return valueInt | |
6b616860 C |
178 | } |
179 | ||
51353d9a C |
180 | async listVideosViewedForStats (hour: number) { |
181 | const { setKey } = this.generateVideoViewStatsKeys({ hour }) | |
6b616860 | 182 | |
51353d9a | 183 | const stringIds = await this.getSet(setKey) |
6b616860 C |
184 | return stringIds.map(s => parseInt(s, 10)) |
185 | } | |
186 | ||
51353d9a C |
187 | deleteVideoViewsStats (videoId: number, hour: number) { |
188 | const { setKey, videoKey } = this.generateVideoViewStatsKeys({ videoId, hour }) | |
189 | ||
190 | return Promise.all([ | |
191 | this.deleteFromSet(setKey, videoId.toString()), | |
192 | this.deleteKey(videoKey) | |
193 | ]) | |
194 | } | |
195 | ||
196 | /* ************ Local video views buffer ************ */ | |
197 | ||
198 | addLocalVideoView (videoId: number) { | |
199 | const { videoKey, setKey } = this.generateLocalVideoViewsKeys(videoId) | |
6b616860 C |
200 | |
201 | return Promise.all([ | |
51353d9a C |
202 | this.addToSet(setKey, videoId.toString()), |
203 | this.increment(videoKey) | |
204 | ]) | |
205 | } | |
206 | ||
207 | async getLocalVideoViews (videoId: number) { | |
208 | const { videoKey } = this.generateLocalVideoViewsKeys(videoId) | |
209 | ||
210 | const valueString = await this.getValue(videoKey) | |
211 | const valueInt = parseInt(valueString, 10) | |
212 | ||
213 | if (isNaN(valueInt)) { | |
214 | logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString) | |
215 | return undefined | |
216 | } | |
217 | ||
218 | return valueInt | |
219 | } | |
220 | ||
221 | async listLocalVideosViewed () { | |
222 | const { setKey } = this.generateLocalVideoViewsKeys() | |
223 | ||
224 | const stringIds = await this.getSet(setKey) | |
225 | return stringIds.map(s => parseInt(s, 10)) | |
226 | } | |
227 | ||
228 | deleteLocalVideoViews (videoId: number) { | |
229 | const { setKey, videoKey } = this.generateLocalVideoViewsKeys(videoId) | |
230 | ||
231 | return Promise.all([ | |
232 | this.deleteFromSet(setKey, videoId.toString()), | |
233 | this.deleteKey(videoKey) | |
6b616860 C |
234 | ]) |
235 | } | |
236 | ||
276250f0 RK |
237 | /* ************ Resumable uploads final responses ************ */ |
238 | ||
239 | setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) { | |
240 | return this.setValue( | |
241 | 'resumable-upload-' + uploadId, | |
242 | response | |
243 | ? JSON.stringify(response) | |
244 | : '', | |
245 | RESUMABLE_UPLOAD_SESSION_LIFETIME | |
246 | ) | |
247 | } | |
248 | ||
249 | doesUploadSessionExist (uploadId: string) { | |
250 | return this.exists('resumable-upload-' + uploadId) | |
251 | } | |
252 | ||
253 | async getUploadSession (uploadId: string) { | |
254 | const value = await this.getValue('resumable-upload-' + uploadId) | |
255 | ||
256 | return value | |
257 | ? JSON.parse(value) | |
258 | : '' | |
259 | } | |
260 | ||
020d3d3d C |
261 | deleteUploadSession (uploadId: string) { |
262 | return this.deleteKey('resumable-upload-' + uploadId) | |
263 | } | |
264 | ||
f1569117 C |
265 | /* ************ AP ressource unavailability ************ */ |
266 | ||
267 | async addAPUnavailability (url: string) { | |
268 | const key = this.generateAPUnavailabilityKey(url) | |
269 | ||
270 | const value = await this.increment(key) | |
271 | await this.setExpiration(key, AP_CLEANER.PERIOD * 2) | |
272 | ||
273 | return value | |
274 | } | |
275 | ||
a1587156 | 276 | /* ************ Keys generation ************ */ |
6e46de09 | 277 | |
51353d9a C |
278 | private generateLocalVideoViewsKeys (videoId?: Number) { |
279 | return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` } | |
6b616860 C |
280 | } |
281 | ||
51353d9a C |
282 | private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) { |
283 | const hour = exists(options.hour) | |
284 | ? options.hour | |
285 | : new Date().getHours() | |
6b616860 | 286 | |
51353d9a | 287 | return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` } |
6b616860 C |
288 | } |
289 | ||
6e46de09 | 290 | private generateResetPasswordKey (userId: number) { |
b40f0575 C |
291 | return 'reset-password-' + userId |
292 | } | |
293 | ||
6e46de09 | 294 | private generateVerifyEmailKey (userId: number) { |
d9eaee39 JM |
295 | return 'verify-email-' + userId |
296 | } | |
297 | ||
51353d9a | 298 | private generateIPViewKey (ip: string, videoUUID: string) { |
a4101923 C |
299 | return `views-${videoUUID}-${ip}` |
300 | } | |
301 | ||
51353d9a C |
302 | private generateIPViewerKey (ip: string, videoUUID: string) { |
303 | return `viewer-${videoUUID}-${ip}` | |
304 | } | |
305 | ||
db48de85 C |
306 | private generateTrackerBlockIPKey (ip: string) { |
307 | return `tracker-block-ip-${ip}` | |
308 | } | |
309 | ||
a4101923 C |
310 | private generateContactFormKey (ip: string) { |
311 | return 'contact-form-' + ip | |
b40f0575 C |
312 | } |
313 | ||
f1569117 C |
314 | private generateAPUnavailabilityKey (url: string) { |
315 | return 'ap-unavailability-' + sha256(url) | |
316 | } | |
317 | ||
a1587156 | 318 | /* ************ Redis helpers ************ */ |
b40f0575 | 319 | |
ecb4e35f | 320 | private getValue (key: string) { |
e5d91a9b | 321 | return this.client.get(this.prefix + key) |
ecb4e35f C |
322 | } |
323 | ||
6b616860 | 324 | private getSet (key: string) { |
e5d91a9b | 325 | return this.client.sMembers(this.prefix + key) |
6b616860 C |
326 | } |
327 | ||
328 | private addToSet (key: string, value: string) { | |
e5d91a9b | 329 | return this.client.sAdd(this.prefix + key, value) |
6b616860 C |
330 | } |
331 | ||
332 | private deleteFromSet (key: string, value: string) { | |
e5d91a9b | 333 | return this.client.sRem(this.prefix + key, value) |
6b616860 C |
334 | } |
335 | ||
336 | private deleteKey (key: string) { | |
e5d91a9b | 337 | return this.client.del(this.prefix + key) |
6e46de09 C |
338 | } |
339 | ||
e5d91a9b C |
340 | private async setValue (key: string, value: string, expirationMilliseconds: number) { |
341 | const result = await this.client.set(this.prefix + key, value, { PX: expirationMilliseconds }) | |
ecb4e35f | 342 | |
e5d91a9b | 343 | if (result !== 'OK') throw new Error('Redis set result is not OK.') |
ecb4e35f C |
344 | } |
345 | ||
e9c5f123 | 346 | private removeValue (key: string) { |
e5d91a9b | 347 | return this.client.del(this.prefix + key) |
4195cd2b C |
348 | } |
349 | ||
6b616860 | 350 | private increment (key: string) { |
e5d91a9b | 351 | return this.client.incr(this.prefix + key) |
6b616860 C |
352 | } |
353 | ||
b5c0e955 | 354 | private exists (key: string) { |
e5d91a9b | 355 | return this.client.exists(this.prefix + key) |
b5c0e955 C |
356 | } |
357 | ||
f1569117 C |
358 | private setExpiration (key: string, ms: number) { |
359 | return this.client.expire(this.prefix + key, ms / 1000) | |
360 | } | |
361 | ||
ecb4e35f C |
362 | static get Instance () { |
363 | return this.instance || (this.instance = new this()) | |
364 | } | |
365 | } | |
366 | ||
367 | // --------------------------------------------------------------------------- | |
368 | ||
369 | export { | |
370 | Redis | |
371 | } |