index.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. // @ts-check
  2. const os = require('os');
  3. const throng = require('throng');
  4. const dotenv = require('dotenv');
  5. const express = require('express');
  6. const http = require('http');
  7. const redis = require('redis');
  8. const pg = require('pg');
  9. const log = require('npmlog');
  10. const url = require('url');
  11. const uuid = require('uuid');
  12. const fs = require('fs');
  13. const WebSocket = require('ws');
  14. const { JSDOM } = require('jsdom');
  15. const env = process.env.NODE_ENV || 'development';
  16. const alwaysRequireAuth = process.env.LIMITED_FEDERATION_MODE === 'true' || process.env.WHITELIST_MODE === 'true' || process.env.AUTHORIZED_FETCH === 'true';
  17. dotenv.config({
  18. path: env === 'production' ? '.env.production' : '.env',
  19. });
  20. log.level = process.env.LOG_LEVEL || 'verbose';
  21. /**
  22. * @param {string} dbUrl
  23. * @return {Object.<string, any>}
  24. */
  25. const dbUrlToConfig = (dbUrl) => {
  26. if (!dbUrl) {
  27. return {};
  28. }
  29. const params = url.parse(dbUrl, true);
  30. const config = {};
  31. if (params.auth) {
  32. [config.user, config.password] = params.auth.split(':');
  33. }
  34. if (params.hostname) {
  35. config.host = params.hostname;
  36. }
  37. if (params.port) {
  38. config.port = params.port;
  39. }
  40. if (params.pathname) {
  41. config.database = params.pathname.split('/')[1];
  42. }
  43. const ssl = params.query && params.query.ssl;
  44. if (ssl && ssl === 'true' || ssl === '1') {
  45. config.ssl = true;
  46. }
  47. return config;
  48. };
  49. /**
  50. * @param {Object.<string, any>} defaultConfig
  51. * @param {string} redisUrl
  52. */
  53. const redisUrlToClient = async (defaultConfig, redisUrl) => {
  54. const config = defaultConfig;
  55. let client;
  56. if (!redisUrl) {
  57. client = redis.createClient(config);
  58. } else if (redisUrl.startsWith('unix://')) {
  59. client = redis.createClient(Object.assign(config, {
  60. socket: {
  61. path: redisUrl.slice(7),
  62. },
  63. }));
  64. } else {
  65. client = redis.createClient(Object.assign(config, {
  66. url: redisUrl,
  67. }));
  68. }
  69. client.on('error', (err) => log.error('Redis Client Error!', err));
  70. await client.connect();
  71. return client;
  72. };
  73. const numWorkers = +process.env.STREAMING_CLUSTER_NUM || (env === 'development' ? 1 : Math.max(os.cpus().length - 1, 1));
  74. /**
  75. * Attempts to safely parse a string as JSON, used when both receiving a message
  76. * from redis and when receiving a message from a client over a websocket
  77. * connection, this is why it accepts a `req` argument.
  78. * @param {string} json
  79. * @param {any?} req
  80. * @returns {Object.<string, any>|null}
  81. */
  82. const parseJSON = (json, req) => {
  83. try {
  84. return JSON.parse(json);
  85. } catch (err) {
  86. /* FIXME: This logging isn't great, and should probably be done at the
  87. * call-site of parseJSON, not in the method, but this would require changing
  88. * the signature of parseJSON to return something akin to a Result type:
  89. * [Error|null, null|Object<string,any}], and then handling the error
  90. * scenarios.
  91. */
  92. if (req) {
  93. if (req.accountId) {
  94. log.warn(req.requestId, `Error parsing message from user ${req.accountId}: ${err}`);
  95. } else {
  96. log.silly(req.requestId, `Error parsing message from ${req.remoteAddress}: ${err}`);
  97. }
  98. } else {
  99. log.warn(`Error parsing message from redis: ${err}`);
  100. }
  101. return null;
  102. }
  103. };
  104. const startMaster = () => {
  105. if (!process.env.SOCKET && process.env.PORT && isNaN(+process.env.PORT)) {
  106. log.warn('UNIX domain socket is now supported by using SOCKET. Please migrate from PORT hack.');
  107. }
  108. log.warn(`Starting streaming API server master with ${numWorkers} workers`);
  109. };
  110. const startWorker = async (workerId) => {
  111. log.warn(`Starting worker ${workerId}`);
  112. const pgConfigs = {
  113. development: {
  114. user: process.env.DB_USER || pg.defaults.user,
  115. password: process.env.DB_PASS || pg.defaults.password,
  116. database: process.env.DB_NAME || 'mastodon_development',
  117. host: process.env.DB_HOST || pg.defaults.host,
  118. port: process.env.DB_PORT || pg.defaults.port,
  119. max: 10,
  120. },
  121. production: {
  122. user: process.env.DB_USER || 'mastodon',
  123. password: process.env.DB_PASS || '',
  124. database: process.env.DB_NAME || 'mastodon_production',
  125. host: process.env.DB_HOST || 'localhost',
  126. port: process.env.DB_PORT || 5432,
  127. max: 10,
  128. },
  129. };
  130. if (!!process.env.DB_SSLMODE && process.env.DB_SSLMODE !== 'disable') {
  131. pgConfigs.development.ssl = true;
  132. pgConfigs.production.ssl = true;
  133. }
  134. const app = express();
  135. app.set('trust proxy', process.env.TRUSTED_PROXY_IP ? process.env.TRUSTED_PROXY_IP.split(/(?:\s*,\s*|\s+)/) : 'loopback,uniquelocal');
  136. const pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL)));
  137. const server = http.createServer(app);
  138. const redisNamespace = process.env.REDIS_NAMESPACE || null;
  139. const redisParams = {
  140. socket: {
  141. host: process.env.REDIS_HOST || '127.0.0.1',
  142. port: process.env.REDIS_PORT || 6379,
  143. },
  144. database: process.env.REDIS_DB || 0,
  145. password: process.env.REDIS_PASSWORD || undefined,
  146. };
  147. if (redisNamespace) {
  148. redisParams.namespace = redisNamespace;
  149. }
  150. const redisPrefix = redisNamespace ? `${redisNamespace}:` : '';
  151. /**
  152. * @type {Object.<string, Array.<function(Object<string, any>): void>>}
  153. */
  154. const subs = {};
  155. const redisSubscribeClient = await redisUrlToClient(redisParams, process.env.REDIS_URL);
  156. const redisClient = await redisUrlToClient(redisParams, process.env.REDIS_URL);
  157. /**
  158. * @param {string[]} channels
  159. * @return {function(): void}
  160. */
  161. const subscriptionHeartbeat = channels => {
  162. const interval = 6 * 60;
  163. const tellSubscribed = () => {
  164. channels.forEach(channel => redisClient.set(`${redisPrefix}subscribed:${channel}`, '1', 'EX', interval * 3));
  165. };
  166. tellSubscribed();
  167. const heartbeat = setInterval(tellSubscribed, interval * 1000);
  168. return () => {
  169. clearInterval(heartbeat);
  170. };
  171. };
  172. /**
  173. * @param {string} message
  174. * @param {string} channel
  175. */
  176. const onRedisMessage = (message, channel) => {
  177. const callbacks = subs[channel];
  178. log.silly(`New message on channel ${channel}`);
  179. if (!callbacks) {
  180. return;
  181. }
  182. const json = parseJSON(message, null);
  183. if (!json) return;
  184. callbacks.forEach(callback => callback(json));
  185. };
  186. /**
  187. * @callback SubscriptionListener
  188. * @param {ReturnType<parseJSON>} json of the message
  189. * @returns void
  190. */
  191. /**
  192. * @param {string} channel
  193. * @param {SubscriptionListener} callback
  194. */
  195. const subscribe = (channel, callback) => {
  196. log.silly(`Adding listener for ${channel}`);
  197. subs[channel] = subs[channel] || [];
  198. if (subs[channel].length === 0) {
  199. log.verbose(`Subscribe ${channel}`);
  200. redisSubscribeClient.subscribe(channel, onRedisMessage);
  201. }
  202. subs[channel].push(callback);
  203. };
  204. /**
  205. * @param {string} channel
  206. * @param {SubscriptionListener} callback
  207. */
  208. const unsubscribe = (channel, callback) => {
  209. log.silly(`Removing listener for ${channel}`);
  210. if (!subs[channel]) {
  211. return;
  212. }
  213. subs[channel] = subs[channel].filter(item => item !== callback);
  214. if (subs[channel].length === 0) {
  215. log.verbose(`Unsubscribe ${channel}`);
  216. redisSubscribeClient.unsubscribe(channel);
  217. delete subs[channel];
  218. }
  219. };
  220. const FALSE_VALUES = [
  221. false,
  222. 0,
  223. '0',
  224. 'f',
  225. 'F',
  226. 'false',
  227. 'FALSE',
  228. 'off',
  229. 'OFF',
  230. ];
  231. /**
  232. * @param {any} value
  233. * @return {boolean}
  234. */
  235. const isTruthy = value =>
  236. value && !FALSE_VALUES.includes(value);
  237. /**
  238. * @param {any} req
  239. * @param {any} res
  240. * @param {function(Error=): void}
  241. */
  242. const allowCrossDomain = (req, res, next) => {
  243. res.header('Access-Control-Allow-Origin', '*');
  244. res.header('Access-Control-Allow-Headers', 'Authorization, Accept, Cache-Control');
  245. res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
  246. next();
  247. };
  248. /**
  249. * @param {any} req
  250. * @param {any} res
  251. * @param {function(Error=): void}
  252. */
  253. const setRequestId = (req, res, next) => {
  254. req.requestId = uuid.v4();
  255. res.header('X-Request-Id', req.requestId);
  256. next();
  257. };
  258. /**
  259. * @param {any} req
  260. * @param {any} res
  261. * @param {function(Error=): void}
  262. */
  263. const setRemoteAddress = (req, res, next) => {
  264. req.remoteAddress = req.connection.remoteAddress;
  265. next();
  266. };
  267. /**
  268. * @param {any} req
  269. * @param {string[]} necessaryScopes
  270. * @return {boolean}
  271. */
  272. const isInScope = (req, necessaryScopes) =>
  273. req.scopes.some(scope => necessaryScopes.includes(scope));
  274. /**
  275. * @param {string} token
  276. * @param {any} req
  277. * @return {Promise.<void>}
  278. */
  279. const accountFromToken = (token, req) => new Promise((resolve, reject) => {
  280. pgPool.connect((err, client, done) => {
  281. if (err) {
  282. reject(err);
  283. return;
  284. }
  285. client.query('SELECT oauth_access_tokens.id, oauth_access_tokens.resource_owner_id, users.account_id, users.chosen_languages, oauth_access_tokens.scopes, devices.device_id FROM oauth_access_tokens INNER JOIN users ON oauth_access_tokens.resource_owner_id = users.id LEFT OUTER JOIN devices ON oauth_access_tokens.id = devices.access_token_id WHERE oauth_access_tokens.token = $1 AND oauth_access_tokens.revoked_at IS NULL LIMIT 1', [token], (err, result) => {
  286. done();
  287. if (err) {
  288. reject(err);
  289. return;
  290. }
  291. if (result.rows.length === 0) {
  292. err = new Error('Invalid access token');
  293. err.status = 401;
  294. reject(err);
  295. return;
  296. }
  297. req.accessTokenId = result.rows[0].id;
  298. req.scopes = result.rows[0].scopes.split(' ');
  299. req.accountId = result.rows[0].account_id;
  300. req.chosenLanguages = result.rows[0].chosen_languages;
  301. req.deviceId = result.rows[0].device_id;
  302. resolve();
  303. });
  304. });
  305. });
  306. /**
  307. * @param {any} req
  308. * @param {boolean=} required
  309. * @return {Promise.<void>}
  310. */
  311. const accountFromRequest = (req, required = true) => new Promise((resolve, reject) => {
  312. const authorization = req.headers.authorization;
  313. const location = url.parse(req.url, true);
  314. const accessToken = location.query.access_token || req.headers['sec-websocket-protocol'];
  315. if (!authorization && !accessToken) {
  316. if (required) {
  317. const err = new Error('Missing access token');
  318. err.status = 401;
  319. reject(err);
  320. return;
  321. } else {
  322. resolve();
  323. return;
  324. }
  325. }
  326. const token = authorization ? authorization.replace(/^Bearer /, '') : accessToken;
  327. resolve(accountFromToken(token, req));
  328. });
  329. /**
  330. * @param {any} req
  331. * @returns {string|undefined}
  332. */
  333. const channelNameFromPath = req => {
  334. const { path, query } = req;
  335. const onlyMedia = isTruthy(query.only_media);
  336. switch (path) {
  337. case '/api/v1/streaming/user':
  338. return 'user';
  339. case '/api/v1/streaming/user/notification':
  340. return 'user:notification';
  341. case '/api/v1/streaming/public':
  342. return onlyMedia ? 'public:media' : 'public';
  343. case '/api/v1/streaming/public/local':
  344. return onlyMedia ? 'public:local:media' : 'public:local';
  345. case '/api/v1/streaming/public/remote':
  346. return onlyMedia ? 'public:remote:media' : 'public:remote';
  347. case '/api/v1/streaming/hashtag':
  348. return 'hashtag';
  349. case '/api/v1/streaming/hashtag/local':
  350. return 'hashtag:local';
  351. case '/api/v1/streaming/direct':
  352. return 'direct';
  353. case '/api/v1/streaming/list':
  354. return 'list';
  355. default:
  356. return undefined;
  357. }
  358. };
  359. const PUBLIC_CHANNELS = [
  360. 'public',
  361. 'public:media',
  362. 'public:local',
  363. 'public:local:media',
  364. 'public:remote',
  365. 'public:remote:media',
  366. 'hashtag',
  367. 'hashtag:local',
  368. ];
  369. /**
  370. * @param {any} req
  371. * @param {string} channelName
  372. * @return {Promise.<void>}
  373. */
  374. const checkScopes = (req, channelName) => new Promise((resolve, reject) => {
  375. log.silly(req.requestId, `Checking OAuth scopes for ${channelName}`);
  376. // When accessing public channels, no scopes are needed
  377. if (PUBLIC_CHANNELS.includes(channelName)) {
  378. resolve();
  379. return;
  380. }
  381. // The `read` scope has the highest priority, if the token has it
  382. // then it can access all streams
  383. const requiredScopes = ['read'];
  384. // When accessing specifically the notifications stream,
  385. // we need a read:notifications, while in all other cases,
  386. // we can allow access with read:statuses. Mind that the
  387. // user stream will not contain notifications unless
  388. // the token has either read or read:notifications scope
  389. // as well, this is handled separately.
  390. if (channelName === 'user:notification') {
  391. requiredScopes.push('read:notifications');
  392. } else {
  393. requiredScopes.push('read:statuses');
  394. }
  395. if (req.scopes && requiredScopes.some(requiredScope => req.scopes.includes(requiredScope))) {
  396. resolve();
  397. return;
  398. }
  399. const err = new Error('Access token does not cover required scopes');
  400. err.status = 401;
  401. reject(err);
  402. });
  403. /**
  404. * @param {any} info
  405. * @param {function(boolean, number, string): void} callback
  406. */
  407. const wsVerifyClient = (info, callback) => {
  408. // When verifying the websockets connection, we no longer pre-emptively
  409. // check OAuth scopes and drop the connection if they're missing. We only
  410. // drop the connection if access without token is not allowed by environment
  411. // variables. OAuth scope checks are moved to the point of subscription
  412. // to a specific stream.
  413. accountFromRequest(info.req, alwaysRequireAuth).then(() => {
  414. callback(true, undefined, undefined);
  415. }).catch(err => {
  416. log.error(info.req.requestId, err.toString());
  417. callback(false, 401, 'Unauthorized');
  418. });
  419. };
  420. /**
  421. * @typedef SystemMessageHandlers
  422. * @property {function(): void} onKill
  423. */
  424. /**
  425. * @param {any} req
  426. * @param {SystemMessageHandlers} eventHandlers
  427. * @returns {function(object): void}
  428. */
  429. const createSystemMessageListener = (req, eventHandlers) => {
  430. return message => {
  431. const { event } = message;
  432. log.silly(req.requestId, `System message for ${req.accountId}: ${event}`);
  433. if (event === 'kill') {
  434. log.verbose(req.requestId, `Closing connection for ${req.accountId} due to expired access token`);
  435. eventHandlers.onKill();
  436. } else if (event === 'filters_changed') {
  437. log.verbose(req.requestId, `Invalidating filters cache for ${req.accountId}`);
  438. req.cachedFilters = null;
  439. }
  440. };
  441. };
  442. /**
  443. * @param {any} req
  444. * @param {any} res
  445. */
  446. const subscribeHttpToSystemChannel = (req, res) => {
  447. const accessTokenChannelId = `timeline:access_token:${req.accessTokenId}`;
  448. const systemChannelId = `timeline:system:${req.accountId}`;
  449. const listener = createSystemMessageListener(req, {
  450. onKill() {
  451. res.end();
  452. },
  453. });
  454. res.on('close', () => {
  455. unsubscribe(`${redisPrefix}${accessTokenChannelId}`, listener);
  456. unsubscribe(`${redisPrefix}${systemChannelId}`, listener);
  457. });
  458. subscribe(`${redisPrefix}${accessTokenChannelId}`, listener);
  459. subscribe(`${redisPrefix}${systemChannelId}`, listener);
  460. };
  461. /**
  462. * @param {any} req
  463. * @param {any} res
  464. * @param {function(Error=): void} next
  465. */
  466. const authenticationMiddleware = (req, res, next) => {
  467. if (req.method === 'OPTIONS') {
  468. next();
  469. return;
  470. }
  471. accountFromRequest(req, alwaysRequireAuth).then(() => checkScopes(req, channelNameFromPath(req))).then(() => {
  472. subscribeHttpToSystemChannel(req, res);
  473. }).then(() => {
  474. next();
  475. }).catch(err => {
  476. next(err);
  477. });
  478. };
  479. /**
  480. * @param {Error} err
  481. * @param {any} req
  482. * @param {any} res
  483. * @param {function(Error=): void} next
  484. */
  485. const errorMiddleware = (err, req, res, next) => {
  486. log.error(req.requestId, err.toString());
  487. if (res.headersSent) {
  488. next(err);
  489. return;
  490. }
  491. res.writeHead(err.status || 500, { 'Content-Type': 'application/json' });
  492. res.end(JSON.stringify({ error: err.status ? err.toString() : 'An unexpected error occurred' }));
  493. };
  494. /**
  495. * @param {array} arr
  496. * @param {number=} shift
  497. * @return {string}
  498. */
  499. const placeholders = (arr, shift = 0) => arr.map((_, i) => `$${i + 1 + shift}`).join(', ');
  500. /**
  501. * @param {string} listId
  502. * @param {any} req
  503. * @return {Promise.<void>}
  504. */
  505. const authorizeListAccess = (listId, req) => new Promise((resolve, reject) => {
  506. const { accountId } = req;
  507. pgPool.connect((err, client, done) => {
  508. if (err) {
  509. reject();
  510. return;
  511. }
  512. client.query('SELECT id, account_id FROM lists WHERE id = $1 LIMIT 1', [listId], (err, result) => {
  513. done();
  514. if (err || result.rows.length === 0 || result.rows[0].account_id !== accountId) {
  515. reject();
  516. return;
  517. }
  518. resolve();
  519. });
  520. });
  521. });
  522. /**
  523. * @param {string[]} ids
  524. * @param {any} req
  525. * @param {function(string, string): void} output
  526. * @param {undefined | function(string[], SubscriptionListener): void} attachCloseHandler
  527. * @param {boolean=} needsFiltering
  528. * @returns {SubscriptionListener}
  529. */
  530. const streamFrom = (ids, req, output, attachCloseHandler, needsFiltering = false) => {
  531. const accountId = req.accountId || req.remoteAddress;
  532. log.verbose(req.requestId, `Starting stream from ${ids.join(', ')} for ${accountId}`);
  533. const transmit = (event, payload) => {
  534. // TODO: Replace "string"-based delete payloads with object payloads:
  535. const encodedPayload = typeof payload === 'object' ? JSON.stringify(payload) : payload;
  536. log.silly(req.requestId, `Transmitting for ${accountId}: ${event} ${encodedPayload}`);
  537. output(event, encodedPayload);
  538. };
  539. // The listener used to process each message off the redis subscription,
  540. // message here is an object with an `event` and `payload` property. Some
  541. // events also include a queued_at value, but this is being removed shortly.
  542. /** @type {SubscriptionListener} */
  543. const listener = message => {
  544. const { event, payload } = message;
  545. // Streaming only needs to apply filtering to some channels and only to
  546. // some events. This is because majority of the filtering happens on the
  547. // Ruby on Rails side when producing the event for streaming.
  548. //
  549. // The only events that require filtering from the streaming server are
  550. // `update` and `status.update`, all other events are transmitted to the
  551. // client as soon as they're received (pass-through).
  552. //
  553. // The channels that need filtering are determined in the function
  554. // `channelNameToIds` defined below:
  555. if (!needsFiltering || (event !== 'update' && event !== 'status.update')) {
  556. transmit(event, payload);
  557. return;
  558. }
  559. // The rest of the logic from here on in this function is to handle
  560. // filtering of statuses:
  561. // Filter based on language:
  562. if (Array.isArray(req.chosenLanguages) && payload.language !== null && req.chosenLanguages.indexOf(payload.language) === -1) {
  563. log.silly(req.requestId, `Message ${payload.id} filtered by language (${payload.language})`);
  564. return;
  565. }
  566. // When the account is not logged in, it is not necessary to confirm the block or mute
  567. if (!req.accountId) {
  568. transmit(event, payload);
  569. return;
  570. }
  571. // Filter based on domain blocks, blocks, mutes, or custom filters:
  572. const targetAccountIds = [payload.account.id].concat(payload.mentions.map(item => item.id));
  573. const accountDomain = payload.account.acct.split('@')[1];
  574. // TODO: Move this logic out of the message handling loop
  575. pgPool.connect((err, client, releasePgConnection) => {
  576. if (err) {
  577. log.error(err);
  578. return;
  579. }
  580. const queries = [
  581. client.query(`SELECT 1
  582. FROM blocks
  583. WHERE (account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 2)}))
  584. OR (account_id = $2 AND target_account_id = $1)
  585. UNION
  586. SELECT 1
  587. FROM mutes
  588. WHERE account_id = $1
  589. AND target_account_id IN (${placeholders(targetAccountIds, 2)})`, [req.accountId, payload.account.id].concat(targetAccountIds)),
  590. ];
  591. if (accountDomain) {
  592. queries.push(client.query('SELECT 1 FROM account_domain_blocks WHERE account_id = $1 AND domain = $2', [req.accountId, accountDomain]));
  593. }
  594. if (!payload.filtered && !req.cachedFilters) {
  595. queries.push(client.query('SELECT filter.id AS id, filter.phrase AS title, filter.context AS context, filter.expires_at AS expires_at, filter.action AS filter_action, keyword.keyword AS keyword, keyword.whole_word AS whole_word FROM custom_filter_keywords keyword JOIN custom_filters filter ON keyword.custom_filter_id = filter.id WHERE filter.account_id = $1 AND (filter.expires_at IS NULL OR filter.expires_at > NOW())', [req.accountId]));
  596. }
  597. Promise.all(queries).then(values => {
  598. releasePgConnection();
  599. // Handling blocks & mutes and domain blocks: If one of those applies,
  600. // then we don't transmit the payload of the event to the client
  601. if (values[0].rows.length > 0 || (accountDomain && values[1].rows.length > 0)) {
  602. return;
  603. }
  604. // If the payload already contains the `filtered` property, it means
  605. // that filtering has been applied on the ruby on rails side, as
  606. // such, we don't need to construct or apply the filters in streaming:
  607. if (Object.prototype.hasOwnProperty.call(payload, "filtered")) {
  608. transmit(event, payload);
  609. return;
  610. }
  611. // Handling for constructing the custom filters and caching them on the request
  612. // TODO: Move this logic out of the message handling lifecycle
  613. if (!req.cachedFilters) {
  614. const filterRows = values[accountDomain ? 2 : 1].rows;
  615. req.cachedFilters = filterRows.reduce((cache, filter) => {
  616. if (cache[filter.id]) {
  617. cache[filter.id].keywords.push([filter.keyword, filter.whole_word]);
  618. } else {
  619. cache[filter.id] = {
  620. keywords: [[filter.keyword, filter.whole_word]],
  621. expires_at: filter.expires_at,
  622. filter: {
  623. id: filter.id,
  624. title: filter.title,
  625. context: filter.context,
  626. expires_at: filter.expires_at,
  627. // filter.filter_action is the value from the
  628. // custom_filters.action database column, it is an integer
  629. // representing a value in an enum defined by Ruby on Rails:
  630. //
  631. // enum { warn: 0, hide: 1 }
  632. filter_action: ['warn', 'hide'][filter.filter_action],
  633. },
  634. };
  635. }
  636. return cache;
  637. }, {});
  638. // Construct the regular expressions for the custom filters: This
  639. // needs to be done in a separate loop as the database returns one
  640. // filterRow per keyword, so we need all the keywords before
  641. // constructing the regular expression
  642. Object.keys(req.cachedFilters).forEach((key) => {
  643. req.cachedFilters[key].regexp = new RegExp(req.cachedFilters[key].keywords.map(([keyword, whole_word]) => {
  644. let expr = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');;
  645. if (whole_word) {
  646. if (/^[\w]/.test(expr)) {
  647. expr = `\\b${expr}`;
  648. }
  649. if (/[\w]$/.test(expr)) {
  650. expr = `${expr}\\b`;
  651. }
  652. }
  653. return expr;
  654. }).join('|'), 'i');
  655. });
  656. }
  657. // Apply cachedFilters against the payload, constructing a
  658. // `filter_results` array of FilterResult entities
  659. if (req.cachedFilters) {
  660. const status = payload;
  661. // TODO: Calculate searchableContent in Ruby on Rails:
  662. const searchableContent = ([status.spoiler_text || '', status.content].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).concat(status.media_attachments.map(att => att.description)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  663. const searchableTextContent = JSDOM.fragment(searchableContent).textContent;
  664. const now = new Date();
  665. const filter_results = Object.values(req.cachedFilters).reduce((results, cachedFilter) => {
  666. // Check the filter hasn't expired before applying:
  667. if (cachedFilter.expires_at !== null && cachedFilter.expires_at < now) {
  668. return results;
  669. }
  670. // Just in-case JSDOM fails to find textContent in searchableContent
  671. if (!searchableTextContent) {
  672. return results;
  673. }
  674. const keyword_matches = searchableTextContent.match(cachedFilter.regexp);
  675. if (keyword_matches) {
  676. // results is an Array of FilterResult; status_matches is always
  677. // null as we only are only applying the keyword-based custom
  678. // filters, not the status-based custom filters.
  679. // https://docs.joinmastodon.org/entities/FilterResult/
  680. results.push({
  681. filter: cachedFilter.filter,
  682. keyword_matches,
  683. status_matches: null
  684. });
  685. }
  686. return results;
  687. }, []);
  688. // Send the payload + the FilterResults as the `filtered` property
  689. // to the streaming connection. To reach this code, the `event` must
  690. // have been either `update` or `status.update`, meaning the
  691. // `payload` is a Status entity, which has a `filtered` property:
  692. //
  693. // filtered: https://docs.joinmastodon.org/entities/Status/#filtered
  694. transmit(event, {
  695. ...payload,
  696. filtered: filter_results
  697. });
  698. } else {
  699. transmit(event, payload);
  700. }
  701. }).catch(err => {
  702. releasePgConnection();
  703. log.error(err);
  704. });
  705. });
  706. };
  707. ids.forEach(id => {
  708. subscribe(`${redisPrefix}${id}`, listener);
  709. });
  710. if (typeof attachCloseHandler === 'function') {
  711. attachCloseHandler(ids.map(id => `${redisPrefix}${id}`), listener);
  712. }
  713. return listener;
  714. };
  715. /**
  716. * @param {any} req
  717. * @param {any} res
  718. * @return {function(string, string): void}
  719. */
  720. const streamToHttp = (req, res) => {
  721. const accountId = req.accountId || req.remoteAddress;
  722. res.setHeader('Content-Type', 'text/event-stream');
  723. res.setHeader('Cache-Control', 'no-store');
  724. res.setHeader('Transfer-Encoding', 'chunked');
  725. res.write(':)\n');
  726. const heartbeat = setInterval(() => res.write(':thump\n'), 15000);
  727. req.on('close', () => {
  728. log.verbose(req.requestId, `Ending stream for ${accountId}`);
  729. clearInterval(heartbeat);
  730. });
  731. return (event, payload) => {
  732. res.write(`event: ${event}\n`);
  733. res.write(`data: ${payload}\n\n`);
  734. };
  735. };
  736. /**
  737. * @param {any} req
  738. * @param {function(): void} [closeHandler]
  739. * @returns {function(string[], SubscriptionListener): void}
  740. */
  741. const streamHttpEnd = (req, closeHandler = undefined) => (ids, listener) => {
  742. req.on('close', () => {
  743. ids.forEach(id => {
  744. unsubscribe(id, listener);
  745. });
  746. if (closeHandler) {
  747. closeHandler();
  748. }
  749. });
  750. };
  751. /**
  752. * @param {any} req
  753. * @param {any} ws
  754. * @param {string[]} streamName
  755. * @return {function(string, string): void}
  756. */
  757. const streamToWs = (req, ws, streamName) => (event, payload) => {
  758. if (ws.readyState !== ws.OPEN) {
  759. log.error(req.requestId, 'Tried writing to closed socket');
  760. return;
  761. }
  762. ws.send(JSON.stringify({ stream: streamName, event, payload }));
  763. };
  764. /**
  765. * @param {any} res
  766. */
  767. const httpNotFound = res => {
  768. res.writeHead(404, { 'Content-Type': 'application/json' });
  769. res.end(JSON.stringify({ error: 'Not found' }));
  770. };
  771. app.use(setRequestId);
  772. app.use(setRemoteAddress);
  773. app.use(allowCrossDomain);
  774. app.get('/api/v1/streaming/health', (req, res) => {
  775. res.writeHead(200, { 'Content-Type': 'text/plain' });
  776. res.end('OK');
  777. });
  778. app.use(authenticationMiddleware);
  779. app.use(errorMiddleware);
  780. app.get('/api/v1/streaming/*', (req, res) => {
  781. channelNameToIds(req, channelNameFromPath(req), req.query).then(({ channelIds, options }) => {
  782. const onSend = streamToHttp(req, res);
  783. const onEnd = streamHttpEnd(req, subscriptionHeartbeat(channelIds));
  784. streamFrom(channelIds, req, onSend, onEnd, options.needsFiltering);
  785. }).catch(err => {
  786. log.verbose(req.requestId, 'Subscription error:', err.toString());
  787. httpNotFound(res);
  788. });
  789. });
  790. const wss = new WebSocket.Server({ server, verifyClient: wsVerifyClient });
  791. /**
  792. * @typedef StreamParams
  793. * @property {string} [tag]
  794. * @property {string} [list]
  795. * @property {string} [only_media]
  796. */
  797. /**
  798. * @param {any} req
  799. * @return {string[]}
  800. */
  801. const channelsForUserStream = req => {
  802. const arr = [`timeline:${req.accountId}`];
  803. if (isInScope(req, ['crypto']) && req.deviceId) {
  804. arr.push(`timeline:${req.accountId}:${req.deviceId}`);
  805. }
  806. if (isInScope(req, ['read', 'read:notifications'])) {
  807. arr.push(`timeline:${req.accountId}:notifications`);
  808. }
  809. return arr;
  810. };
  811. /**
  812. * See app/lib/ascii_folder.rb for the canon definitions
  813. * of these constants
  814. */
  815. const NON_ASCII_CHARS = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž';
  816. const EQUIVALENT_ASCII_CHARS = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz';
  817. /**
  818. * @param {string} str
  819. * @return {string}
  820. */
  821. const foldToASCII = str => {
  822. const regex = new RegExp(NON_ASCII_CHARS.split('').join('|'), 'g');
  823. return str.replace(regex, match => {
  824. const index = NON_ASCII_CHARS.indexOf(match);
  825. return EQUIVALENT_ASCII_CHARS[index];
  826. });
  827. };
  828. /**
  829. * @param {string} str
  830. * @return {string}
  831. */
  832. const normalizeHashtag = str => {
  833. return foldToASCII(str.normalize('NFKC').toLowerCase()).replace(/[^\p{L}\p{N}_\u00b7\u200c]/gu, '');
  834. };
  835. /**
  836. * @param {any} req
  837. * @param {string} name
  838. * @param {StreamParams} params
  839. * @return {Promise.<{ channelIds: string[], options: { needsFiltering: boolean } }>}
  840. */
  841. const channelNameToIds = (req, name, params) => new Promise((resolve, reject) => {
  842. switch (name) {
  843. case 'user':
  844. resolve({
  845. channelIds: channelsForUserStream(req),
  846. options: { needsFiltering: false },
  847. });
  848. break;
  849. case 'user:notification':
  850. resolve({
  851. channelIds: [`timeline:${req.accountId}:notifications`],
  852. options: { needsFiltering: false },
  853. });
  854. break;
  855. case 'public':
  856. resolve({
  857. channelIds: ['timeline:public'],
  858. options: { needsFiltering: true },
  859. });
  860. break;
  861. case 'public:local':
  862. resolve({
  863. channelIds: ['timeline:public:local'],
  864. options: { needsFiltering: true },
  865. });
  866. break;
  867. case 'public:remote':
  868. resolve({
  869. channelIds: ['timeline:public:remote'],
  870. options: { needsFiltering: true },
  871. });
  872. break;
  873. case 'public:media':
  874. resolve({
  875. channelIds: ['timeline:public:media'],
  876. options: { needsFiltering: true },
  877. });
  878. break;
  879. case 'public:local:media':
  880. resolve({
  881. channelIds: ['timeline:public:local:media'],
  882. options: { needsFiltering: true },
  883. });
  884. break;
  885. case 'public:remote:media':
  886. resolve({
  887. channelIds: ['timeline:public:remote:media'],
  888. options: { needsFiltering: true },
  889. });
  890. break;
  891. case 'direct':
  892. resolve({
  893. channelIds: [`timeline:direct:${req.accountId}`],
  894. options: { needsFiltering: false },
  895. });
  896. break;
  897. case 'hashtag':
  898. if (!params.tag || params.tag.length === 0) {
  899. reject('No tag for stream provided');
  900. } else {
  901. resolve({
  902. channelIds: [`timeline:hashtag:${normalizeHashtag(params.tag)}`],
  903. options: { needsFiltering: true },
  904. });
  905. }
  906. break;
  907. case 'hashtag:local':
  908. if (!params.tag || params.tag.length === 0) {
  909. reject('No tag for stream provided');
  910. } else {
  911. resolve({
  912. channelIds: [`timeline:hashtag:${normalizeHashtag(params.tag)}:local`],
  913. options: { needsFiltering: true },
  914. });
  915. }
  916. break;
  917. case 'list':
  918. authorizeListAccess(params.list, req).then(() => {
  919. resolve({
  920. channelIds: [`timeline:list:${params.list}`],
  921. options: { needsFiltering: false },
  922. });
  923. }).catch(() => {
  924. reject('Not authorized to stream this list');
  925. });
  926. break;
  927. default:
  928. reject('Unknown stream type');
  929. }
  930. });
  931. /**
  932. * @param {string} channelName
  933. * @param {StreamParams} params
  934. * @return {string[]}
  935. */
  936. const streamNameFromChannelName = (channelName, params) => {
  937. if (channelName === 'list') {
  938. return [channelName, params.list];
  939. } else if (['hashtag', 'hashtag:local'].includes(channelName)) {
  940. return [channelName, params.tag];
  941. } else {
  942. return [channelName];
  943. }
  944. };
  945. /**
  946. * @typedef WebSocketSession
  947. * @property {any} socket
  948. * @property {any} request
  949. * @property {Object.<string, { listener: SubscriptionListener, stopHeartbeat: function(): void }>} subscriptions
  950. */
  951. /**
  952. * @param {WebSocketSession} session
  953. * @param {string} channelName
  954. * @param {StreamParams} params
  955. */
  956. const subscribeWebsocketToChannel = ({ socket, request, subscriptions }, channelName, params) =>
  957. checkScopes(request, channelName).then(() => channelNameToIds(request, channelName, params)).then(({
  958. channelIds,
  959. options,
  960. }) => {
  961. if (subscriptions[channelIds.join(';')]) {
  962. return;
  963. }
  964. const onSend = streamToWs(request, socket, streamNameFromChannelName(channelName, params));
  965. const stopHeartbeat = subscriptionHeartbeat(channelIds);
  966. const listener = streamFrom(channelIds, request, onSend, undefined, options.needsFiltering);
  967. subscriptions[channelIds.join(';')] = {
  968. listener,
  969. stopHeartbeat,
  970. };
  971. }).catch(err => {
  972. log.verbose(request.requestId, 'Subscription error:', err.toString());
  973. socket.send(JSON.stringify({ error: err.toString() }));
  974. });
  975. /**
  976. * @param {WebSocketSession} session
  977. * @param {string} channelName
  978. * @param {StreamParams} params
  979. */
  980. const unsubscribeWebsocketFromChannel = ({ socket, request, subscriptions }, channelName, params) =>
  981. channelNameToIds(request, channelName, params).then(({ channelIds }) => {
  982. log.verbose(request.requestId, `Ending stream from ${channelIds.join(', ')} for ${request.accountId}`);
  983. const subscription = subscriptions[channelIds.join(';')];
  984. if (!subscription) {
  985. return;
  986. }
  987. const { listener, stopHeartbeat } = subscription;
  988. channelIds.forEach(channelId => {
  989. unsubscribe(`${redisPrefix}${channelId}`, listener);
  990. });
  991. stopHeartbeat();
  992. delete subscriptions[channelIds.join(';')];
  993. }).catch(err => {
  994. log.verbose(request.requestId, 'Unsubscription error:', err);
  995. socket.send(JSON.stringify({ error: err.toString() }));
  996. });
  997. /**
  998. * @param {WebSocketSession} session
  999. */
  1000. const subscribeWebsocketToSystemChannel = ({ socket, request, subscriptions }) => {
  1001. const accessTokenChannelId = `timeline:access_token:${request.accessTokenId}`;
  1002. const systemChannelId = `timeline:system:${request.accountId}`;
  1003. const listener = createSystemMessageListener(request, {
  1004. onKill() {
  1005. socket.close();
  1006. },
  1007. });
  1008. subscribe(`${redisPrefix}${accessTokenChannelId}`, listener);
  1009. subscribe(`${redisPrefix}${systemChannelId}`, listener);
  1010. subscriptions[accessTokenChannelId] = {
  1011. listener,
  1012. stopHeartbeat: () => {
  1013. },
  1014. };
  1015. subscriptions[systemChannelId] = {
  1016. listener,
  1017. stopHeartbeat: () => {
  1018. },
  1019. };
  1020. };
  1021. /**
  1022. * @param {string|string[]} arrayOrString
  1023. * @return {string}
  1024. */
  1025. const firstParam = arrayOrString => {
  1026. if (Array.isArray(arrayOrString)) {
  1027. return arrayOrString[0];
  1028. } else {
  1029. return arrayOrString;
  1030. }
  1031. };
  1032. wss.on('connection', (ws, req) => {
  1033. const location = url.parse(req.url, true);
  1034. req.requestId = uuid.v4();
  1035. req.remoteAddress = ws._socket.remoteAddress;
  1036. ws.isAlive = true;
  1037. ws.on('pong', () => {
  1038. ws.isAlive = true;
  1039. });
  1040. /**
  1041. * @type {WebSocketSession}
  1042. */
  1043. const session = {
  1044. socket: ws,
  1045. request: req,
  1046. subscriptions: {},
  1047. };
  1048. const onEnd = () => {
  1049. const keys = Object.keys(session.subscriptions);
  1050. keys.forEach(channelIds => {
  1051. const { listener, stopHeartbeat } = session.subscriptions[channelIds];
  1052. channelIds.split(';').forEach(channelId => {
  1053. unsubscribe(`${redisPrefix}${channelId}`, listener);
  1054. });
  1055. stopHeartbeat();
  1056. });
  1057. };
  1058. ws.on('close', onEnd);
  1059. ws.on('error', onEnd);
  1060. ws.on('message', (data, isBinary) => {
  1061. if (isBinary) {
  1062. log.warn('socket', 'Received binary data, closing connection');
  1063. ws.close(1003, 'The mastodon streaming server does not support binary messages');
  1064. return;
  1065. }
  1066. const message = data.toString('utf8');
  1067. const json = parseJSON(message, session.request);
  1068. if (!json) return;
  1069. const { type, stream, ...params } = json;
  1070. if (type === 'subscribe') {
  1071. subscribeWebsocketToChannel(session, firstParam(stream), params);
  1072. } else if (type === 'unsubscribe') {
  1073. unsubscribeWebsocketFromChannel(session, firstParam(stream), params);
  1074. } else {
  1075. // Unknown action type
  1076. }
  1077. });
  1078. subscribeWebsocketToSystemChannel(session);
  1079. if (location.query.stream) {
  1080. subscribeWebsocketToChannel(session, firstParam(location.query.stream), location.query);
  1081. }
  1082. });
  1083. setInterval(() => {
  1084. wss.clients.forEach(ws => {
  1085. if (ws.isAlive === false) {
  1086. ws.terminate();
  1087. return;
  1088. }
  1089. ws.isAlive = false;
  1090. ws.ping('', false);
  1091. });
  1092. }, 30000);
  1093. attachServerWithConfig(server, address => {
  1094. log.warn(`Worker ${workerId} now listening on ${address}`);
  1095. });
  1096. const onExit = () => {
  1097. log.warn(`Worker ${workerId} exiting`);
  1098. server.close();
  1099. process.exit(0);
  1100. };
  1101. const onError = (err) => {
  1102. log.error(err);
  1103. server.close();
  1104. process.exit(0);
  1105. };
  1106. process.on('SIGINT', onExit);
  1107. process.on('SIGTERM', onExit);
  1108. process.on('exit', onExit);
  1109. process.on('uncaughtException', onError);
  1110. };
  1111. /**
  1112. * @param {any} server
  1113. * @param {function(string): void} [onSuccess]
  1114. */
  1115. const attachServerWithConfig = (server, onSuccess) => {
  1116. if (process.env.SOCKET || process.env.PORT && isNaN(+process.env.PORT)) {
  1117. server.listen(process.env.SOCKET || process.env.PORT, () => {
  1118. if (onSuccess) {
  1119. fs.chmodSync(server.address(), 0o666);
  1120. onSuccess(server.address());
  1121. }
  1122. });
  1123. } else {
  1124. server.listen(+process.env.PORT || 4000, process.env.BIND || '127.0.0.1', () => {
  1125. if (onSuccess) {
  1126. onSuccess(`${server.address().address}:${server.address().port}`);
  1127. }
  1128. });
  1129. }
  1130. };
  1131. /**
  1132. * @param {function(Error=): void} onSuccess
  1133. */
  1134. const onPortAvailable = onSuccess => {
  1135. const testServer = http.createServer();
  1136. testServer.once('error', err => {
  1137. onSuccess(err);
  1138. });
  1139. testServer.once('listening', () => {
  1140. testServer.once('close', () => onSuccess());
  1141. testServer.close();
  1142. });
  1143. attachServerWithConfig(testServer);
  1144. };
  1145. onPortAvailable(err => {
  1146. if (err) {
  1147. log.error('Could not start server, the port or socket is in use');
  1148. return;
  1149. }
  1150. throng({
  1151. workers: numWorkers,
  1152. lifetime: Infinity,
  1153. start: startWorker,
  1154. master: startMaster,
  1155. });
  1156. });