]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as cors from 'cors' |
50d6de9c | 2 | import * as express from 'express' |
a8b1b404 C |
3 | import { join } from 'path' |
4 | import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' | |
5 | import { serveIndexHTML } from '@server/lib/client-html' | |
a8b1b404 | 6 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
a8b1b404 C |
7 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' |
8 | import { root } from '../helpers/core-utils' | |
9 | import { CONFIG, isEmailEnabled } from '../initializers/config' | |
9c6ca37f | 10 | import { |
4c1c1709 C |
11 | CONSTRAINTS_FIELDS, |
12 | DEFAULT_THEME_NAME, | |
34dd7cb4 C |
13 | HLS_STREAMING_PLAYLIST_DIRECTORY, |
14 | PEERTUBE_VERSION, | |
9c6ca37f | 15 | ROUTE_CACHE_LIFETIME, |
9c6ca37f | 16 | STATIC_MAX_AGE, |
6dd9de95 | 17 | STATIC_PATHS, |
4c1c1709 | 18 | WEBSERVER |
74dc3bca | 19 | } from '../initializers/constants' |
a8b1b404 C |
20 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' |
21 | import { getEnabledResolutions } from '../lib/video-transcoding' | |
90a8bd30 | 22 | import { asyncMiddleware } from '../middlewares' |
a8b1b404 | 23 | import { cacheRoute } from '../middlewares/cache' |
3f6d68d9 | 24 | import { UserModel } from '../models/account/user' |
a8b1b404 | 25 | import { VideoModel } from '../models/video/video' |
3f6d68d9 | 26 | import { VideoCommentModel } from '../models/video/video-comment' |
65fcc311 C |
27 | |
28 | const staticRouter = express.Router() | |
29 | ||
62945f06 C |
30 | staticRouter.use(cors()) |
31 | ||
65fcc311 | 32 | /* |
60862425 | 33 | Cors is very important to let other servers access torrent and video files |
65fcc311 C |
34 | */ |
35 | ||
90a8bd30 | 36 | // FIXME: deprecated in 3.2, use lazy-statics instead |
53c06121 | 37 | // Due to historical reasons, we can't really remove this controller |
65fcc311 C |
38 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR |
39 | staticRouter.use( | |
40 | STATIC_PATHS.TORRENTS, | |
49379960 | 41 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file |
65fcc311 C |
42 | ) |
43 | ||
90a8bd30 | 44 | // Videos path for webseed |
65fcc311 C |
45 | staticRouter.use( |
46 | STATIC_PATHS.WEBSEED, | |
b9fffa29 | 47 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video |
65fcc311 | 48 | ) |
6040f87d | 49 | staticRouter.use( |
b9fffa29 | 50 | STATIC_PATHS.REDUNDANCY, |
b9fffa29 | 51 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video |
6040f87d C |
52 | ) |
53 | ||
09209296 C |
54 | // HLS |
55 | staticRouter.use( | |
9c6ca37f | 56 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, |
09209296 | 57 | cors(), |
9c6ca37f | 58 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist |
09209296 C |
59 | ) |
60 | ||
65fcc311 C |
61 | // Thumbnails path for express |
62 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
63 | staticRouter.use( | |
64 | STATIC_PATHS.THUMBNAILS, | |
cd4cb177 | 65 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist |
65fcc311 C |
66 | ) |
67 | ||
ac235c37 | 68 | // robots.txt service |
3f6d68d9 | 69 | staticRouter.get('/robots.txt', |
f2f0eda5 | 70 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ROBOTS)), |
3f6d68d9 RK |
71 | (_, res: express.Response) => { |
72 | res.type('text/plain') | |
73 | return res.send(CONFIG.INSTANCE.ROBOTS) | |
74 | } | |
75 | ) | |
76 | ||
f2eb23cd RK |
77 | staticRouter.all('/teapot', |
78 | getCup, | |
79 | asyncMiddleware(serveIndexHTML) | |
80 | ) | |
81 | ||
5447516b AH |
82 | // security.txt service |
83 | staticRouter.get('/security.txt', | |
84 | (_, res: express.Response) => { | |
2d53be02 | 85 | return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt') |
5447516b AH |
86 | } |
87 | ) | |
88 | ||
89 | staticRouter.get('/.well-known/security.txt', | |
f2f0eda5 | 90 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.SECURITYTXT)), |
5447516b AH |
91 | (_, res: express.Response) => { |
92 | res.type('text/plain') | |
93 | return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT) | |
94 | } | |
95 | ) | |
96 | ||
3f6d68d9 RK |
97 | // nodeinfo service |
98 | staticRouter.use('/.well-known/nodeinfo', | |
f2f0eda5 | 99 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), |
3f6d68d9 RK |
100 | (_, res: express.Response) => { |
101 | return res.json({ | |
102 | links: [ | |
103 | { | |
104 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
6dd9de95 | 105 | href: WEBSERVER.URL + '/nodeinfo/2.0.json' |
3f6d68d9 RK |
106 | } |
107 | ] | |
108 | }) | |
109 | } | |
110 | ) | |
111 | staticRouter.use('/nodeinfo/:version.json', | |
f2f0eda5 | 112 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), |
3f6d68d9 RK |
113 | asyncMiddleware(generateNodeinfo) |
114 | ) | |
ac235c37 | 115 | |
aad0ec24 RK |
116 | // dnt-policy.txt service (see https://www.eff.org/dnt-policy) |
117 | staticRouter.use('/.well-known/dnt-policy.txt', | |
f2f0eda5 | 118 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.DNT_POLICY)), |
aad0ec24 RK |
119 | (_, res: express.Response) => { |
120 | res.type('text/plain') | |
aac0118d | 121 | |
d1105b97 | 122 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) |
aad0ec24 RK |
123 | } |
124 | ) | |
125 | ||
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' }) | |
31414127 RK |
130 | } |
131 | ) | |
132 | ||
133 | staticRouter.use('/.well-known/change-password', | |
134 | (_, res: express.Response) => { | |
135 | res.redirect('/my-account/settings') | |
aad0ec24 RK |
136 | } |
137 | ) | |
138 | ||
3ddb1ec5 C |
139 | staticRouter.use('/.well-known/host-meta', |
140 | (_, res: express.Response) => { | |
03371ad9 | 141 | res.type('application/xml') |
3ddb1ec5 C |
142 | |
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` + | |
146 | '</XRD>' | |
147 | ||
148 | res.send(xml).end() | |
149 | } | |
150 | ) | |
151 | ||
65fcc311 C |
152 | // --------------------------------------------------------------------------- |
153 | ||
154 | export { | |
155 | staticRouter | |
156 | } | |
f981dae8 C |
157 | |
158 | // --------------------------------------------------------------------------- | |
159 | ||
536598cf | 160 | async function generateNodeinfo (req: express.Request, res: express.Response) { |
3f6d68d9 RK |
161 | const { totalVideos } = await VideoModel.getStats() |
162 | const { totalLocalVideoComments } = await VideoCommentModel.getStats() | |
47d8e266 | 163 | const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats() |
3f6d68d9 RK |
164 | let json = {} |
165 | ||
166 | if (req.params.version && (req.params.version === '2.0')) { | |
167 | json = { | |
168 | version: '2.0', | |
169 | software: { | |
170 | name: 'peertube', | |
66170ca8 | 171 | version: PEERTUBE_VERSION |
3f6d68d9 RK |
172 | }, |
173 | protocols: [ | |
174 | 'activitypub' | |
175 | ], | |
176 | services: { | |
177 | inbound: [], | |
178 | outbound: [ | |
179 | 'atom1.0', | |
180 | 'rss2.0' | |
181 | ] | |
182 | }, | |
183 | openRegistrations: CONFIG.SIGNUP.ENABLED, | |
184 | usage: { | |
185 | users: { | |
47d8e266 C |
186 | total: totalUsers, |
187 | activeMonth: totalMonthlyActiveUsers, | |
188 | activeHalfyear: totalHalfYearActiveUsers | |
3f6d68d9 RK |
189 | }, |
190 | localPosts: totalVideos, | |
191 | localComments: totalLocalVideoComments | |
192 | }, | |
193 | metadata: { | |
194 | taxonomy: { | |
195 | postsName: 'Videos' | |
196 | }, | |
197 | nodeName: CONFIG.INSTANCE.NAME, | |
174e0855 RK |
198 | nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
199 | nodeConfig: { | |
9677fca7 RK |
200 | search: { |
201 | remoteUri: { | |
202 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | |
203 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | |
204 | } | |
205 | }, | |
174e0855 RK |
206 | plugin: { |
207 | registered: getRegisteredPlugins() | |
208 | }, | |
209 | theme: { | |
210 | registered: getRegisteredThemes(), | |
211 | default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | |
212 | }, | |
213 | email: { | |
4c1c1709 | 214 | enabled: isEmailEnabled() |
174e0855 RK |
215 | }, |
216 | contactForm: { | |
217 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
218 | }, | |
219 | transcoding: { | |
220 | hls: { | |
221 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | |
222 | }, | |
223 | webtorrent: { | |
224 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | |
225 | }, | |
c6c0fa6c C |
226 | enabledResolutions: getEnabledResolutions('vod') |
227 | }, | |
228 | live: { | |
229 | enabled: CONFIG.LIVE.ENABLED, | |
230 | transcoding: { | |
231 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, | |
232 | enabledResolutions: getEnabledResolutions('live') | |
233 | } | |
174e0855 RK |
234 | }, |
235 | import: { | |
236 | videos: { | |
237 | http: { | |
238 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
239 | }, | |
240 | torrent: { | |
241 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
242 | } | |
243 | } | |
244 | }, | |
245 | autoBlacklist: { | |
246 | videos: { | |
247 | ofUsers: { | |
248 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | |
249 | } | |
250 | } | |
251 | }, | |
252 | avatar: { | |
253 | file: { | |
254 | size: { | |
255 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
256 | }, | |
257 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
258 | } | |
259 | }, | |
260 | video: { | |
261 | image: { | |
262 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
263 | size: { | |
264 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
265 | } | |
266 | }, | |
267 | file: { | |
268 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
269 | } | |
270 | }, | |
271 | videoCaption: { | |
272 | file: { | |
273 | size: { | |
274 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
275 | }, | |
276 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | |
277 | } | |
278 | }, | |
279 | user: { | |
280 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | |
281 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
282 | }, | |
283 | trending: { | |
284 | videos: { | |
285 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
286 | } | |
287 | }, | |
288 | tracker: { | |
289 | enabled: CONFIG.TRACKER.ENABLED | |
290 | } | |
291 | } | |
3f6d68d9 RK |
292 | } |
293 | } as HttpNodeinfoDiasporaSoftwareNsSchema20 | |
98d3324d | 294 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') |
3f6d68d9 RK |
295 | } else { |
296 | json = { error: 'Nodeinfo schema version not handled' } | |
2d53be02 | 297 | res.status(HttpStatusCode.NOT_FOUND_404) |
3f6d68d9 RK |
298 | } |
299 | ||
98d3324d | 300 | return res.send(json).end() |
3f6d68d9 RK |
301 | } |
302 | ||
f2eb23cd RK |
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') | |
307 | ||
308 | return next() | |
309 | } |