WebsocketController.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. logger = require "logger-sharelatex"
  2. metrics = require "metrics-sharelatex"
  3. settings = require "settings-sharelatex"
  4. WebApiManager = require "./WebApiManager"
  5. AuthorizationManager = require "./AuthorizationManager"
  6. DocumentUpdaterManager = require "./DocumentUpdaterManager"
  7. ConnectedUsersManager = require "./ConnectedUsersManager"
  8. WebsocketLoadBalancer = require "./WebsocketLoadBalancer"
  9. RoomManager = require "./RoomManager"
  10. Utils = require "./Utils"
  11. module.exports = WebsocketController =
  12. # If the protocol version changes when the client reconnects,
  13. # it will force a full refresh of the page. Useful for non-backwards
  14. # compatible protocol changes. Use only in extreme need.
  15. PROTOCOL_VERSION: 2
  16. joinProject: (client, user, project_id, callback = (error, project, privilegeLevel, protocolVersion) ->) ->
  17. if client.disconnected
  18. metrics.inc('editor.join-project.disconnected', 1, {status: 'immediately'})
  19. return callback()
  20. user_id = user?._id
  21. logger.log {user_id, project_id, client_id: client.id}, "user joining project"
  22. metrics.inc "editor.join-project"
  23. WebApiManager.joinProject project_id, user, (error, project, privilegeLevel, isRestrictedUser) ->
  24. return callback(error) if error?
  25. if client.disconnected
  26. metrics.inc('editor.join-project.disconnected', 1, {status: 'after-web-api-call'})
  27. return callback()
  28. if !privilegeLevel or privilegeLevel == ""
  29. err = new Error("not authorized")
  30. logger.warn {err, project_id, user_id, client_id: client.id}, "user is not authorized to join project"
  31. return callback(err)
  32. client.set("privilege_level", privilegeLevel)
  33. client.set("user_id", user_id)
  34. client.set("project_id", project_id)
  35. client.set("owner_id", project?.owner?._id)
  36. client.set("first_name", user?.first_name)
  37. client.set("last_name", user?.last_name)
  38. client.set("email", user?.email)
  39. client.set("connected_time", new Date())
  40. client.set("signup_date", user?.signUpDate)
  41. client.set("login_count", user?.loginCount)
  42. client.set("is_restricted_user", !!(isRestrictedUser))
  43. RoomManager.joinProject client, project_id, (err) ->
  44. return callback(err) if err
  45. logger.log {user_id, project_id, client_id: client.id}, "user joined project"
  46. callback null, project, privilegeLevel, WebsocketController.PROTOCOL_VERSION
  47. # No need to block for setting the user as connected in the cursor tracking
  48. ConnectedUsersManager.updateUserPosition project_id, client.publicId, user, null, () ->
  49. # We want to flush a project if there are no more (local) connected clients
  50. # but we need to wait for the triggering client to disconnect. How long we wait
  51. # is determined by FLUSH_IF_EMPTY_DELAY.
  52. FLUSH_IF_EMPTY_DELAY: 500 #ms
  53. leaveProject: (io, client, callback = (error) ->) ->
  54. Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
  55. return callback(error) if error?
  56. return callback() unless project_id # client did not join project
  57. metrics.inc "editor.leave-project"
  58. logger.log {project_id, user_id, client_id: client.id}, "client leaving project"
  59. WebsocketLoadBalancer.emitToRoom project_id, "clientTracking.clientDisconnected", client.publicId
  60. # We can do this in the background
  61. ConnectedUsersManager.markUserAsDisconnected project_id, client.publicId, (err) ->
  62. if err?
  63. logger.error {err, project_id, user_id, client_id: client.id}, "error marking client as disconnected"
  64. RoomManager.leaveProjectAndDocs(client)
  65. setTimeout () ->
  66. remainingClients = io.sockets.clients(project_id)
  67. if remainingClients.length == 0
  68. # Flush project in the background
  69. DocumentUpdaterManager.flushProjectToMongoAndDelete project_id, (err) ->
  70. if err?
  71. logger.error {err, project_id, user_id, client_id: client.id}, "error flushing to doc updater after leaving project"
  72. callback()
  73. , WebsocketController.FLUSH_IF_EMPTY_DELAY
  74. joinDoc: (client, doc_id, fromVersion = -1, options, callback = (error, doclines, version, ops, ranges) ->) ->
  75. if client.disconnected
  76. metrics.inc('editor.join-doc.disconnected', 1, {status: 'immediately'})
  77. return callback()
  78. metrics.inc "editor.join-doc"
  79. Utils.getClientAttributes client, ["project_id", "user_id", "is_restricted_user"], (error, {project_id, user_id, is_restricted_user}) ->
  80. return callback(error) if error?
  81. return callback(new Error("no project_id found on client")) if !project_id?
  82. logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joining doc"
  83. AuthorizationManager.assertClientCanViewProject client, (error) ->
  84. return callback(error) if error?
  85. # ensure the per-doc applied-ops channel is subscribed before sending the
  86. # doc to the client, so that no events are missed.
  87. RoomManager.joinDoc client, doc_id, (error) ->
  88. return callback(error) if error?
  89. if client.disconnected
  90. metrics.inc('editor.join-doc.disconnected', 1, {status: 'after-joining-room'})
  91. # the client will not read the response anyways
  92. return callback()
  93. DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ranges, ops) ->
  94. return callback(error) if error?
  95. if client.disconnected
  96. metrics.inc('editor.join-doc.disconnected', 1, {status: 'after-doc-updater-call'})
  97. # the client will not read the response anyways
  98. return callback()
  99. if is_restricted_user and ranges?.comments?
  100. ranges.comments = []
  101. # Encode any binary bits of data so it can go via WebSockets
  102. # See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
  103. encodeForWebsockets = (text) -> unescape(encodeURIComponent(text))
  104. escapedLines = []
  105. for line in lines
  106. try
  107. line = encodeForWebsockets(line)
  108. catch err
  109. logger.err {err, project_id, doc_id, fromVersion, line, client_id: client.id}, "error encoding line uri component"
  110. return callback(err)
  111. escapedLines.push line
  112. if options.encodeRanges
  113. try
  114. for comment in ranges?.comments or []
  115. comment.op.c = encodeForWebsockets(comment.op.c) if comment.op.c?
  116. for change in ranges?.changes or []
  117. change.op.i = encodeForWebsockets(change.op.i) if change.op.i?
  118. change.op.d = encodeForWebsockets(change.op.d) if change.op.d?
  119. catch err
  120. logger.err {err, project_id, doc_id, fromVersion, ranges, client_id: client.id}, "error encoding range uri component"
  121. return callback(err)
  122. AuthorizationManager.addAccessToDoc client, doc_id
  123. logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joined doc"
  124. callback null, escapedLines, version, ops, ranges
  125. leaveDoc: (client, doc_id, callback = (error) ->) ->
  126. # client may have disconnected, but we have to cleanup internal state.
  127. metrics.inc "editor.leave-doc"
  128. Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
  129. logger.log {user_id, project_id, doc_id, client_id: client.id}, "client leaving doc"
  130. RoomManager.leaveDoc(client, doc_id)
  131. # we could remove permission when user leaves a doc, but because
  132. # the connection is per-project, we continue to allow access
  133. # after the initial joinDoc since we know they are already authorised.
  134. ## AuthorizationManager.removeAccessToDoc client, doc_id
  135. callback()
  136. updateClientPosition: (client, cursorData, callback = (error) ->) ->
  137. if client.disconnected
  138. # do not create a ghost entry in redis
  139. return callback()
  140. metrics.inc "editor.update-client-position", 0.1
  141. Utils.getClientAttributes client, [
  142. "project_id", "first_name", "last_name", "email", "user_id"
  143. ], (error, {project_id, first_name, last_name, email, user_id}) ->
  144. return callback(error) if error?
  145. logger.log {user_id, project_id, client_id: client.id, cursorData: cursorData}, "updating client position"
  146. AuthorizationManager.assertClientCanViewProjectAndDoc client, cursorData.doc_id, (error) ->
  147. if error?
  148. logger.warn {err: error, client_id: client.id, project_id, user_id}, "silently ignoring unauthorized updateClientPosition. Client likely hasn't called joinProject yet."
  149. return callback()
  150. cursorData.id = client.publicId
  151. cursorData.user_id = user_id if user_id?
  152. cursorData.email = email if email?
  153. # Don't store anonymous users in redis to avoid influx
  154. if !user_id or user_id == 'anonymous-user'
  155. cursorData.name = ""
  156. callback()
  157. else
  158. cursorData.name = if first_name && last_name
  159. "#{first_name} #{last_name}"
  160. else if first_name
  161. first_name
  162. else if last_name
  163. last_name
  164. else
  165. ""
  166. ConnectedUsersManager.updateUserPosition(project_id, client.publicId, {
  167. first_name: first_name,
  168. last_name: last_name,
  169. email: email,
  170. _id: user_id
  171. }, {
  172. row: cursorData.row,
  173. column: cursorData.column,
  174. doc_id: cursorData.doc_id
  175. }, callback)
  176. WebsocketLoadBalancer.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)
  177. CLIENT_REFRESH_DELAY: 1000
  178. getConnectedUsers: (client, callback = (error, users) ->) ->
  179. if client.disconnected
  180. # they are not interested anymore, skip the redis lookups
  181. return callback()
  182. metrics.inc "editor.get-connected-users"
  183. Utils.getClientAttributes client, ["project_id", "user_id", "is_restricted_user"], (error, clientAttributes) ->
  184. return callback(error) if error?
  185. {project_id, user_id, is_restricted_user} = clientAttributes
  186. if is_restricted_user
  187. return callback(null, [])
  188. return callback(new Error("no project_id found on client")) if !project_id?
  189. logger.log {user_id, project_id, client_id: client.id}, "getting connected users"
  190. AuthorizationManager.assertClientCanViewProject client, (error) ->
  191. return callback(error) if error?
  192. WebsocketLoadBalancer.emitToRoom project_id, 'clientTracking.refresh'
  193. setTimeout () ->
  194. ConnectedUsersManager.getConnectedUsers project_id, (error, users) ->
  195. return callback(error) if error?
  196. callback null, users
  197. logger.log {user_id, project_id, client_id: client.id}, "got connected users"
  198. , WebsocketController.CLIENT_REFRESH_DELAY
  199. applyOtUpdate: (client, doc_id, update, callback = (error) ->) ->
  200. # client may have disconnected, but we can submit their update to doc-updater anyways.
  201. Utils.getClientAttributes client, ["user_id", "project_id"], (error, {user_id, project_id}) ->
  202. return callback(error) if error?
  203. return callback(new Error("no project_id found on client")) if !project_id?
  204. WebsocketController._assertClientCanApplyUpdate client, doc_id, update, (error) ->
  205. if error?
  206. logger.warn {err: error, doc_id, client_id: client.id, version: update.v}, "client is not authorized to make update"
  207. setTimeout () ->
  208. # Disconnect, but give the client the chance to receive the error
  209. client.disconnect()
  210. , 100
  211. return callback(error)
  212. update.meta ||= {}
  213. update.meta.source = client.publicId
  214. update.meta.user_id = user_id
  215. metrics.inc "editor.doc-update", 0.3
  216. logger.log {user_id, doc_id, project_id, client_id: client.id, version: update.v}, "sending update to doc updater"
  217. DocumentUpdaterManager.queueChange project_id, doc_id, update, (error) ->
  218. if error?.message == "update is too large"
  219. metrics.inc "update_too_large"
  220. updateSize = error.updateSize
  221. logger.warn({user_id, project_id, doc_id, updateSize}, "update is too large")
  222. # mark the update as received -- the client should not send it again!
  223. callback()
  224. # trigger an out-of-sync error
  225. message = {project_id, doc_id, error: "update is too large"}
  226. setTimeout () ->
  227. if client.disconnected
  228. # skip the message broadcast, the client has moved on
  229. return metrics.inc('editor.doc-update.disconnected', 1, {status:'at-otUpdateError'})
  230. client.emit "otUpdateError", message.error, message
  231. client.disconnect()
  232. , 100
  233. return
  234. if error?
  235. logger.error {err: error, project_id, doc_id, client_id: client.id, version: update.v}, "document was not available for update"
  236. client.disconnect()
  237. callback(error)
  238. _assertClientCanApplyUpdate: (client, doc_id, update, callback) ->
  239. AuthorizationManager.assertClientCanEditProjectAndDoc client, doc_id, (error) ->
  240. if error?
  241. if error.message == "not authorized" and WebsocketController._isCommentUpdate(update)
  242. # This might be a comment op, which we only need read-only priveleges for
  243. AuthorizationManager.assertClientCanViewProjectAndDoc client, doc_id, callback
  244. else
  245. return callback(error)
  246. else
  247. return callback(null)
  248. _isCommentUpdate: (update) ->
  249. for op in update.op
  250. if !op.c?
  251. return false
  252. return true