1 import * as cors from 'cors'
2 import * as express from 'express'
3 import { join } from 'path'
4 import { serveIndexHTML } from '@server/lib/client-html'
5 import { getRegisteredPlugins, getRegisteredThemes } from '@server/lib/config'
6 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
7 import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
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 { getEnabledResolutions } from '../lib/video-transcoding'
22 import { asyncMiddleware } from '../middlewares'
23 import { cacheRoute } from '../middlewares/cache'
24 import { UserModel } from '../models/account/user'
25 import { VideoModel } from '../models/video/video'
26 import { VideoCommentModel } from '../models/video/video-comment'
28 const staticRouter = express.Router()
30 staticRouter.use(cors())
33 Cors is very important to let other servers access torrent and video files
36 // FIXME: deprecated in 3.2, use lazy-statics instead
37 // Due to historical reasons, we can't really remove this controller
38 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
40 STATIC_PATHS.TORRENTS,
41 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
44 // Videos path for webseed
47 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
50 STATIC_PATHS.REDUNDANCY,
51 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
56 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
58 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
61 // Thumbnails path for express
62 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
64 STATIC_PATHS.THUMBNAILS,
65 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
69 staticRouter.get('/robots.txt',
70 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ROBOTS)),
71 (_, res: express.Response) => {
72 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 asyncMiddleware(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 asyncMiddleware(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 asyncMiddleware(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 asyncMiddleware(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()
166 if (req.params.version && (req.params.version === '2.0')) {
171 version: PEERTUBE_VERSION
183 openRegistrations: CONFIG.SIGNUP.ENABLED,
187 activeMonth: totalMonthlyActiveUsers,
188 activeHalfyear: totalHalfYearActiveUsers
190 localPosts: totalVideos,
191 localComments: totalLocalVideoComments
197 nodeName: CONFIG.INSTANCE.NAME,
198 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
202 users: CONFIG.SEARCH.REMOTE_URI.USERS,
203 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
207 registered: getRegisteredPlugins()
210 registered: getRegisteredThemes(),
211 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
214 enabled: isEmailEnabled()
217 enabled: CONFIG.CONTACT_FORM.ENABLED
221 enabled: CONFIG.TRANSCODING.HLS.ENABLED
224 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
226 enabledResolutions: getEnabledResolutions('vod')
229 enabled: CONFIG.LIVE.ENABLED,
231 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
232 enabledResolutions: getEnabledResolutions('live')
238 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
241 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
248 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
255 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
257 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
262 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
264 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
268 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
274 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
276 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
280 videoQuota: CONFIG.USER.VIDEO_QUOTA,
281 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
285 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
289 enabled: CONFIG.TRACKER.ENABLED
293 } as HttpNodeinfoDiasporaSoftwareNsSchema20
294 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
296 json = { error: 'Nodeinfo schema version not handled' }
297 res.status(HttpStatusCode.NOT_FOUND_404)
300 return res.send(json).end()
303 function getCup (req: express.Request, res: express.Response, next: express.NextFunction) {
304 res.status(HttpStatusCode.I_AM_A_TEAPOT_418)
305 res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1')
306 res.setHeader('Safe', 'if-sepia-awake')