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 = 225
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.get<string>('redis.hostname'),
120 PORT: config.get<number>('redis.port'),
121 AUTH: config.get<string>('redis.auth'),
122 DB: config.get<number>('redis.db')
125 HOSTNAME: config.get<string>('smtp.hostname'),
126 PORT: config.get<number>('smtp.port'),
127 USERNAME: config.get<string>('smtp.username'),
128 PASSWORD: config.get<string>('smtp.password'),
129 TLS: config.get<boolean>('smtp.tls'),
130 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
131 CA_FILE: config.get<string>('smtp.ca_file'),
132 FROM_ADDRESS: config.get<string>('smtp.from_address')
135 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
136 LOG_DIR: buildPath(config.get<string>('storage.logs')),
137 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
138 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
139 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
140 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
141 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
144 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
145 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
146 HOSTNAME: config.get<string>('webserver.hostname'),
147 PORT: config.get<number>('webserver.port'),
151 TRUST_PROXY: config.get<string[]>('trust_proxy'),
153 LEVEL: config.get<string>('log.level')
156 get EMAIL () { return config.get<string>('admin.email') }
159 get ENABLED () { return config.get<boolean>('signup.enabled') },
160 get LIMIT () { return config.get<number>('signup.limit') },
163 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
164 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
169 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
172 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
173 get THREADS () { return config.get<number>('transcoding.threads') },
175 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
176 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
177 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
178 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
179 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
184 get SIZE () { return config.get<number>('cache.previews.size') }
188 get NAME () { return config.get<string>('instance.name') },
189 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
190 get DESCRIPTION () { return config.get<string>('instance.description') },
191 get TERMS () { return config.get<string>('instance.terms') },
192 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
193 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
195 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
196 get CSS () { return config.get<string>('instance.customizations.css') }
198 get ROBOTS () { return config.get<string>('instance.robots') }
202 get USERNAME () { return config.get<string>('services.twitter.username') },
203 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
208 // ---------------------------------------------------------------------------
210 const CONSTRAINTS_FIELDS = {
212 NAME: { min: 3, max: 120 }, // Length
213 DESCRIPTION: { min: 3, max: 250 }, // Length
214 USERNAME: { min: 3, max: 20 }, // Length
215 PASSWORD: { min: 6, max: 255 }, // Length
216 VIDEO_QUOTA: { min: -1 }
219 REASON: { min: 2, max: 300 } // Length
222 NAME: { min: 3, max: 120 }, // Length
223 DESCRIPTION: { min: 3, max: 500 }, // Length
224 SUPPORT: { min: 3, max: 500 }, // Length
225 URL: { min: 3, max: 2000 } // Length
228 NAME: { min: 3, max: 120 }, // Length
229 LANGUAGE: { min: 1, max: 10 }, // Length
230 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
231 DESCRIPTION: { min: 3, max: 10000 }, // Length
232 SUPPORT: { min: 3, max: 500 }, // Length
234 EXTNAME: [ '.jpg', '.jpeg' ],
236 max: 2 * 1024 * 1024 // 2MB
239 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
240 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
241 DURATION: { min: 1 }, // Number
242 TAGS: { min: 0, max: 5 }, // Number of total tags
243 TAG: { min: 2, max: 30 }, // Length
244 THUMBNAIL: { min: 2, max: 30 },
245 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
248 DISLIKES: { min: 0 },
249 FILE_SIZE: { min: 10 },
250 URL: { min: 3, max: 2000 } // Length
253 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
254 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
255 URL: { min: 3, max: 2000 }, // Length
257 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
259 max: 2 * 1024 * 1024 // 2MB
267 TEXT: { min: 1, max: 3000 }, // Length
268 URL: { min: 3, max: 2000 } // Length
271 URL: { min: 3, max: 2000 } // Length
275 const RATES_LIMIT = {
277 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
278 MAX: 15 // 15 attempts
282 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
283 const VIDEO_TRANSCODING_FPS = {
287 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
290 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
295 const VIDEO_CATEGORIES = {
310 15: 'Science & Technology',
316 // See https://creativecommons.org/licenses/?lang=en
317 const VIDEO_LICENCES = {
319 2: 'Attribution - Share Alike',
320 3: 'Attribution - No Derivatives',
321 4: 'Attribution - Non Commercial',
322 5: 'Attribution - Non Commercial - Share Alike',
323 6: 'Attribution - Non Commercial - No Derivatives',
324 7: 'Public Domain Dedication'
327 const VIDEO_LANGUAGES = buildLanguages()
329 const VIDEO_PRIVACIES = {
330 [VideoPrivacy.PUBLIC]: 'Public',
331 [VideoPrivacy.UNLISTED]: 'Unlisted',
332 [VideoPrivacy.PRIVATE]: 'Private'
335 const VIDEO_STATES = {
336 [VideoState.PUBLISHED]: 'Published',
337 [VideoState.TO_TRANSCODE]: 'To transcode'
340 const VIDEO_MIMETYPE_EXT = {
341 'video/webm': '.webm',
345 const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT)
347 const IMAGE_MIMETYPE_EXT = {
353 // ---------------------------------------------------------------------------
355 const SERVER_ACTOR_NAME = 'peertube'
357 const ACTIVITY_PUB = {
358 POTENTIAL_ACCEPT_HEADERS: [
359 'application/activity+json',
360 'application/ld+json',
361 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
363 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
364 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
365 COLLECTION_ITEMS_PER_PAGE: 10,
366 FETCH_PAGE_LIMIT: 100,
368 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
369 TORRENT: [ 'application/x-bittorrent' ],
370 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
372 MAX_RECURSION_COMMENTS: 100,
373 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
376 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
379 APPLICATION: 'Application'
382 // ---------------------------------------------------------------------------
384 const PRIVATE_RSA_KEY_SIZE = 2048
386 // Password encryption
387 const BCRYPT_SALT_SIZE = 10
389 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
391 const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
392 DO_NOT_LIST: 'do_not_list',
397 // ---------------------------------------------------------------------------
399 // Express static paths (router)
400 const STATIC_PATHS = {
401 PREVIEWS: '/static/previews/',
402 THUMBNAILS: '/static/thumbnails/',
403 TORRENTS: '/static/torrents/',
404 WEBSEED: '/static/webseed/',
405 AVATARS: '/static/avatars/'
407 const STATIC_DOWNLOAD_PATHS = {
408 TORRENTS: '/download/torrents/',
409 VIDEOS: '/download/videos/'
413 let STATIC_MAX_AGE = '30d'
415 // Videos thumbnail size
416 const THUMBNAILS_SIZE = {
420 const PREVIEWS_SIZE = {
424 const AVATARS_SIZE = {
434 // Sub folders of cache directory
437 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
441 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
443 // ---------------------------------------------------------------------------
445 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
447 // ---------------------------------------------------------------------------
453 // ---------------------------------------------------------------------------
455 const TRACKER_RATE_LIMITS = {
456 INTERVAL: 60000 * 5, // 5 minutes
457 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
458 ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
461 // ---------------------------------------------------------------------------
463 // Special constants for a test instance
464 if (isTestInstance() === true) {
465 ACTOR_FOLLOW_SCORE.BASE = 20
467 REMOTE_SCHEME.HTTP = 'http'
468 REMOTE_SCHEME.WS = 'ws'
472 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
473 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
475 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
477 SCHEDULER_INTERVALS_MS.badActorFollow = 10000
478 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
479 SCHEDULER_INTERVALS_MS.updateVideos = 5000
481 VIDEO_VIEW_LIFETIME = 1000 // 1 second
483 JOB_ATTEMPTS['email'] = 1
486 updateWebserverConfig()
488 // ---------------------------------------------------------------------------
502 LAST_MIGRATION_VERSION,
504 OPENGRAPH_AND_OEMBED_COMMENT,
505 BROADCAST_CONCURRENCY,
506 PAGINATION_COUNT_DEFAULT,
512 PRIVATE_RSA_KEY_SIZE,
513 ROUTE_CACHE_LIFETIME,
520 ACTIVITY_PUB_ACTOR_TYPES,
529 VIDEO_TRANSCODING_FPS,
532 USER_PASSWORD_RESET_LIFETIME,
534 SCHEDULER_INTERVALS_MS,
535 STATIC_DOWNLOAD_PATHS,
538 JOB_COMPLETED_LIFETIME,
543 // ---------------------------------------------------------------------------
545 function getLocalConfigFilePath () {
546 const configSources = config.util.getConfigSources()
547 if (configSources.length === 0) throw new Error('Invalid config source.')
549 let filename = 'local'
550 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
551 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
553 return join(dirname(configSources[ 0 ].name), filename + '.json')
556 function updateWebserverConfig () {
557 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
558 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
561 function buildLanguages () {
562 const iso639 = require('iso-639-3')
564 const languages: { [ id: string ]: string } = {}
566 const signLanguages = [
567 'sgn', // Sign languages (macro language)
579 'sfs', // South African
584 // Only add ISO639-1 languages and some sign languages (ISO639-3)
587 return (l.iso6391 !== null && l.type === 'living') ||
588 signLanguages.indexOf(l.iso6393) !== -1
590 .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
595 export function reloadConfig () {
597 function directory () {
598 if (process.env.NODE_CONFIG_DIR) {
599 return process.env.NODE_CONFIG_DIR
602 return join(root(), 'config')
606 for (const fileName in require.cache) {
607 if (-1 === fileName.indexOf(directory())) {
611 delete require.cache[fileName]
614 delete require.cache[require.resolve('config')]
619 config = require('config')
621 updateWebserverConfig()