overleaf/services/real-time/app/coffee/Router.coffee

140 lines
5.3 KiB
CoffeeScript
Raw Normal View History

2014-11-17 13:12:49 +00:00
metrics = require "metrics-sharelatex"
logger = require "logger-sharelatex"
settings = require "settings-sharelatex"
2014-11-10 11:27:08 +00:00
WebsocketController = require "./WebsocketController"
HttpController = require "./HttpController"
HttpApiController = require "./HttpApiController"
Utils = require "./Utils"
bodyParser = require "body-parser"
basicAuth = require('basic-auth-connect')
httpAuth = basicAuth (user, pass)->
isValid = user == settings.internal.realTime.user and pass == settings.internal.realTime.pass
if !isValid
logger.err user:user, pass:pass, "invalid login details"
return isValid
module.exports = Router =
_handleError: (callback = ((error) ->), error, client, method, extraAttrs = {}) ->
Utils.getClientAttributes client, ["project_id", "doc_id", "user_id"], (_, attrs) ->
for key, value of extraAttrs
attrs[key] = value
attrs.client_id = client.id
attrs.err = error
2017-11-10 15:01:23 +00:00
if error.message in ["not authorized", "doc updater could not load requested ops", "no project_id found on client"]
logger.warn attrs, error.message
return callback {message: error.message}
else
logger.error attrs, "server side error in #{method}"
# Don't return raw error to prevent leaking server side info
return callback {message: "Something went wrong in real-time service"}
configure: (app, io, session) ->
app.set("io", io)
app.get "/clients", HttpController.getConnectedClients
app.get "/clients/:client_id", HttpController.getConnectedClient
app.post "/project/:project_id/message/:message", httpAuth, bodyParser.json(limit: "5mb"), HttpApiController.sendMessage
app.post "/drain", httpAuth, HttpApiController.startDrain
2016-09-07 07:58:35 +00:00
session.on 'connection', (error, client, session) ->
if client? and error?.message?.match(/could not look up session by key/)
2017-11-10 15:01:23 +00:00
logger.warn err: error, client: client?, session: session?, "invalid session"
# tell the client to reauthenticate if it has an invalid session key
client.emit("connectionRejected", {message: "invalid session"})
client.disconnect()
return
if error?
logger.err err: error, client: client?, session: session?, "error when client connected"
client?.emit("connectionRejected", {message: "error"})
client?.disconnect()
return
2016-09-07 07:58:35 +00:00
# send positive confirmation that the client has a valid connection
client.emit("connectionAccepted")
2014-11-17 13:12:49 +00:00
metrics.inc('socket-io.connection')
2016-09-07 07:58:35 +00:00
2014-11-12 15:54:55 +00:00
logger.log session: session, client_id: client.id, "client connected"
2016-09-07 07:58:35 +00:00
if session?.passport?.user?
user = session.passport.user
else if session?.user?
2014-11-21 11:48:59 +00:00
user = session.user
2016-09-07 07:58:35 +00:00
else
user = {_id: "anonymous-user"}
2014-11-10 11:27:08 +00:00
client.on "joinProject", (data = {}, callback) ->
if data.anonymousAccessToken
user.anonymousAccessToken = data.anonymousAccessToken
2014-11-12 15:54:55 +00:00
WebsocketController.joinProject client, user, data.project_id, (err, args...) ->
if err?
Router._handleError callback, err, client, "joinProject", {project_id: data.project_id, user_id: user?.id}
2014-11-12 15:54:55 +00:00
else
callback(null, args...)
2016-09-07 07:58:35 +00:00
client.on "disconnect", () ->
2014-11-17 13:12:49 +00:00
metrics.inc('socket-io.disconnect')
WebsocketController.leaveProject io, client, (err) ->
if err?
Router._handleError null, err, client, "leaveProject"
2016-09-07 07:58:35 +00:00
2017-09-21 14:19:19 +00:00
# Variadic. The possible arguments:
2017-09-21 13:58:49 +00:00
# doc_id, callback
# doc_id, fromVersion, callback
# doc_id, options, callback
2017-09-21 14:19:19 +00:00
# doc_id, fromVersion, options, callback
client.on "joinDoc", (doc_id, fromVersion, options, callback) ->
2017-09-21 15:55:49 +00:00
if typeof fromVersion == "function" and !options
2014-11-12 15:54:55 +00:00
callback = fromVersion
fromVersion = -1
2017-09-21 15:55:49 +00:00
options = {}
else if typeof fromVersion == "number" and typeof options == "function"
2017-09-21 14:19:19 +00:00
callback = options
options = {}
2017-09-21 15:55:49 +00:00
else if typeof fromVersion == "object" and typeof options == "function"
2017-09-21 14:19:19 +00:00
callback = options
2017-09-21 15:55:49 +00:00
options = fromVersion
fromVersion = -1
else if typeof fromVersion == "number" and typeof options == "object"
# Called with 4 args, things are as expected
else
logger.error { arguments: arguments }, "unexpected arguments"
2017-09-22 08:25:24 +00:00
return callback?(new Error("unexpected arguments"))
2016-09-07 07:58:35 +00:00
2017-09-21 15:56:09 +00:00
WebsocketController.joinDoc client, doc_id, fromVersion, options, (err, args...) ->
2014-11-12 15:54:55 +00:00
if err?
Router._handleError callback, err, client, "joinDoc", {doc_id, fromVersion}
2014-11-12 15:54:55 +00:00
else
callback(null, args...)
2016-09-07 07:58:35 +00:00
2014-11-12 16:51:48 +00:00
client.on "leaveDoc", (doc_id, callback) ->
WebsocketController.leaveDoc client, doc_id, (err, args...) ->
if err?
Router._handleError callback, err, client, "leaveDoc"
2014-11-12 16:51:48 +00:00
else
callback(null, args...)
2016-09-07 07:58:35 +00:00
client.on "clientTracking.getConnectedUsers", (callback = (error, users) ->) ->
WebsocketController.getConnectedUsers client, (err, users) ->
if err?
Router._handleError callback, err, client, "clientTracking.getConnectedUsers"
else
callback(null, users)
2016-09-07 07:58:35 +00:00
client.on "clientTracking.updatePosition", (cursorData, callback = (error) ->) ->
WebsocketController.updateClientPosition client, cursorData, (err) ->
if err?
Router._handleError callback, err, client, "clientTracking.updatePosition"
2014-11-14 10:12:35 +00:00
else
callback()
2016-09-07 07:58:35 +00:00
2014-11-14 10:12:35 +00:00
client.on "applyOtUpdate", (doc_id, update, callback = (error) ->) ->
WebsocketController.applyOtUpdate client, doc_id, update, (err) ->
if err?
Router._handleError callback, err, client, "applyOtUpdate", {doc_id, update}
else
callback()