Router.coffee 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. metrics = require "metrics-sharelatex"
  2. logger = require "logger-sharelatex"
  3. settings = require "settings-sharelatex"
  4. WebsocketController = require "./WebsocketController"
  5. HttpController = require "./HttpController"
  6. HttpApiController = require "./HttpApiController"
  7. Utils = require "./Utils"
  8. bodyParser = require "body-parser"
  9. basicAuth = require('basic-auth-connect')
  10. httpAuth = basicAuth (user, pass)->
  11. isValid = user == settings.internal.realTime.user and pass == settings.internal.realTime.pass
  12. if !isValid
  13. logger.err user:user, pass:pass, "invalid login details"
  14. return isValid
  15. module.exports = Router =
  16. _handleError: (callback = ((error) ->), error, client, method, extraAttrs = {}) ->
  17. Utils.getClientAttributes client, ["project_id", "doc_id", "user_id"], (_, attrs) ->
  18. for key, value of extraAttrs
  19. attrs[key] = value
  20. attrs.client_id = client.id
  21. attrs.err = error
  22. if error.name == "CodedError"
  23. logger.warn attrs, error.message, code: error.code
  24. return callback {message: error.message, code: error.code}
  25. if error.message in ["not authorized", "doc updater could not load requested ops", "no project_id found on client"]
  26. logger.warn attrs, error.message
  27. return callback {message: error.message}
  28. else
  29. logger.error attrs, "server side error in #{method}"
  30. # Don't return raw error to prevent leaking server side info
  31. return callback {message: "Something went wrong in real-time service"}
  32. configure: (app, io, session) ->
  33. app.set("io", io)
  34. app.get "/clients", HttpController.getConnectedClients
  35. app.get "/clients/:client_id", HttpController.getConnectedClient
  36. app.post "/project/:project_id/message/:message", httpAuth, bodyParser.json(limit: "5mb"), HttpApiController.sendMessage
  37. app.post "/drain", httpAuth, HttpApiController.startDrain
  38. session.on 'connection', (error, client, session) ->
  39. client?.on "error", (err) ->
  40. logger.err { clientErr: err }, "socket.io client error"
  41. if client.connected
  42. client.emit("reconnectGracefully")
  43. client.disconnect()
  44. if settings.shutDownInProgress
  45. client.emit("connectionRejected", {message: "retry"})
  46. client.disconnect()
  47. return
  48. if client? and error?.message?.match(/could not look up session by key/)
  49. logger.warn err: error, client: client?, session: session?, "invalid session"
  50. # tell the client to reauthenticate if it has an invalid session key
  51. client.emit("connectionRejected", {message: "invalid session"})
  52. client.disconnect()
  53. return
  54. if error?
  55. logger.err err: error, client: client?, session: session?, "error when client connected"
  56. client?.emit("connectionRejected", {message: "error"})
  57. client?.disconnect()
  58. return
  59. # send positive confirmation that the client has a valid connection
  60. client.emit("connectionAccepted")
  61. metrics.inc('socket-io.connection')
  62. metrics.gauge('socket-io.clients', io.sockets.clients()?.length)
  63. logger.log session: session, client_id: client.id, "client connected"
  64. if session?.passport?.user?
  65. user = session.passport.user
  66. else if session?.user?
  67. user = session.user
  68. else
  69. user = {_id: "anonymous-user"}
  70. client.on "joinProject", (data = {}, callback) ->
  71. if data.anonymousAccessToken
  72. user.anonymousAccessToken = data.anonymousAccessToken
  73. WebsocketController.joinProject client, user, data.project_id, (err, args...) ->
  74. if err?
  75. Router._handleError callback, err, client, "joinProject", {project_id: data.project_id, user_id: user?.id}
  76. else
  77. callback(null, args...)
  78. client.on "disconnect", () ->
  79. metrics.inc('socket-io.disconnect')
  80. metrics.gauge('socket-io.clients', io.sockets.clients()?.length - 1)
  81. WebsocketController.leaveProject io, client, (err) ->
  82. if err?
  83. Router._handleError null, err, client, "leaveProject"
  84. # Variadic. The possible arguments:
  85. # doc_id, callback
  86. # doc_id, fromVersion, callback
  87. # doc_id, options, callback
  88. # doc_id, fromVersion, options, callback
  89. client.on "joinDoc", (doc_id, fromVersion, options, callback) ->
  90. if typeof fromVersion == "function" and !options
  91. callback = fromVersion
  92. fromVersion = -1
  93. options = {}
  94. else if typeof fromVersion == "number" and typeof options == "function"
  95. callback = options
  96. options = {}
  97. else if typeof fromVersion == "object" and typeof options == "function"
  98. callback = options
  99. options = fromVersion
  100. fromVersion = -1
  101. else if typeof fromVersion == "number" and typeof options == "object"
  102. # Called with 4 args, things are as expected
  103. else
  104. logger.error { arguments: arguments }, "unexpected arguments"
  105. return callback?(new Error("unexpected arguments"))
  106. WebsocketController.joinDoc client, doc_id, fromVersion, options, (err, args...) ->
  107. if err?
  108. Router._handleError callback, err, client, "joinDoc", {doc_id, fromVersion}
  109. else
  110. callback(null, args...)
  111. client.on "leaveDoc", (doc_id, callback) ->
  112. WebsocketController.leaveDoc client, doc_id, (err, args...) ->
  113. if err?
  114. Router._handleError callback, err, client, "leaveDoc"
  115. else
  116. callback(null, args...)
  117. client.on "clientTracking.getConnectedUsers", (callback = (error, users) ->) ->
  118. WebsocketController.getConnectedUsers client, (err, users) ->
  119. if err?
  120. Router._handleError callback, err, client, "clientTracking.getConnectedUsers"
  121. else
  122. callback(null, users)
  123. client.on "clientTracking.updatePosition", (cursorData, callback = (error) ->) ->
  124. WebsocketController.updateClientPosition client, cursorData, (err) ->
  125. if err?
  126. Router._handleError callback, err, client, "clientTracking.updatePosition"
  127. else
  128. callback()
  129. client.on "applyOtUpdate", (doc_id, update, callback = (error) ->) ->
  130. WebsocketController.applyOtUpdate client, doc_id, update, (err) ->
  131. if err?
  132. Router._handleError callback, err, client, "applyOtUpdate", {doc_id, update}
  133. else
  134. callback()