Skip to content

Commit

Permalink
feat: Added '@cocreate-server' handle http and https
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Dec 31, 2023
1 parent b830e27 commit 5f530b1
Showing 1 changed file with 78 additions and 75 deletions.
153 changes: 78 additions & 75 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,94 +25,97 @@ class SocketServer extends EventEmitter {
socket.destroy();
});

server.on('upgrade', (request, socket, head) => {
const self = this;
let organization_id = request.url.split('/')
organization_id = organization_id[organization_id.length - 1]

this.wss.handleUpgrade(request, socket, head, async function (socket) {
if (organization_id) {
let organization = self.organizations.get(organization_id)
if (organization && organization.status === false) {
let errors = {}
errors.serverOrganization = organization.serverOrganization
errors.serverStorage = organization.serverStorage
errors.organizationBalance = organization.organizationBalance
errors.error = organization.error
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
}
server.https.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head))
server.http.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head))
}

upgrade(request, socket, head) {
const self = this;
let organization_id = request.url.split('/')
organization_id = organization_id[organization_id.length - 1]

let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
options = JSON.parse(options)
this.wss.handleUpgrade(request, socket, head, async function (socket) {
if (organization_id) {
let organization = self.organizations.get(organization_id)
if (organization && organization.status === false) {
let errors = {}
errors.serverOrganization = organization.serverOrganization
errors.serverStorage = organization.serverStorage
errors.organizationBalance = organization.organizationBalance
errors.error = organization.error
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
}

socket.organization_id = organization_id
socket.id = options.socketId;
socket.clientId = options.clientId;
socket.pathname = request.url
socket.origin = request.headers.origin || request.headers.host
let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
options = JSON.parse(options)

if (socket.origin.startsWith('http'))
socket.origin = socket.origin.replace('http', 'ws')
socket.organization_id = organization_id
socket.id = options.socketId;
socket.clientId = options.clientId;
socket.pathname = request.url
socket.origin = request.headers.origin || request.headers.host

socket.socketUrl = socket.origin + socket.pathname
if (socket.origin.startsWith('http'))
socket.origin = socket.origin.replace('http', 'ws')

if (socket.origin && socket.origin !== 'null') {
if (socket.origin.includes('://'))
socket.host = new URL(socket.origin).host
else
socket.host = socket.origin;
}
socket.socketUrl = socket.origin + socket.pathname

if (!await self.acme.checkCertificate(socket.host, organization_id))
return socket.send(JSON.stringify({ method: 'Access Denied', error: 'Host not whitelisted' }))

if (!organization || organization && organization.status !== false) {
let data = {
socket,
method: 'object.read',
array: 'message_log',
$filter: {
sort: [
{ key: '_id', direction: 'desc' }
]
},
sync: true,
organization_id
}
if (socket.origin && socket.origin !== 'null') {
if (socket.origin.includes('://'))
socket.host = new URL(socket.origin).host
else
socket.host = socket.origin;
}

if (options.lastSynced)
data.$filter.query = [
{ key: '_id', value: options.lastSynced, operator: '$gt' }
if (!await self.acme.checkCertificate(socket.host, organization_id))
return socket.send(JSON.stringify({ method: 'Access Denied', error: 'Host not whitelisted' }))

if (!organization || organization && organization.status !== false) {
let data = {
socket,
method: 'object.read',
array: 'message_log',
$filter: {
sort: [
{ key: '_id', direction: 'desc' }
]
else
data.$filter.limit = 1

self.emit('object.read', data);

if (self.authenticate) {
const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
if (user_id) {
options.user_id = user_id
socket.user_id = user_id;
socket.expires = expires;
userStatus.userStatus = 'on'
self.emit("notification.user", socket)
}
},
sync: true,
organization_id
}

self.emit('userStatus', userStatus);
if (options.lastSynced)
data.$filter.query = [
{ key: '_id', value: options.lastSynced, operator: '$gt' }
]
else
data.$filter.limit = 1

self.emit('object.read', data);

if (self.authenticate) {
const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
if (user_id) {
options.user_id = user_id
socket.user_id = user_id;
socket.expires = expires;
userStatus.userStatus = 'on'
self.emit("notification.user", socket)
}

self.onWebSocket(socket);
self.emit('userStatus', userStatus);

} else
self.onWebSocket(socket);
}
self.onWebSocket(socket);

} else {
socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
} else
self.onWebSocket(socket);
}
})
});

} else {
socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
}
})
}

onWebSocket(socket) {
Expand Down

0 comments on commit 5f530b1

Please sign in to comment.