Router.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. const metrics = require('@overleaf/metrics')
  2. const logger = require('@overleaf/logger')
  3. const settings = require('@overleaf/settings')
  4. const WebsocketController = require('./WebsocketController')
  5. const HttpController = require('./HttpController')
  6. const HttpApiController = require('./HttpApiController')
  7. const WebsocketAddressManager = require('./WebsocketAddressManager')
  8. const bodyParser = require('body-parser')
  9. const base64id = require('base64id')
  10. const { UnexpectedArgumentsError } = require('./Errors')
  11. const Joi = require('@hapi/joi')
  12. const HOSTNAME = require('os').hostname()
  13. const JOI_OBJECT_ID = Joi.string()
  14. .required()
  15. .regex(/^[0-9a-f]{24}$/)
  16. .message('invalid id')
  17. let Router
  18. module.exports = Router = {
  19. _handleError(callback, error, client, method, attrs) {
  20. attrs = attrs || {}
  21. for (const key of ['project_id', 'user_id']) {
  22. attrs[key] = attrs[key] || client.ol_context[key]
  23. }
  24. attrs.client_id = client.id
  25. attrs.err = error
  26. attrs.method = method
  27. if (Joi.isError(error)) {
  28. logger.info(attrs, 'validation error')
  29. let message = 'invalid'
  30. try {
  31. message = error.details[0].message
  32. } catch (e) {
  33. // ignore unexpected errors
  34. logger.warn({ error, e }, 'unexpected validation error')
  35. }
  36. const serializedError = { message }
  37. metrics.inc('validation-error', 1, {
  38. status: method,
  39. })
  40. callback(serializedError)
  41. } else if (error.name === 'CodedError') {
  42. logger.warn(attrs, error.message)
  43. const serializedError = { message: error.message, code: error.info.code }
  44. callback(serializedError)
  45. } else if (error.message === 'unexpected arguments') {
  46. // the payload might be very large; put it on level debug
  47. logger.debug(attrs, 'unexpected arguments')
  48. metrics.inc('unexpected-arguments', 1, { status: method })
  49. const serializedError = { message: error.message }
  50. callback(serializedError)
  51. } else if (error.message === 'no project_id found on client') {
  52. logger.debug(attrs, error.message)
  53. const serializedError = { message: error.message }
  54. callback(serializedError)
  55. } else if (
  56. [
  57. 'not authorized',
  58. 'joinLeaveEpoch mismatch',
  59. 'doc updater could not load requested ops',
  60. 'no project_id found on client',
  61. 'cannot join multiple projects',
  62. ].includes(error.message)
  63. ) {
  64. logger.warn(attrs, error.message)
  65. const serializedError = { message: error.message }
  66. callback(serializedError)
  67. } else {
  68. logger.error(attrs, `server side error in ${method}`)
  69. // Don't return raw error to prevent leaking server side info
  70. const serializedError = {
  71. message: 'Something went wrong in real-time service',
  72. }
  73. callback(serializedError)
  74. }
  75. if (attrs.disconnect) {
  76. setTimeout(function () {
  77. client.disconnect()
  78. }, 100)
  79. }
  80. },
  81. _handleInvalidArguments(client, method, args) {
  82. const error = new UnexpectedArgumentsError()
  83. let callback = args[args.length - 1]
  84. if (typeof callback !== 'function') {
  85. callback = function () {}
  86. }
  87. const attrs = { arguments: args }
  88. Router._handleError(callback, error, client, method, attrs)
  89. },
  90. configure(app, io, session) {
  91. app.set('io', io)
  92. if (settings.behindProxy) {
  93. app.set('trust proxy', settings.trustedProxyIps)
  94. }
  95. const websocketAddressManager = new WebsocketAddressManager(
  96. settings.behindProxy,
  97. settings.trustedProxyIps
  98. )
  99. app.get('/clients', HttpController.getConnectedClients)
  100. app.get('/clients/:client_id', HttpController.getConnectedClient)
  101. app.post(
  102. '/project/:project_id/message/:message',
  103. bodyParser.json({ limit: '5mb' }),
  104. HttpApiController.sendMessage
  105. )
  106. app.post('/drain', HttpApiController.startDrain)
  107. app.post(
  108. '/client/:client_id/disconnect',
  109. HttpApiController.disconnectClient
  110. )
  111. session.on('connection', function (error, client, session) {
  112. // init client context, we may access it in Router._handleError before
  113. // setting any values
  114. client.ol_context = {}
  115. // bail out from joinDoc when a parallel joinDoc or leaveDoc is running
  116. client.joinLeaveEpoch = 0
  117. if (client) {
  118. client.on('error', function (err) {
  119. logger.err({ clientErr: err }, 'socket.io client error')
  120. if (client.connected) {
  121. client.emit('reconnectGracefully')
  122. client.disconnect()
  123. }
  124. })
  125. }
  126. if (settings.shutDownInProgress) {
  127. client.emit('connectionRejected', { message: 'retry' })
  128. client.disconnect()
  129. return
  130. }
  131. if (
  132. client &&
  133. error &&
  134. error.message.match(/could not look up session by key/)
  135. ) {
  136. logger.warn(
  137. { err: error, client: !!client, session: !!session },
  138. 'invalid session'
  139. )
  140. // tell the client to reauthenticate if it has an invalid session key
  141. client.emit('connectionRejected', { message: 'invalid session' })
  142. client.disconnect()
  143. return
  144. }
  145. if (error) {
  146. logger.err(
  147. { err: error, client: !!client, session: !!session },
  148. 'error when client connected'
  149. )
  150. if (client) {
  151. client.emit('connectionRejected', { message: 'error' })
  152. }
  153. if (client) {
  154. client.disconnect()
  155. }
  156. return
  157. }
  158. // send positive confirmation that the client has a valid connection
  159. client.publicId = 'P.' + base64id.generateId()
  160. client.emit('connectionAccepted', null, client.publicId)
  161. client.remoteIp = websocketAddressManager.getRemoteIp(client.handshake)
  162. const headers = client.handshake && client.handshake.headers
  163. client.userAgent = headers && headers['user-agent']
  164. metrics.inc('socket-io.connection', 1, { status: client.transport })
  165. metrics.gauge('socket-io.clients', io.sockets.clients().length)
  166. logger.debug({ session, clientId: client.id }, 'client connected')
  167. let user
  168. if (session && session.passport && session.passport.user) {
  169. ;({ user } = session.passport)
  170. } else if (session && session.user) {
  171. ;({ user } = session)
  172. } else {
  173. user = { _id: 'anonymous-user' }
  174. }
  175. if (settings.exposeHostname) {
  176. client.on('debug.getHostname', function (callback) {
  177. if (typeof callback !== 'function') {
  178. return Router._handleInvalidArguments(
  179. client,
  180. 'debug.getHostname',
  181. arguments
  182. )
  183. }
  184. callback(HOSTNAME)
  185. })
  186. }
  187. client.on('joinProject', function (data, callback) {
  188. data = data || {}
  189. if (typeof callback !== 'function') {
  190. return Router._handleInvalidArguments(
  191. client,
  192. 'joinProject',
  193. arguments
  194. )
  195. }
  196. try {
  197. Joi.assert(
  198. data,
  199. Joi.object({
  200. project_id: JOI_OBJECT_ID,
  201. anonymousAccessToken: Joi.string(),
  202. })
  203. )
  204. } catch (error) {
  205. return Router._handleError(callback, error, client, 'joinProject', {
  206. disconnect: 1,
  207. })
  208. }
  209. const { project_id: projectId, anonymousAccessToken } = data
  210. // only allow connection to a single project
  211. if (
  212. client.ol_current_project_id &&
  213. projectId !== client.ol_current_project_id
  214. ) {
  215. return Router._handleError(
  216. callback,
  217. new Error('cannot join multiple projects'),
  218. client,
  219. 'joinProject',
  220. { disconnect: 1 }
  221. )
  222. }
  223. client.ol_current_project_id = projectId
  224. if (anonymousAccessToken) {
  225. user.anonymousAccessToken = anonymousAccessToken
  226. }
  227. WebsocketController.joinProject(
  228. client,
  229. user,
  230. projectId,
  231. function (err, ...args) {
  232. if (err) {
  233. Router._handleError(callback, err, client, 'joinProject', {
  234. project_id: projectId,
  235. user_id: user._id,
  236. })
  237. } else {
  238. callback(null, ...args)
  239. }
  240. }
  241. )
  242. })
  243. client.on('disconnect', function () {
  244. metrics.inc('socket-io.disconnect', 1, { status: client.transport })
  245. metrics.gauge('socket-io.clients', io.sockets.clients().length)
  246. WebsocketController.leaveProject(io, client, function (err) {
  247. if (err) {
  248. Router._handleError(function () {}, err, client, 'leaveProject')
  249. }
  250. })
  251. })
  252. // Variadic. The possible arguments:
  253. // doc_id, callback
  254. // doc_id, fromVersion, callback
  255. // doc_id, options, callback
  256. // doc_id, fromVersion, options, callback
  257. client.on('joinDoc', function (docId, fromVersion, options, callback) {
  258. if (typeof fromVersion === 'function' && !options) {
  259. callback = fromVersion
  260. fromVersion = -1
  261. options = {}
  262. } else if (
  263. typeof fromVersion === 'number' &&
  264. typeof options === 'function'
  265. ) {
  266. callback = options
  267. options = {}
  268. } else if (
  269. typeof fromVersion === 'object' &&
  270. typeof options === 'function'
  271. ) {
  272. callback = options
  273. options = fromVersion
  274. fromVersion = -1
  275. } else if (
  276. typeof fromVersion === 'number' &&
  277. typeof options === 'object' &&
  278. typeof callback === 'function'
  279. ) {
  280. // Called with 4 args, things are as expected
  281. } else {
  282. return Router._handleInvalidArguments(client, 'joinDoc', arguments)
  283. }
  284. try {
  285. Joi.assert(
  286. { doc_id: docId, fromVersion, options },
  287. Joi.object({
  288. doc_id: JOI_OBJECT_ID,
  289. fromVersion: Joi.number().integer(),
  290. options: Joi.object().required(),
  291. })
  292. )
  293. } catch (error) {
  294. return Router._handleError(callback, error, client, 'joinDoc', {
  295. disconnect: 1,
  296. })
  297. }
  298. WebsocketController.joinDoc(
  299. client,
  300. docId,
  301. fromVersion,
  302. options,
  303. function (err, ...args) {
  304. if (err) {
  305. Router._handleError(callback, err, client, 'joinDoc', {
  306. doc_id: docId,
  307. fromVersion,
  308. })
  309. } else {
  310. callback(null, ...args)
  311. }
  312. }
  313. )
  314. })
  315. client.on('leaveDoc', function (docId, callback) {
  316. if (typeof callback !== 'function') {
  317. return Router._handleInvalidArguments(client, 'leaveDoc', arguments)
  318. }
  319. try {
  320. Joi.assert(docId, JOI_OBJECT_ID)
  321. } catch (error) {
  322. return Router._handleError(callback, error, client, 'joinDoc', {
  323. disconnect: 1,
  324. })
  325. }
  326. WebsocketController.leaveDoc(client, docId, function (err, ...args) {
  327. if (err) {
  328. Router._handleError(callback, err, client, 'leaveDoc', {
  329. doc_id: docId,
  330. })
  331. } else {
  332. callback(null, ...args)
  333. }
  334. })
  335. })
  336. client.on('clientTracking.getConnectedUsers', function (callback) {
  337. if (typeof callback !== 'function') {
  338. return Router._handleInvalidArguments(
  339. client,
  340. 'clientTracking.getConnectedUsers',
  341. arguments
  342. )
  343. }
  344. WebsocketController.getConnectedUsers(client, function (err, users) {
  345. if (err) {
  346. Router._handleError(
  347. callback,
  348. err,
  349. client,
  350. 'clientTracking.getConnectedUsers'
  351. )
  352. } else {
  353. callback(null, users)
  354. }
  355. })
  356. })
  357. client.on(
  358. 'clientTracking.updatePosition',
  359. function (cursorData, callback) {
  360. if (!callback) {
  361. callback = function () {
  362. // NOTE: The frontend does not pass any callback to socket.io.
  363. // Any error is already logged via Router._handleError.
  364. }
  365. }
  366. if (typeof callback !== 'function') {
  367. return Router._handleInvalidArguments(
  368. client,
  369. 'clientTracking.updatePosition',
  370. arguments
  371. )
  372. }
  373. WebsocketController.updateClientPosition(
  374. client,
  375. cursorData,
  376. function (err) {
  377. if (err) {
  378. Router._handleError(
  379. callback,
  380. err,
  381. client,
  382. 'clientTracking.updatePosition'
  383. )
  384. } else {
  385. callback()
  386. }
  387. }
  388. )
  389. }
  390. )
  391. client.on('applyOtUpdate', function (docId, update, callback) {
  392. if (typeof callback !== 'function') {
  393. return Router._handleInvalidArguments(
  394. client,
  395. 'applyOtUpdate',
  396. arguments
  397. )
  398. }
  399. try {
  400. Joi.assert(
  401. { doc_id: docId, update },
  402. Joi.object({
  403. doc_id: JOI_OBJECT_ID,
  404. update: Joi.object().required(),
  405. })
  406. )
  407. } catch (error) {
  408. return Router._handleError(callback, error, client, 'applyOtUpdate', {
  409. disconnect: 1,
  410. })
  411. }
  412. WebsocketController.applyOtUpdate(
  413. client,
  414. docId,
  415. update,
  416. function (err) {
  417. if (err) {
  418. Router._handleError(callback, err, client, 'applyOtUpdate', {
  419. doc_id: docId,
  420. update,
  421. })
  422. } else {
  423. callback()
  424. }
  425. }
  426. )
  427. })
  428. })
  429. },
  430. }