]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/config.ts
Add ability to accept or not remote redundancies
[github/Chocobozzz/PeerTube.git] / server / initializers / config.ts
1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { VideosRedundancyStrategy } 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 import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
9
10 // Use a variable to reload the configuration if we need
11 let config: IConfig = require('config')
12
13 const configChangedHandlers: Function[] = []
14
15 const CONFIG = {
16 CUSTOM_FILE: getLocalConfigFilePath(),
17 LISTEN: {
18 PORT: config.get<number>('listen.port'),
19 HOSTNAME: config.get<string>('listen.hostname')
20 },
21 DATABASE: {
22 DBNAME: 'peertube' + config.get<string>('database.suffix'),
23 HOSTNAME: config.get<string>('database.hostname'),
24 PORT: config.get<number>('database.port'),
25 USERNAME: config.get<string>('database.username'),
26 PASSWORD: config.get<string>('database.password'),
27 POOL: {
28 MAX: config.get<number>('database.pool.max')
29 }
30 },
31 REDIS: {
32 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
33 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
34 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
35 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
36 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
37 },
38 SMTP: {
39 HOSTNAME: config.get<string>('smtp.hostname'),
40 PORT: config.get<number>('smtp.port'),
41 USERNAME: config.get<string>('smtp.username'),
42 PASSWORD: config.get<string>('smtp.password'),
43 TLS: config.get<boolean>('smtp.tls'),
44 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
45 CA_FILE: config.get<string>('smtp.ca_file'),
46 FROM_ADDRESS: config.get<string>('smtp.from_address')
47 },
48 EMAIL: {
49 BODY: {
50 SIGNATURE: config.get<string>('email.body.signature')
51 },
52 SUBJECT: {
53 PREFIX: config.get<string>('email.subject.prefix') + ' '
54 }
55 },
56 STORAGE: {
57 TMP_DIR: buildPath(config.get<string>('storage.tmp')),
58 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
59 LOG_DIR: buildPath(config.get<string>('storage.logs')),
60 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
61 STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')),
62 REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
63 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
64 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
65 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
66 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
67 CACHE_DIR: buildPath(config.get<string>('storage.cache')),
68 PLUGINS_DIR: buildPath(config.get<string>('storage.plugins'))
69 },
70 WEBSERVER: {
71 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
72 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
73 HOSTNAME: config.get<string>('webserver.hostname'),
74 PORT: config.get<number>('webserver.port')
75 },
76 RATES_LIMIT: {
77 API: {
78 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.api.window')),
79 MAX: config.get<number>('rates_limit.api.max')
80 },
81 SIGNUP: {
82 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.signup.window')),
83 MAX: config.get<number>('rates_limit.signup.max')
84 },
85 LOGIN: {
86 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')),
87 MAX: config.get<number>('rates_limit.login.max')
88 },
89 ASK_SEND_EMAIL: {
90 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')),
91 MAX: config.get<number>('rates_limit.ask_send_email.max')
92 }
93 },
94 TRUST_PROXY: config.get<string[]>('trust_proxy'),
95 LOG: {
96 LEVEL: config.get<string>('log.level'),
97 ROTATION: {
98 ENABLED: config.get<boolean>('log.rotation.enabled'),
99 MAX_FILE_SIZE: bytes.parse(config.get<string>('log.rotation.maxFileSize')),
100 MAX_FILES: config.get<number>('log.rotation.maxFiles')
101 },
102 ANONYMIZE_IP: config.get<boolean>('log.anonymizeIP')
103 },
104 SEARCH: {
105 REMOTE_URI: {
106 USERS: config.get<boolean>('search.remote_uri.users'),
107 ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
108 }
109 },
110 TRENDING: {
111 VIDEOS: {
112 INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
113 }
114 },
115 REDUNDANCY: {
116 VIDEOS: {
117 CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
118 STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
119 }
120 },
121 REMOTE_REDUNDANCY: {
122 VIDEOS: {
123 ACCEPT_FROM: config.get<VideoRedundancyConfigFilter>('remote_redundancy.videos.accept_from')
124 }
125 },
126 CSP: {
127 ENABLED: config.get<boolean>('csp.enabled'),
128 REPORT_ONLY: config.get<boolean>('csp.report_only'),
129 REPORT_URI: config.get<boolean>('csp.report_uri')
130 },
131 TRACKER: {
132 ENABLED: config.get<boolean>('tracker.enabled'),
133 PRIVATE: config.get<boolean>('tracker.private'),
134 REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
135 },
136 HISTORY: {
137 VIDEOS: {
138 MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
139 }
140 },
141 VIEWS: {
142 VIDEOS: {
143 REMOTE: {
144 MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
145 }
146 }
147 },
148 PLUGINS: {
149 INDEX: {
150 ENABLED: config.get<boolean>('plugins.index.enabled'),
151 CHECK_LATEST_VERSIONS_INTERVAL: parseDurationToMs(config.get<string>('plugins.index.check_latest_versions_interval')),
152 URL: config.get<string>('plugins.index.url')
153 }
154 },
155 ADMIN: {
156 get EMAIL () { return config.get<string>('admin.email') }
157 },
158 CONTACT_FORM: {
159 get ENABLED () { return config.get<boolean>('contact_form.enabled') }
160 },
161 SIGNUP: {
162 get ENABLED () { return config.get<boolean>('signup.enabled') },
163 get LIMIT () { return config.get<number>('signup.limit') },
164 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
165 FILTERS: {
166 CIDR: {
167 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
168 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
169 }
170 }
171 },
172 USER: {
173 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
174 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
175 },
176 TRANSCODING: {
177 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
178 get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
179 get ALLOW_AUDIO_FILES () { return config.get<boolean>('transcoding.allow_audio_files') },
180 get THREADS () { return config.get<number>('transcoding.threads') },
181 RESOLUTIONS: {
182 get '0p' () { return config.get<boolean>('transcoding.resolutions.0p') },
183 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
184 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
185 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
186 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
187 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') },
188 get '2160p' () { return config.get<boolean>('transcoding.resolutions.2160p') }
189 },
190 HLS: {
191 get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
192 },
193 WEBTORRENT: {
194 get ENABLED () { return config.get<boolean>('transcoding.webtorrent.enabled') }
195 }
196 },
197 IMPORT: {
198 VIDEOS: {
199 HTTP: {
200 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') },
201 PROXY: {
202 get ENABLED () { return config.get<boolean>('import.videos.http.proxy.enabled') },
203 get URL () { return config.get<string>('import.videos.http.proxy.url') }
204 }
205 },
206 TORRENT: {
207 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
208 }
209 }
210 },
211 AUTO_BLACKLIST: {
212 VIDEOS: {
213 OF_USERS: {
214 get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
215 }
216 }
217 },
218 CACHE: {
219 PREVIEWS: {
220 get SIZE () { return config.get<number>('cache.previews.size') }
221 },
222 VIDEO_CAPTIONS: {
223 get SIZE () { return config.get<number>('cache.captions.size') }
224 }
225 },
226 INSTANCE: {
227 get NAME () { return config.get<string>('instance.name') },
228 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
229 get DESCRIPTION () { return config.get<string>('instance.description') },
230 get TERMS () { return config.get<string>('instance.terms') },
231 get CODE_OF_CONDUCT () { return config.get<string>('instance.code_of_conduct') },
232
233 get CREATION_REASON () { return config.get<string>('instance.creation_reason') },
234
235 get MODERATION_INFORMATION () { return config.get<string>('instance.moderation_information') },
236 get ADMINISTRATOR () { return config.get<string>('instance.administrator') },
237 get MAINTENANCE_LIFETIME () { return config.get<string>('instance.maintenance_lifetime') },
238 get BUSINESS_MODEL () { return config.get<string>('instance.business_model') },
239 get HARDWARE_INFORMATION () { return config.get<string>('instance.hardware_information') },
240
241 get LANGUAGES () { return config.get<string[]>('instance.languages') || [] },
242 get CATEGORIES () { return config.get<number[]>('instance.categories') || [] },
243
244 get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
245 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
246 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
247 CUSTOMIZATIONS: {
248 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
249 get CSS () { return config.get<string>('instance.customizations.css') }
250 },
251 get ROBOTS () { return config.get<string>('instance.robots') },
252 get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
253 get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
254 },
255 SERVICES: {
256 TWITTER: {
257 get USERNAME () { return config.get<string>('services.twitter.username') },
258 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
259 }
260 },
261 FOLLOWERS: {
262 INSTANCE: {
263 get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
264 get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
265 }
266 },
267 FOLLOWINGS: {
268 INSTANCE: {
269 AUTO_FOLLOW_BACK: {
270 get ENABLED () {
271 return config.get<boolean>('followings.instance.auto_follow_back.enabled')
272 }
273 },
274 AUTO_FOLLOW_INDEX: {
275 get ENABLED () {
276 return config.get<boolean>('followings.instance.auto_follow_index.enabled')
277 },
278 get INDEX_URL () {
279 return config.get<string>('followings.instance.auto_follow_index.index_url')
280 }
281 }
282 }
283 },
284 THEME: {
285 get DEFAULT () { return config.get<string>('theme.default') }
286 }
287 }
288
289 function registerConfigChangedHandler (fun: Function) {
290 configChangedHandlers.push(fun)
291 }
292
293 function isEmailEnabled () {
294 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
295 }
296
297 // ---------------------------------------------------------------------------
298
299 export {
300 CONFIG,
301 registerConfigChangedHandler,
302 isEmailEnabled
303 }
304
305 // ---------------------------------------------------------------------------
306
307 function getLocalConfigFilePath () {
308 const configSources = config.util.getConfigSources()
309 if (configSources.length === 0) throw new Error('Invalid config source.')
310
311 let filename = 'local'
312 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
313 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
314
315 return join(dirname(configSources[0].name), filename + '.json')
316 }
317
318 function buildVideosRedundancy (objs: any[]): VideosRedundancyStrategy[] {
319 if (!objs) return []
320
321 if (!Array.isArray(objs)) return objs
322
323 return objs.map(obj => {
324 return Object.assign({}, obj, {
325 minLifetime: parseDurationToMs(obj.min_lifetime),
326 size: bytes.parse(obj.size),
327 minViews: obj.min_views
328 })
329 })
330 }
331
332 export function reloadConfig () {
333
334 function directory () {
335 if (process.env.NODE_CONFIG_DIR) {
336 return process.env.NODE_CONFIG_DIR
337 }
338
339 return join(root(), 'config')
340 }
341
342 function purge () {
343 for (const fileName in require.cache) {
344 if (fileName.includes(directory()) === false) {
345 continue
346 }
347
348 delete require.cache[fileName]
349 }
350
351 delete require.cache[require.resolve('config')]
352 }
353
354 purge()
355
356 config = require('config')
357
358 for (const configChangedHandler of configChangedHandlers) {
359 configChangedHandler()
360 }
361 }