Router.coffee 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if settings.shutDownInProgress
  40. client.emit("connectionRejected", {message: "retry"})
  41. client.disconnect()
  42. return
  43. if client? and error?.message?.match(/could not look up session by key/)
  44. logger.warn err: error, client: client?, session: session?, "invalid session"
  45. # tell the client to reauthenticate if it has an invalid session key
  46. client.emit("connectionRejected", {message: "invalid session"})
  47. client.disconnect()
  48. return
  49. if error?
  50. logger.err err: error, client: client?, session: session?, "error when client connected"
  51. client?.emit("connectionRejected", {message: "error"})
  52. client?.disconnect()
  53. return
  54. # send positive confirmation that the client has a valid connection
  55. client.emit("connectionAccepted")
  56. metrics.inc('socket-io.connection')
  57. metrics.gauge('socket-io.clients', io.sockets.clients()?.length)
  58. logger.log session: session, client_id: client.id, "client connected"
  59. if session?.passport?.user?
  60. user = session.passport.user
  61. else if session?.user?
  62. user = session.user
  63. else
  64. user = {_id: "anonymous-user"}
  65. client.on "joinProject", (data = {}, callback) ->
  66. if data.anonymousAccessToken
  67. user.anonymousAccessToken = data.anonymousAccessToken
  68. WebsocketController.joinProject client, user, data.project_id, (err, args...) ->
  69. if err?
  70. Router._handleError callback, err, client, "joinProject", {project_id: data.project_id, user_id: user?.id}
  71. else
  72. callback(null, args...)
  73. client.on "disconnect", () ->
  74. metrics.inc('socket-io.disconnect')
  75. metrics.gauge('socket-io.clients', io.sockets.clients()?.length - 1)
  76. WebsocketController.leaveProject io, client, (err) ->
  77. if err?
  78. Router._handleError null, err, client, "leaveProject"
  79. # Variadic. The possible arguments:
  80. # doc_id, callback
  81. # doc_id, fromVersion, callback
  82. # doc_id, options, callback
  83. # doc_id, fromVersion, options, callback
  84. client.on "joinDoc", (doc_id, fromVersion, options, callback) ->
  85. if typeof fromVersion == "function" and !options
  86. callback = fromVersion
  87. fromVersion = -1
  88. options = {}
  89. else if typeof fromVersion == "number" and typeof options == "function"
  90. callback = options
  91. options = {}
  92. else if typeof fromVersion == "object" and typeof options == "function"
  93. callback = options
  94. options = fromVersion
  95. fromVersion = -1
  96. else if typeof fromVersion == "number" and typeof options == "object"
  97. # Called with 4 args, things are as expected
  98. else
  99. logger.error { arguments: arguments }, "unexpected arguments"
  100. return callback?(new Error("unexpected arguments"))
  101. WebsocketController.joinDoc client, doc_id, fromVersion, options, (err, args...) ->
  102. if err?
  103. Router._handleError callback, err, client, "joinDoc", {doc_id, fromVersion}
  104. else
  105. callback(null, args...)
  106. client.on "leaveDoc", (doc_id, callback) ->
  107. WebsocketController.leaveDoc client, doc_id, (err, args...) ->
  108. if err?
  109. Router._handleError callback, err, client, "leaveDoc"
  110. else
  111. callback(null, args...)
  112. client.on "clientTracking.getConnectedUsers", (callback = (error, users) ->) ->
  113. WebsocketController.getConnectedUsers client, (err, users) ->
  114. if err?
  115. Router._handleError callback, err, client, "clientTracking.getConnectedUsers"
  116. else
  117. callback(null, users)
  118. client.on "clientTracking.updatePosition", (cursorData, callback = (error) ->) ->
  119. WebsocketController.updateClientPosition client, cursorData, (err) ->
  120. if err?
  121. Router._handleError callback, err, client, "clientTracking.updatePosition"
  122. else
  123. callback()
  124. client.on "applyOtUpdate", (doc_id, update, callback = (error) ->) ->
  125. WebsocketController.applyOtUpdate client, doc_id, update, (err) ->
  126. if err?
  127. Router._handleError callback, err, client, "applyOtUpdate", {doc_id, update}
  128. else
  129. callback()