WebsocketController.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* eslint-disable
  2. camelcase,
  3. */
  4. const logger = require('logger-sharelatex')
  5. const metrics = require('metrics-sharelatex')
  6. const WebApiManager = require('./WebApiManager')
  7. const AuthorizationManager = require('./AuthorizationManager')
  8. const DocumentUpdaterManager = require('./DocumentUpdaterManager')
  9. const ConnectedUsersManager = require('./ConnectedUsersManager')
  10. const WebsocketLoadBalancer = require('./WebsocketLoadBalancer')
  11. const RoomManager = require('./RoomManager')
  12. let WebsocketController
  13. module.exports = WebsocketController = {
  14. // If the protocol version changes when the client reconnects,
  15. // it will force a full refresh of the page. Useful for non-backwards
  16. // compatible protocol changes. Use only in extreme need.
  17. PROTOCOL_VERSION: 2,
  18. joinProject(client, user, project_id, callback) {
  19. if (client.disconnected) {
  20. metrics.inc('editor.join-project.disconnected', 1, {
  21. status: 'immediately'
  22. })
  23. return callback()
  24. }
  25. const user_id = user._id
  26. logger.log(
  27. { user_id, project_id, client_id: client.id },
  28. 'user joining project'
  29. )
  30. metrics.inc('editor.join-project')
  31. WebApiManager.joinProject(project_id, user, function (
  32. error,
  33. project,
  34. privilegeLevel,
  35. isRestrictedUser
  36. ) {
  37. if (error) {
  38. return callback(error)
  39. }
  40. if (client.disconnected) {
  41. metrics.inc('editor.join-project.disconnected', 1, {
  42. status: 'after-web-api-call'
  43. })
  44. return callback()
  45. }
  46. if (!privilegeLevel) {
  47. const err = new Error('not authorized')
  48. logger.warn(
  49. { err, project_id, user_id, client_id: client.id },
  50. 'user is not authorized to join project'
  51. )
  52. return callback(err)
  53. }
  54. client.ol_context = {}
  55. client.ol_context.privilege_level = privilegeLevel
  56. client.ol_context.user_id = user_id
  57. client.ol_context.project_id = project_id
  58. client.ol_context.owner_id = project.owner && project.owner._id
  59. client.ol_context.first_name = user.first_name
  60. client.ol_context.last_name = user.last_name
  61. client.ol_context.email = user.email
  62. client.ol_context.connected_time = new Date()
  63. client.ol_context.signup_date = user.signUpDate
  64. client.ol_context.login_count = user.loginCount
  65. client.ol_context.is_restricted_user = !!isRestrictedUser
  66. RoomManager.joinProject(client, project_id, function (err) {
  67. if (err) {
  68. return callback(err)
  69. }
  70. logger.log(
  71. { user_id, project_id, client_id: client.id },
  72. 'user joined project'
  73. )
  74. callback(
  75. null,
  76. project,
  77. privilegeLevel,
  78. WebsocketController.PROTOCOL_VERSION
  79. )
  80. })
  81. // No need to block for setting the user as connected in the cursor tracking
  82. ConnectedUsersManager.updateUserPosition(
  83. project_id,
  84. client.publicId,
  85. user,
  86. null,
  87. function () {}
  88. )
  89. })
  90. },
  91. // We want to flush a project if there are no more (local) connected clients
  92. // but we need to wait for the triggering client to disconnect. How long we wait
  93. // is determined by FLUSH_IF_EMPTY_DELAY.
  94. FLUSH_IF_EMPTY_DELAY: 500, // ms
  95. leaveProject(io, client, callback) {
  96. const { project_id, user_id } = client.ol_context
  97. if (!project_id) {
  98. return callback()
  99. } // client did not join project
  100. metrics.inc('editor.leave-project')
  101. logger.log(
  102. { project_id, user_id, client_id: client.id },
  103. 'client leaving project'
  104. )
  105. WebsocketLoadBalancer.emitToRoom(
  106. project_id,
  107. 'clientTracking.clientDisconnected',
  108. client.publicId
  109. )
  110. // We can do this in the background
  111. ConnectedUsersManager.markUserAsDisconnected(
  112. project_id,
  113. client.publicId,
  114. function (err) {
  115. if (err) {
  116. logger.error(
  117. { err, project_id, user_id, client_id: client.id },
  118. 'error marking client as disconnected'
  119. )
  120. }
  121. }
  122. )
  123. RoomManager.leaveProjectAndDocs(client)
  124. setTimeout(function () {
  125. const remainingClients = io.sockets.clients(project_id)
  126. if (remainingClients.length === 0) {
  127. // Flush project in the background
  128. DocumentUpdaterManager.flushProjectToMongoAndDelete(
  129. project_id,
  130. function (err) {
  131. if (err) {
  132. logger.error(
  133. { err, project_id, user_id, client_id: client.id },
  134. 'error flushing to doc updater after leaving project'
  135. )
  136. }
  137. }
  138. )
  139. }
  140. callback()
  141. }, WebsocketController.FLUSH_IF_EMPTY_DELAY)
  142. },
  143. joinDoc(client, doc_id, fromVersion, options, callback) {
  144. if (client.disconnected) {
  145. metrics.inc('editor.join-doc.disconnected', 1, { status: 'immediately' })
  146. return callback()
  147. }
  148. metrics.inc('editor.join-doc')
  149. const { project_id, user_id, is_restricted_user } = client.ol_context
  150. if (!project_id) {
  151. return callback(new Error('no project_id found on client'))
  152. }
  153. logger.log(
  154. { user_id, project_id, doc_id, fromVersion, client_id: client.id },
  155. 'client joining doc'
  156. )
  157. AuthorizationManager.assertClientCanViewProject(client, function (error) {
  158. if (error) {
  159. return callback(error)
  160. }
  161. // ensure the per-doc applied-ops channel is subscribed before sending the
  162. // doc to the client, so that no events are missed.
  163. RoomManager.joinDoc(client, doc_id, function (error) {
  164. if (error) {
  165. return callback(error)
  166. }
  167. if (client.disconnected) {
  168. metrics.inc('editor.join-doc.disconnected', 1, {
  169. status: 'after-joining-room'
  170. })
  171. // the client will not read the response anyways
  172. return callback()
  173. }
  174. DocumentUpdaterManager.getDocument(
  175. project_id,
  176. doc_id,
  177. fromVersion,
  178. function (error, lines, version, ranges, ops) {
  179. if (error) {
  180. return callback(error)
  181. }
  182. if (client.disconnected) {
  183. metrics.inc('editor.join-doc.disconnected', 1, {
  184. status: 'after-doc-updater-call'
  185. })
  186. // the client will not read the response anyways
  187. return callback()
  188. }
  189. if (is_restricted_user && ranges && ranges.comments) {
  190. ranges.comments = []
  191. }
  192. // Encode any binary bits of data so it can go via WebSockets
  193. // See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
  194. const encodeForWebsockets = (text) =>
  195. unescape(encodeURIComponent(text))
  196. const escapedLines = []
  197. for (let line of lines) {
  198. try {
  199. line = encodeForWebsockets(line)
  200. } catch (err) {
  201. logger.err(
  202. {
  203. err,
  204. project_id,
  205. doc_id,
  206. fromVersion,
  207. line,
  208. client_id: client.id
  209. },
  210. 'error encoding line uri component'
  211. )
  212. return callback(err)
  213. }
  214. escapedLines.push(line)
  215. }
  216. if (options.encodeRanges) {
  217. try {
  218. for (const comment of (ranges && ranges.comments) || []) {
  219. if (comment.op.c) {
  220. comment.op.c = encodeForWebsockets(comment.op.c)
  221. }
  222. }
  223. for (const change of (ranges && ranges.changes) || []) {
  224. if (change.op.i) {
  225. change.op.i = encodeForWebsockets(change.op.i)
  226. }
  227. if (change.op.d) {
  228. change.op.d = encodeForWebsockets(change.op.d)
  229. }
  230. }
  231. } catch (err) {
  232. logger.err(
  233. {
  234. err,
  235. project_id,
  236. doc_id,
  237. fromVersion,
  238. ranges,
  239. client_id: client.id
  240. },
  241. 'error encoding range uri component'
  242. )
  243. return callback(err)
  244. }
  245. }
  246. AuthorizationManager.addAccessToDoc(client, doc_id, () => {})
  247. logger.log(
  248. {
  249. user_id,
  250. project_id,
  251. doc_id,
  252. fromVersion,
  253. client_id: client.id
  254. },
  255. 'client joined doc'
  256. )
  257. callback(null, escapedLines, version, ops, ranges)
  258. }
  259. )
  260. })
  261. })
  262. },
  263. leaveDoc(client, doc_id, callback) {
  264. // client may have disconnected, but we have to cleanup internal state.
  265. metrics.inc('editor.leave-doc')
  266. const { project_id, user_id } = client.ol_context
  267. logger.log(
  268. { user_id, project_id, doc_id, client_id: client.id },
  269. 'client leaving doc'
  270. )
  271. RoomManager.leaveDoc(client, doc_id)
  272. // we could remove permission when user leaves a doc, but because
  273. // the connection is per-project, we continue to allow access
  274. // after the initial joinDoc since we know they are already authorised.
  275. // # AuthorizationManager.removeAccessToDoc client, doc_id
  276. callback()
  277. },
  278. updateClientPosition(client, cursorData, callback) {
  279. if (client.disconnected) {
  280. // do not create a ghost entry in redis
  281. return callback()
  282. }
  283. metrics.inc('editor.update-client-position', 0.1)
  284. const {
  285. project_id,
  286. first_name,
  287. last_name,
  288. email,
  289. user_id
  290. } = client.ol_context
  291. logger.log(
  292. { user_id, project_id, client_id: client.id, cursorData },
  293. 'updating client position'
  294. )
  295. AuthorizationManager.assertClientCanViewProjectAndDoc(
  296. client,
  297. cursorData.doc_id,
  298. function (error) {
  299. if (error) {
  300. logger.warn(
  301. { err: error, client_id: client.id, project_id, user_id },
  302. "silently ignoring unauthorized updateClientPosition. Client likely hasn't called joinProject yet."
  303. )
  304. return callback()
  305. }
  306. cursorData.id = client.publicId
  307. if (user_id) {
  308. cursorData.user_id = user_id
  309. }
  310. if (email) {
  311. cursorData.email = email
  312. }
  313. // Don't store anonymous users in redis to avoid influx
  314. if (!user_id || user_id === 'anonymous-user') {
  315. cursorData.name = ''
  316. // consistent async behaviour
  317. setTimeout(callback)
  318. } else {
  319. cursorData.name =
  320. first_name && last_name
  321. ? `${first_name} ${last_name}`
  322. : first_name || last_name || ''
  323. ConnectedUsersManager.updateUserPosition(
  324. project_id,
  325. client.publicId,
  326. {
  327. first_name,
  328. last_name,
  329. email,
  330. _id: user_id
  331. },
  332. {
  333. row: cursorData.row,
  334. column: cursorData.column,
  335. doc_id: cursorData.doc_id
  336. },
  337. callback
  338. )
  339. }
  340. WebsocketLoadBalancer.emitToRoom(
  341. project_id,
  342. 'clientTracking.clientUpdated',
  343. cursorData
  344. )
  345. }
  346. )
  347. },
  348. CLIENT_REFRESH_DELAY: 1000,
  349. getConnectedUsers(client, callback) {
  350. if (client.disconnected) {
  351. // they are not interested anymore, skip the redis lookups
  352. return callback()
  353. }
  354. metrics.inc('editor.get-connected-users')
  355. const { project_id, user_id, is_restricted_user } = client.ol_context
  356. if (is_restricted_user) {
  357. return callback(null, [])
  358. }
  359. if (!project_id) {
  360. return callback(new Error('no project_id found on client'))
  361. }
  362. logger.log(
  363. { user_id, project_id, client_id: client.id },
  364. 'getting connected users'
  365. )
  366. AuthorizationManager.assertClientCanViewProject(client, function (error) {
  367. if (error) {
  368. return callback(error)
  369. }
  370. WebsocketLoadBalancer.emitToRoom(project_id, 'clientTracking.refresh')
  371. setTimeout(
  372. () =>
  373. ConnectedUsersManager.getConnectedUsers(project_id, function (
  374. error,
  375. users
  376. ) {
  377. if (error) {
  378. return callback(error)
  379. }
  380. logger.log(
  381. { user_id, project_id, client_id: client.id },
  382. 'got connected users'
  383. )
  384. callback(null, users)
  385. }),
  386. WebsocketController.CLIENT_REFRESH_DELAY
  387. )
  388. })
  389. },
  390. applyOtUpdate(client, doc_id, update, callback) {
  391. // client may have disconnected, but we can submit their update to doc-updater anyways.
  392. const { user_id, project_id } = client.ol_context
  393. if (!project_id) {
  394. return callback(new Error('no project_id found on client'))
  395. }
  396. WebsocketController._assertClientCanApplyUpdate(
  397. client,
  398. doc_id,
  399. update,
  400. function (error) {
  401. if (error) {
  402. logger.warn(
  403. { err: error, doc_id, client_id: client.id, version: update.v },
  404. 'client is not authorized to make update'
  405. )
  406. setTimeout(
  407. () =>
  408. // Disconnect, but give the client the chance to receive the error
  409. client.disconnect(),
  410. 100
  411. )
  412. return callback(error)
  413. }
  414. if (!update.meta) {
  415. update.meta = {}
  416. }
  417. update.meta.source = client.publicId
  418. update.meta.user_id = user_id
  419. metrics.inc('editor.doc-update', 0.3)
  420. logger.log(
  421. {
  422. user_id,
  423. doc_id,
  424. project_id,
  425. client_id: client.id,
  426. version: update.v
  427. },
  428. 'sending update to doc updater'
  429. )
  430. DocumentUpdaterManager.queueChange(
  431. project_id,
  432. doc_id,
  433. update,
  434. function (error) {
  435. if ((error && error.message) === 'update is too large') {
  436. metrics.inc('update_too_large')
  437. const { updateSize } = error
  438. logger.warn(
  439. { user_id, project_id, doc_id, updateSize },
  440. 'update is too large'
  441. )
  442. // mark the update as received -- the client should not send it again!
  443. callback()
  444. // trigger an out-of-sync error
  445. const message = {
  446. project_id,
  447. doc_id,
  448. error: 'update is too large'
  449. }
  450. setTimeout(function () {
  451. if (client.disconnected) {
  452. // skip the message broadcast, the client has moved on
  453. return metrics.inc('editor.doc-update.disconnected', 1, {
  454. status: 'at-otUpdateError'
  455. })
  456. }
  457. client.emit('otUpdateError', message.error, message)
  458. client.disconnect()
  459. }, 100)
  460. return
  461. }
  462. if (error) {
  463. logger.error(
  464. {
  465. err: error,
  466. project_id,
  467. doc_id,
  468. client_id: client.id,
  469. version: update.v
  470. },
  471. 'document was not available for update'
  472. )
  473. client.disconnect()
  474. }
  475. callback(error)
  476. }
  477. )
  478. }
  479. )
  480. },
  481. _assertClientCanApplyUpdate(client, doc_id, update, callback) {
  482. AuthorizationManager.assertClientCanEditProjectAndDoc(
  483. client,
  484. doc_id,
  485. function (error) {
  486. if (
  487. error &&
  488. error.message === 'not authorized' &&
  489. WebsocketController._isCommentUpdate(update)
  490. ) {
  491. // This might be a comment op, which we only need read-only priveleges for
  492. AuthorizationManager.assertClientCanViewProjectAndDoc(
  493. client,
  494. doc_id,
  495. callback
  496. )
  497. return
  498. }
  499. callback(error)
  500. }
  501. )
  502. },
  503. _isCommentUpdate(update) {
  504. if (!(update && update.op instanceof Array)) {
  505. return false
  506. }
  507. for (const op of update.op) {
  508. if (!op.c) {
  509. return false
  510. }
  511. }
  512. return true
  513. }
  514. }