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'
11 import { VideoImportState } from '../../shared/models/videos/video-import-state.enum'
13 // Use a variable to reload the configuration if we need
14 let config: IConfig = require('config')
16 // ---------------------------------------------------------------------------
18 const LAST_MIGRATION_VERSION = 245
20 // ---------------------------------------------------------------------------
23 const API_VERSION = 'v1'
32 // Sortable columns per schema
33 const SORTABLE_COLUMNS = {
34 USERS: [ 'id', 'username', 'createdAt' ],
35 ACCOUNTS: [ 'createdAt' ],
36 JOBS: [ 'createdAt' ],
37 VIDEO_ABUSES: [ 'id', 'createdAt' ],
38 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
39 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ],
40 VIDEO_IMPORTS: [ 'createdAt' ],
41 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
42 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
43 FOLLOWERS: [ 'createdAt' ],
44 FOLLOWING: [ 'createdAt' ],
46 VIDEOS_SEARCH: [ 'match', 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ]
49 const OAUTH_LIFETIME = {
50 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
51 REFRESH_TOKEN: 1209600 // 2 weeks
54 const ROUTE_CACHE_LIFETIME = {
57 NODEINFO: '10 minutes',
60 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
64 // ---------------------------------------------------------------------------
66 // Number of points we add/remove after a successful/bad request
67 const ACTOR_FOLLOW_SCORE = {
74 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
79 const REMOTE_SCHEME = {
84 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
85 'activitypub-http-broadcast': 5,
86 'activitypub-http-unicast': 5,
87 'activitypub-http-fetcher': 5,
88 'activitypub-follow': 5,
89 'video-file-import': 1,
94 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
95 'activitypub-http-broadcast': 1,
96 'activitypub-http-unicast': 5,
97 'activitypub-http-fetcher': 1,
98 'activitypub-follow': 3,
99 'video-file-import': 1,
104 const JOB_TTL: { [ id in JobType ]: number } = {
105 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
106 'activitypub-http-unicast': 60000 * 10, // 10 minutes
107 'activitypub-http-fetcher': 60000 * 10, // 10 minutes
108 'activitypub-follow': 60000 * 10, // 10 minutes
109 'video-file-import': 1000 * 3600, // 1 hour
110 'video-file': 1000 * 3600 * 48, // 2 days, transcoding could be long
111 'video-import': 1000 * 3600 * 5, // 5 hours
112 'email': 60000 * 10 // 10 minutes
114 const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-http-broadcast job
115 const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
116 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
119 let SCHEDULER_INTERVALS_MS = {
120 badActorFollow: 60000 * 60, // 1 hour
121 removeOldJobs: 60000 * 60, // 1 hour
122 updateVideos: 60000, // 1 minute
123 youtubeDLUpdate: 60000 * 60 * 24 // 1 day
126 // ---------------------------------------------------------------------------
129 CUSTOM_FILE: getLocalConfigFilePath(),
131 PORT: config.get<number>('listen.port'),
132 HOSTNAME: config.get<string>('listen.hostname')
135 DBNAME: 'peertube' + config.get<string>('database.suffix'),
136 HOSTNAME: config.get<string>('database.hostname'),
137 PORT: config.get<number>('database.port'),
138 USERNAME: config.get<string>('database.username'),
139 PASSWORD: config.get<string>('database.password'),
141 MAX: config.get<number>('database.pool.max')
145 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
146 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
147 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
148 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
149 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
152 HOSTNAME: config.get<string>('smtp.hostname'),
153 PORT: config.get<number>('smtp.port'),
154 USERNAME: config.get<string>('smtp.username'),
155 PASSWORD: config.get<string>('smtp.password'),
156 TLS: config.get<boolean>('smtp.tls'),
157 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
158 CA_FILE: config.get<string>('smtp.ca_file'),
159 FROM_ADDRESS: config.get<string>('smtp.from_address')
162 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
163 LOG_DIR: buildPath(config.get<string>('storage.logs')),
164 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
165 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
166 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
167 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
168 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
169 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
172 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
173 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
174 HOSTNAME: config.get<string>('webserver.hostname'),
175 PORT: config.get<number>('webserver.port'),
179 TRUST_PROXY: config.get<string[]>('trust_proxy'),
181 LEVEL: config.get<string>('log.level')
184 get EMAIL () { return config.get<string>('admin.email') }
187 get ENABLED () { return config.get<boolean>('signup.enabled') },
188 get LIMIT () { return config.get<number>('signup.limit') },
191 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
192 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
197 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
200 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
201 get THREADS () { return config.get<number>('transcoding.threads') },
203 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
204 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
205 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
206 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
207 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
213 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
216 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
222 get SIZE () { return config.get<number>('cache.previews.size') }
225 get SIZE () { return config.get<number>('cache.captions.size') }
229 get NAME () { return config.get<string>('instance.name') },
230 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
231 get DESCRIPTION () { return config.get<string>('instance.description') },
232 get TERMS () { return config.get<string>('instance.terms') },
233 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
234 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
236 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
237 get CSS () { return config.get<string>('instance.customizations.css') }
239 get ROBOTS () { return config.get<string>('instance.robots') }
243 get USERNAME () { return config.get<string>('services.twitter.username') },
244 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
249 // ---------------------------------------------------------------------------
251 const CONSTRAINTS_FIELDS = {
253 NAME: { min: 3, max: 120 }, // Length
254 DESCRIPTION: { min: 3, max: 250 }, // Length
255 USERNAME: { min: 3, max: 20 }, // Length
256 PASSWORD: { min: 6, max: 255 }, // Length
257 VIDEO_QUOTA: { min: -1 }
260 REASON: { min: 2, max: 300 } // Length
263 NAME: { min: 3, max: 120 }, // Length
264 DESCRIPTION: { min: 3, max: 500 }, // Length
265 SUPPORT: { min: 3, max: 500 }, // Length
266 URL: { min: 3, max: 2000 } // Length
270 EXTNAME: [ '.vtt', '.srt' ],
272 max: 2 * 1024 * 1024 // 2MB
277 URL: { min: 3, max: 2000 }, // Length
278 TORRENT_NAME: { min: 3, max: 255 }, // Length
280 EXTNAME: [ '.torrent' ],
282 max: 1024 * 200 // 200 KB
287 NAME: { min: 3, max: 120 }, // Length
288 LANGUAGE: { min: 1, max: 10 }, // Length
289 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
290 DESCRIPTION: { min: 3, max: 10000 }, // Length
291 SUPPORT: { min: 3, max: 500 }, // Length
293 EXTNAME: [ '.jpg', '.jpeg' ],
295 max: 2 * 1024 * 1024 // 2MB
298 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
299 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
300 DURATION: { min: 0 }, // Number
301 TAGS: { min: 0, max: 5 }, // Number of total tags
302 TAG: { min: 2, max: 30 }, // Length
303 THUMBNAIL: { min: 2, max: 30 },
304 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
307 DISLIKES: { min: 0 },
308 FILE_SIZE: { min: 10 },
309 URL: { min: 3, max: 2000 } // Length
312 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
313 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
314 URL: { min: 3, max: 2000 }, // Length
316 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
318 max: 2 * 1024 * 1024 // 2MB
326 TEXT: { min: 1, max: 3000 }, // Length
327 URL: { min: 3, max: 2000 } // Length
330 URL: { min: 3, max: 2000 } // Length
334 const RATES_LIMIT = {
336 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
337 MAX: 15 // 15 attempts
341 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
342 const VIDEO_TRANSCODING_FPS = {
346 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
349 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
354 const FFMPEG_NICE: { [ id: string ]: number } = {
355 THUMBNAIL: 2, // 2 just for don't blocking servers
359 const VIDEO_CATEGORIES = {
374 15: 'Science & Technology',
380 // See https://creativecommons.org/licenses/?lang=en
381 const VIDEO_LICENCES = {
383 2: 'Attribution - Share Alike',
384 3: 'Attribution - No Derivatives',
385 4: 'Attribution - Non Commercial',
386 5: 'Attribution - Non Commercial - Share Alike',
387 6: 'Attribution - Non Commercial - No Derivatives',
388 7: 'Public Domain Dedication'
391 const VIDEO_LANGUAGES = buildLanguages()
393 const VIDEO_PRIVACIES = {
394 [VideoPrivacy.PUBLIC]: 'Public',
395 [VideoPrivacy.UNLISTED]: 'Unlisted',
396 [VideoPrivacy.PRIVATE]: 'Private'
399 const VIDEO_STATES = {
400 [VideoState.PUBLISHED]: 'Published',
401 [VideoState.TO_TRANSCODE]: 'To transcode',
402 [VideoState.TO_IMPORT]: 'To import'
405 const VIDEO_IMPORT_STATES = {
406 [VideoImportState.FAILED]: 'Failed',
407 [VideoImportState.PENDING]: 'Pending',
408 [VideoImportState.SUCCESS]: 'Success'
411 const VIDEO_MIMETYPE_EXT = {
412 'video/webm': '.webm',
416 const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT)
418 const IMAGE_MIMETYPE_EXT = {
424 const VIDEO_CAPTIONS_MIMETYPE_EXT = {
426 'application/x-subrip': '.srt'
429 const TORRENT_MIMETYPE_EXT = {
430 'application/x-bittorrent': '.torrent'
433 // ---------------------------------------------------------------------------
435 const SERVER_ACTOR_NAME = 'peertube'
437 const ACTIVITY_PUB = {
438 POTENTIAL_ACCEPT_HEADERS: [
439 'application/activity+json',
440 'application/ld+json',
441 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
443 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
444 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
445 COLLECTION_ITEMS_PER_PAGE: 10,
446 FETCH_PAGE_LIMIT: 100,
448 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
449 TORRENT: [ 'application/x-bittorrent' ],
450 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
452 MAX_RECURSION_COMMENTS: 100,
453 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
456 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
459 APPLICATION: 'Application'
462 // ---------------------------------------------------------------------------
464 const PRIVATE_RSA_KEY_SIZE = 2048
466 // Password encryption
467 const BCRYPT_SALT_SIZE = 10
469 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
471 const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
472 DO_NOT_LIST: 'do_not_list',
477 // ---------------------------------------------------------------------------
479 // Express static paths (router)
480 const STATIC_PATHS = {
481 PREVIEWS: '/static/previews/',
482 THUMBNAILS: '/static/thumbnails/',
483 TORRENTS: '/static/torrents/',
484 WEBSEED: '/static/webseed/',
485 AVATARS: '/static/avatars/',
486 VIDEO_CAPTIONS: '/static/video-captions/'
488 const STATIC_DOWNLOAD_PATHS = {
489 TORRENTS: '/download/torrents/',
490 VIDEOS: '/download/videos/'
494 let STATIC_MAX_AGE = '2h'
496 // Videos thumbnail size
497 const THUMBNAILS_SIZE = {
501 const PREVIEWS_SIZE = {
505 const AVATARS_SIZE = {
515 // Sub folders of cache directory
518 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
519 MAX_AGE: 1000 * 3600 * 3 // 3 hours
522 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
523 MAX_AGE: 1000 * 3600 * 3 // 3 hours
527 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
529 // ---------------------------------------------------------------------------
531 const CUSTOM_HTML_TAG_COMMENTS = {
532 TITLE: '<!-- title tag -->',
533 DESCRIPTION: '<!-- description tag -->',
534 CUSTOM_CSS: '<!-- custom css tag -->',
535 OPENGRAPH_AND_OEMBED: '<!-- open graph and oembed tags -->'
538 // ---------------------------------------------------------------------------
544 // ---------------------------------------------------------------------------
546 const TRACKER_RATE_LIMITS = {
547 INTERVAL: 60000 * 5, // 5 minutes
548 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
549 ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
552 // ---------------------------------------------------------------------------
554 // Special constants for a test instance
555 if (isTestInstance() === true) {
556 ACTOR_FOLLOW_SCORE.BASE = 20
558 REMOTE_SCHEME.HTTP = 'http'
559 REMOTE_SCHEME.WS = 'ws'
563 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
564 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
566 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
568 SCHEDULER_INTERVALS_MS.badActorFollow = 10000
569 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
570 SCHEDULER_INTERVALS_MS.updateVideos = 5000
572 VIDEO_VIEW_LIFETIME = 1000 // 1 second
574 JOB_ATTEMPTS['email'] = 1
576 CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
579 updateWebserverConfig()
581 // ---------------------------------------------------------------------------
585 VIDEO_CAPTIONS_MIMETYPE_EXT,
596 LAST_MIGRATION_VERSION,
598 CUSTOM_HTML_TAG_COMMENTS,
599 BROADCAST_CONCURRENCY,
606 PRIVATE_RSA_KEY_SIZE,
607 ROUTE_CACHE_LIFETIME,
612 TORRENT_MIMETYPE_EXT,
616 ACTIVITY_PUB_ACTOR_TYPES,
625 VIDEO_TRANSCODING_FPS,
628 USER_PASSWORD_RESET_LIFETIME,
630 SCHEDULER_INTERVALS_MS,
631 STATIC_DOWNLOAD_PATHS,
634 JOB_COMPLETED_LIFETIME,
640 // ---------------------------------------------------------------------------
642 function getLocalConfigFilePath () {
643 const configSources = config.util.getConfigSources()
644 if (configSources.length === 0) throw new Error('Invalid config source.')
646 let filename = 'local'
647 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
648 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
650 return join(dirname(configSources[ 0 ].name), filename + '.json')
653 function updateWebserverConfig () {
654 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
655 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
658 function buildLanguages () {
659 const iso639 = require('iso-639-3')
661 const languages: { [ id: string ]: string } = {}
663 const additionalLanguages = {
664 'sgn': true, // Sign languages (macro language)
665 'ase': true, // American sign language
666 'sdl': true, // Arabian sign language
667 'bfi': true, // British sign language
668 'bzs': true, // Brazilian sign language
669 'csl': true, // Chinese sign language
670 'cse': true, // Czech sign language
671 'dsl': true, // Danish sign language
672 'fsl': true, // French sign language
673 'gsg': true, // German sign language
674 'pks': true, // Pakistan sign language
675 'jsl': true, // Japanese sign language
676 'sfs': true, // South African sign language
677 'swl': true, // Swedish sign language
678 'rsl': true, // Russian sign language: true
680 'epo': true, // Esperanto
681 'tlh': true, // Klingon
682 'jbo': true, // Lojban
683 'avk': true // Kotava
686 // Only add ISO639-1 languages and some sign languages (ISO639-3)
689 return (l.iso6391 !== null && l.type === 'living') ||
690 additionalLanguages[l.iso6393] === true
692 .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
697 export function reloadConfig () {
699 function directory () {
700 if (process.env.NODE_CONFIG_DIR) {
701 return process.env.NODE_CONFIG_DIR
704 return join(root(), 'config')
708 for (const fileName in require.cache) {
709 if (-1 === fileName.indexOf(directory())) {
713 delete require.cache[fileName]
716 delete require.cache[require.resolve('config')]
721 config = require('config')
723 updateWebserverConfig()