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 { VideoAbuseState, VideoImportState, 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'
11 import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
13 // Use a variable to reload the configuration if we need
14 let config: IConfig = require('config')
16 // ---------------------------------------------------------------------------
18 const LAST_MIGRATION_VERSION = 265
20 // ---------------------------------------------------------------------------
23 const API_VERSION = 'v1'
32 // Sortable columns per schema
33 const SORTABLE_COLUMNS = {
34 USERS: [ 'id', 'username', 'createdAt' ],
35 USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
36 ACCOUNTS: [ 'createdAt' ],
37 JOBS: [ 'createdAt' ],
38 VIDEO_ABUSES: [ 'id', 'createdAt', 'state' ],
39 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
40 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ],
41 VIDEO_IMPORTS: [ 'createdAt' ],
42 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
43 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
44 FOLLOWERS: [ 'createdAt' ],
45 FOLLOWING: [ 'createdAt' ],
47 VIDEOS_SEARCH: [ 'match', 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ],
48 VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ]
51 const OAUTH_LIFETIME = {
52 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
53 REFRESH_TOKEN: 1209600 // 2 weeks
56 const ROUTE_CACHE_LIFETIME = {
59 NODEINFO: '10 minutes',
62 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
66 // ---------------------------------------------------------------------------
68 // Number of points we add/remove after a successful/bad request
69 const ACTOR_FOLLOW_SCORE = {
76 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
81 const REMOTE_SCHEME = {
86 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
87 'activitypub-http-broadcast': 5,
88 'activitypub-http-unicast': 5,
89 'activitypub-http-fetcher': 5,
90 'activitypub-follow': 5,
91 'video-file-import': 1,
97 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
98 'activitypub-http-broadcast': 1,
99 'activitypub-http-unicast': 5,
100 'activitypub-http-fetcher': 1,
101 'activitypub-follow': 3,
102 'video-file-import': 1,
108 const JOB_TTL: { [ id in JobType ]: number } = {
109 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
110 'activitypub-http-unicast': 60000 * 10, // 10 minutes
111 'activitypub-http-fetcher': 60000 * 10, // 10 minutes
112 'activitypub-follow': 60000 * 10, // 10 minutes
113 'video-file-import': 1000 * 3600, // 1 hour
114 'video-file': 1000 * 3600 * 48, // 2 days, transcoding could be long
115 'video-import': 1000 * 3600 * 5, // 5 hours
116 'email': 60000 * 10, // 10 minutes
117 'videos-views': undefined // Unlimited
119 const REPEAT_JOBS: { [ id: string ]: EveryRepeatOptions | CronRepeatOptions } = {
121 cron: '1 * * * *' // At 1 minutes past the hour
125 const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-http-broadcast job
126 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
127 const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
128 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
131 let SCHEDULER_INTERVALS_MS = {
132 badActorFollow: 60000 * 60, // 1 hour
133 removeOldJobs: 60000 * 60, // 1 hour
134 updateVideos: 60000, // 1 minute
135 youtubeDLUpdate: 60000 * 60 * 24 // 1 day
138 // ---------------------------------------------------------------------------
141 CUSTOM_FILE: getLocalConfigFilePath(),
143 PORT: config.get<number>('listen.port'),
144 HOSTNAME: config.get<string>('listen.hostname')
147 DBNAME: 'peertube' + config.get<string>('database.suffix'),
148 HOSTNAME: config.get<string>('database.hostname'),
149 PORT: config.get<number>('database.port'),
150 USERNAME: config.get<string>('database.username'),
151 PASSWORD: config.get<string>('database.password'),
153 MAX: config.get<number>('database.pool.max')
157 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
158 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
159 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
160 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
161 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
164 HOSTNAME: config.get<string>('smtp.hostname'),
165 PORT: config.get<number>('smtp.port'),
166 USERNAME: config.get<string>('smtp.username'),
167 PASSWORD: config.get<string>('smtp.password'),
168 TLS: config.get<boolean>('smtp.tls'),
169 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
170 CA_FILE: config.get<string>('smtp.ca_file'),
171 FROM_ADDRESS: config.get<string>('smtp.from_address')
174 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
175 LOG_DIR: buildPath(config.get<string>('storage.logs')),
176 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
177 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
178 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
179 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
180 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
181 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
184 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
185 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
186 HOSTNAME: config.get<string>('webserver.hostname'),
187 PORT: config.get<number>('webserver.port'),
191 TRUST_PROXY: config.get<string[]>('trust_proxy'),
193 LEVEL: config.get<string>('log.level')
197 USERS: config.get<boolean>('search.remote_uri.users'),
198 ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
202 get EMAIL () { return config.get<string>('admin.email') }
205 get ENABLED () { return config.get<boolean>('signup.enabled') },
206 get LIMIT () { return config.get<number>('signup.limit') },
207 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
210 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
211 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
216 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') },
217 get VIDEO_QUOTA_DAILY () { return config.get<number>('user.video_quota_daily') }
220 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
221 get THREADS () { return config.get<number>('transcoding.threads') },
223 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
224 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
225 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
226 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
227 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
233 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
236 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
242 get SIZE () { return config.get<number>('cache.previews.size') }
245 get SIZE () { return config.get<number>('cache.captions.size') }
249 get NAME () { return config.get<string>('instance.name') },
250 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
251 get DESCRIPTION () { return config.get<string>('instance.description') },
252 get TERMS () { return config.get<string>('instance.terms') },
253 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
254 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
256 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
257 get CSS () { return config.get<string>('instance.customizations.css') }
259 get ROBOTS () { return config.get<string>('instance.robots') }
263 get USERNAME () { return config.get<string>('services.twitter.username') },
264 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
269 // ---------------------------------------------------------------------------
271 const CONSTRAINTS_FIELDS = {
273 NAME: { min: 3, max: 120 }, // Length
274 DESCRIPTION: { min: 3, max: 250 }, // Length
275 USERNAME: { min: 3, max: 20 }, // Length
276 PASSWORD: { min: 6, max: 255 }, // Length
277 VIDEO_QUOTA: { min: -1 },
278 VIDEO_QUOTA_DAILY: { min: -1 },
279 BLOCKED_REASON: { min: 3, max: 250 } // Length
282 REASON: { min: 2, max: 300 }, // Length
283 MODERATION_COMMENT: { min: 2, max: 300 } // Length
286 REASON: { min: 2, max: 300 } // Length
289 NAME: { min: 3, max: 120 }, // Length
290 DESCRIPTION: { min: 3, max: 500 }, // Length
291 SUPPORT: { min: 3, max: 500 }, // Length
292 URL: { min: 3, max: 2000 } // Length
296 EXTNAME: [ '.vtt', '.srt' ],
298 max: 2 * 1024 * 1024 // 2MB
303 URL: { min: 3, max: 2000 }, // Length
304 TORRENT_NAME: { min: 3, max: 255 }, // Length
306 EXTNAME: [ '.torrent' ],
308 max: 1024 * 200 // 200 KB
313 NAME: { min: 3, max: 120 }, // Length
314 LANGUAGE: { min: 1, max: 10 }, // Length
315 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
316 DESCRIPTION: { min: 3, max: 10000 }, // Length
317 SUPPORT: { min: 3, max: 500 }, // Length
319 EXTNAME: [ '.jpg', '.jpeg' ],
321 max: 2 * 1024 * 1024 // 2MB
324 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
325 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
326 DURATION: { min: 0 }, // Number
327 TAGS: { min: 0, max: 5 }, // Number of total tags
328 TAG: { min: 2, max: 30 }, // Length
329 THUMBNAIL: { min: 2, max: 30 },
330 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
333 DISLIKES: { min: 0 },
334 FILE_SIZE: { min: 10 },
335 URL: { min: 3, max: 2000 } // Length
338 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
339 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
340 URL: { min: 3, max: 2000 }, // Length
342 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
344 max: 2 * 1024 * 1024 // 2MB
352 TEXT: { min: 1, max: 3000 }, // Length
353 URL: { min: 3, max: 2000 } // Length
356 URL: { min: 3, max: 2000 } // Length
360 const RATES_LIMIT = {
362 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
363 MAX: 15 // 15 attempts
367 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
368 const VIDEO_TRANSCODING_FPS = {
372 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
375 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
380 const FFMPEG_NICE: { [ id: string ]: number } = {
381 THUMBNAIL: 2, // 2 just for don't blocking servers
385 const VIDEO_CATEGORIES = {
400 15: 'Science & Technology',
406 // See https://creativecommons.org/licenses/?lang=en
407 const VIDEO_LICENCES = {
409 2: 'Attribution - Share Alike',
410 3: 'Attribution - No Derivatives',
411 4: 'Attribution - Non Commercial',
412 5: 'Attribution - Non Commercial - Share Alike',
413 6: 'Attribution - Non Commercial - No Derivatives',
414 7: 'Public Domain Dedication'
417 const VIDEO_LANGUAGES = buildLanguages()
419 const VIDEO_PRIVACIES = {
420 [VideoPrivacy.PUBLIC]: 'Public',
421 [VideoPrivacy.UNLISTED]: 'Unlisted',
422 [VideoPrivacy.PRIVATE]: 'Private'
425 const VIDEO_STATES = {
426 [VideoState.PUBLISHED]: 'Published',
427 [VideoState.TO_TRANSCODE]: 'To transcode',
428 [VideoState.TO_IMPORT]: 'To import'
431 const VIDEO_IMPORT_STATES = {
432 [VideoImportState.FAILED]: 'Failed',
433 [VideoImportState.PENDING]: 'Pending',
434 [VideoImportState.SUCCESS]: 'Success'
437 const VIDEO_ABUSE_STATES = {
438 [VideoAbuseState.PENDING]: 'Pending',
439 [VideoAbuseState.REJECTED]: 'Rejected',
440 [VideoAbuseState.ACCEPTED]: 'Accepted'
443 const VIDEO_MIMETYPE_EXT = {
444 'video/webm': '.webm',
448 const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT)
450 const IMAGE_MIMETYPE_EXT = {
456 const VIDEO_CAPTIONS_MIMETYPE_EXT = {
458 'application/x-subrip': '.srt'
461 const TORRENT_MIMETYPE_EXT = {
462 'application/x-bittorrent': '.torrent'
465 // ---------------------------------------------------------------------------
467 const SERVER_ACTOR_NAME = 'peertube'
469 const ACTIVITY_PUB = {
470 POTENTIAL_ACCEPT_HEADERS: [
471 'application/activity+json',
472 'application/ld+json',
473 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
475 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
476 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
477 COLLECTION_ITEMS_PER_PAGE: 10,
478 FETCH_PAGE_LIMIT: 100,
480 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
481 TORRENT: [ 'application/x-bittorrent' ],
482 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
484 MAX_RECURSION_COMMENTS: 100,
485 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000, // 1 day
486 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
489 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
492 APPLICATION: 'Application'
495 // ---------------------------------------------------------------------------
497 const PRIVATE_RSA_KEY_SIZE = 2048
499 // Password encryption
500 const BCRYPT_SALT_SIZE = 10
502 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
504 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
506 const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
507 DO_NOT_LIST: 'do_not_list',
512 // ---------------------------------------------------------------------------
514 // Express static paths (router)
515 const STATIC_PATHS = {
516 PREVIEWS: '/static/previews/',
517 THUMBNAILS: '/static/thumbnails/',
518 TORRENTS: '/static/torrents/',
519 WEBSEED: '/static/webseed/',
520 AVATARS: '/static/avatars/',
521 VIDEO_CAPTIONS: '/static/video-captions/'
523 const STATIC_DOWNLOAD_PATHS = {
524 TORRENTS: '/download/torrents/',
525 VIDEOS: '/download/videos/'
529 let STATIC_MAX_AGE = '2h'
531 // Videos thumbnail size
532 const THUMBNAILS_SIZE = {
536 const PREVIEWS_SIZE = {
540 const AVATARS_SIZE = {
550 // Sub folders of cache directory
553 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
554 MAX_AGE: 1000 * 3600 * 3 // 3 hours
557 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
558 MAX_AGE: 1000 * 3600 * 3 // 3 hours
562 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
564 // ---------------------------------------------------------------------------
566 const CUSTOM_HTML_TAG_COMMENTS = {
567 TITLE: '<!-- title tag -->',
568 DESCRIPTION: '<!-- description tag -->',
569 CUSTOM_CSS: '<!-- custom css tag -->',
570 OPENGRAPH_AND_OEMBED: '<!-- open graph and oembed tags -->'
573 // ---------------------------------------------------------------------------
579 // ---------------------------------------------------------------------------
581 const TRACKER_RATE_LIMITS = {
582 INTERVAL: 60000 * 5, // 5 minutes
583 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
584 ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
587 // ---------------------------------------------------------------------------
589 // Special constants for a test instance
590 if (isTestInstance() === true) {
591 ACTOR_FOLLOW_SCORE.BASE = 20
593 REMOTE_SCHEME.HTTP = 'http'
594 REMOTE_SCHEME.WS = 'ws'
598 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
599 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
600 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
602 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
604 SCHEDULER_INTERVALS_MS.badActorFollow = 10000
605 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
606 SCHEDULER_INTERVALS_MS.updateVideos = 5000
607 REPEAT_JOBS['videos-views'] = { every: 5000 }
609 VIDEO_VIEW_LIFETIME = 1000 // 1 second
611 JOB_ATTEMPTS['email'] = 1
613 CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
616 updateWebserverConfig()
618 // ---------------------------------------------------------------------------
622 VIDEO_CAPTIONS_MIMETYPE_EXT,
633 LAST_MIGRATION_VERSION,
635 CUSTOM_HTML_TAG_COMMENTS,
636 BROADCAST_CONCURRENCY,
643 PRIVATE_RSA_KEY_SIZE,
644 ROUTE_CACHE_LIFETIME,
649 TORRENT_MIMETYPE_EXT,
653 ACTIVITY_PUB_ACTOR_TYPES,
662 VIDEO_TRANSCODING_FPS,
666 USER_PASSWORD_RESET_LIFETIME,
667 USER_EMAIL_VERIFY_LIFETIME,
669 SCHEDULER_INTERVALS_MS,
671 STATIC_DOWNLOAD_PATHS,
674 CRAWL_REQUEST_CONCURRENCY,
675 JOB_COMPLETED_LIFETIME,
681 // ---------------------------------------------------------------------------
683 function getLocalConfigFilePath () {
684 const configSources = config.util.getConfigSources()
685 if (configSources.length === 0) throw new Error('Invalid config source.')
687 let filename = 'local'
688 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
689 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
691 return join(dirname(configSources[ 0 ].name), filename + '.json')
694 function updateWebserverConfig () {
695 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
696 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
699 function buildLanguages () {
700 const iso639 = require('iso-639-3')
702 const languages: { [ id: string ]: string } = {}
704 const additionalLanguages = {
705 'sgn': true, // Sign languages (macro language)
706 'ase': true, // American sign language
707 'sdl': true, // Arabian sign language
708 'bfi': true, // British sign language
709 'bzs': true, // Brazilian sign language
710 'csl': true, // Chinese sign language
711 'cse': true, // Czech sign language
712 'dsl': true, // Danish sign language
713 'fsl': true, // French sign language
714 'gsg': true, // German sign language
715 'pks': true, // Pakistan sign language
716 'jsl': true, // Japanese sign language
717 'sfs': true, // South African sign language
718 'swl': true, // Swedish sign language
719 'rsl': true, // Russian sign language: true
721 'epo': true, // Esperanto
722 'tlh': true, // Klingon
723 'jbo': true, // Lojban
724 'avk': true // Kotava
727 // Only add ISO639-1 languages and some sign languages (ISO639-3)
730 return (l.iso6391 !== null && l.type === 'living') ||
731 additionalLanguages[l.iso6393] === true
733 .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
738 export function reloadConfig () {
740 function directory () {
741 if (process.env.NODE_CONFIG_DIR) {
742 return process.env.NODE_CONFIG_DIR
745 return join(root(), 'config')
749 for (const fileName in require.cache) {
750 if (-1 === fileName.indexOf(directory())) {
754 delete require.cache[fileName]
757 delete require.cache[require.resolve('config')]
762 config = require('config')
764 updateWebserverConfig()