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 '@shared/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 // Videos path for webseed
38 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
41 STATIC_PATHS.REDUNDANCY,
42 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
47 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
49 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
52 // Thumbnails path for express
53 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
55 STATIC_PATHS.THUMBNAILS,
56 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
60 staticRouter.get('/robots.txt',
61 cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS),
62 (_, res: express.Response) => {
63 res.type('text/plain')
65 return res.send(CONFIG.INSTANCE.ROBOTS)
69 staticRouter.all('/teapot',
71 asyncMiddleware(serveIndexHTML)
74 // security.txt service
75 staticRouter.get('/security.txt',
76 (_, res: express.Response) => {
77 return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt')
81 staticRouter.get('/.well-known/security.txt',
82 cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT),
83 (_, res: express.Response) => {
84 res.type('text/plain')
85 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
90 staticRouter.use('/.well-known/nodeinfo',
91 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
92 (_, res: express.Response) => {
96 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
97 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
103 staticRouter.use('/nodeinfo/:version.json',
104 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
105 asyncMiddleware(generateNodeinfo)
108 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
109 staticRouter.use('/.well-known/dnt-policy.txt',
110 cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY),
111 (_, res: express.Response) => {
112 res.type('text/plain')
114 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
118 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
119 staticRouter.use('/.well-known/dnt/',
120 (_, res: express.Response) => {
121 res.json({ tracking: 'N' })
125 staticRouter.use('/.well-known/change-password',
126 (_, res: express.Response) => {
127 res.redirect('/my-account/settings')
131 staticRouter.use('/.well-known/host-meta',
132 (_, res: express.Response) => {
133 res.type('application/xml')
135 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
136 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
137 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
144 // ---------------------------------------------------------------------------
150 // ---------------------------------------------------------------------------
152 async function generateNodeinfo (req: express.Request, res: express.Response) {
153 const { totalVideos } = await VideoModel.getStats()
154 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
155 const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats()
157 if (!req.params.version || req.params.version !== '2.0') {
159 status: HttpStatusCode.NOT_FOUND_404,
160 message: 'Nodeinfo schema version not handled'
168 version: PEERTUBE_VERSION
180 openRegistrations: CONFIG.SIGNUP.ENABLED,
184 activeMonth: totalMonthlyActiveUsers,
185 activeHalfyear: totalHalfYearActiveUsers
187 localPosts: totalVideos,
188 localComments: totalLocalVideoComments
194 nodeName: CONFIG.INSTANCE.NAME,
195 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
199 users: CONFIG.SEARCH.REMOTE_URI.USERS,
200 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
204 registered: ServerConfigManager.Instance.getRegisteredPlugins()
207 registered: ServerConfigManager.Instance.getRegisteredThemes(),
208 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
211 enabled: isEmailEnabled()
214 enabled: CONFIG.CONTACT_FORM.ENABLED
218 enabled: CONFIG.TRANSCODING.HLS.ENABLED
221 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
223 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('vod')
226 enabled: CONFIG.LIVE.ENABLED,
228 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
229 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('live')
235 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
238 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
245 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
252 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
254 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
259 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
261 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
265 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
271 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
273 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
277 videoQuota: CONFIG.USER.VIDEO_QUOTA,
278 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
282 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
286 enabled: CONFIG.TRACKER.ENABLED
290 } as HttpNodeinfoDiasporaSoftwareNsSchema20
292 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
297 function getCup (req: express.Request, res: express.Response, next: express.NextFunction) {
298 res.status(HttpStatusCode.I_AM_A_TEAPOT_418)
299 res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1')
300 res.setHeader('Safe', 'if-sepia-awake')