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' ]
41 const OAUTH_LIFETIME = {
42 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
43 REFRESH_TOKEN: 1209600 // 2 weeks
46 const ROUTE_CACHE_LIFETIME = {
47 FEEDS: 1000 * 60 * 15, // 15 minutes
49 VIDEOS: 1000 // 1 second, cache concurrent requests after a broadcast for example
53 // ---------------------------------------------------------------------------
55 // Number of points we add/remove after a successful/bad request
56 const ACTOR_FOLLOW_SCORE = {
63 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
68 const REMOTE_SCHEME = {
73 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
74 'activitypub-http-broadcast': 5,
75 'activitypub-http-unicast': 5,
76 'activitypub-http-fetcher': 5,
77 'activitypub-follow': 5,
78 'video-file-import': 1,
82 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
83 'activitypub-http-broadcast': 1,
84 'activitypub-http-unicast': 5,
85 'activitypub-http-fetcher': 1,
86 'activitypub-follow': 3,
87 'video-file-import': 1,
91 const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-http-broadcast job
92 const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
93 const JOB_REQUEST_TTL = 60000 * 10 // 10 minutes
94 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
97 let SCHEDULER_INTERVALS_MS = {
98 badActorFollow: 60000 * 60, // 1 hour
99 removeOldJobs: 60000 * 60, // 1 hour
100 updateVideos: 60000 // 1 minute
103 // ---------------------------------------------------------------------------
106 CUSTOM_FILE: getLocalConfigFilePath(),
108 PORT: config.get<number>('listen.port'),
109 HOSTNAME: config.get<string>('listen.hostname')
112 DBNAME: 'peertube' + config.get<string>('database.suffix'),
113 HOSTNAME: config.get<string>('database.hostname'),
114 PORT: config.get<number>('database.port'),
115 USERNAME: config.get<string>('database.username'),
116 PASSWORD: config.get<string>('database.password')
119 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
120 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
121 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
122 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
123 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
126 HOSTNAME: config.get<string>('smtp.hostname'),
127 PORT: config.get<number>('smtp.port'),
128 USERNAME: config.get<string>('smtp.username'),
129 PASSWORD: config.get<string>('smtp.password'),
130 TLS: config.get<boolean>('smtp.tls'),
131 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
132 CA_FILE: config.get<string>('smtp.ca_file'),
133 FROM_ADDRESS: config.get<string>('smtp.from_address')
136 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
137 LOG_DIR: buildPath(config.get<string>('storage.logs')),
138 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
139 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
140 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
141 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
142 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
143 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
146 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
147 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
148 HOSTNAME: config.get<string>('webserver.hostname'),
149 PORT: config.get<number>('webserver.port'),
153 TRUST_PROXY: config.get<string[]>('trust_proxy'),
155 LEVEL: config.get<string>('log.level')
158 get EMAIL () { return config.get<string>('admin.email') }
161 get ENABLED () { return config.get<boolean>('signup.enabled') },
162 get LIMIT () { return config.get<number>('signup.limit') },
165 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
166 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
171 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
174 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
175 get THREADS () { return config.get<number>('transcoding.threads') },
177 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
178 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
179 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
180 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
181 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
186 get SIZE () { return config.get<number>('cache.previews.size') }
189 get SIZE () { return config.get<number>('cache.captions.size') }
193 get NAME () { return config.get<string>('instance.name') },
194 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
195 get DESCRIPTION () { return config.get<string>('instance.description') },
196 get TERMS () { return config.get<string>('instance.terms') },
197 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
198 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
200 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
201 get CSS () { return config.get<string>('instance.customizations.css') }
203 get ROBOTS () { return config.get<string>('instance.robots') }
207 get USERNAME () { return config.get<string>('services.twitter.username') },
208 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
213 // ---------------------------------------------------------------------------
215 const CONSTRAINTS_FIELDS = {
217 NAME: { min: 3, max: 120 }, // Length
218 DESCRIPTION: { min: 3, max: 250 }, // Length
219 USERNAME: { min: 3, max: 20 }, // Length
220 PASSWORD: { min: 6, max: 255 }, // Length
221 VIDEO_QUOTA: { min: -1 }
224 REASON: { min: 2, max: 300 } // Length
227 NAME: { min: 3, max: 120 }, // Length
228 DESCRIPTION: { min: 3, max: 500 }, // Length
229 SUPPORT: { min: 3, max: 500 }, // Length
230 URL: { min: 3, max: 2000 } // Length
234 EXTNAME: [ '.vtt', '.srt' ],
236 max: 2 * 1024 * 1024 // 2MB
241 NAME: { min: 3, max: 120 }, // Length
242 LANGUAGE: { min: 1, max: 10 }, // Length
243 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
244 DESCRIPTION: { min: 3, max: 10000 }, // Length
245 SUPPORT: { min: 3, max: 500 }, // Length
247 EXTNAME: [ '.jpg', '.jpeg' ],
249 max: 2 * 1024 * 1024 // 2MB
252 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
253 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
254 DURATION: { min: 1 }, // Number
255 TAGS: { min: 0, max: 5 }, // Number of total tags
256 TAG: { min: 2, max: 30 }, // Length
257 THUMBNAIL: { min: 2, max: 30 },
258 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
261 DISLIKES: { min: 0 },
262 FILE_SIZE: { min: 10 },
263 URL: { min: 3, max: 2000 } // Length
266 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
267 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
268 URL: { min: 3, max: 2000 }, // Length
270 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
272 max: 2 * 1024 * 1024 // 2MB
280 TEXT: { min: 1, max: 3000 }, // Length
281 URL: { min: 3, max: 2000 } // Length
284 URL: { min: 3, max: 2000 } // Length
288 const RATES_LIMIT = {
290 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
291 MAX: 15 // 15 attempts
295 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
296 const VIDEO_TRANSCODING_FPS = {
300 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
303 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
308 const VIDEO_CATEGORIES = {
323 15: 'Science & Technology',
329 // See https://creativecommons.org/licenses/?lang=en
330 const VIDEO_LICENCES = {
332 2: 'Attribution - Share Alike',
333 3: 'Attribution - No Derivatives',
334 4: 'Attribution - Non Commercial',
335 5: 'Attribution - Non Commercial - Share Alike',
336 6: 'Attribution - Non Commercial - No Derivatives',
337 7: 'Public Domain Dedication'
340 const VIDEO_LANGUAGES = buildLanguages()
342 const VIDEO_PRIVACIES = {
343 [VideoPrivacy.PUBLIC]: 'Public',
344 [VideoPrivacy.UNLISTED]: 'Unlisted',
345 [VideoPrivacy.PRIVATE]: 'Private'
348 const VIDEO_STATES = {
349 [VideoState.PUBLISHED]: 'Published',
350 [VideoState.TO_TRANSCODE]: 'To transcode'
353 const VIDEO_MIMETYPE_EXT = {
354 'video/webm': '.webm',
358 const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT)
360 const IMAGE_MIMETYPE_EXT = {
366 const VIDEO_CAPTIONS_MIMETYPE_EXT = {
368 'application/x-subrip': '.srt'
371 // ---------------------------------------------------------------------------
373 const SERVER_ACTOR_NAME = 'peertube'
375 const ACTIVITY_PUB = {
376 POTENTIAL_ACCEPT_HEADERS: [
377 'application/activity+json',
378 'application/ld+json',
379 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
381 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
382 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
383 COLLECTION_ITEMS_PER_PAGE: 10,
384 FETCH_PAGE_LIMIT: 100,
386 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
387 TORRENT: [ 'application/x-bittorrent' ],
388 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
390 MAX_RECURSION_COMMENTS: 100,
391 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
394 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
397 APPLICATION: 'Application'
400 // ---------------------------------------------------------------------------
402 const PRIVATE_RSA_KEY_SIZE = 2048
404 // Password encryption
405 const BCRYPT_SALT_SIZE = 10
407 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
409 const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
410 DO_NOT_LIST: 'do_not_list',
415 // ---------------------------------------------------------------------------
417 // Express static paths (router)
418 const STATIC_PATHS = {
419 PREVIEWS: '/static/previews/',
420 THUMBNAILS: '/static/thumbnails/',
421 TORRENTS: '/static/torrents/',
422 WEBSEED: '/static/webseed/',
423 AVATARS: '/static/avatars/',
424 VIDEO_CAPTIONS: '/static/video-captions/'
426 const STATIC_DOWNLOAD_PATHS = {
427 TORRENTS: '/download/torrents/',
428 VIDEOS: '/download/videos/'
432 let STATIC_MAX_AGE = '2h'
434 // Videos thumbnail size
435 const THUMBNAILS_SIZE = {
439 const PREVIEWS_SIZE = {
443 const AVATARS_SIZE = {
453 // Sub folders of cache directory
456 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
457 MAX_AGE: 1000 * 3600 * 3 // 3 hours
460 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
461 MAX_AGE: 1000 * 3600 * 3 // 3 hours
465 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
467 // ---------------------------------------------------------------------------
469 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
471 // ---------------------------------------------------------------------------
477 // ---------------------------------------------------------------------------
479 const TRACKER_RATE_LIMITS = {
480 INTERVAL: 60000 * 5, // 5 minutes
481 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
482 ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
485 // ---------------------------------------------------------------------------
487 // Special constants for a test instance
488 if (isTestInstance() === true) {
489 ACTOR_FOLLOW_SCORE.BASE = 20
491 REMOTE_SCHEME.HTTP = 'http'
492 REMOTE_SCHEME.WS = 'ws'
496 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
497 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
499 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
501 SCHEDULER_INTERVALS_MS.badActorFollow = 10000
502 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
503 SCHEDULER_INTERVALS_MS.updateVideos = 5000
505 VIDEO_VIEW_LIFETIME = 1000 // 1 second
507 JOB_ATTEMPTS['email'] = 1
509 CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
512 updateWebserverConfig()
514 // ---------------------------------------------------------------------------
518 VIDEO_CAPTIONS_MIMETYPE_EXT,
529 LAST_MIGRATION_VERSION,
531 OPENGRAPH_AND_OEMBED_COMMENT,
532 BROADCAST_CONCURRENCY,
533 PAGINATION_COUNT_DEFAULT,
539 PRIVATE_RSA_KEY_SIZE,
540 ROUTE_CACHE_LIFETIME,
547 ACTIVITY_PUB_ACTOR_TYPES,
556 VIDEO_TRANSCODING_FPS,
559 USER_PASSWORD_RESET_LIFETIME,
561 SCHEDULER_INTERVALS_MS,
562 STATIC_DOWNLOAD_PATHS,
565 JOB_COMPLETED_LIFETIME,
570 // ---------------------------------------------------------------------------
572 function getLocalConfigFilePath () {
573 const configSources = config.util.getConfigSources()
574 if (configSources.length === 0) throw new Error('Invalid config source.')
576 let filename = 'local'
577 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
578 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
580 return join(dirname(configSources[ 0 ].name), filename + '.json')
583 function updateWebserverConfig () {
584 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
585 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
588 function buildLanguages () {
589 const iso639 = require('iso-639-3')
591 const languages: { [ id: string ]: string } = {}
593 const signLanguages = [
594 'sgn', // Sign languages (macro language)
606 'sfs', // South African
611 // Only add ISO639-1 languages and some sign languages (ISO639-3)
614 return (l.iso6391 !== null && l.type === 'living') ||
615 signLanguages.indexOf(l.iso6393) !== -1
617 .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
622 export function reloadConfig () {
624 function directory () {
625 if (process.env.NODE_CONFIG_DIR) {
626 return process.env.NODE_CONFIG_DIR
629 return join(root(), 'config')
633 for (const fileName in require.cache) {
634 if (-1 === fileName.indexOf(directory())) {
638 delete require.cache[fileName]
641 delete require.cache[require.resolve('config')]
646 config = require('config')
648 updateWebserverConfig()