2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const WebsocketController = require('./WebsocketController')
|
|
|
|
const HttpController = require('./HttpController')
|
|
|
|
const HttpApiController = require('./HttpApiController')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const base64id = require('base64id')
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const basicAuth = require('basic-auth-connect')
|
|
|
|
const httpAuth = basicAuth(function (user, pass) {
|
|
|
|
const isValid =
|
|
|
|
user === settings.internal.realTime.user &&
|
|
|
|
pass === settings.internal.realTime.pass
|
|
|
|
if (!isValid) {
|
|
|
|
logger.err({ user, pass }, 'invalid login details')
|
|
|
|
}
|
|
|
|
return isValid
|
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-06 09:27:46 -04:00
|
|
|
const HOSTNAME = require('os').hostname()
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
let Router
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = Router = {
|
|
|
|
_handleError(callback, error, client, method, attrs) {
|
2020-07-07 06:06:02 -04:00
|
|
|
attrs = attrs || {}
|
2020-08-20 09:03:03 -04:00
|
|
|
for (const key of ['project_id', 'user_id']) {
|
2020-08-20 11:23:57 -04:00
|
|
|
attrs[key] = attrs[key] || client.ol_context[key]
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
attrs.client_id = client.id
|
|
|
|
attrs.err = error
|
|
|
|
if (error.name === 'CodedError') {
|
|
|
|
logger.warn(attrs, error.message, { code: error.code })
|
2020-07-07 06:06:02 -04:00
|
|
|
const serializedError = { message: error.message, code: error.code }
|
|
|
|
callback(serializedError)
|
|
|
|
} else if (error.message === 'unexpected arguments') {
|
2020-06-23 13:29:44 -04:00
|
|
|
// the payload might be very large, put it on level info
|
|
|
|
logger.log(attrs, 'unexpected arguments')
|
|
|
|
metrics.inc('unexpected-arguments', 1, { status: method })
|
2020-07-07 06:06:02 -04:00
|
|
|
const serializedError = { message: error.message }
|
|
|
|
callback(serializedError)
|
|
|
|
} else if (
|
2020-06-23 13:29:44 -04:00
|
|
|
[
|
|
|
|
'not authorized',
|
2020-07-13 05:42:50 -04:00
|
|
|
'joinLeaveEpoch mismatch',
|
2020-06-23 13:29:44 -04:00
|
|
|
'doc updater could not load requested ops',
|
|
|
|
'no project_id found on client'
|
|
|
|
].includes(error.message)
|
|
|
|
) {
|
|
|
|
logger.warn(attrs, error.message)
|
2020-07-07 06:06:02 -04:00
|
|
|
const serializedError = { message: error.message }
|
|
|
|
callback(serializedError)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else {
|
|
|
|
logger.error(attrs, `server side error in ${method}`)
|
|
|
|
// Don't return raw error to prevent leaking server side info
|
2020-07-07 06:06:02 -04:00
|
|
|
const serializedError = {
|
|
|
|
message: 'Something went wrong in real-time service'
|
|
|
|
}
|
|
|
|
callback(serializedError)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
},
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
_handleInvalidArguments(client, method, args) {
|
|
|
|
const error = new Error('unexpected arguments')
|
|
|
|
let callback = args[args.length - 1]
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
callback = function () {}
|
|
|
|
}
|
|
|
|
const attrs = { arguments: args }
|
2020-07-07 06:06:02 -04:00
|
|
|
Router._handleError(callback, error, client, method, attrs)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
configure(app, io, session) {
|
|
|
|
app.set('io', io)
|
|
|
|
app.get('/clients', HttpController.getConnectedClients)
|
|
|
|
app.get('/clients/:client_id', HttpController.getConnectedClient)
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/message/:message',
|
|
|
|
httpAuth,
|
|
|
|
bodyParser.json({ limit: '5mb' }),
|
|
|
|
HttpApiController.sendMessage
|
|
|
|
)
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
app.post('/drain', httpAuth, HttpApiController.startDrain)
|
|
|
|
app.post(
|
|
|
|
'/client/:client_id/disconnect',
|
|
|
|
httpAuth,
|
|
|
|
HttpApiController.disconnectClient
|
|
|
|
)
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
session.on('connection', function (error, client, session) {
|
2020-06-23 13:29:44 -04:00
|
|
|
// init client context, we may access it in Router._handleError before
|
|
|
|
// setting any values
|
|
|
|
client.ol_context = {}
|
2020-07-13 05:42:50 -04:00
|
|
|
// bail out from joinDoc when a parallel joinDoc or leaveDoc is running
|
|
|
|
client.joinLeaveEpoch = 0
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
if (client) {
|
2020-06-23 13:29:44 -04:00
|
|
|
client.on('error', function (err) {
|
|
|
|
logger.err({ clientErr: err }, 'socket.io client error')
|
|
|
|
if (client.connected) {
|
|
|
|
client.emit('reconnectGracefully')
|
2020-07-07 06:06:02 -04:00
|
|
|
client.disconnect()
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
if (settings.shutDownInProgress) {
|
|
|
|
client.emit('connectionRejected', { message: 'retry' })
|
|
|
|
client.disconnect()
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
if (
|
2020-07-07 06:06:02 -04:00
|
|
|
client &&
|
|
|
|
error &&
|
|
|
|
error.message.match(/could not look up session by key/)
|
2020-06-23 13:29:44 -04:00
|
|
|
) {
|
|
|
|
logger.warn(
|
2020-07-07 06:06:02 -04:00
|
|
|
{ err: error, client: !!client, session: !!session },
|
2020-06-23 13:29:44 -04:00
|
|
|
'invalid session'
|
|
|
|
)
|
|
|
|
// tell the client to reauthenticate if it has an invalid session key
|
|
|
|
client.emit('connectionRejected', { message: 'invalid session' })
|
|
|
|
client.disconnect()
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.err(
|
2020-07-07 06:06:02 -04:00
|
|
|
{ err: error, client: !!client, session: !!session },
|
2020-06-23 13:29:44 -04:00
|
|
|
'error when client connected'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
if (client) {
|
2020-06-23 13:29:44 -04:00
|
|
|
client.emit('connectionRejected', { message: 'error' })
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
if (client) {
|
2020-06-23 13:29:44 -04:00
|
|
|
client.disconnect()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// send positive confirmation that the client has a valid connection
|
|
|
|
client.publicId = 'P.' + base64id.generateId()
|
|
|
|
client.emit('connectionAccepted', null, client.publicId)
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
metrics.inc('socket-io.connection')
|
2020-07-07 06:06:02 -04:00
|
|
|
metrics.gauge('socket-io.clients', io.sockets.clients().length)
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.log({ session, client_id: client.id }, 'client connected')
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
let user
|
|
|
|
if (session && session.passport && session.passport.user) {
|
2020-06-23 13:29:44 -04:00
|
|
|
;({ user } = session.passport)
|
2020-07-07 06:06:02 -04:00
|
|
|
} else if (session && session.user) {
|
2020-06-23 13:29:44 -04:00
|
|
|
;({ user } = session)
|
|
|
|
} else {
|
|
|
|
user = { _id: 'anonymous-user' }
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-06 09:27:46 -04:00
|
|
|
if (settings.exposeHostname) {
|
|
|
|
client.on('debug.getHostname', function (callback) {
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return Router._handleInvalidArguments(
|
|
|
|
client,
|
|
|
|
'debug.getHostname',
|
|
|
|
arguments
|
|
|
|
)
|
|
|
|
}
|
|
|
|
callback(HOSTNAME)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
client.on('joinProject', function (data, callback) {
|
2020-07-07 06:06:02 -04:00
|
|
|
data = data || {}
|
2020-06-23 13:29:44 -04:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return Router._handleInvalidArguments(
|
|
|
|
client,
|
|
|
|
'joinProject',
|
|
|
|
arguments
|
|
|
|
)
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
if (data.anonymousAccessToken) {
|
|
|
|
user.anonymousAccessToken = data.anonymousAccessToken
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.joinProject(
|
2020-06-23 13:29:44 -04:00
|
|
|
client,
|
|
|
|
user,
|
|
|
|
data.project_id,
|
|
|
|
function (err, ...args) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (err) {
|
|
|
|
Router._handleError(callback, err, client, 'joinProject', {
|
2020-06-23 13:29:44 -04:00
|
|
|
project_id: data.project_id,
|
2020-07-07 06:06:02 -04:00
|
|
|
user_id: user._id
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
} else {
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(null, ...args)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
client.on('disconnect', function () {
|
|
|
|
metrics.inc('socket-io.disconnect')
|
2020-07-07 06:06:02 -04:00
|
|
|
metrics.gauge('socket-io.clients', io.sockets.clients().length)
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.leaveProject(io, client, function (err) {
|
|
|
|
if (err) {
|
|
|
|
Router._handleError(function () {}, err, client, 'leaveProject')
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// Variadic. The possible arguments:
|
|
|
|
// doc_id, callback
|
|
|
|
// doc_id, fromVersion, callback
|
|
|
|
// doc_id, options, callback
|
|
|
|
// doc_id, fromVersion, options, callback
|
|
|
|
client.on('joinDoc', function (doc_id, fromVersion, options, callback) {
|
|
|
|
if (typeof fromVersion === 'function' && !options) {
|
|
|
|
callback = fromVersion
|
|
|
|
fromVersion = -1
|
|
|
|
options = {}
|
|
|
|
} else if (
|
|
|
|
typeof fromVersion === 'number' &&
|
|
|
|
typeof options === 'function'
|
|
|
|
) {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
} else if (
|
|
|
|
typeof fromVersion === 'object' &&
|
|
|
|
typeof options === 'function'
|
|
|
|
) {
|
|
|
|
callback = options
|
|
|
|
options = fromVersion
|
|
|
|
fromVersion = -1
|
|
|
|
} else if (
|
|
|
|
typeof fromVersion === 'number' &&
|
|
|
|
typeof options === 'object' &&
|
|
|
|
typeof callback === 'function'
|
|
|
|
) {
|
|
|
|
// Called with 4 args, things are as expected
|
|
|
|
} else {
|
|
|
|
return Router._handleInvalidArguments(client, 'joinDoc', arguments)
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.joinDoc(
|
2020-06-23 13:29:44 -04:00
|
|
|
client,
|
|
|
|
doc_id,
|
|
|
|
fromVersion,
|
|
|
|
options,
|
|
|
|
function (err, ...args) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (err) {
|
|
|
|
Router._handleError(callback, err, client, 'joinDoc', {
|
2020-06-23 13:29:44 -04:00
|
|
|
doc_id,
|
|
|
|
fromVersion
|
|
|
|
})
|
|
|
|
} else {
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(null, ...args)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
client.on('leaveDoc', function (doc_id, callback) {
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return Router._handleInvalidArguments(client, 'leaveDoc', arguments)
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.leaveDoc(client, doc_id, function (err, ...args) {
|
|
|
|
if (err) {
|
2020-08-20 09:05:12 -04:00
|
|
|
Router._handleError(callback, err, client, 'leaveDoc', {
|
|
|
|
doc_id
|
|
|
|
})
|
2020-06-23 13:29:44 -04:00
|
|
|
} else {
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(null, ...args)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
client.on('clientTracking.getConnectedUsers', function (callback) {
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return Router._handleInvalidArguments(
|
|
|
|
client,
|
|
|
|
'clientTracking.getConnectedUsers',
|
|
|
|
arguments
|
|
|
|
)
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.getConnectedUsers(client, function (err, users) {
|
|
|
|
if (err) {
|
|
|
|
Router._handleError(
|
2020-06-23 13:29:44 -04:00
|
|
|
callback,
|
|
|
|
err,
|
|
|
|
client,
|
|
|
|
'clientTracking.getConnectedUsers'
|
|
|
|
)
|
|
|
|
} else {
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(null, users)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
client.on('clientTracking.updatePosition', function (
|
|
|
|
cursorData,
|
|
|
|
callback
|
|
|
|
) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (!callback) {
|
|
|
|
callback = function () {}
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return Router._handleInvalidArguments(
|
|
|
|
client,
|
|
|
|
'clientTracking.updatePosition',
|
|
|
|
arguments
|
|
|
|
)
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.updateClientPosition(client, cursorData, function (
|
|
|
|
err
|
|
|
|
) {
|
|
|
|
if (err) {
|
|
|
|
Router._handleError(
|
|
|
|
callback,
|
|
|
|
err,
|
|
|
|
client,
|
|
|
|
'clientTracking.updatePosition'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
callback()
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
})
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
client.on('applyOtUpdate', function (doc_id, update, callback) {
|
2020-06-23 13:29:44 -04:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return Router._handleInvalidArguments(
|
|
|
|
client,
|
|
|
|
'applyOtUpdate',
|
|
|
|
arguments
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController.applyOtUpdate(client, doc_id, update, function (
|
|
|
|
err
|
|
|
|
) {
|
|
|
|
if (err) {
|
|
|
|
Router._handleError(callback, err, client, 'applyOtUpdate', {
|
|
|
|
doc_id,
|
|
|
|
update
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
callback()
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
})
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|