Add rate limit to registration and API endpoints
[oweals/peertube.git] / server / initializers / config.ts
1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { VideosRedundancy } from '../../shared/models'
4 // Do not use barrels, remain constants as independent as possible
5 import { buildPath, parseBytes, parseDurationToMs, root } from '../helpers/core-utils'
6 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
7 import * as bytes from 'bytes'
8
9 // Use a variable to reload the configuration if we need
10 let config: IConfig = require('config')
11
12 const configChangedHandlers: Function[] = []
13
14 const CONFIG = {
15   CUSTOM_FILE: getLocalConfigFilePath(),
16   LISTEN: {
17     PORT: config.get<number>('listen.port'),
18     HOSTNAME: config.get<string>('listen.hostname')
19   },
20   DATABASE: {
21     DBNAME: 'peertube' + config.get<string>('database.suffix'),
22     HOSTNAME: config.get<string>('database.hostname'),
23     PORT: config.get<number>('database.port'),
24     USERNAME: config.get<string>('database.username'),
25     PASSWORD: config.get<string>('database.password'),
26     POOL: {
27       MAX: config.get<number>('database.pool.max')
28     }
29   },
30   REDIS: {
31     HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
32     PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
33     SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
34     AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
35     DB: config.has('redis.db') ? config.get<number>('redis.db') : null
36   },
37   SMTP: {
38     HOSTNAME: config.get<string>('smtp.hostname'),
39     PORT: config.get<number>('smtp.port'),
40     USERNAME: config.get<string>('smtp.username'),
41     PASSWORD: config.get<string>('smtp.password'),
42     TLS: config.get<boolean>('smtp.tls'),
43     DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
44     CA_FILE: config.get<string>('smtp.ca_file'),
45     FROM_ADDRESS: config.get<string>('smtp.from_address')
46   },
47   EMAIL: {
48     BODY: {
49       SIGNATURE: config.get<string>('email.body.signature')
50     },
51     OBJECT: {
52       PREFIX: config.get<string>('email.object.prefix') + ' '
53     }
54   },
55   STORAGE: {
56     TMP_DIR: buildPath(config.get<string>('storage.tmp')),
57     AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
58     LOG_DIR: buildPath(config.get<string>('storage.logs')),
59     VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
60     STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')),
61     REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
62     THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
63     PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
64     CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
65     TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
66     CACHE_DIR: buildPath(config.get<string>('storage.cache'))
67   },
68   WEBSERVER: {
69     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
70     WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
71     HOSTNAME: config.get<string>('webserver.hostname'),
72     PORT: config.get<number>('webserver.port')
73   },
74   RATES_LIMIT: {
75     API: {
76       WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.api.window')),
77       MAX: config.get<number>('rates_limit.api.max')
78     },
79     SIGNUP: {
80       WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.signup.window')),
81       MAX: config.get<number>('rates_limit.signup.max')
82     },
83     LOGIN: {
84       WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')),
85       MAX: config.get<number>('rates_limit.login.max')
86     },
87     ASK_SEND_EMAIL: {
88       WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')),
89       MAX: config.get<number>('rates_limit.ask_send_email.max')
90     }
91   },
92   TRUST_PROXY: config.get<string[]>('trust_proxy'),
93   LOG: {
94     LEVEL: config.get<string>('log.level'),
95     ROTATION: config.get<boolean>('log.rotation.enabled')
96   },
97   SEARCH: {
98     REMOTE_URI: {
99       USERS: config.get<boolean>('search.remote_uri.users'),
100       ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
101     }
102   },
103   TRENDING: {
104     VIDEOS: {
105       INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
106     }
107   },
108   REDUNDANCY: {
109     VIDEOS: {
110       CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
111       STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
112     }
113   },
114   CSP: {
115     ENABLED: config.get<boolean>('csp.enabled'),
116     REPORT_ONLY: config.get<boolean>('csp.report_only'),
117     REPORT_URI: config.get<boolean>('csp.report_uri')
118   },
119   TRACKER: {
120     ENABLED: config.get<boolean>('tracker.enabled'),
121     PRIVATE: config.get<boolean>('tracker.private'),
122     REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
123   },
124   HISTORY: {
125     VIDEOS: {
126       MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
127     }
128   },
129   VIEWS: {
130     VIDEOS: {
131       REMOTE: {
132         MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
133       }
134     }
135   },
136   ADMIN: {
137     get EMAIL () { return config.get<string>('admin.email') }
138   },
139   CONTACT_FORM: {
140     get ENABLED () { return config.get<boolean>('contact_form.enabled') }
141   },
142   SIGNUP: {
143     get ENABLED () { return config.get<boolean>('signup.enabled') },
144     get LIMIT () { return config.get<number>('signup.limit') },
145     get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
146     FILTERS: {
147       CIDR: {
148         get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
149         get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
150       }
151     }
152   },
153   USER: {
154     get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
155     get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
156   },
157   TRANSCODING: {
158     get ENABLED () { return config.get<boolean>('transcoding.enabled') },
159     get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
160     get ALLOW_AUDIO_FILES () { return config.get<boolean>('transcoding.allow_audio_files') },
161     get THREADS () { return config.get<number>('transcoding.threads') },
162     RESOLUTIONS: {
163       get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
164       get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
165       get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
166       get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
167       get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') },
168       get '2160p' () { return config.get<boolean>('transcoding.resolutions.2160p') }
169     },
170     HLS: {
171       get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
172     }
173   },
174   IMPORT: {
175     VIDEOS: {
176       HTTP: {
177         get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
178       },
179       TORRENT: {
180         get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
181       }
182     }
183   },
184   AUTO_BLACKLIST: {
185     VIDEOS: {
186       OF_USERS: {
187         get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
188       }
189     }
190   },
191   CACHE: {
192     PREVIEWS: {
193       get SIZE () { return config.get<number>('cache.previews.size') }
194     },
195     VIDEO_CAPTIONS: {
196       get SIZE () { return config.get<number>('cache.captions.size') }
197     }
198   },
199   INSTANCE: {
200     get NAME () { return config.get<string>('instance.name') },
201     get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
202     get DESCRIPTION () { return config.get<string>('instance.description') },
203     get TERMS () { return config.get<string>('instance.terms') },
204     get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
205     get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
206     get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
207     CUSTOMIZATIONS: {
208       get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
209       get CSS () { return config.get<string>('instance.customizations.css') }
210     },
211     get ROBOTS () { return config.get<string>('instance.robots') },
212     get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
213     get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
214   },
215   SERVICES: {
216     TWITTER: {
217       get USERNAME () { return config.get<string>('services.twitter.username') },
218       get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
219     }
220   },
221   FOLLOWERS: {
222     INSTANCE: {
223       get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
224       get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
225     }
226   }
227 }
228
229 function registerConfigChangedHandler (fun: Function) {
230   configChangedHandlers.push(fun)
231 }
232
233 // ---------------------------------------------------------------------------
234
235 export {
236   CONFIG,
237   registerConfigChangedHandler
238 }
239
240 // ---------------------------------------------------------------------------
241
242 function getLocalConfigFilePath () {
243   const configSources = config.util.getConfigSources()
244   if (configSources.length === 0) throw new Error('Invalid config source.')
245
246   let filename = 'local'
247   if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
248   if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
249
250   return join(dirname(configSources[ 0 ].name), filename + '.json')
251 }
252
253 function buildVideosRedundancy (objs: any[]): VideosRedundancy[] {
254   if (!objs) return []
255
256   if (!Array.isArray(objs)) return objs
257
258   return objs.map(obj => {
259     return Object.assign({}, obj, {
260       minLifetime: parseDurationToMs(obj.min_lifetime),
261       size: bytes.parse(obj.size),
262       minViews: obj.min_views
263     })
264   })
265 }
266
267 export function reloadConfig () {
268
269   function directory () {
270     if (process.env.NODE_CONFIG_DIR) {
271       return process.env.NODE_CONFIG_DIR
272     }
273
274     return join(root(), 'config')
275   }
276
277   function purge () {
278     for (const fileName in require.cache) {
279       if (-1 === fileName.indexOf(directory())) {
280         continue
281       }
282
283       delete require.cache[fileName]
284     }
285
286     delete require.cache[require.resolve('config')]
287   }
288
289   purge()
290
291   config = require('config')
292
293   for (const configChangedHandler of configChangedHandlers) {
294     configChangedHandler()
295   }
296 }