]>
Commit | Line | Data |
---|---|---|
1 | import { Hooks } from '@server/lib/plugins/hooks' | |
2 | import * as express from 'express' | |
3 | import { remove, writeJSON } from 'fs-extra' | |
4 | import { snakeCase } from 'lodash' | |
5 | import validator from 'validator' | |
6 | import { RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig, UserRight } from '../../../shared' | |
7 | import { About } from '../../../shared/models/server/about.model' | |
8 | import { CustomConfig } from '../../../shared/models/server/custom-config.model' | |
9 | import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' | |
10 | import { objectConverter } from '../../helpers/core-utils' | |
11 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' | |
12 | import { getServerCommit } from '../../helpers/utils' | |
13 | import { CONFIG, isEmailEnabled, reloadConfig } from '../../initializers/config' | |
14 | import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '../../initializers/constants' | |
15 | import { ClientHtml } from '../../lib/client-html' | |
16 | import { PluginManager } from '../../lib/plugins/plugin-manager' | |
17 | import { getThemeOrDefault } from '../../lib/plugins/theme-utils' | |
18 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' | |
19 | import { customConfigUpdateValidator } from '../../middlewares/validators/config' | |
20 | ||
21 | const configRouter = express.Router() | |
22 | ||
23 | const auditLogger = auditLoggerFactory('config') | |
24 | ||
25 | configRouter.get('/about', getAbout) | |
26 | configRouter.get('/', | |
27 | asyncMiddleware(getConfig) | |
28 | ) | |
29 | ||
30 | configRouter.get('/custom', | |
31 | authenticate, | |
32 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
33 | getCustomConfig | |
34 | ) | |
35 | configRouter.put('/custom', | |
36 | authenticate, | |
37 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
38 | customConfigUpdateValidator, | |
39 | asyncMiddleware(updateCustomConfig) | |
40 | ) | |
41 | configRouter.delete('/custom', | |
42 | authenticate, | |
43 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
44 | asyncMiddleware(deleteCustomConfig) | |
45 | ) | |
46 | ||
47 | let serverCommit: string | |
48 | ||
49 | async function getConfig (req: express.Request, res: express.Response) { | |
50 | const { allowed } = await Hooks.wrapPromiseFun( | |
51 | isSignupAllowed, | |
52 | { | |
53 | ip: req.ip | |
54 | }, | |
55 | 'filter:api.user.signup.allowed.result' | |
56 | ) | |
57 | ||
58 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) | |
59 | const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | |
60 | ||
61 | if (serverCommit === undefined) serverCommit = await getServerCommit() | |
62 | ||
63 | const json: ServerConfig = { | |
64 | instance: { | |
65 | name: CONFIG.INSTANCE.NAME, | |
66 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
67 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | |
68 | isNSFW: CONFIG.INSTANCE.IS_NSFW, | |
69 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | |
70 | customizations: { | |
71 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
72 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
73 | } | |
74 | }, | |
75 | search: { | |
76 | remoteUri: { | |
77 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | |
78 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | |
79 | } | |
80 | }, | |
81 | plugin: { | |
82 | registered: getRegisteredPlugins(), | |
83 | registeredExternalAuths: getExternalAuthsPlugins(), | |
84 | registeredIdAndPassAuths: getIdAndPassAuthPlugins() | |
85 | }, | |
86 | theme: { | |
87 | registered: getRegisteredThemes(), | |
88 | default: defaultTheme | |
89 | }, | |
90 | email: { | |
91 | enabled: isEmailEnabled() | |
92 | }, | |
93 | contactForm: { | |
94 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
95 | }, | |
96 | serverVersion: PEERTUBE_VERSION, | |
97 | serverCommit, | |
98 | signup: { | |
99 | allowed, | |
100 | allowedForCurrentIP, | |
101 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
102 | }, | |
103 | transcoding: { | |
104 | hls: { | |
105 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | |
106 | }, | |
107 | webtorrent: { | |
108 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | |
109 | }, | |
110 | enabledResolutions: getEnabledResolutions() | |
111 | }, | |
112 | import: { | |
113 | videos: { | |
114 | http: { | |
115 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
116 | }, | |
117 | torrent: { | |
118 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
119 | } | |
120 | } | |
121 | }, | |
122 | autoBlacklist: { | |
123 | videos: { | |
124 | ofUsers: { | |
125 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | |
126 | } | |
127 | } | |
128 | }, | |
129 | avatar: { | |
130 | file: { | |
131 | size: { | |
132 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
133 | }, | |
134 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
135 | } | |
136 | }, | |
137 | video: { | |
138 | image: { | |
139 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
140 | size: { | |
141 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
142 | } | |
143 | }, | |
144 | file: { | |
145 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
146 | } | |
147 | }, | |
148 | videoCaption: { | |
149 | file: { | |
150 | size: { | |
151 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
152 | }, | |
153 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | |
154 | } | |
155 | }, | |
156 | user: { | |
157 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | |
158 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
159 | }, | |
160 | trending: { | |
161 | videos: { | |
162 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
163 | } | |
164 | }, | |
165 | tracker: { | |
166 | enabled: CONFIG.TRACKER.ENABLED | |
167 | }, | |
168 | ||
169 | followings: { | |
170 | instance: { | |
171 | autoFollowIndex: { | |
172 | indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL | |
173 | } | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | return res.json(json) | |
179 | } | |
180 | ||
181 | function getAbout (req: express.Request, res: express.Response) { | |
182 | const about: About = { | |
183 | instance: { | |
184 | name: CONFIG.INSTANCE.NAME, | |
185 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
186 | description: CONFIG.INSTANCE.DESCRIPTION, | |
187 | terms: CONFIG.INSTANCE.TERMS, | |
188 | codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT, | |
189 | ||
190 | hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION, | |
191 | ||
192 | creationReason: CONFIG.INSTANCE.CREATION_REASON, | |
193 | moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION, | |
194 | administrator: CONFIG.INSTANCE.ADMINISTRATOR, | |
195 | maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME, | |
196 | businessModel: CONFIG.INSTANCE.BUSINESS_MODEL, | |
197 | ||
198 | languages: CONFIG.INSTANCE.LANGUAGES, | |
199 | categories: CONFIG.INSTANCE.CATEGORIES | |
200 | } | |
201 | } | |
202 | ||
203 | return res.json(about).end() | |
204 | } | |
205 | ||
206 | function getCustomConfig (req: express.Request, res: express.Response) { | |
207 | const data = customConfig() | |
208 | ||
209 | return res.json(data).end() | |
210 | } | |
211 | ||
212 | async function deleteCustomConfig (req: express.Request, res: express.Response) { | |
213 | await remove(CONFIG.CUSTOM_FILE) | |
214 | ||
215 | auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig())) | |
216 | ||
217 | reloadConfig() | |
218 | ClientHtml.invalidCache() | |
219 | ||
220 | const data = customConfig() | |
221 | ||
222 | return res.json(data).end() | |
223 | } | |
224 | ||
225 | async function updateCustomConfig (req: express.Request, res: express.Response) { | |
226 | const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig()) | |
227 | ||
228 | // camelCase to snake_case key + Force number conversion | |
229 | const toUpdateJSON = convertCustomConfigBody(req.body) | |
230 | ||
231 | await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 }) | |
232 | ||
233 | reloadConfig() | |
234 | ClientHtml.invalidCache() | |
235 | ||
236 | const data = customConfig() | |
237 | ||
238 | auditLogger.update( | |
239 | getAuditIdFromRes(res), | |
240 | new CustomConfigAuditView(data), | |
241 | oldCustomConfigAuditKeys | |
242 | ) | |
243 | ||
244 | return res.json(data).end() | |
245 | } | |
246 | ||
247 | function getRegisteredThemes () { | |
248 | return PluginManager.Instance.getRegisteredThemes() | |
249 | .map(t => ({ | |
250 | name: t.name, | |
251 | version: t.version, | |
252 | description: t.description, | |
253 | css: t.css, | |
254 | clientScripts: t.clientScripts | |
255 | })) | |
256 | } | |
257 | ||
258 | function getEnabledResolutions () { | |
259 | return Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) | |
260 | .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true) | |
261 | .map(r => parseInt(r, 10)) | |
262 | } | |
263 | ||
264 | function getRegisteredPlugins () { | |
265 | return PluginManager.Instance.getRegisteredPlugins() | |
266 | .map(p => ({ | |
267 | name: p.name, | |
268 | version: p.version, | |
269 | description: p.description, | |
270 | clientScripts: p.clientScripts | |
271 | })) | |
272 | } | |
273 | ||
274 | function getIdAndPassAuthPlugins () { | |
275 | const result: RegisteredIdAndPassAuthConfig[] = [] | |
276 | ||
277 | for (const p of PluginManager.Instance.getIdAndPassAuths()) { | |
278 | for (const auth of p.idAndPassAuths) { | |
279 | result.push({ | |
280 | npmName: p.npmName, | |
281 | name: p.name, | |
282 | version: p.version, | |
283 | authName: auth.authName, | |
284 | weight: auth.getWeight() | |
285 | }) | |
286 | } | |
287 | } | |
288 | ||
289 | return result | |
290 | } | |
291 | ||
292 | function getExternalAuthsPlugins () { | |
293 | const result: RegisteredExternalAuthConfig[] = [] | |
294 | ||
295 | for (const p of PluginManager.Instance.getExternalAuths()) { | |
296 | for (const auth of p.externalAuths) { | |
297 | result.push({ | |
298 | npmName: p.npmName, | |
299 | name: p.name, | |
300 | version: p.version, | |
301 | authName: auth.authName, | |
302 | authDisplayName: auth.authDisplayName | |
303 | }) | |
304 | } | |
305 | } | |
306 | ||
307 | return result | |
308 | } | |
309 | ||
310 | // --------------------------------------------------------------------------- | |
311 | ||
312 | export { | |
313 | configRouter, | |
314 | getEnabledResolutions, | |
315 | getRegisteredPlugins, | |
316 | getRegisteredThemes | |
317 | } | |
318 | ||
319 | // --------------------------------------------------------------------------- | |
320 | ||
321 | function customConfig (): CustomConfig { | |
322 | return { | |
323 | instance: { | |
324 | name: CONFIG.INSTANCE.NAME, | |
325 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
326 | description: CONFIG.INSTANCE.DESCRIPTION, | |
327 | terms: CONFIG.INSTANCE.TERMS, | |
328 | codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT, | |
329 | ||
330 | creationReason: CONFIG.INSTANCE.CREATION_REASON, | |
331 | moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION, | |
332 | administrator: CONFIG.INSTANCE.ADMINISTRATOR, | |
333 | maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME, | |
334 | businessModel: CONFIG.INSTANCE.BUSINESS_MODEL, | |
335 | hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION, | |
336 | ||
337 | languages: CONFIG.INSTANCE.LANGUAGES, | |
338 | categories: CONFIG.INSTANCE.CATEGORIES, | |
339 | ||
340 | isNSFW: CONFIG.INSTANCE.IS_NSFW, | |
341 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | |
342 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | |
343 | customizations: { | |
344 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
345 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
346 | } | |
347 | }, | |
348 | theme: { | |
349 | default: CONFIG.THEME.DEFAULT | |
350 | }, | |
351 | services: { | |
352 | twitter: { | |
353 | username: CONFIG.SERVICES.TWITTER.USERNAME, | |
354 | whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED | |
355 | } | |
356 | }, | |
357 | cache: { | |
358 | previews: { | |
359 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
360 | }, | |
361 | captions: { | |
362 | size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE | |
363 | } | |
364 | }, | |
365 | signup: { | |
366 | enabled: CONFIG.SIGNUP.ENABLED, | |
367 | limit: CONFIG.SIGNUP.LIMIT, | |
368 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
369 | }, | |
370 | admin: { | |
371 | email: CONFIG.ADMIN.EMAIL | |
372 | }, | |
373 | contactForm: { | |
374 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
375 | }, | |
376 | user: { | |
377 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | |
378 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
379 | }, | |
380 | transcoding: { | |
381 | enabled: CONFIG.TRANSCODING.ENABLED, | |
382 | allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS, | |
383 | allowAudioFiles: CONFIG.TRANSCODING.ALLOW_AUDIO_FILES, | |
384 | threads: CONFIG.TRANSCODING.THREADS, | |
385 | resolutions: { | |
386 | '0p': CONFIG.TRANSCODING.RESOLUTIONS['0p'], | |
387 | '240p': CONFIG.TRANSCODING.RESOLUTIONS['240p'], | |
388 | '360p': CONFIG.TRANSCODING.RESOLUTIONS['360p'], | |
389 | '480p': CONFIG.TRANSCODING.RESOLUTIONS['480p'], | |
390 | '720p': CONFIG.TRANSCODING.RESOLUTIONS['720p'], | |
391 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS['1080p'], | |
392 | '2160p': CONFIG.TRANSCODING.RESOLUTIONS['2160p'] | |
393 | }, | |
394 | webtorrent: { | |
395 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | |
396 | }, | |
397 | hls: { | |
398 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | |
399 | } | |
400 | }, | |
401 | import: { | |
402 | videos: { | |
403 | http: { | |
404 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
405 | }, | |
406 | torrent: { | |
407 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
408 | } | |
409 | } | |
410 | }, | |
411 | autoBlacklist: { | |
412 | videos: { | |
413 | ofUsers: { | |
414 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | |
415 | } | |
416 | } | |
417 | }, | |
418 | followers: { | |
419 | instance: { | |
420 | enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED, | |
421 | manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL | |
422 | } | |
423 | }, | |
424 | followings: { | |
425 | instance: { | |
426 | autoFollowBack: { | |
427 | enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED | |
428 | }, | |
429 | ||
430 | autoFollowIndex: { | |
431 | enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED, | |
432 | indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL | |
433 | } | |
434 | } | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
439 | function convertCustomConfigBody (body: CustomConfig) { | |
440 | function keyConverter (k: string) { | |
441 | // Transcoding resolutions exception | |
442 | if (/^\d{3,4}p$/.exec(k)) return k | |
443 | if (k === '0p') return k | |
444 | ||
445 | return snakeCase(k) | |
446 | } | |
447 | ||
448 | function valueConverter (v: any) { | |
449 | if (validator.isNumeric(v + '')) return parseInt('' + v, 10) | |
450 | ||
451 | return v | |
452 | } | |
453 | ||
454 | return objectConverter(body, keyConverter, valueConverter) | |
455 | } |