]>
Commit | Line | Data |
---|---|---|
1 | import { createClient } 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' | |
7 | import { | |
8 | AP_CLEANER, | |
9 | CONTACT_FORM_LIFETIME, | |
10 | RESUMABLE_UPLOAD_SESSION_LIFETIME, | |
11 | TRACKER_RATE_LIMITS, | |
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 | // Only used for typings | |
20 | const redisClientWrapperForType = () => createClient<{}>() | |
21 | ||
22 | class Redis { | |
23 | ||
24 | private static instance: Redis | |
25 | private initialized = false | |
26 | private connected = false | |
27 | private client: ReturnType<typeof redisClientWrapperForType> | |
28 | private prefix: string | |
29 | ||
30 | private constructor () { | |
31 | } | |
32 | ||
33 | init () { | |
34 | // Already initialized | |
35 | if (this.initialized === true) return | |
36 | this.initialized = true | |
37 | ||
38 | this.client = createClient(Redis.getRedisClientOptions()) | |
39 | ||
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 | ||
47 | this.client.on('error', err => { | |
48 | logger.error('Error in Redis client.', { err }) | |
49 | process.exit(-1) | |
50 | }) | |
51 | ||
52 | this.prefix = 'redis-' + WEBSERVER.HOST + '-' | |
53 | } | |
54 | ||
55 | static getRedisClientOptions () { | |
56 | return Object.assign({}, | |
57 | CONFIG.REDIS.AUTH ? { password: CONFIG.REDIS.AUTH } : {}, | |
58 | (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, | |
59 | (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) | |
60 | ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } | |
61 | : { path: CONFIG.REDIS.SOCKET } | |
62 | ) | |
63 | } | |
64 | ||
65 | getClient () { | |
66 | return this.client | |
67 | } | |
68 | ||
69 | getPrefix () { | |
70 | return this.prefix | |
71 | } | |
72 | ||
73 | isConnected () { | |
74 | return this.connected | |
75 | } | |
76 | ||
77 | /* ************ Forgot password ************ */ | |
78 | ||
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 | ||
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 | ||
92 | return generatedString | |
93 | } | |
94 | ||
95 | async removePasswordVerificationString (userId: number) { | |
96 | return this.removeValue(this.generateResetPasswordKey(userId)) | |
97 | } | |
98 | ||
99 | async getResetPasswordLink (userId: number) { | |
100 | return this.getValue(this.generateResetPasswordKey(userId)) | |
101 | } | |
102 | ||
103 | /* ************ Email verification ************ */ | |
104 | ||
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 | ||
117 | /* ************ Contact form per IP ************ */ | |
118 | ||
119 | async setContactFormIp (ip: string) { | |
120 | return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) | |
121 | } | |
122 | ||
123 | async doesContactFormIpExist (ip: string) { | |
124 | return this.exists(this.generateContactFormKey(ip)) | |
125 | } | |
126 | ||
127 | /* ************ Views per IP ************ */ | |
128 | ||
129 | setIPVideoView (ip: string, videoUUID: string) { | |
130 | return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW) | |
131 | } | |
132 | ||
133 | setIPVideoViewer (ip: string, videoUUID: string) { | |
134 | return this.setValue(this.generateIPViewerKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEWER) | |
135 | } | |
136 | ||
137 | async doesVideoIPViewExist (ip: string, videoUUID: string) { | |
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)) | |
143 | } | |
144 | ||
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 | ||
155 | /* ************ Video views stats ************ */ | |
156 | ||
157 | addVideoViewStats (videoId: number) { | |
158 | const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId }) | |
159 | ||
160 | return Promise.all([ | |
161 | this.addToSet(setKey, videoId.toString()), | |
162 | this.increment(videoKey) | |
163 | ]) | |
164 | } | |
165 | ||
166 | async getVideoViewsStats (videoId: number, hour: number) { | |
167 | const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour }) | |
168 | ||
169 | const valueString = await this.getValue(videoKey) | |
170 | const valueInt = parseInt(valueString, 10) | |
171 | ||
172 | if (isNaN(valueInt)) { | |
173 | logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString) | |
174 | return undefined | |
175 | } | |
176 | ||
177 | return valueInt | |
178 | } | |
179 | ||
180 | async listVideosViewedForStats (hour: number) { | |
181 | const { setKey } = this.generateVideoViewStatsKeys({ hour }) | |
182 | ||
183 | const stringIds = await this.getSet(setKey) | |
184 | return stringIds.map(s => parseInt(s, 10)) | |
185 | } | |
186 | ||
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) | |
200 | ||
201 | return Promise.all([ | |
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) | |
234 | ]) | |
235 | } | |
236 | ||
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 | ||
261 | deleteUploadSession (uploadId: string) { | |
262 | return this.deleteKey('resumable-upload-' + uploadId) | |
263 | } | |
264 | ||
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 | ||
276 | /* ************ Keys generation ************ */ | |
277 | ||
278 | private generateLocalVideoViewsKeys (videoId?: Number) { | |
279 | return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` } | |
280 | } | |
281 | ||
282 | private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) { | |
283 | const hour = exists(options.hour) | |
284 | ? options.hour | |
285 | : new Date().getHours() | |
286 | ||
287 | return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` } | |
288 | } | |
289 | ||
290 | private generateResetPasswordKey (userId: number) { | |
291 | return 'reset-password-' + userId | |
292 | } | |
293 | ||
294 | private generateVerifyEmailKey (userId: number) { | |
295 | return 'verify-email-' + userId | |
296 | } | |
297 | ||
298 | private generateIPViewKey (ip: string, videoUUID: string) { | |
299 | return `views-${videoUUID}-${ip}` | |
300 | } | |
301 | ||
302 | private generateIPViewerKey (ip: string, videoUUID: string) { | |
303 | return `viewer-${videoUUID}-${ip}` | |
304 | } | |
305 | ||
306 | private generateTrackerBlockIPKey (ip: string) { | |
307 | return `tracker-block-ip-${ip}` | |
308 | } | |
309 | ||
310 | private generateContactFormKey (ip: string) { | |
311 | return 'contact-form-' + ip | |
312 | } | |
313 | ||
314 | private generateAPUnavailabilityKey (url: string) { | |
315 | return 'ap-unavailability-' + sha256(url) | |
316 | } | |
317 | ||
318 | /* ************ Redis helpers ************ */ | |
319 | ||
320 | private getValue (key: string) { | |
321 | return this.client.get(this.prefix + key) | |
322 | } | |
323 | ||
324 | private getSet (key: string) { | |
325 | return this.client.sMembers(this.prefix + key) | |
326 | } | |
327 | ||
328 | private addToSet (key: string, value: string) { | |
329 | return this.client.sAdd(this.prefix + key, value) | |
330 | } | |
331 | ||
332 | private deleteFromSet (key: string, value: string) { | |
333 | return this.client.sRem(this.prefix + key, value) | |
334 | } | |
335 | ||
336 | private deleteKey (key: string) { | |
337 | return this.client.del(this.prefix + key) | |
338 | } | |
339 | ||
340 | private async setValue (key: string, value: string, expirationMilliseconds: number) { | |
341 | const result = await this.client.set(this.prefix + key, value, { PX: expirationMilliseconds }) | |
342 | ||
343 | if (result !== 'OK') throw new Error('Redis set result is not OK.') | |
344 | } | |
345 | ||
346 | private removeValue (key: string) { | |
347 | return this.client.del(this.prefix + key) | |
348 | } | |
349 | ||
350 | private increment (key: string) { | |
351 | return this.client.incr(this.prefix + key) | |
352 | } | |
353 | ||
354 | private exists (key: string) { | |
355 | return this.client.exists(this.prefix + key) | |
356 | } | |
357 | ||
358 | private setExpiration (key: string, ms: number) { | |
359 | return this.client.expire(this.prefix + key, ms / 1000) | |
360 | } | |
361 | ||
362 | static get Instance () { | |
363 | return this.instance || (this.instance = new this()) | |
364 | } | |
365 | } | |
366 | ||
367 | // --------------------------------------------------------------------------- | |
368 | ||
369 | export { | |
370 | Redis | |
371 | } |