index.js 41 KB

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