Router.js 14 KB

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