index.js 49 KB

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