redis.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import * as express from 'express'
  2. import { createClient, RedisClient } from 'redis'
  3. import { logger } from '../helpers/logger'
  4. import { generateRandomString } from '../helpers/utils'
  5. import {
  6. CONTACT_FORM_LIFETIME,
  7. USER_EMAIL_VERIFY_LIFETIME,
  8. USER_PASSWORD_RESET_LIFETIME,
  9. USER_PASSWORD_CREATE_LIFETIME,
  10. VIEW_LIFETIME,
  11. WEBSERVER,
  12. TRACKER_RATE_LIMITS
  13. } from '../initializers/constants'
  14. import { CONFIG } from '../initializers/config'
  15. type CachedRoute = {
  16. body: string
  17. contentType?: string
  18. statusCode?: string
  19. }
  20. class Redis {
  21. private static instance: Redis
  22. private initialized = false
  23. private client: RedisClient
  24. private prefix: string
  25. private constructor () {
  26. }
  27. init () {
  28. // Already initialized
  29. if (this.initialized === true) return
  30. this.initialized = true
  31. this.client = createClient(Redis.getRedisClientOptions())
  32. this.client.on('error', err => {
  33. logger.error('Error in Redis client.', { err })
  34. process.exit(-1)
  35. })
  36. if (CONFIG.REDIS.AUTH) {
  37. this.client.auth(CONFIG.REDIS.AUTH)
  38. }
  39. this.prefix = 'redis-' + WEBSERVER.HOST + '-'
  40. }
  41. static getRedisClientOptions () {
  42. return Object.assign({},
  43. (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
  44. (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
  45. (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
  46. ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
  47. : { path: CONFIG.REDIS.SOCKET }
  48. )
  49. }
  50. getClient () {
  51. return this.client
  52. }
  53. getPrefix () {
  54. return this.prefix
  55. }
  56. /* ************ Forgot password ************ */
  57. async setResetPasswordVerificationString (userId: number) {
  58. const generatedString = await generateRandomString(32)
  59. await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
  60. return generatedString
  61. }
  62. async setCreatePasswordVerificationString (userId: number) {
  63. const generatedString = await generateRandomString(32)
  64. await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
  65. return generatedString
  66. }
  67. async removePasswordVerificationString (userId: number) {
  68. return this.removeValue(this.generateResetPasswordKey(userId))
  69. }
  70. async getResetPasswordLink (userId: number) {
  71. return this.getValue(this.generateResetPasswordKey(userId))
  72. }
  73. /* ************ Email verification ************ */
  74. async setVerifyEmailVerificationString (userId: number) {
  75. const generatedString = await generateRandomString(32)
  76. await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
  77. return generatedString
  78. }
  79. async getVerifyEmailLink (userId: number) {
  80. return this.getValue(this.generateVerifyEmailKey(userId))
  81. }
  82. /* ************ Contact form per IP ************ */
  83. async setContactFormIp (ip: string) {
  84. return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
  85. }
  86. async doesContactFormIpExist (ip: string) {
  87. return this.exists(this.generateContactFormKey(ip))
  88. }
  89. /* ************ Views per IP ************ */
  90. setIPVideoView (ip: string, videoUUID: string, isLive: boolean) {
  91. const lifetime = isLive
  92. ? VIEW_LIFETIME.LIVE
  93. : VIEW_LIFETIME.VIDEO
  94. return this.setValue(this.generateViewKey(ip, videoUUID), '1', lifetime)
  95. }
  96. async doesVideoIPViewExist (ip: string, videoUUID: string) {
  97. return this.exists(this.generateViewKey(ip, videoUUID))
  98. }
  99. /* ************ Tracker IP block ************ */
  100. setTrackerBlockIP (ip: string) {
  101. return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
  102. }
  103. async doesTrackerBlockIPExist (ip: string) {
  104. return this.exists(this.generateTrackerBlockIPKey(ip))
  105. }
  106. /* ************ API cache ************ */
  107. async getCachedRoute (req: express.Request) {
  108. const cached = await this.getObject(this.generateCachedRouteKey(req))
  109. return cached as CachedRoute
  110. }
  111. setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
  112. const cached: CachedRoute = Object.assign(
  113. {},
  114. { body: body.toString() },
  115. (contentType) ? { contentType } : null,
  116. (statusCode) ? { statusCode: statusCode.toString() } : null
  117. )
  118. return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
  119. }
  120. /* ************ Video views ************ */
  121. addVideoView (videoId: number) {
  122. const keyIncr = this.generateVideoViewKey(videoId)
  123. const keySet = this.generateVideosViewKey()
  124. return Promise.all([
  125. this.addToSet(keySet, videoId.toString()),
  126. this.increment(keyIncr)
  127. ])
  128. }
  129. async getVideoViews (videoId: number, hour: number) {
  130. const key = this.generateVideoViewKey(videoId, hour)
  131. const valueString = await this.getValue(key)
  132. const valueInt = parseInt(valueString, 10)
  133. if (isNaN(valueInt)) {
  134. logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
  135. return undefined
  136. }
  137. return valueInt
  138. }
  139. async getVideosIdViewed (hour: number) {
  140. const key = this.generateVideosViewKey(hour)
  141. const stringIds = await this.getSet(key)
  142. return stringIds.map(s => parseInt(s, 10))
  143. }
  144. deleteVideoViews (videoId: number, hour: number) {
  145. const keySet = this.generateVideosViewKey(hour)
  146. const keyIncr = this.generateVideoViewKey(videoId, hour)
  147. return Promise.all([
  148. this.deleteFromSet(keySet, videoId.toString()),
  149. this.deleteKey(keyIncr)
  150. ])
  151. }
  152. /* ************ Keys generation ************ */
  153. generateCachedRouteKey (req: express.Request) {
  154. return req.method + '-' + req.originalUrl
  155. }
  156. private generateVideosViewKey (hour?: number) {
  157. if (!hour) hour = new Date().getHours()
  158. return `videos-view-h${hour}`
  159. }
  160. private generateVideoViewKey (videoId: number, hour?: number) {
  161. if (hour === undefined || hour === null) hour = new Date().getHours()
  162. return `video-view-${videoId}-h${hour}`
  163. }
  164. private generateResetPasswordKey (userId: number) {
  165. return 'reset-password-' + userId
  166. }
  167. private generateVerifyEmailKey (userId: number) {
  168. return 'verify-email-' + userId
  169. }
  170. private generateViewKey (ip: string, videoUUID: string) {
  171. return `views-${videoUUID}-${ip}`
  172. }
  173. private generateTrackerBlockIPKey (ip: string) {
  174. return `tracker-block-ip-${ip}`
  175. }
  176. private generateContactFormKey (ip: string) {
  177. return 'contact-form-' + ip
  178. }
  179. /* ************ Redis helpers ************ */
  180. private getValue (key: string) {
  181. return new Promise<string>((res, rej) => {
  182. this.client.get(this.prefix + key, (err, value) => {
  183. if (err) return rej(err)
  184. return res(value)
  185. })
  186. })
  187. }
  188. private getSet (key: string) {
  189. return new Promise<string[]>((res, rej) => {
  190. this.client.smembers(this.prefix + key, (err, value) => {
  191. if (err) return rej(err)
  192. return res(value)
  193. })
  194. })
  195. }
  196. private addToSet (key: string, value: string) {
  197. return new Promise<string[]>((res, rej) => {
  198. this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
  199. })
  200. }
  201. private deleteFromSet (key: string, value: string) {
  202. return new Promise<void>((res, rej) => {
  203. this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
  204. })
  205. }
  206. private deleteKey (key: string) {
  207. return new Promise<void>((res, rej) => {
  208. this.client.del(this.prefix + key, err => err ? rej(err) : res())
  209. })
  210. }
  211. private deleteFieldInHash (key: string, field: string) {
  212. return new Promise<void>((res, rej) => {
  213. this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res())
  214. })
  215. }
  216. private setValue (key: string, value: string, expirationMilliseconds: number) {
  217. return new Promise<void>((res, rej) => {
  218. this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
  219. if (err) return rej(err)
  220. if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
  221. return res()
  222. })
  223. })
  224. }
  225. private removeValue (key: string) {
  226. return new Promise<void>((res, rej) => {
  227. this.client.del(this.prefix + key, err => {
  228. if (err) return rej(err)
  229. return res()
  230. })
  231. })
  232. }
  233. private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
  234. return new Promise<void>((res, rej) => {
  235. this.client.hmset(this.prefix + key, obj, (err, ok) => {
  236. if (err) return rej(err)
  237. if (!ok) return rej(new Error('Redis mset result is not OK.'))
  238. this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
  239. if (err) return rej(err)
  240. if (!ok) return rej(new Error('Redis expiration result is not OK.'))
  241. return res()
  242. })
  243. })
  244. })
  245. }
  246. private getObject (key: string) {
  247. return new Promise<{ [id: string]: string }>((res, rej) => {
  248. this.client.hgetall(this.prefix + key, (err, value) => {
  249. if (err) return rej(err)
  250. return res(value)
  251. })
  252. })
  253. }
  254. private setValueInHash (key: string, field: string, value: string) {
  255. return new Promise<void>((res, rej) => {
  256. this.client.hset(this.prefix + key, field, value, (err) => {
  257. if (err) return rej(err)
  258. return res()
  259. })
  260. })
  261. }
  262. private increment (key: string) {
  263. return new Promise<number>((res, rej) => {
  264. this.client.incr(this.prefix + key, (err, value) => {
  265. if (err) return rej(err)
  266. return res(value)
  267. })
  268. })
  269. }
  270. private exists (key: string) {
  271. return new Promise<boolean>((res, rej) => {
  272. this.client.exists(this.prefix + key, (err, existsNumber) => {
  273. if (err) return rej(err)
  274. return res(existsNumber === 1)
  275. })
  276. })
  277. }
  278. static get Instance () {
  279. return this.instance || (this.instance = new this())
  280. }
  281. }
  282. // ---------------------------------------------------------------------------
  283. export {
  284. Redis
  285. }