1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobType, VideoRateType, VideoState } from '../../shared/models'
4 import { ActivityPubActorType } from '../../shared/models/activitypub'
5 import { FollowState } from '../../shared/models/actors'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 // Do not use barrels, remain constants as independent as possible
8 import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
10 import { invert } from 'lodash'
12 // Use a variable to reload the configuration if we need
13 let config: IConfig = require('config')
15 // ---------------------------------------------------------------------------
17 const LAST_MIGRATION_VERSION = 230
19 // ---------------------------------------------------------------------------
22 const API_VERSION = 'v1'
24 // Number of results by default for the pagination
25 const PAGINATION_COUNT_DEFAULT = 15
27 // Sortable columns per schema
28 const SORTABLE_COLUMNS = {
29 USERS: [ 'id', 'username', 'createdAt' ],
30 ACCOUNTS: [ 'createdAt' ],
31 JOBS: [ 'createdAt' ],
32 VIDEO_ABUSES: [ 'id', 'createdAt' ],
33 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
34 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ],
35 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
36 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
37 FOLLOWERS: [ 'createdAt' ],
38 FOLLOWING: [ 'createdAt' ],
40 VIDEOS_SEARCH: [ 'bestmatch', 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ]
43 const OAUTH_LIFETIME = {
44 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
45 REFRESH_TOKEN: 1209600 // 2 weeks
48 const ROUTE_CACHE_LIFETIME = {
49 FEEDS: 1000 * 60 * 15, // 15 minutes
51 VIDEOS: 1000 // 1 second, cache concurrent requests after a broadcast for example
55 // ---------------------------------------------------------------------------
57 // Number of points we add/remove after a successful/bad request
58 const ACTOR_FOLLOW_SCORE = {
65 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
70 const REMOTE_SCHEME = {
75 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
76 'activitypub-http-broadcast': 5,
77 'activitypub-http-unicast': 5,
78 'activitypub-http-fetcher': 5,
79 'activitypub-follow': 5,
80 'video-file-import': 1,
84 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
85 'activitypub-http-broadcast': 1,
86 'activitypub-http-unicast': 5,
87 'activitypub-http-fetcher': 1,
88 'activitypub-follow': 3,
89 'video-file-import': 1,
93 const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-http-broadcast job
94 const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
95 const JOB_REQUEST_TTL = 60000 * 10 // 10 minutes
96 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
99 let SCHEDULER_INTERVALS_MS = {
100 badActorFollow: 60000 * 60, // 1 hour
101 removeOldJobs: 60000 * 60, // 1 hour
102 updateVideos: 60000 // 1 minute
105 // ---------------------------------------------------------------------------
108 CUSTOM_FILE: getLocalConfigFilePath(),
110 PORT: config.get<number>('listen.port'),
111 HOSTNAME: config.get<string>('listen.hostname')
114 DBNAME: 'peertube' + config.get<string>('database.suffix'),
115 HOSTNAME: config.get<string>('database.hostname'),
116 PORT: config.get<number>('database.port'),
117 USERNAME: config.get<string>('database.username'),
118 PASSWORD: config.get<string>('database.password')
121 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
122 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
123 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
124 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
125 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
128 HOSTNAME: config.get<string>('smtp.hostname'),
129 PORT: config.get<number>('smtp.port'),
130 USERNAME: config.get<string>('smtp.username'),
131 PASSWORD: config.get<string>('smtp.password'),
132 TLS: config.get<boolean>('smtp.tls'),
133 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
134 CA_FILE: config.get<string>('smtp.ca_file'),
135 FROM_ADDRESS: config.get<string>('smtp.from_address')
138 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
139 LOG_DIR: buildPath(config.get<string>('storage.logs')),
140 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
141 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
142 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
143 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
144 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
145 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
148 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
149 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
150 HOSTNAME: config.get<string>('webserver.hostname'),
151 PORT: config.get<number>('webserver.port'),
155 TRUST_PROXY: config.get<string[]>('trust_proxy'),
157 LEVEL: config.get<string>('log.level')
160 get EMAIL () { return config.get<string>('admin.email') }
163 get ENABLED () { return config.get<boolean>('signup.enabled') },
164 get LIMIT () { return config.get<number>('signup.limit') },
167 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
168 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
173 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
176 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
177 get THREADS () { return config.get<number>('transcoding.threads') },
179 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
180 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
181 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
182 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
183 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
188 get SIZE () { return config.get<number>('cache.previews.size') }
191 get SIZE () { return config.get<number>('cache.captions.size') }
195 get NAME () { return config.get<string>('instance.name') },
196 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
197 get DESCRIPTION () { return config.get<string>('instance.description') },
198 get TERMS () { return config.get<string>('instance.terms') },
199 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
200 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
202 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
203 get CSS () { return config.get<string>('instance.customizations.css') }
205 get ROBOTS () { return config.get<string>('instance.robots') }
209 get USERNAME () { return config.get<string>('services.twitter.username') },
210 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
215 // ---------------------------------------------------------------------------
217 const CONSTRAINTS_FIELDS = {
219 NAME: { min: 3, max: 120 }, // Length
220 DESCRIPTION: { min: 3, max: 250 }, // Length
221 USERNAME: { min: 3, max: 20 }, // Length
222 PASSWORD: { min: 6, max: 255 }, // Length
223 VIDEO_QUOTA: { min: -1 }
226 REASON: { min: 2, max: 300 } // Length
229 NAME: { min: 3, max: 120 }, // Length
230 DESCRIPTION: { min: 3, max: 500 }, // Length
231 SUPPORT: { min: 3, max: 500 }, // Length
232 URL: { min: 3, max: 2000 } // Length
236 EXTNAME: [ '.vtt', '.srt' ],
238 max: 2 * 1024 * 1024 // 2MB
243 NAME: { min: 3, max: 120 }, // Length
244 LANGUAGE: { min: 1, max: 10 }, // Length
245 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
246 DESCRIPTION: { min: 3, max: 10000 }, // Length
247 SUPPORT: { min: 3, max: 500 }, // Length
249 EXTNAME: [ '.jpg', '.jpeg' ],
251 max: 2 * 1024 * 1024 // 2MB
254 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
255 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
256 DURATION: { min: 1 }, // Number
257 TAGS: { min: 0, max: 5 }, // Number of total tags
258 TAG: { min: 2, max: 30 }, // Length
259 THUMBNAIL: { min: 2, max: 30 },
260 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
263 DISLIKES: { min: 0 },
264 FILE_SIZE: { min: 10 },
265 URL: { min: 3, max: 2000 } // Length
268 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
269 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
270 URL: { min: 3, max: 2000 }, // Length
272 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
274 max: 2 * 1024 * 1024 // 2MB
282 TEXT: { min: 1, max: 3000 }, // Length
283 URL: { min: 3, max: 2000 } // Length
286 URL: { min: 3, max: 2000 } // Length
290 const RATES_LIMIT = {
292 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
293 MAX: 15 // 15 attempts
297 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
298 const VIDEO_TRANSCODING_FPS = {
302 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
305 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
310 const VIDEO_CATEGORIES = {
325 15: 'Science & Technology',
331 // See https://creativecommons.org/licenses/?lang=en
332 const VIDEO_LICENCES = {
334 2: 'Attribution - Share Alike',
335 3: 'Attribution - No Derivatives',
336 4: 'Attribution - Non Commercial',
337 5: 'Attribution - Non Commercial - Share Alike',
338 6: 'Attribution - Non Commercial - No Derivatives',
339 7: 'Public Domain Dedication'
342 const VIDEO_LANGUAGES = buildLanguages()
344 const VIDEO_PRIVACIES = {
345 [VideoPrivacy.PUBLIC]: 'Public',
346 [VideoPrivacy.UNLISTED]: 'Unlisted',
347 [VideoPrivacy.PRIVATE]: 'Private'
350 const VIDEO_STATES = {
351 [VideoState.PUBLISHED]: 'Published',
352 [VideoState.TO_TRANSCODE]: 'To transcode'
355 const VIDEO_MIMETYPE_EXT = {
356 'video/webm': '.webm',
360 const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT)
362 const IMAGE_MIMETYPE_EXT = {
368 const VIDEO_CAPTIONS_MIMETYPE_EXT = {
370 'application/x-subrip': '.srt'
373 // ---------------------------------------------------------------------------
375 const SERVER_ACTOR_NAME = 'peertube'
377 const ACTIVITY_PUB = {
378 POTENTIAL_ACCEPT_HEADERS: [
379 'application/activity+json',
380 'application/ld+json',
381 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
383 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
384 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
385 COLLECTION_ITEMS_PER_PAGE: 10,
386 FETCH_PAGE_LIMIT: 100,
388 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
389 TORRENT: [ 'application/x-bittorrent' ],
390 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
392 MAX_RECURSION_COMMENTS: 100,
393 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
396 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
399 APPLICATION: 'Application'
402 // ---------------------------------------------------------------------------
404 const PRIVATE_RSA_KEY_SIZE = 2048
406 // Password encryption
407 const BCRYPT_SALT_SIZE = 10
409 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
411 const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
412 DO_NOT_LIST: 'do_not_list',
417 // ---------------------------------------------------------------------------
419 // Express static paths (router)
420 const STATIC_PATHS = {
421 PREVIEWS: '/static/previews/',
422 THUMBNAILS: '/static/thumbnails/',
423 TORRENTS: '/static/torrents/',
424 WEBSEED: '/static/webseed/',
425 AVATARS: '/static/avatars/',
426 VIDEO_CAPTIONS: '/static/video-captions/'
428 const STATIC_DOWNLOAD_PATHS = {
429 TORRENTS: '/download/torrents/',
430 VIDEOS: '/download/videos/'
434 let STATIC_MAX_AGE = '2h'
436 // Videos thumbnail size
437 const THUMBNAILS_SIZE = {
441 const PREVIEWS_SIZE = {
445 const AVATARS_SIZE = {
455 // Sub folders of cache directory
458 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
459 MAX_AGE: 1000 * 3600 * 3 // 3 hours
462 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
463 MAX_AGE: 1000 * 3600 * 3 // 3 hours
467 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
469 // ---------------------------------------------------------------------------
471 const CUSTOM_HTML_TAG_COMMENTS = {
472 TITLE: '<!-- title tag -->',
473 DESCRIPTION: '<!-- description tag -->',
474 CUSTOM_CSS: '<!-- custom css tag -->',
475 OPENGRAPH_AND_OEMBED: '<!-- open graph and oembed tags -->'
478 // ---------------------------------------------------------------------------
484 // ---------------------------------------------------------------------------
486 const TRACKER_RATE_LIMITS = {
487 INTERVAL: 60000 * 5, // 5 minutes
488 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
489 ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
492 // ---------------------------------------------------------------------------
494 // Special constants for a test instance
495 if (isTestInstance() === true) {
496 ACTOR_FOLLOW_SCORE.BASE = 20
498 REMOTE_SCHEME.HTTP = 'http'
499 REMOTE_SCHEME.WS = 'ws'
503 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
504 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
506 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
508 SCHEDULER_INTERVALS_MS.badActorFollow = 10000
509 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
510 SCHEDULER_INTERVALS_MS.updateVideos = 5000
512 VIDEO_VIEW_LIFETIME = 1000 // 1 second
514 JOB_ATTEMPTS['email'] = 1
516 CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
519 updateWebserverConfig()
521 // ---------------------------------------------------------------------------
525 VIDEO_CAPTIONS_MIMETYPE_EXT,
536 LAST_MIGRATION_VERSION,
538 CUSTOM_HTML_TAG_COMMENTS,
539 BROADCAST_CONCURRENCY,
540 PAGINATION_COUNT_DEFAULT,
546 PRIVATE_RSA_KEY_SIZE,
547 ROUTE_CACHE_LIFETIME,
554 ACTIVITY_PUB_ACTOR_TYPES,
563 VIDEO_TRANSCODING_FPS,
566 USER_PASSWORD_RESET_LIFETIME,
568 SCHEDULER_INTERVALS_MS,
569 STATIC_DOWNLOAD_PATHS,
572 JOB_COMPLETED_LIFETIME,
577 // ---------------------------------------------------------------------------
579 function getLocalConfigFilePath () {
580 const configSources = config.util.getConfigSources()
581 if (configSources.length === 0) throw new Error('Invalid config source.')
583 let filename = 'local'
584 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
585 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
587 return join(dirname(configSources[ 0 ].name), filename + '.json')
590 function updateWebserverConfig () {
591 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
592 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
595 function buildLanguages () {
596 const iso639 = require('iso-639-3')
598 const languages: { [ id: string ]: string } = {}
600 const additionalLanguages = {
601 'sgn': true, // Sign languages (macro language)
602 'ase': true, // American sign language
603 'sdl': true, // Arabian sign language
604 'bfi': true, // British sign language
605 'bzs': true, // Brazilian sign language
606 'csl': true, // Chinese sign language
607 'cse': true, // Czech sign language
608 'dsl': true, // Danish sign language
609 'fsl': true, // French sign language
610 'gsg': true, // German sign language
611 'pks': true, // Pakistan sign language
612 'jsl': true, // Japanese sign language
613 'sfs': true, // South African sign language
614 'swl': true, // Swedish sign language
615 'rsl': true, // Russian sign language: true
617 'epo': true, // Esperanto
618 'tlh': true, // Klingon
619 'jbo': true, // Lojban
620 'avk': true // Kotava
623 // Only add ISO639-1 languages and some sign languages (ISO639-3)
626 return (l.iso6391 !== null && l.type === 'living') ||
627 additionalLanguages[l.iso6393] === true
629 .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
634 export function reloadConfig () {
636 function directory () {
637 if (process.env.NODE_CONFIG_DIR) {
638 return process.env.NODE_CONFIG_DIR
641 return join(root(), 'config')
645 for (const fileName in require.cache) {
646 if (-1 === fileName.indexOf(directory())) {
650 delete require.cache[fileName]
653 delete require.cache[require.resolve('config')]
658 config = require('config')
660 updateWebserverConfig()