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