1 import cors from 'cors'
2 import express from 'express'
3 import { join } from 'path'
4 import { serveIndexHTML } from '@server/lib/client-html'
5 import { ServerConfigManager } from '@server/lib/server-config-manager'
6 import { HttpStatusCode } from '@shared/models'
7 import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo/nodeinfo.model'
8 import { root } from '../helpers/core-utils'
9 import { CONFIG, isEmailEnabled } from '../initializers/config'
13 HLS_STREAMING_PLAYLIST_DIRECTORY,
19 } from '../initializers/constants'
20 import { getThemeOrDefault } from '../lib/plugins/theme-utils'
21 import { asyncMiddleware } from '../middlewares'
22 import { cacheRoute } from '../middlewares/cache/cache'
23 import { UserModel } from '../models/user/user'
24 import { VideoModel } from '../models/video/video'
25 import { VideoCommentModel } from '../models/video/video-comment'
27 const staticRouter = express.Router()
29 staticRouter.use(cors())
32 Cors is very important to let other servers access torrent and video files
35 // FIXME: deprecated in 3.2, use lazy-statics instead
36 // Due to historical reasons, we can't really remove this controller
37 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
39 STATIC_PATHS.TORRENTS,
40 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
43 // Videos path for webseed
46 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
49 STATIC_PATHS.REDUNDANCY,
50 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
55 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
57 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
60 // Thumbnails path for express
61 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
63 STATIC_PATHS.THUMBNAILS,
64 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
68 staticRouter.get('/robots.txt',
69 cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS),
70 (_, res: express.Response) => {
71 res.type('text/plain')
73 return res.send(CONFIG.INSTANCE.ROBOTS)
77 staticRouter.all('/teapot',
79 asyncMiddleware(serveIndexHTML)
82 // security.txt service
83 staticRouter.get('/security.txt',
84 (_, res: express.Response) => {
85 return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt')
89 staticRouter.get('/.well-known/security.txt',
90 cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT),
91 (_, res: express.Response) => {
92 res.type('text/plain')
93 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
98 staticRouter.use('/.well-known/nodeinfo',
99 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
100 (_, res: express.Response) => {
104 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
105 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
111 staticRouter.use('/nodeinfo/:version.json',
112 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
113 asyncMiddleware(generateNodeinfo)
116 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
117 staticRouter.use('/.well-known/dnt-policy.txt',
118 cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY),
119 (_, res: express.Response) => {
120 res.type('text/plain')
122 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
126 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
127 staticRouter.use('/.well-known/dnt/',
128 (_, res: express.Response) => {
129 res.json({ tracking: 'N' })
133 staticRouter.use('/.well-known/change-password',
134 (_, res: express.Response) => {
135 res.redirect('/my-account/settings')
139 staticRouter.use('/.well-known/host-meta',
140 (_, res: express.Response) => {
141 res.type('application/xml')
143 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
144 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
145 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
152 // ---------------------------------------------------------------------------
158 // ---------------------------------------------------------------------------
160 async function generateNodeinfo (req: express.Request, res: express.Response) {
161 const { totalVideos } = await VideoModel.getStats()
162 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
163 const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats()
165 if (!req.params.version || req.params.version !== '2.0') {
167 status: HttpStatusCode.NOT_FOUND_404,
168 message: 'Nodeinfo schema version not handled'
176 version: PEERTUBE_VERSION
188 openRegistrations: CONFIG.SIGNUP.ENABLED,
192 activeMonth: totalMonthlyActiveUsers,
193 activeHalfyear: totalHalfYearActiveUsers
195 localPosts: totalVideos,
196 localComments: totalLocalVideoComments
202 nodeName: CONFIG.INSTANCE.NAME,
203 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
207 users: CONFIG.SEARCH.REMOTE_URI.USERS,
208 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
212 registered: ServerConfigManager.Instance.getRegisteredPlugins()
215 registered: ServerConfigManager.Instance.getRegisteredThemes(),
216 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
219 enabled: isEmailEnabled()
222 enabled: CONFIG.CONTACT_FORM.ENABLED
226 enabled: CONFIG.TRANSCODING.HLS.ENABLED
229 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
231 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('vod')
234 enabled: CONFIG.LIVE.ENABLED,
236 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
237 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('live')
243 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
246 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
253 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
260 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
262 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
267 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
269 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
273 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
279 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
281 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
285 videoQuota: CONFIG.USER.VIDEO_QUOTA,
286 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
290 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
294 enabled: CONFIG.TRACKER.ENABLED
298 } as HttpNodeinfoDiasporaSoftwareNsSchema20
300 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
305 function getCup (req: express.Request, res: express.Response, next: express.NextFunction) {
306 res.status(HttpStatusCode.I_AM_A_TEAPOT_418)
307 res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1')
308 res.setHeader('Safe', 'if-sepia-awake')