2021-07-12 12:47:18 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-06-23 13:29:44 -04:00
|
|
|
const RedisClientManager = require('./RedisClientManager')
|
|
|
|
const SafeJsonParse = require('./SafeJsonParse')
|
|
|
|
const EventLogger = require('./EventLogger')
|
|
|
|
const HealthCheckManager = require('./HealthCheckManager')
|
|
|
|
const RoomManager = require('./RoomManager')
|
|
|
|
const ChannelManager = require('./ChannelManager')
|
|
|
|
const ConnectedUsersManager = require('./ConnectedUsersManager')
|
2019-10-30 09:52:36 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
const RESTRICTED_USER_MESSAGE_TYPE_PASS_LIST = [
|
2020-06-23 13:29:44 -04:00
|
|
|
'otUpdateApplied',
|
|
|
|
'otUpdateError',
|
|
|
|
'joinDoc',
|
|
|
|
'reciveNewDoc',
|
|
|
|
'reciveNewFile',
|
|
|
|
'reciveNewFolder',
|
2023-07-24 08:11:51 -04:00
|
|
|
'reciveEntityMove',
|
|
|
|
'reciveEntityRename',
|
2021-07-13 07:04:45 -04:00
|
|
|
'removeEntity',
|
2022-03-24 05:58:49 -04:00
|
|
|
'accept-changes',
|
2023-07-24 08:11:51 -04:00
|
|
|
'projectNameUpdated',
|
|
|
|
'rootDocUpdated',
|
|
|
|
'toggle-track-changes',
|
|
|
|
'projectRenamedOrDeletedByExternalSource',
|
2020-06-23 13:29:44 -04:00
|
|
|
]
|
2019-10-04 08:41:49 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
let WebsocketLoadBalancer
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = WebsocketLoadBalancer = {
|
|
|
|
rclientPubList: RedisClientManager.createClientList(Settings.redis.pubsub),
|
|
|
|
rclientSubList: RedisClientManager.createClientList(Settings.redis.pubsub),
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2023-05-17 05:42:40 -04:00
|
|
|
shouldDisconnectClient(client, message) {
|
|
|
|
const userId = client.ol_context.user_id
|
|
|
|
if (message?.message === 'userRemovedFromProject') {
|
|
|
|
if (message?.payload?.includes(userId)) {
|
|
|
|
return true
|
|
|
|
}
|
2023-05-30 08:21:55 -04:00
|
|
|
} else if (message?.message === 'project:publicAccessLevel:changed') {
|
|
|
|
const [info] = message.payload
|
|
|
|
if (
|
|
|
|
info.newAccessLevel === 'private' &&
|
|
|
|
!client.ol_context.is_invited_member
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
2024-04-17 03:05:17 -04:00
|
|
|
} else if (message?.message === 'project:collaboratorAccessLevel:changed') {
|
|
|
|
const changedUserId = message.payload[0].userId
|
|
|
|
return userId === changedUserId
|
2023-05-17 05:42:40 -04:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
emitToRoom(roomId, message, ...payload) {
|
|
|
|
if (!roomId) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.warn(
|
|
|
|
{ message, payload },
|
|
|
|
'no room_id provided, ignoring emitToRoom'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const data = JSON.stringify({
|
2023-03-20 10:10:40 -04:00
|
|
|
room_id: roomId,
|
2020-06-23 13:29:44 -04:00
|
|
|
message,
|
2021-07-13 07:04:45 -04:00
|
|
|
payload,
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ roomId, message, payload, length: data.length },
|
2020-06-23 13:29:44 -04:00
|
|
|
'emitting to room'
|
|
|
|
)
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
this.rclientPubList.map(rclientPub =>
|
2023-03-20 10:10:40 -04:00
|
|
|
ChannelManager.publish(rclientPub, 'editor-events', roomId, data)
|
2020-06-23 13:29:44 -04:00
|
|
|
)
|
|
|
|
},
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
emitToAll(message, ...payload) {
|
2020-07-07 06:06:02 -04:00
|
|
|
this.emitToRoom('all', message, ...payload)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
listenForEditorEvents(io) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{ rclients: this.rclientSubList.length },
|
|
|
|
'listening for editor events'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
for (const rclientSub of this.rclientSubList) {
|
2020-06-23 13:29:44 -04:00
|
|
|
rclientSub.subscribe('editor-events')
|
|
|
|
rclientSub.on('message', function (channel, message) {
|
|
|
|
if (Settings.debugEvents > 0) {
|
|
|
|
EventLogger.debugEvent(channel, message)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketLoadBalancer._processEditorEvent(io, channel, message)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
this.handleRoomUpdates(this.rclientSubList)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
handleRoomUpdates(rclientSubList) {
|
|
|
|
const roomEvents = RoomManager.eventSource()
|
2023-03-20 10:10:40 -04:00
|
|
|
roomEvents.on('project-active', function (projectId) {
|
2021-07-13 07:04:45 -04:00
|
|
|
const subscribePromises = rclientSubList.map(rclient =>
|
2023-03-20 10:10:40 -04:00
|
|
|
ChannelManager.subscribe(rclient, 'editor-events', projectId)
|
2020-06-23 13:29:44 -04:00
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
RoomManager.emitOnCompletion(
|
2020-06-23 13:29:44 -04:00
|
|
|
subscribePromises,
|
2023-03-20 10:10:40 -04:00
|
|
|
`project-subscribed-${projectId}`
|
2020-06-23 13:29:44 -04:00
|
|
|
)
|
|
|
|
})
|
2023-03-20 10:10:40 -04:00
|
|
|
roomEvents.on('project-empty', projectId =>
|
2021-07-13 07:04:45 -04:00
|
|
|
rclientSubList.map(rclient =>
|
2023-03-20 10:10:40 -04:00
|
|
|
ChannelManager.unsubscribe(rclient, 'editor-events', projectId)
|
2020-06-23 13:29:44 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
_processEditorEvent(io, channel, message) {
|
2020-07-07 06:06:02 -04:00
|
|
|
SafeJsonParse.parse(message, function (error, message) {
|
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.error({ err: error, channel }, 'error parsing JSON')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (message.room_id === 'all') {
|
2020-07-07 06:06:02 -04:00
|
|
|
io.sockets.emit(message.message, ...message.payload)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else if (
|
|
|
|
message.message === 'clientTracking.refresh' &&
|
2020-07-07 06:06:02 -04:00
|
|
|
message.room_id
|
2020-06-23 13:29:44 -04:00
|
|
|
) {
|
2020-07-07 06:06:02 -04:00
|
|
|
const clientList = io.sockets.clients(message.room_id)
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
|
|
|
channel,
|
|
|
|
message: message.message,
|
2023-03-20 10:10:40 -04:00
|
|
|
roomId: message.room_id,
|
|
|
|
messageId: message._id,
|
2021-07-13 07:04:45 -04:00
|
|
|
socketIoClients: clientList.map(client => client.id),
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'refreshing client list'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
for (const client of clientList) {
|
|
|
|
ConnectedUsersManager.refreshClient(message.room_id, client.publicId)
|
|
|
|
}
|
|
|
|
} else if (message.room_id) {
|
|
|
|
if (message._id && Settings.checkEventOrder) {
|
2020-06-23 13:29:44 -04:00
|
|
|
const status = EventLogger.checkEventOrder(
|
|
|
|
'editor-events',
|
|
|
|
message._id,
|
|
|
|
message
|
|
|
|
)
|
|
|
|
if (status === 'duplicate') {
|
|
|
|
return // skip duplicate events
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 08:39:34 -05:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
const isRestrictedMessage =
|
2021-07-13 07:04:45 -04:00
|
|
|
!RESTRICTED_USER_MESSAGE_TYPE_PASS_LIST.includes(message.message)
|
2020-02-24 08:39:34 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// send messages only to unique clients (due to duplicate entries in io.sockets.clients)
|
2023-05-30 08:21:55 -04:00
|
|
|
const clientList = io.sockets.clients(message.room_id)
|
2020-02-24 08:39:34 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// avoid unnecessary work if no clients are connected
|
|
|
|
if (clientList.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
|
|
|
channel,
|
|
|
|
message: message.message,
|
2023-03-20 10:10:40 -04:00
|
|
|
roomId: message.room_id,
|
|
|
|
messageId: message._id,
|
2021-07-13 07:04:45 -04:00
|
|
|
socketIoClients: clientList.map(client => client.id),
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'distributing event to clients'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
const seen = new Map()
|
|
|
|
for (const client of clientList) {
|
|
|
|
if (!seen.has(client.id)) {
|
|
|
|
seen.set(client.id, true)
|
2023-05-17 05:42:40 -04:00
|
|
|
if (WebsocketLoadBalancer.shouldDisconnectClient(client, message)) {
|
|
|
|
logger.debug(
|
|
|
|
{
|
|
|
|
message,
|
|
|
|
userId: client?.ol_context?.user_id,
|
|
|
|
projectId: client?.ol_context?.project_id,
|
|
|
|
},
|
|
|
|
'disconnecting client'
|
|
|
|
)
|
2024-04-17 03:05:17 -04:00
|
|
|
if (
|
|
|
|
message?.message !== 'project:collaboratorAccessLevel:changed'
|
|
|
|
) {
|
|
|
|
client.emit('project:access:revoked')
|
|
|
|
}
|
2023-05-17 05:42:40 -04:00
|
|
|
client.disconnect()
|
|
|
|
} else {
|
2023-06-05 06:18:00 -04:00
|
|
|
if (isRestrictedMessage && client.ol_context.is_restricted_user) {
|
|
|
|
// hide restricted message
|
|
|
|
logger.debug(
|
|
|
|
{
|
|
|
|
message,
|
|
|
|
clientId: client.id,
|
|
|
|
userId: client.ol_context.user_id,
|
|
|
|
projectId: client.ol_context.project_id,
|
|
|
|
},
|
|
|
|
'hiding restricted message from client'
|
|
|
|
)
|
|
|
|
} else {
|
2023-05-30 08:21:55 -04:00
|
|
|
client.emit(message.message, ...message.payload)
|
|
|
|
}
|
2023-05-17 05:42:40 -04:00
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
}
|
|
|
|
} else if (message.health_check) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.debug(
|
|
|
|
{ message },
|
|
|
|
'got health check message in editor events channel'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
HealthCheckManager.check(channel, message.key)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|