]>
Commit | Line | Data |
---|---|---|
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' | |
10 | import { | |
11 | CONSTRAINTS_FIELDS, | |
12 | DEFAULT_THEME_NAME, | |
13 | HLS_STREAMING_PLAYLIST_DIRECTORY, | |
14 | PEERTUBE_VERSION, | |
15 | ROUTE_CACHE_LIFETIME, | |
16 | STATIC_MAX_AGE, | |
17 | STATIC_PATHS, | |
18 | WEBSERVER | |
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' | |
27 | ||
28 | const staticRouter = express.Router() | |
29 | ||
30 | staticRouter.use(cors()) | |
31 | ||
32 | /* | |
33 | Cors is very important to let other servers access torrent and video files | |
34 | */ | |
35 | ||
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 | |
39 | staticRouter.use( | |
40 | STATIC_PATHS.TORRENTS, | |
41 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file | |
42 | ) | |
43 | ||
44 | // Videos path for webseed | |
45 | staticRouter.use( | |
46 | STATIC_PATHS.WEBSEED, | |
47 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video | |
48 | ) | |
49 | staticRouter.use( | |
50 | STATIC_PATHS.REDUNDANCY, | |
51 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video | |
52 | ) | |
53 | ||
54 | // HLS | |
55 | staticRouter.use( | |
56 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, | |
57 | cors(), | |
58 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist | |
59 | ) | |
60 | ||
61 | // Thumbnails path for express | |
62 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
63 | staticRouter.use( | |
64 | STATIC_PATHS.THUMBNAILS, | |
65 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | |
66 | ) | |
67 | ||
68 | // robots.txt service | |
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) | |
74 | } | |
75 | ) | |
76 | ||
77 | staticRouter.all('/teapot', | |
78 | getCup, | |
79 | asyncMiddleware(serveIndexHTML) | |
80 | ) | |
81 | ||
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') | |
86 | } | |
87 | ) | |
88 | ||
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) | |
94 | } | |
95 | ) | |
96 | ||
97 | // nodeinfo service | |
98 | staticRouter.use('/.well-known/nodeinfo', | |
99 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
100 | (_, res: express.Response) => { | |
101 | return res.json({ | |
102 | links: [ | |
103 | { | |
104 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
105 | href: WEBSERVER.URL + '/nodeinfo/2.0.json' | |
106 | } | |
107 | ] | |
108 | }) | |
109 | } | |
110 | ) | |
111 | staticRouter.use('/nodeinfo/:version.json', | |
112 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
113 | asyncMiddleware(generateNodeinfo) | |
114 | ) | |
115 | ||
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') | |
121 | ||
122 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) | |
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' }) | |
130 | } | |
131 | ) | |
132 | ||
133 | staticRouter.use('/.well-known/change-password', | |
134 | (_, res: express.Response) => { | |
135 | res.redirect('/my-account/settings') | |
136 | } | |
137 | ) | |
138 | ||
139 | staticRouter.use('/.well-known/host-meta', | |
140 | (_, res: express.Response) => { | |
141 | res.type('application/xml') | |
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 | ||
152 | // --------------------------------------------------------------------------- | |
153 | ||
154 | export { | |
155 | staticRouter | |
156 | } | |
157 | ||
158 | // --------------------------------------------------------------------------- | |
159 | ||
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() | |
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', | |
171 | version: PEERTUBE_VERSION | |
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: { | |
186 | total: totalUsers, | |
187 | activeMonth: totalMonthlyActiveUsers, | |
188 | activeHalfyear: totalHalfYearActiveUsers | |
189 | }, | |
190 | localPosts: totalVideos, | |
191 | localComments: totalLocalVideoComments | |
192 | }, | |
193 | metadata: { | |
194 | taxonomy: { | |
195 | postsName: 'Videos' | |
196 | }, | |
197 | nodeName: CONFIG.INSTANCE.NAME, | |
198 | nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
199 | nodeConfig: { | |
200 | search: { | |
201 | remoteUri: { | |
202 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | |
203 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | |
204 | } | |
205 | }, | |
206 | plugin: { | |
207 | registered: getRegisteredPlugins() | |
208 | }, | |
209 | theme: { | |
210 | registered: getRegisteredThemes(), | |
211 | default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | |
212 | }, | |
213 | email: { | |
214 | enabled: isEmailEnabled() | |
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 | }, | |
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 | } | |
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.IMAGE.FILE_SIZE.max | |
256 | }, | |
257 | extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.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 | } | |
292 | } | |
293 | } as HttpNodeinfoDiasporaSoftwareNsSchema20 | |
294 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') | |
295 | } else { | |
296 | json = { error: 'Nodeinfo schema version not handled' } | |
297 | res.status(HttpStatusCode.NOT_FOUND_404) | |
298 | } | |
299 | ||
300 | return res.send(json).end() | |
301 | } | |
302 | ||
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 | } |