2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:30:29 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:30:45 -04:00
|
|
|
let Client
|
|
|
|
const { XMLHttpRequest } = require('../../libs/XMLHttpRequest')
|
|
|
|
const io = require('socket.io-client')
|
|
|
|
const async = require('async')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
const request = require('request')
|
2021-07-12 12:47:18 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-11-10 06:32:06 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-06-23 13:30:45 -04:00
|
|
|
const rclient = redis.createClient(Settings.redis.websessions)
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
const uid = require('uid-safe').sync
|
|
|
|
const signature = require('cookie-signature')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
io.util.request = function () {
|
|
|
|
const xhr = new XMLHttpRequest()
|
|
|
|
const _open = xhr.open
|
|
|
|
xhr.open = function () {
|
|
|
|
_open.apply(xhr, arguments)
|
|
|
|
if (Client.cookie != null) {
|
|
|
|
return xhr.setRequestHeader('Cookie', Client.cookie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xhr
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
module.exports = Client = {
|
|
|
|
cookie: null,
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
setSession(session, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
const sessionId = uid(24)
|
|
|
|
session.cookie = {}
|
2021-07-13 07:04:45 -04:00
|
|
|
return rclient.set('sess:' + sessionId, JSON.stringify(session), error => {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
const secret = Settings.security.sessionSecret
|
|
|
|
const cookieKey = 's:' + signature.sign(sessionId, secret)
|
|
|
|
Client.cookie = `${Settings.cookieName}=${cookieKey}`
|
|
|
|
return callback()
|
|
|
|
})
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
unsetSession(callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
Client.cookie = null
|
|
|
|
return callback()
|
|
|
|
},
|
2020-02-24 07:30:24 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
connect(cookie) {
|
|
|
|
const client = io.connect('http://localhost:3026', {
|
2021-07-13 07:04:45 -04:00
|
|
|
'force new connection': true,
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
client.on(
|
|
|
|
'connectionAccepted',
|
|
|
|
(_, publicId) => (client.publicId = publicId)
|
|
|
|
)
|
|
|
|
return client
|
|
|
|
},
|
2020-02-24 07:30:24 -05:00
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
getConnectedClients(callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url: 'http://localhost:3026/clients',
|
2021-07-13 07:04:45 -04:00
|
|
|
json: true,
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
(error, response, data) => callback(error, data)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getConnectedClient(client_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url: `http://localhost:3026/clients/${client_id}`,
|
2021-07-13 07:04:45 -04:00
|
|
|
json: true,
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
(error, response, data) => callback(error, data)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnectClient(client_id, callback) {
|
|
|
|
request.post(
|
|
|
|
{
|
|
|
|
url: `http://localhost:3026/client/${client_id}/disconnect`,
|
|
|
|
},
|
|
|
|
(error, response, data) => callback(error, data)
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnectAllClients(callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
return Client.getConnectedClients((error, clients) => {
|
|
|
|
if (error) return callback(error)
|
2020-06-23 13:30:45 -04:00
|
|
|
async.each(
|
|
|
|
clients,
|
|
|
|
(clientView, cb) => Client.disconnectClient(clientView.client_id, cb),
|
|
|
|
callback
|
|
|
|
)
|
2021-10-27 05:49:18 -04:00
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|