2014-11-07 12:38:12 -05:00
|
|
|
XMLHttpRequest = require("../../libs/XMLHttpRequest").XMLHttpRequest
|
|
|
|
io = require("socket.io-client")
|
2020-02-24 07:30:24 -05:00
|
|
|
async = require("async")
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2014-11-13 06:48:49 -05:00
|
|
|
request = require "request"
|
2014-11-07 12:38:12 -05:00
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
redis = require "redis-sharelatex"
|
2018-05-23 10:00:46 -04:00
|
|
|
rclient = redis.createClient(Settings.redis.websessions)
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
uid = require('uid-safe').sync
|
|
|
|
signature = require("cookie-signature")
|
|
|
|
|
|
|
|
io.util.request = () ->
|
|
|
|
xhr = new XMLHttpRequest()
|
|
|
|
_open = xhr.open
|
|
|
|
xhr.open = () =>
|
|
|
|
_open.apply(xhr, arguments)
|
|
|
|
if Client.cookie?
|
|
|
|
xhr.setRequestHeader("Cookie", Client.cookie)
|
|
|
|
return xhr
|
|
|
|
|
|
|
|
module.exports = Client =
|
|
|
|
cookie: null
|
|
|
|
|
|
|
|
setSession: (session, callback = (error) ->) ->
|
|
|
|
sessionId = uid(24)
|
|
|
|
session.cookie = {}
|
|
|
|
rclient.set "sess:" + sessionId, JSON.stringify(session), (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
secret = Settings.security.sessionSecret
|
|
|
|
cookieKey = 's:' + signature.sign(sessionId, secret)
|
|
|
|
Client.cookie = "#{Settings.cookieName}=#{cookieKey}"
|
|
|
|
callback()
|
|
|
|
|
|
|
|
unsetSession: (callback = (error) ->) ->
|
|
|
|
Client.cookie = null
|
|
|
|
callback()
|
|
|
|
|
|
|
|
connect: (cookie) ->
|
|
|
|
client = io.connect("http://localhost:3026", 'force new connection': true)
|
2020-06-04 10:52:13 -04:00
|
|
|
client.on 'connectionAccepted', (_, publicId) ->
|
|
|
|
client.publicId = publicId
|
2014-11-07 12:38:12 -05:00
|
|
|
return client
|
2014-11-13 06:48:49 -05:00
|
|
|
|
|
|
|
getConnectedClients: (callback = (error, clients) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "http://localhost:3026/clients"
|
|
|
|
json: true
|
|
|
|
}, (error, response, data) ->
|
|
|
|
callback error, data
|
|
|
|
|
|
|
|
getConnectedClient: (client_id, callback = (error, clients) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "http://localhost:3026/clients/#{client_id}"
|
|
|
|
json: true
|
|
|
|
}, (error, response, data) ->
|
|
|
|
callback error, data
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-02-24 07:30:24 -05:00
|
|
|
|
|
|
|
disconnectClient: (client_id, callback) ->
|
|
|
|
request.post {
|
|
|
|
url: "http://localhost:3026/client/#{client_id}/disconnect"
|
|
|
|
auth: {
|
|
|
|
user: Settings.internal.realTime.user,
|
|
|
|
pass: Settings.internal.realTime.pass
|
|
|
|
}
|
|
|
|
}, (error, response, data) ->
|
|
|
|
callback error, data
|
|
|
|
return null
|
|
|
|
|
|
|
|
disconnectAllClients: (callback) ->
|
|
|
|
Client.getConnectedClients (error, clients) ->
|
|
|
|
async.each clients, (clientView, cb) ->
|
|
|
|
Client.disconnectClient clientView.client_id, cb
|
|
|
|
, callback
|