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