index.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. // @ts-check
  2. import fs from 'node:fs';
  3. import http from 'node:http';
  4. import path from 'node:path';
  5. import url from 'node:url';
  6. import cors from 'cors';
  7. import dotenv from 'dotenv';
  8. import express from 'express';
  9. import { Redis } from 'ioredis';
  10. import { JSDOM } from 'jsdom';
  11. import pg from 'pg';
  12. import pgConnectionString from 'pg-connection-string';
  13. import WebSocket from 'ws';
  14. import { AuthenticationError, RequestError, extractStatusAndMessage as extractErrorStatusAndMessage } from './errors.js';
  15. import { logger, httpLogger, initializeLogLevel, attachWebsocketHttpLogger, createWebsocketLogger } from './logging.js';
  16. import { setupMetrics } from './metrics.js';
  17. import { isTruthy, normalizeHashtag, firstParam } from './utils.js';
  18. const environment = process.env.NODE_ENV || 'development';
  19. // Correctly detect and load .env or .env.production file based on environment:
  20. const dotenvFile = environment === 'production' ? '.env.production' : '.env';
  21. const dotenvFilePath = path.resolve(
  22. url.fileURLToPath(
  23. new URL(path.join('..', dotenvFile), import.meta.url)
  24. )
  25. );
  26. dotenv.config({
  27. path: dotenvFilePath
  28. });
  29. initializeLogLevel(process.env, environment);
  30. /**
  31. * Declares the result type for accountFromToken / accountFromRequest.
  32. *
  33. * Note: This is here because jsdoc doesn't like importing types that
  34. * are nested in functions
  35. * @typedef ResolvedAccount
  36. * @property {string} accessTokenId
  37. * @property {string[]} scopes
  38. * @property {string} accountId
  39. * @property {string[]} chosenLanguages
  40. * @property {string} deviceId
  41. */
  42. /**
  43. * @param {RedisConfiguration} config
  44. * @returns {Promise<Redis>}
  45. */
  46. const createRedisClient = async ({ redisParams, redisUrl }) => {
  47. let client;
  48. if (typeof redisUrl === 'string') {
  49. client = new Redis(redisUrl, redisParams);
  50. } else {
  51. client = new Redis(redisParams);
  52. }
  53. client.on('error', (err) => logger.error({ err }, 'Redis Client Error!'));
  54. return client;
  55. };
  56. /**
  57. * Attempts to safely parse a string as JSON, used when both receiving a message
  58. * from redis and when receiving a message from a client over a websocket
  59. * connection, this is why it accepts a `req` argument.
  60. * @param {string} json
  61. * @param {any?} req
  62. * @returns {Object.<string, any>|null}
  63. */
  64. const parseJSON = (json, req) => {
  65. try {
  66. return JSON.parse(json);
  67. } catch (err) {
  68. /* FIXME: This logging isn't great, and should probably be done at the
  69. * call-site of parseJSON, not in the method, but this would require changing
  70. * the signature of parseJSON to return something akin to a Result type:
  71. * [Error|null, null|Object<string,any}], and then handling the error
  72. * scenarios.
  73. */
  74. if (req) {
  75. if (req.accountId) {
  76. req.log.error({ err }, `Error parsing message from user ${req.accountId}`);
  77. } else {
  78. req.log.error({ err }, `Error parsing message from ${req.remoteAddress}`);
  79. }
  80. } else {
  81. logger.error({ err }, `Error parsing message from redis`);
  82. }
  83. return null;
  84. }
  85. };
  86. /**
  87. * Takes an environment variable that should be an integer, attempts to parse
  88. * it falling back to a default if not set, and handles errors parsing.
  89. * @param {string|undefined} value
  90. * @param {number} defaultValue
  91. * @param {string} variableName
  92. * @returns {number}
  93. */
  94. const parseIntFromEnv = (value, defaultValue, variableName) => {
  95. if (typeof value === 'string' && value.length > 0) {
  96. const parsedValue = parseInt(value, 10);
  97. if (isNaN(parsedValue)) {
  98. throw new Error(`Invalid ${variableName} environment variable: ${value}`);
  99. }
  100. return parsedValue;
  101. } else {
  102. return defaultValue;
  103. }
  104. };
  105. /**
  106. * @param {NodeJS.ProcessEnv} env the `process.env` value to read configuration from
  107. * @returns {pg.PoolConfig} the configuration for the PostgreSQL connection
  108. */
  109. const pgConfigFromEnv = (env) => {
  110. /** @type {Record<string, pg.PoolConfig>} */
  111. const pgConfigs = {
  112. development: {
  113. user: env.DB_USER || pg.defaults.user,
  114. password: env.DB_PASS || pg.defaults.password,
  115. database: env.DB_NAME || 'mastodon_development',
  116. host: env.DB_HOST || pg.defaults.host,
  117. port: parseIntFromEnv(env.DB_PORT, pg.defaults.port ?? 5432, 'DB_PORT')
  118. },
  119. production: {
  120. user: env.DB_USER || 'mastodon',
  121. password: env.DB_PASS || '',
  122. database: env.DB_NAME || 'mastodon_production',
  123. host: env.DB_HOST || 'localhost',
  124. port: parseIntFromEnv(env.DB_PORT, 5432, 'DB_PORT')
  125. },
  126. };
  127. /**
  128. * @type {pg.PoolConfig}
  129. */
  130. let baseConfig = {};
  131. if (env.DATABASE_URL) {
  132. const parsedUrl = pgConnectionString.parse(env.DATABASE_URL);
  133. // The result of dbUrlToConfig from pg-connection-string is not type
  134. // compatible with pg.PoolConfig, since parts of the connection URL may be
  135. // `null` when pg.PoolConfig expects `undefined`, as such we have to
  136. // manually create the baseConfig object from the properties of the
  137. // parsedUrl.
  138. //
  139. // For more information see:
  140. // https://github.com/brianc/node-postgres/issues/2280
  141. //
  142. // FIXME: clean up once brianc/node-postgres#3128 lands
  143. if (typeof parsedUrl.password === 'string') baseConfig.password = parsedUrl.password;
  144. if (typeof parsedUrl.host === 'string') baseConfig.host = parsedUrl.host;
  145. if (typeof parsedUrl.user === 'string') baseConfig.user = parsedUrl.user;
  146. if (typeof parsedUrl.port === 'string') {
  147. const parsedPort = parseInt(parsedUrl.port, 10);
  148. if (isNaN(parsedPort)) {
  149. throw new Error('Invalid port specified in DATABASE_URL environment variable');
  150. }
  151. baseConfig.port = parsedPort;
  152. }
  153. if (typeof parsedUrl.database === 'string') baseConfig.database = parsedUrl.database;
  154. if (typeof parsedUrl.options === 'string') baseConfig.options = parsedUrl.options;
  155. // The pg-connection-string type definition isn't correct, as parsedUrl.ssl
  156. // can absolutely be an Object, this is to work around these incorrect
  157. // types, including the casting of parsedUrl.ssl to Record<string, any>
  158. if (typeof parsedUrl.ssl === 'boolean') {
  159. baseConfig.ssl = parsedUrl.ssl;
  160. } else if (typeof parsedUrl.ssl === 'object' && !Array.isArray(parsedUrl.ssl) && parsedUrl.ssl !== null) {
  161. /** @type {Record<string, any>} */
  162. const sslOptions = parsedUrl.ssl;
  163. baseConfig.ssl = {};
  164. baseConfig.ssl.cert = sslOptions.cert;
  165. baseConfig.ssl.key = sslOptions.key;
  166. baseConfig.ssl.ca = sslOptions.ca;
  167. baseConfig.ssl.rejectUnauthorized = sslOptions.rejectUnauthorized;
  168. }
  169. // Support overriding the database password in the connection URL
  170. if (!baseConfig.password && env.DB_PASS) {
  171. baseConfig.password = env.DB_PASS;
  172. }
  173. } else if (Object.hasOwn(pgConfigs, environment)) {
  174. baseConfig = pgConfigs[environment];
  175. if (env.DB_SSLMODE) {
  176. switch(env.DB_SSLMODE) {
  177. case 'disable':
  178. case '':
  179. baseConfig.ssl = false;
  180. break;
  181. case 'no-verify':
  182. baseConfig.ssl = { rejectUnauthorized: false };
  183. break;
  184. default:
  185. baseConfig.ssl = {};
  186. break;
  187. }
  188. }
  189. } else {
  190. throw new Error('Unable to resolve postgresql database configuration.');
  191. }
  192. return {
  193. ...baseConfig,
  194. max: parseIntFromEnv(env.DB_POOL, 10, 'DB_POOL'),
  195. connectionTimeoutMillis: 15000,
  196. // Deliberately set application_name to an empty string to prevent excessive
  197. // CPU usage with PG Bouncer. See:
  198. // - https://github.com/mastodon/mastodon/pull/23958
  199. // - https://github.com/pgbouncer/pgbouncer/issues/349
  200. application_name: '',
  201. };
  202. };
  203. /**
  204. * @typedef RedisConfiguration
  205. * @property {import('ioredis').RedisOptions} redisParams
  206. * @property {string} redisPrefix
  207. * @property {string|undefined} redisUrl
  208. */
  209. /**
  210. * @param {NodeJS.ProcessEnv} env the `process.env` value to read configuration from
  211. * @returns {RedisConfiguration} configuration for the Redis connection
  212. */
  213. const redisConfigFromEnv = (env) => {
  214. // ioredis *can* transparently add prefixes for us, but it doesn't *in some cases*,
  215. // which means we can't use it. But this is something that should be looked into.
  216. const redisPrefix = env.REDIS_NAMESPACE ? `${env.REDIS_NAMESPACE}:` : '';
  217. let redisPort = parseIntFromEnv(env.REDIS_PORT, 6379, 'REDIS_PORT');
  218. let redisDatabase = parseIntFromEnv(env.REDIS_DB, 0, 'REDIS_DB');
  219. /** @type {import('ioredis').RedisOptions} */
  220. const redisParams = {
  221. host: env.REDIS_HOST || '127.0.0.1',
  222. port: redisPort,
  223. db: redisDatabase,
  224. password: env.REDIS_PASSWORD || undefined,
  225. };
  226. // redisParams.path takes precedence over host and port.
  227. if (env.REDIS_URL && env.REDIS_URL.startsWith('unix://')) {
  228. redisParams.path = env.REDIS_URL.slice(7);
  229. }
  230. return {
  231. redisParams,
  232. redisPrefix,
  233. redisUrl: typeof env.REDIS_URL === 'string' ? env.REDIS_URL : undefined,
  234. };
  235. };
  236. const PUBLIC_CHANNELS = [
  237. 'public',
  238. 'public:media',
  239. 'public:local',
  240. 'public:local:media',
  241. 'public:remote',
  242. 'public:remote:media',
  243. 'hashtag',
  244. 'hashtag:local',
  245. ];
  246. // Used for priming the counters/gauges for the various metrics that are
  247. // per-channel
  248. const CHANNEL_NAMES = [
  249. 'system',
  250. 'user',
  251. 'user:notification',
  252. 'list',
  253. 'direct',
  254. ...PUBLIC_CHANNELS
  255. ];
  256. const startServer = async () => {
  257. const pgPool = new pg.Pool(pgConfigFromEnv(process.env));
  258. const server = http.createServer();
  259. const wss = new WebSocket.Server({ noServer: true });
  260. // Set the X-Request-Id header on WebSockets:
  261. wss.on("headers", function onHeaders(headers, req) {
  262. headers.push(`X-Request-Id: ${req.id}`);
  263. });
  264. const app = express();
  265. app.set('trust proxy', process.env.TRUSTED_PROXY_IP ? process.env.TRUSTED_PROXY_IP.split(/(?:\s*,\s*|\s+)/) : 'loopback,uniquelocal');
  266. app.use(httpLogger);
  267. app.use(cors());
  268. // Handle eventsource & other http requests:
  269. server.on('request', app);
  270. // Handle upgrade requests:
  271. server.on('upgrade', async function handleUpgrade(request, socket, head) {
  272. // Setup the HTTP logger, since websocket upgrades don't get the usual http
  273. // logger. This decorates the `request` object.
  274. attachWebsocketHttpLogger(request);
  275. request.log.info("HTTP Upgrade Requested");
  276. /** @param {Error} err */
  277. const onSocketError = (err) => {
  278. request.log.error({ error: err }, err.message);
  279. };
  280. socket.on('error', onSocketError);
  281. /** @type {ResolvedAccount} */
  282. let resolvedAccount;
  283. try {
  284. resolvedAccount = await accountFromRequest(request);
  285. } catch (err) {
  286. // Unfortunately for using the on('upgrade') setup, we need to manually
  287. // write a HTTP Response to the Socket to close the connection upgrade
  288. // attempt, so the following code is to handle all of that.
  289. const {statusCode, errorMessage } = extractErrorStatusAndMessage(err);
  290. /** @type {Record<string, string | number | import('pino-http').ReqId>} */
  291. const headers = {
  292. 'Connection': 'close',
  293. 'Content-Type': 'text/plain',
  294. 'Content-Length': 0,
  295. 'X-Request-Id': request.id,
  296. 'X-Error-Message': errorMessage
  297. };
  298. // Ensure the socket is closed once we've finished writing to it:
  299. socket.once('finish', () => {
  300. socket.destroy();
  301. });
  302. // Write the HTTP response manually:
  303. socket.end(`HTTP/1.1 ${statusCode} ${http.STATUS_CODES[statusCode]}\r\n${Object.keys(headers).map((key) => `${key}: ${headers[key]}`).join('\r\n')}\r\n\r\n`);
  304. // Finally, log the error:
  305. request.log.error({
  306. err,
  307. res: {
  308. statusCode,
  309. headers
  310. }
  311. }, errorMessage);
  312. return;
  313. }
  314. // Remove the error handler, wss.handleUpgrade has its own:
  315. socket.removeListener('error', onSocketError);
  316. wss.handleUpgrade(request, socket, head, function done(ws) {
  317. request.log.info("Authenticated request & upgraded to WebSocket connection");
  318. const wsLogger = createWebsocketLogger(request, resolvedAccount);
  319. // Start the connection:
  320. wss.emit('connection', ws, request, wsLogger);
  321. });
  322. });
  323. /**
  324. * @type {Object.<string, Array.<function(Object<string, any>): void>>}
  325. */
  326. const subs = {};
  327. const redisConfig = redisConfigFromEnv(process.env);
  328. const redisSubscribeClient = await createRedisClient(redisConfig);
  329. const redisClient = await createRedisClient(redisConfig);
  330. const { redisPrefix } = redisConfig;
  331. const metrics = setupMetrics(CHANNEL_NAMES, pgPool);
  332. // TODO: migrate all metrics to metrics.X.method() instead of just X.method()
  333. const {
  334. connectedClients,
  335. connectedChannels,
  336. redisSubscriptions,
  337. redisMessagesReceived,
  338. messagesSent,
  339. } = metrics;
  340. // When checking metrics in the browser, the favicon is requested this
  341. // prevents the request from falling through to the API Router, which would
  342. // error for this endpoint:
  343. app.get('/favicon.ico', (_req, res) => res.status(404).end());
  344. app.get('/api/v1/streaming/health', (_req, res) => {
  345. res.writeHead(200, { 'Content-Type': 'text/plain' });
  346. res.end('OK');
  347. });
  348. app.get('/metrics', async (req, res) => {
  349. try {
  350. res.set('Content-Type', metrics.register.contentType);
  351. res.end(await metrics.register.metrics());
  352. } catch (ex) {
  353. req.log.error(ex);
  354. res.status(500).end();
  355. }
  356. });
  357. /**
  358. * @param {string[]} channels
  359. * @returns {function(): void}
  360. */
  361. const subscriptionHeartbeat = channels => {
  362. const interval = 6 * 60;
  363. const tellSubscribed = () => {
  364. channels.forEach(channel => redisClient.set(`${redisPrefix}subscribed:${channel}`, '1', 'EX', interval * 3));
  365. };
  366. tellSubscribed();
  367. const heartbeat = setInterval(tellSubscribed, interval * 1000);
  368. return () => {
  369. clearInterval(heartbeat);
  370. };
  371. };
  372. /**
  373. * @param {string} channel
  374. * @param {string} message
  375. */
  376. const onRedisMessage = (channel, message) => {
  377. redisMessagesReceived.inc();
  378. const callbacks = subs[channel];
  379. logger.debug(`New message on channel ${redisPrefix}${channel}`);
  380. if (!callbacks) {
  381. return;
  382. }
  383. const json = parseJSON(message, null);
  384. if (!json) return;
  385. callbacks.forEach(callback => callback(json));
  386. };
  387. redisSubscribeClient.on("message", onRedisMessage);
  388. /**
  389. * @callback SubscriptionListener
  390. * @param {ReturnType<parseJSON>} json of the message
  391. * @returns void
  392. */
  393. /**
  394. * @param {string} channel
  395. * @param {SubscriptionListener} callback
  396. */
  397. const subscribe = (channel, callback) => {
  398. logger.debug(`Adding listener for ${channel}`);
  399. subs[channel] = subs[channel] || [];
  400. if (subs[channel].length === 0) {
  401. logger.debug(`Subscribe ${channel}`);
  402. redisSubscribeClient.subscribe(channel, (err, count) => {
  403. if (err) {
  404. logger.error(`Error subscribing to ${channel}`);
  405. } else if (typeof count === 'number') {
  406. redisSubscriptions.set(count);
  407. }
  408. });
  409. }
  410. subs[channel].push(callback);
  411. };
  412. /**
  413. * @param {string} channel
  414. * @param {SubscriptionListener} callback
  415. */
  416. const unsubscribe = (channel, callback) => {
  417. logger.debug(`Removing listener for ${channel}`);
  418. if (!subs[channel]) {
  419. return;
  420. }
  421. subs[channel] = subs[channel].filter(item => item !== callback);
  422. if (subs[channel].length === 0) {
  423. logger.debug(`Unsubscribe ${channel}`);
  424. redisSubscribeClient.unsubscribe(channel, (err, count) => {
  425. if (err) {
  426. logger.error(`Error unsubscribing to ${channel}`);
  427. } else if (typeof count === 'number') {
  428. redisSubscriptions.set(count);
  429. }
  430. });
  431. delete subs[channel];
  432. }
  433. };
  434. /**
  435. * @param {http.IncomingMessage & ResolvedAccount} req
  436. * @param {string[]} necessaryScopes
  437. * @returns {boolean}
  438. */
  439. const isInScope = (req, necessaryScopes) =>
  440. req.scopes.some(scope => necessaryScopes.includes(scope));
  441. /**
  442. * @param {string} token
  443. * @param {any} req
  444. * @returns {Promise<ResolvedAccount>}
  445. */
  446. const accountFromToken = (token, req) => new Promise((resolve, reject) => {
  447. pgPool.connect((err, client, done) => {
  448. if (err) {
  449. reject(err);
  450. return;
  451. }
  452. // @ts-ignore
  453. 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) => {
  454. done();
  455. if (err) {
  456. reject(err);
  457. return;
  458. }
  459. if (result.rows.length === 0) {
  460. reject(new AuthenticationError('Invalid access token'));
  461. return;
  462. }
  463. req.accessTokenId = result.rows[0].id;
  464. req.scopes = result.rows[0].scopes.split(' ');
  465. req.accountId = result.rows[0].account_id;
  466. req.chosenLanguages = result.rows[0].chosen_languages;
  467. req.deviceId = result.rows[0].device_id;
  468. resolve({
  469. accessTokenId: result.rows[0].id,
  470. scopes: result.rows[0].scopes.split(' '),
  471. accountId: result.rows[0].account_id,
  472. chosenLanguages: result.rows[0].chosen_languages,
  473. deviceId: result.rows[0].device_id
  474. });
  475. });
  476. });
  477. });
  478. /**
  479. * @param {any} req
  480. * @returns {Promise<ResolvedAccount>}
  481. */
  482. const accountFromRequest = (req) => new Promise((resolve, reject) => {
  483. const authorization = req.headers.authorization;
  484. const location = url.parse(req.url, true);
  485. const accessToken = location.query.access_token || req.headers['sec-websocket-protocol'];
  486. if (!authorization && !accessToken) {
  487. reject(new AuthenticationError('Missing access token'));
  488. return;
  489. }
  490. const token = authorization ? authorization.replace(/^Bearer /, '') : accessToken;
  491. resolve(accountFromToken(token, req));
  492. });
  493. /**
  494. * @param {any} req
  495. * @returns {string|undefined}
  496. */
  497. const channelNameFromPath = req => {
  498. const { path, query } = req;
  499. const onlyMedia = isTruthy(query.only_media);
  500. switch (path) {
  501. case '/api/v1/streaming/user':
  502. return 'user';
  503. case '/api/v1/streaming/user/notification':
  504. return 'user:notification';
  505. case '/api/v1/streaming/public':
  506. return onlyMedia ? 'public:media' : 'public';
  507. case '/api/v1/streaming/public/local':
  508. return onlyMedia ? 'public:local:media' : 'public:local';
  509. case '/api/v1/streaming/public/remote':
  510. return onlyMedia ? 'public:remote:media' : 'public:remote';
  511. case '/api/v1/streaming/hashtag':
  512. return 'hashtag';
  513. case '/api/v1/streaming/hashtag/local':
  514. return 'hashtag:local';
  515. case '/api/v1/streaming/direct':
  516. return 'direct';
  517. case '/api/v1/streaming/list':
  518. return 'list';
  519. default:
  520. return undefined;
  521. }
  522. };
  523. /**
  524. * @param {http.IncomingMessage & ResolvedAccount} req
  525. * @param {import('pino').Logger} logger
  526. * @param {string|undefined} channelName
  527. * @returns {Promise.<void>}
  528. */
  529. const checkScopes = (req, logger, channelName) => new Promise((resolve, reject) => {
  530. logger.debug(`Checking OAuth scopes for ${channelName}`);
  531. // When accessing public channels, no scopes are needed
  532. if (channelName && PUBLIC_CHANNELS.includes(channelName)) {
  533. resolve();
  534. return;
  535. }
  536. // The `read` scope has the highest priority, if the token has it
  537. // then it can access all streams
  538. const requiredScopes = ['read'];
  539. // When accessing specifically the notifications stream,
  540. // we need a read:notifications, while in all other cases,
  541. // we can allow access with read:statuses. Mind that the
  542. // user stream will not contain notifications unless
  543. // the token has either read or read:notifications scope
  544. // as well, this is handled separately.
  545. if (channelName === 'user:notification') {
  546. requiredScopes.push('read:notifications');
  547. } else {
  548. requiredScopes.push('read:statuses');
  549. }
  550. if (req.scopes && requiredScopes.some(requiredScope => req.scopes.includes(requiredScope))) {
  551. resolve();
  552. return;
  553. }
  554. reject(new AuthenticationError('Access token does not have the required scopes'));
  555. });
  556. /**
  557. * @typedef SystemMessageHandlers
  558. * @property {function(): void} onKill
  559. */
  560. /**
  561. * @param {any} req
  562. * @param {SystemMessageHandlers} eventHandlers
  563. * @returns {SubscriptionListener}
  564. */
  565. const createSystemMessageListener = (req, eventHandlers) => {
  566. return message => {
  567. if (!message?.event) {
  568. return;
  569. }
  570. const { event } = message;
  571. req.log.debug(`System message for ${req.accountId}: ${event}`);
  572. if (event === 'kill') {
  573. req.log.debug(`Closing connection for ${req.accountId} due to expired access token`);
  574. eventHandlers.onKill();
  575. } else if (event === 'filters_changed') {
  576. req.log.debug(`Invalidating filters cache for ${req.accountId}`);
  577. req.cachedFilters = null;
  578. }
  579. };
  580. };
  581. /**
  582. * @param {http.IncomingMessage & ResolvedAccount} req
  583. * @param {http.OutgoingMessage} res
  584. */
  585. const subscribeHttpToSystemChannel = (req, res) => {
  586. const accessTokenChannelId = `timeline:access_token:${req.accessTokenId}`;
  587. const systemChannelId = `timeline:system:${req.accountId}`;
  588. const listener = createSystemMessageListener(req, {
  589. onKill() {
  590. res.end();
  591. },
  592. });
  593. res.on('close', () => {
  594. unsubscribe(`${redisPrefix}${accessTokenChannelId}`, listener);
  595. unsubscribe(`${redisPrefix}${systemChannelId}`, listener);
  596. connectedChannels.labels({ type: 'eventsource', channel: 'system' }).dec(2);
  597. });
  598. subscribe(`${redisPrefix}${accessTokenChannelId}`, listener);
  599. subscribe(`${redisPrefix}${systemChannelId}`, listener);
  600. connectedChannels.labels({ type: 'eventsource', channel: 'system' }).inc(2);
  601. };
  602. /**
  603. * @param {any} req
  604. * @param {any} res
  605. * @param {function(Error=): void} next
  606. */
  607. const authenticationMiddleware = (req, res, next) => {
  608. if (req.method === 'OPTIONS') {
  609. next();
  610. return;
  611. }
  612. const channelName = channelNameFromPath(req);
  613. // If no channelName can be found for the request, then we should terminate
  614. // the connection, as there's nothing to stream back
  615. if (!channelName) {
  616. next(new RequestError('Unknown channel requested'));
  617. return;
  618. }
  619. accountFromRequest(req).then(() => checkScopes(req, req.log, channelName)).then(() => {
  620. subscribeHttpToSystemChannel(req, res);
  621. }).then(() => {
  622. next();
  623. }).catch(err => {
  624. next(err);
  625. });
  626. };
  627. /**
  628. * @param {Error} err
  629. * @param {any} req
  630. * @param {any} res
  631. * @param {function(Error=): void} next
  632. */
  633. const errorMiddleware = (err, req, res, next) => {
  634. req.log.error({ err }, err.toString());
  635. if (res.headersSent) {
  636. next(err);
  637. return;
  638. }
  639. const {statusCode, errorMessage } = extractErrorStatusAndMessage(err);
  640. res.writeHead(statusCode, { 'Content-Type': 'application/json' });
  641. res.end(JSON.stringify({ error: errorMessage }));
  642. };
  643. /**
  644. * @param {any[]} arr
  645. * @param {number=} shift
  646. * @returns {string}
  647. */
  648. // @ts-ignore
  649. const placeholders = (arr, shift = 0) => arr.map((_, i) => `$${i + 1 + shift}`).join(', ');
  650. /**
  651. * @param {string} listId
  652. * @param {any} req
  653. * @returns {Promise.<void>}
  654. */
  655. const authorizeListAccess = (listId, req) => new Promise((resolve, reject) => {
  656. const { accountId } = req;
  657. pgPool.connect((err, client, done) => {
  658. if (err) {
  659. reject();
  660. return;
  661. }
  662. // @ts-ignore
  663. client.query('SELECT id, account_id FROM lists WHERE id = $1 LIMIT 1', [listId], (err, result) => {
  664. done();
  665. if (err || result.rows.length === 0 || result.rows[0].account_id !== accountId) {
  666. reject();
  667. return;
  668. }
  669. resolve();
  670. });
  671. });
  672. });
  673. /**
  674. * @param {string[]} channelIds
  675. * @param {http.IncomingMessage & ResolvedAccount} req
  676. * @param {import('pino').Logger} log
  677. * @param {function(string, string): void} output
  678. * @param {undefined | function(string[], SubscriptionListener): void} attachCloseHandler
  679. * @param {'websocket' | 'eventsource'} destinationType
  680. * @param {boolean=} needsFiltering
  681. * @returns {SubscriptionListener}
  682. */
  683. const streamFrom = (channelIds, req, log, output, attachCloseHandler, destinationType, needsFiltering = false) => {
  684. log.info({ channelIds }, `Starting stream`);
  685. /**
  686. * @param {string} event
  687. * @param {object|string} payload
  688. */
  689. const transmit = (event, payload) => {
  690. // TODO: Replace "string"-based delete payloads with object payloads:
  691. const encodedPayload = typeof payload === 'object' ? JSON.stringify(payload) : payload;
  692. messagesSent.labels({ type: destinationType }).inc(1);
  693. log.debug({ event, payload }, `Transmitting ${event} to ${req.accountId}`);
  694. output(event, encodedPayload);
  695. };
  696. // The listener used to process each message off the redis subscription,
  697. // message here is an object with an `event` and `payload` property. Some
  698. // events also include a queued_at value, but this is being removed shortly.
  699. /** @type {SubscriptionListener} */
  700. const listener = message => {
  701. if (!message?.event || !message?.payload) {
  702. return;
  703. }
  704. const { event, payload } = message;
  705. // Streaming only needs to apply filtering to some channels and only to
  706. // some events. This is because majority of the filtering happens on the
  707. // Ruby on Rails side when producing the event for streaming.
  708. //
  709. // The only events that require filtering from the streaming server are
  710. // `update` and `status.update`, all other events are transmitted to the
  711. // client as soon as they're received (pass-through).
  712. //
  713. // The channels that need filtering are determined in the function
  714. // `channelNameToIds` defined below:
  715. if (!needsFiltering || (event !== 'update' && event !== 'status.update')) {
  716. transmit(event, payload);
  717. return;
  718. }
  719. // The rest of the logic from here on in this function is to handle
  720. // filtering of statuses:
  721. // Filter based on language:
  722. if (Array.isArray(req.chosenLanguages) && payload.language !== null && req.chosenLanguages.indexOf(payload.language) === -1) {
  723. log.debug(`Message ${payload.id} filtered by language (${payload.language})`);
  724. return;
  725. }
  726. // When the account is not logged in, it is not necessary to confirm the block or mute
  727. if (!req.accountId) {
  728. transmit(event, payload);
  729. return;
  730. }
  731. // Filter based on domain blocks, blocks, mutes, or custom filters:
  732. // @ts-ignore
  733. const targetAccountIds = [payload.account.id].concat(payload.mentions.map(item => item.id));
  734. const accountDomain = payload.account.acct.split('@')[1];
  735. // TODO: Move this logic out of the message handling loop
  736. pgPool.connect((err, client, releasePgConnection) => {
  737. if (err) {
  738. log.error(err);
  739. return;
  740. }
  741. const queries = [
  742. // @ts-ignore
  743. client.query(`SELECT 1
  744. FROM blocks
  745. WHERE (account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 2)}))
  746. OR (account_id = $2 AND target_account_id = $1)
  747. UNION
  748. SELECT 1
  749. FROM mutes
  750. WHERE account_id = $1
  751. AND target_account_id IN (${placeholders(targetAccountIds, 2)})`, [req.accountId, payload.account.id].concat(targetAccountIds)),
  752. ];
  753. if (accountDomain) {
  754. // @ts-ignore
  755. queries.push(client.query('SELECT 1 FROM account_domain_blocks WHERE account_id = $1 AND domain = $2', [req.accountId, accountDomain]));
  756. }
  757. // @ts-ignore
  758. if (!payload.filtered && !req.cachedFilters) {
  759. // @ts-ignore
  760. 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]));
  761. }
  762. Promise.all(queries).then(values => {
  763. releasePgConnection();
  764. // Handling blocks & mutes and domain blocks: If one of those applies,
  765. // then we don't transmit the payload of the event to the client
  766. if (values[0].rows.length > 0 || (accountDomain && values[1].rows.length > 0)) {
  767. return;
  768. }
  769. // If the payload already contains the `filtered` property, it means
  770. // that filtering has been applied on the ruby on rails side, as
  771. // such, we don't need to construct or apply the filters in streaming:
  772. if (Object.hasOwn(payload, "filtered")) {
  773. transmit(event, payload);
  774. return;
  775. }
  776. // Handling for constructing the custom filters and caching them on the request
  777. // TODO: Move this logic out of the message handling lifecycle
  778. // @ts-ignore
  779. if (!req.cachedFilters) {
  780. const filterRows = values[accountDomain ? 2 : 1].rows;
  781. // @ts-ignore
  782. req.cachedFilters = filterRows.reduce((cache, filter) => {
  783. if (cache[filter.id]) {
  784. cache[filter.id].keywords.push([filter.keyword, filter.whole_word]);
  785. } else {
  786. cache[filter.id] = {
  787. keywords: [[filter.keyword, filter.whole_word]],
  788. expires_at: filter.expires_at,
  789. filter: {
  790. id: filter.id,
  791. title: filter.title,
  792. context: filter.context,
  793. expires_at: filter.expires_at,
  794. // filter.filter_action is the value from the
  795. // custom_filters.action database column, it is an integer
  796. // representing a value in an enum defined by Ruby on Rails:
  797. //
  798. // enum { warn: 0, hide: 1 }
  799. filter_action: ['warn', 'hide'][filter.filter_action],
  800. },
  801. };
  802. }
  803. return cache;
  804. }, {});
  805. // Construct the regular expressions for the custom filters: This
  806. // needs to be done in a separate loop as the database returns one
  807. // filterRow per keyword, so we need all the keywords before
  808. // constructing the regular expression
  809. // @ts-ignore
  810. Object.keys(req.cachedFilters).forEach((key) => {
  811. // @ts-ignore
  812. req.cachedFilters[key].regexp = new RegExp(req.cachedFilters[key].keywords.map(([keyword, whole_word]) => {
  813. let expr = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  814. if (whole_word) {
  815. if (/^[\w]/.test(expr)) {
  816. expr = `\\b${expr}`;
  817. }
  818. if (/[\w]$/.test(expr)) {
  819. expr = `${expr}\\b`;
  820. }
  821. }
  822. return expr;
  823. }).join('|'), 'i');
  824. });
  825. }
  826. // Apply cachedFilters against the payload, constructing a
  827. // `filter_results` array of FilterResult entities
  828. // @ts-ignore
  829. if (req.cachedFilters) {
  830. const status = payload;
  831. // TODO: Calculate searchableContent in Ruby on Rails:
  832. // @ts-ignore
  833. 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');
  834. const searchableTextContent = JSDOM.fragment(searchableContent).textContent;
  835. const now = new Date();
  836. // @ts-ignore
  837. const filter_results = Object.values(req.cachedFilters).reduce((results, cachedFilter) => {
  838. // Check the filter hasn't expired before applying:
  839. if (cachedFilter.expires_at !== null && cachedFilter.expires_at < now) {
  840. return results;
  841. }
  842. // Just in-case JSDOM fails to find textContent in searchableContent
  843. if (!searchableTextContent) {
  844. return results;
  845. }
  846. const keyword_matches = searchableTextContent.match(cachedFilter.regexp);
  847. if (keyword_matches) {
  848. // results is an Array of FilterResult; status_matches is always
  849. // null as we only are only applying the keyword-based custom
  850. // filters, not the status-based custom filters.
  851. // https://docs.joinmastodon.org/entities/FilterResult/
  852. results.push({
  853. filter: cachedFilter.filter,
  854. keyword_matches,
  855. status_matches: null
  856. });
  857. }
  858. return results;
  859. }, []);
  860. // Send the payload + the FilterResults as the `filtered` property
  861. // to the streaming connection. To reach this code, the `event` must
  862. // have been either `update` or `status.update`, meaning the
  863. // `payload` is a Status entity, which has a `filtered` property:
  864. //
  865. // filtered: https://docs.joinmastodon.org/entities/Status/#filtered
  866. transmit(event, {
  867. ...payload,
  868. filtered: filter_results
  869. });
  870. } else {
  871. transmit(event, payload);
  872. }
  873. }).catch(err => {
  874. log.error(err);
  875. releasePgConnection();
  876. });
  877. });
  878. };
  879. channelIds.forEach(id => {
  880. subscribe(`${redisPrefix}${id}`, listener);
  881. });
  882. if (typeof attachCloseHandler === 'function') {
  883. attachCloseHandler(channelIds.map(id => `${redisPrefix}${id}`), listener);
  884. }
  885. return listener;
  886. };
  887. /**
  888. * @param {any} req
  889. * @param {any} res
  890. * @returns {function(string, string): void}
  891. */
  892. const streamToHttp = (req, res) => {
  893. const channelName = channelNameFromPath(req);
  894. connectedClients.labels({ type: 'eventsource' }).inc();
  895. // In theory we'll always have a channel name, but channelNameFromPath can return undefined:
  896. if (typeof channelName === 'string') {
  897. connectedChannels.labels({ type: 'eventsource', channel: channelName }).inc();
  898. }
  899. res.setHeader('Content-Type', 'text/event-stream');
  900. res.setHeader('Cache-Control', 'no-store');
  901. res.setHeader('Transfer-Encoding', 'chunked');
  902. res.write(':)\n');
  903. const heartbeat = setInterval(() => res.write(':thump\n'), 15000);
  904. req.on('close', () => {
  905. req.log.info({ accountId: req.accountId }, `Ending stream`);
  906. // We decrement these counters here instead of in streamHttpEnd as in that
  907. // method we don't have knowledge of the channel names
  908. connectedClients.labels({ type: 'eventsource' }).dec();
  909. // In theory we'll always have a channel name, but channelNameFromPath can return undefined:
  910. if (typeof channelName === 'string') {
  911. connectedChannels.labels({ type: 'eventsource', channel: channelName }).dec();
  912. }
  913. clearInterval(heartbeat);
  914. });
  915. return (event, payload) => {
  916. res.write(`event: ${event}\n`);
  917. res.write(`data: ${payload}\n\n`);
  918. };
  919. };
  920. /**
  921. * @param {any} req
  922. * @param {function(): void} [closeHandler]
  923. * @returns {function(string[], SubscriptionListener): void}
  924. */
  925. const streamHttpEnd = (req, closeHandler = undefined) => (ids, listener) => {
  926. req.on('close', () => {
  927. ids.forEach(id => {
  928. unsubscribe(id, listener);
  929. });
  930. if (closeHandler) {
  931. closeHandler();
  932. }
  933. });
  934. };
  935. /**
  936. * @param {http.IncomingMessage} req
  937. * @param {WebSocket} ws
  938. * @param {string[]} streamName
  939. * @returns {function(string, string): void}
  940. */
  941. const streamToWs = (req, ws, streamName) => (event, payload) => {
  942. if (ws.readyState !== ws.OPEN) {
  943. req.log.error('Tried writing to closed socket');
  944. return;
  945. }
  946. const message = JSON.stringify({ stream: streamName, event, payload });
  947. ws.send(message, (/** @type {Error|undefined} */ err) => {
  948. if (err) {
  949. req.log.error({err}, `Failed to send to websocket`);
  950. }
  951. });
  952. };
  953. /**
  954. * @param {http.ServerResponse} res
  955. */
  956. const httpNotFound = res => {
  957. res.writeHead(404, { 'Content-Type': 'application/json' });
  958. res.end(JSON.stringify({ error: 'Not found' }));
  959. };
  960. const api = express.Router();
  961. app.use(api);
  962. api.use(authenticationMiddleware);
  963. api.use(errorMiddleware);
  964. api.get('/api/v1/streaming/*', (req, res) => {
  965. const channelName = channelNameFromPath(req);
  966. // FIXME: In theory we'd never actually reach here due to
  967. // authenticationMiddleware catching this case, however, we need to refactor
  968. // how those middlewares work, so I'm adding the extra check in here.
  969. if (!channelName) {
  970. httpNotFound(res);
  971. return;
  972. }
  973. channelNameToIds(req, channelName, req.query).then(({ channelIds, options }) => {
  974. const onSend = streamToHttp(req, res);
  975. const onEnd = streamHttpEnd(req, subscriptionHeartbeat(channelIds));
  976. // @ts-ignore
  977. streamFrom(channelIds, req, req.log, onSend, onEnd, 'eventsource', options.needsFiltering);
  978. }).catch(err => {
  979. const {statusCode, errorMessage } = extractErrorStatusAndMessage(err);
  980. res.log.info({ err }, 'Eventsource subscription error');
  981. res.writeHead(statusCode, { 'Content-Type': 'application/json' });
  982. res.end(JSON.stringify({ error: errorMessage }));
  983. });
  984. });
  985. /**
  986. * @typedef StreamParams
  987. * @property {string} [tag]
  988. * @property {string} [list]
  989. * @property {string} [only_media]
  990. */
  991. /**
  992. * @param {any} req
  993. * @returns {string[]}
  994. */
  995. const channelsForUserStream = req => {
  996. const arr = [`timeline:${req.accountId}`];
  997. if (isInScope(req, ['crypto']) && req.deviceId) {
  998. arr.push(`timeline:${req.accountId}:${req.deviceId}`);
  999. }
  1000. if (isInScope(req, ['read', 'read:notifications'])) {
  1001. arr.push(`timeline:${req.accountId}:notifications`);
  1002. }
  1003. return arr;
  1004. };
  1005. /**
  1006. * @param {any} req
  1007. * @param {string} name
  1008. * @param {StreamParams} params
  1009. * @returns {Promise.<{ channelIds: string[], options: { needsFiltering: boolean } }>}
  1010. */
  1011. const channelNameToIds = (req, name, params) => new Promise((resolve, reject) => {
  1012. switch (name) {
  1013. case 'user':
  1014. resolve({
  1015. channelIds: channelsForUserStream(req),
  1016. options: { needsFiltering: false },
  1017. });
  1018. break;
  1019. case 'user:notification':
  1020. resolve({
  1021. channelIds: [`timeline:${req.accountId}:notifications`],
  1022. options: { needsFiltering: false },
  1023. });
  1024. break;
  1025. case 'public':
  1026. resolve({
  1027. channelIds: ['timeline:public'],
  1028. options: { needsFiltering: true },
  1029. });
  1030. break;
  1031. case 'public:local':
  1032. resolve({
  1033. channelIds: ['timeline:public:local'],
  1034. options: { needsFiltering: true },
  1035. });
  1036. break;
  1037. case 'public:remote':
  1038. resolve({
  1039. channelIds: ['timeline:public:remote'],
  1040. options: { needsFiltering: true },
  1041. });
  1042. break;
  1043. case 'public:media':
  1044. resolve({
  1045. channelIds: ['timeline:public:media'],
  1046. options: { needsFiltering: true },
  1047. });
  1048. break;
  1049. case 'public:local:media':
  1050. resolve({
  1051. channelIds: ['timeline:public:local:media'],
  1052. options: { needsFiltering: true },
  1053. });
  1054. break;
  1055. case 'public:remote:media':
  1056. resolve({
  1057. channelIds: ['timeline:public:remote:media'],
  1058. options: { needsFiltering: true },
  1059. });
  1060. break;
  1061. case 'direct':
  1062. resolve({
  1063. channelIds: [`timeline:direct:${req.accountId}`],
  1064. options: { needsFiltering: false },
  1065. });
  1066. break;
  1067. case 'hashtag':
  1068. if (!params.tag) {
  1069. reject(new RequestError('Missing tag name parameter'));
  1070. } else {
  1071. resolve({
  1072. channelIds: [`timeline:hashtag:${normalizeHashtag(params.tag)}`],
  1073. options: { needsFiltering: true },
  1074. });
  1075. }
  1076. break;
  1077. case 'hashtag:local':
  1078. if (!params.tag) {
  1079. reject(new RequestError('Missing tag name parameter'));
  1080. } else {
  1081. resolve({
  1082. channelIds: [`timeline:hashtag:${normalizeHashtag(params.tag)}:local`],
  1083. options: { needsFiltering: true },
  1084. });
  1085. }
  1086. break;
  1087. case 'list':
  1088. if (!params.list) {
  1089. reject(new RequestError('Missing list name parameter'));
  1090. return;
  1091. }
  1092. authorizeListAccess(params.list, req).then(() => {
  1093. resolve({
  1094. channelIds: [`timeline:list:${params.list}`],
  1095. options: { needsFiltering: false },
  1096. });
  1097. }).catch(() => {
  1098. reject(new AuthenticationError('Not authorized to stream this list'));
  1099. });
  1100. break;
  1101. default:
  1102. reject(new RequestError('Unknown stream type'));
  1103. }
  1104. });
  1105. /**
  1106. * @param {string} channelName
  1107. * @param {StreamParams} params
  1108. * @returns {string[]}
  1109. */
  1110. const streamNameFromChannelName = (channelName, params) => {
  1111. if (channelName === 'list' && params.list) {
  1112. return [channelName, params.list];
  1113. } else if (['hashtag', 'hashtag:local'].includes(channelName) && params.tag) {
  1114. return [channelName, params.tag];
  1115. } else {
  1116. return [channelName];
  1117. }
  1118. };
  1119. /**
  1120. * @typedef WebSocketSession
  1121. * @property {WebSocket & { isAlive: boolean}} websocket
  1122. * @property {http.IncomingMessage & ResolvedAccount} request
  1123. * @property {import('pino').Logger} logger
  1124. * @property {Object.<string, { channelName: string, listener: SubscriptionListener, stopHeartbeat: function(): void }>} subscriptions
  1125. */
  1126. /**
  1127. * @param {WebSocketSession} session
  1128. * @param {string} channelName
  1129. * @param {StreamParams} params
  1130. * @returns {void}
  1131. */
  1132. const subscribeWebsocketToChannel = ({ websocket, request, logger, subscriptions }, channelName, params) => {
  1133. checkScopes(request, logger, channelName).then(() => channelNameToIds(request, channelName, params)).then(({
  1134. channelIds,
  1135. options,
  1136. }) => {
  1137. if (subscriptions[channelIds.join(';')]) {
  1138. return;
  1139. }
  1140. const onSend = streamToWs(request, websocket, streamNameFromChannelName(channelName, params));
  1141. const stopHeartbeat = subscriptionHeartbeat(channelIds);
  1142. const listener = streamFrom(channelIds, request, logger, onSend, undefined, 'websocket', options.needsFiltering);
  1143. connectedChannels.labels({ type: 'websocket', channel: channelName }).inc();
  1144. subscriptions[channelIds.join(';')] = {
  1145. channelName,
  1146. listener,
  1147. stopHeartbeat,
  1148. };
  1149. }).catch(err => {
  1150. const {statusCode, errorMessage } = extractErrorStatusAndMessage(err);
  1151. logger.error({ err }, 'Websocket subscription error');
  1152. // If we have a socket that is alive and open still, send the error back to the client:
  1153. if (websocket.isAlive && websocket.readyState === websocket.OPEN) {
  1154. websocket.send(JSON.stringify({
  1155. error: errorMessage,
  1156. status: statusCode
  1157. }));
  1158. }
  1159. });
  1160. };
  1161. /**
  1162. * @param {WebSocketSession} session
  1163. * @param {string[]} channelIds
  1164. */
  1165. const removeSubscription = ({ request, logger, subscriptions }, channelIds) => {
  1166. logger.info({ channelIds, accountId: request.accountId }, `Ending stream`);
  1167. const subscription = subscriptions[channelIds.join(';')];
  1168. if (!subscription) {
  1169. return;
  1170. }
  1171. channelIds.forEach(channelId => {
  1172. unsubscribe(`${redisPrefix}${channelId}`, subscription.listener);
  1173. });
  1174. connectedChannels.labels({ type: 'websocket', channel: subscription.channelName }).dec();
  1175. subscription.stopHeartbeat();
  1176. delete subscriptions[channelIds.join(';')];
  1177. };
  1178. /**
  1179. * @param {WebSocketSession} session
  1180. * @param {string} channelName
  1181. * @param {StreamParams} params
  1182. * @returns {void}
  1183. */
  1184. const unsubscribeWebsocketFromChannel = (session, channelName, params) => {
  1185. const { websocket, request, logger } = session;
  1186. channelNameToIds(request, channelName, params).then(({ channelIds }) => {
  1187. removeSubscription(session, channelIds);
  1188. }).catch(err => {
  1189. logger.error({err}, 'Websocket unsubscribe error');
  1190. // If we have a socket that is alive and open still, send the error back to the client:
  1191. if (websocket.isAlive && websocket.readyState === websocket.OPEN) {
  1192. // TODO: Use a better error response here
  1193. websocket.send(JSON.stringify({ error: "Error unsubscribing from channel" }));
  1194. }
  1195. });
  1196. };
  1197. /**
  1198. * @param {WebSocketSession} session
  1199. */
  1200. const subscribeWebsocketToSystemChannel = ({ websocket, request, subscriptions }) => {
  1201. const accessTokenChannelId = `timeline:access_token:${request.accessTokenId}`;
  1202. const systemChannelId = `timeline:system:${request.accountId}`;
  1203. const listener = createSystemMessageListener(request, {
  1204. onKill() {
  1205. websocket.close();
  1206. },
  1207. });
  1208. subscribe(`${redisPrefix}${accessTokenChannelId}`, listener);
  1209. subscribe(`${redisPrefix}${systemChannelId}`, listener);
  1210. subscriptions[accessTokenChannelId] = {
  1211. channelName: 'system',
  1212. listener,
  1213. stopHeartbeat: () => {
  1214. },
  1215. };
  1216. subscriptions[systemChannelId] = {
  1217. channelName: 'system',
  1218. listener,
  1219. stopHeartbeat: () => {
  1220. },
  1221. };
  1222. connectedChannels.labels({ type: 'websocket', channel: 'system' }).inc(2);
  1223. };
  1224. /**
  1225. * @param {WebSocket & { isAlive: boolean }} ws
  1226. * @param {http.IncomingMessage & ResolvedAccount} req
  1227. * @param {import('pino').Logger} log
  1228. */
  1229. function onConnection(ws, req, log) {
  1230. // Note: url.parse could throw, which would terminate the connection, so we
  1231. // increment the connected clients metric straight away when we establish
  1232. // the connection, without waiting:
  1233. connectedClients.labels({ type: 'websocket' }).inc();
  1234. // Setup connection keep-alive state:
  1235. ws.isAlive = true;
  1236. ws.on('pong', () => {
  1237. ws.isAlive = true;
  1238. });
  1239. /**
  1240. * @type {WebSocketSession}
  1241. */
  1242. const session = {
  1243. websocket: ws,
  1244. request: req,
  1245. logger: log,
  1246. subscriptions: {},
  1247. };
  1248. ws.on('close', function onWebsocketClose() {
  1249. const subscriptions = Object.keys(session.subscriptions);
  1250. subscriptions.forEach(channelIds => {
  1251. removeSubscription(session, channelIds.split(';'));
  1252. });
  1253. // Decrement the metrics for connected clients:
  1254. connectedClients.labels({ type: 'websocket' }).dec();
  1255. // We need to unassign the session object as to ensure it correctly gets
  1256. // garbage collected, without doing this we could accidentally hold on to
  1257. // references to the websocket, the request, and the logger, causing
  1258. // memory leaks.
  1259. // This is commented out because `delete` only operated on object properties
  1260. // It needs to be replaced by `session = undefined`, but it requires every calls to
  1261. // `session` to check for it, thus a significant refactor
  1262. // delete session;
  1263. });
  1264. // Note: immediately after the `error` event is emitted, the `close` event
  1265. // is emitted. As such, all we need to do is log the error here.
  1266. ws.on('error', (/** @type {Error} */ err) => {
  1267. log.error(err);
  1268. });
  1269. ws.on('message', (data, isBinary) => {
  1270. if (isBinary) {
  1271. log.warn('Received binary data, closing connection');
  1272. ws.close(1003, 'The mastodon streaming server does not support binary messages');
  1273. return;
  1274. }
  1275. const message = data.toString('utf8');
  1276. const json = parseJSON(message, session.request);
  1277. if (!json) return;
  1278. const { type, stream, ...params } = json;
  1279. if (type === 'subscribe') {
  1280. subscribeWebsocketToChannel(session, firstParam(stream), params);
  1281. } else if (type === 'unsubscribe') {
  1282. unsubscribeWebsocketFromChannel(session, firstParam(stream), params);
  1283. } else {
  1284. // Unknown action type
  1285. }
  1286. });
  1287. subscribeWebsocketToSystemChannel(session);
  1288. // Parse the URL for the connection arguments (if supplied), url.parse can throw:
  1289. const location = req.url && url.parse(req.url, true);
  1290. if (location && location.query.stream) {
  1291. subscribeWebsocketToChannel(session, firstParam(location.query.stream), location.query);
  1292. }
  1293. }
  1294. wss.on('connection', onConnection);
  1295. setInterval(() => {
  1296. wss.clients.forEach(ws => {
  1297. // @ts-ignore
  1298. if (ws.isAlive === false) {
  1299. ws.terminate();
  1300. return;
  1301. }
  1302. // @ts-ignore
  1303. ws.isAlive = false;
  1304. ws.ping('', false);
  1305. });
  1306. }, 30000);
  1307. attachServerWithConfig(server, address => {
  1308. logger.info(`Streaming API now listening on ${address}`);
  1309. });
  1310. const onExit = () => {
  1311. server.close();
  1312. process.exit(0);
  1313. };
  1314. /** @param {Error} err */
  1315. const onError = (err) => {
  1316. logger.error(err);
  1317. server.close();
  1318. process.exit(0);
  1319. };
  1320. process.on('SIGINT', onExit);
  1321. process.on('SIGTERM', onExit);
  1322. process.on('exit', onExit);
  1323. process.on('uncaughtException', onError);
  1324. };
  1325. /**
  1326. * @param {any} server
  1327. * @param {function(string): void} [onSuccess]
  1328. */
  1329. const attachServerWithConfig = (server, onSuccess) => {
  1330. if (process.env.SOCKET || process.env.PORT && isNaN(+process.env.PORT)) {
  1331. server.listen(process.env.SOCKET || process.env.PORT, () => {
  1332. if (onSuccess) {
  1333. fs.chmodSync(server.address(), 0o666);
  1334. onSuccess(server.address());
  1335. }
  1336. });
  1337. } else {
  1338. server.listen(+(process.env.PORT || 4000), process.env.BIND || '127.0.0.1', () => {
  1339. if (onSuccess) {
  1340. onSuccess(`${server.address().address}:${server.address().port}`);
  1341. }
  1342. });
  1343. }
  1344. };
  1345. startServer();