@cocreate/socket-server 1.24.3 → 1.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/package.json +1 -1
- package/src/index.js +78 -72
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
# [1.26.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.25.0...v1.26.0) (2024-01-03)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* comment checkCertificate ([f70e8de](https://github.com/CoCreate-app/CoCreate-socket-server/commit/f70e8def302498d1f93cc4badde012333bbe8ea0))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Added '[@cocreate-server](https://github.com/cocreate-server)' handle http and https ([5f530b1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/5f530b135cb25a3af51fe059af57a235d0c3d51c))
|
|
12
|
+
|
|
13
|
+
# [1.25.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.24.3...v1.25.0) (2023-12-31)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Features
|
|
17
|
+
|
|
18
|
+
* Added '@cocreate/acme' for dynamic ssl management ([f01831b](https://github.com/CoCreate-app/CoCreate-socket-server/commit/f01831bf2d3a8a4812c369fb61927667af56f7d5))
|
|
19
|
+
|
|
1
20
|
## [1.24.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.24.2...v1.24.3) (2023-12-18)
|
|
2
21
|
|
|
3
22
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -25,91 +25,97 @@ class SocketServer extends EventEmitter {
|
|
|
25
25
|
socket.destroy();
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
server.on('upgrade', (request, socket, head) =>
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (organization && organization.status === false) {
|
|
37
|
-
let errors = {}
|
|
38
|
-
errors.serverOrganization = organization.serverOrganization
|
|
39
|
-
errors.serverStorage = organization.serverStorage
|
|
40
|
-
errors.organizationBalance = organization.organizationBalance
|
|
41
|
-
errors.error = organization.error
|
|
42
|
-
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
|
|
43
|
-
}
|
|
28
|
+
server.https.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head))
|
|
29
|
+
server.http.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
upgrade(request, socket, head) {
|
|
33
|
+
const self = this;
|
|
34
|
+
let organization_id = request.url.split('/')
|
|
35
|
+
organization_id = organization_id[organization_id.length - 1]
|
|
44
36
|
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
this.wss.handleUpgrade(request, socket, head, async function (socket) {
|
|
38
|
+
if (organization_id) {
|
|
39
|
+
let organization = self.organizations.get(organization_id)
|
|
40
|
+
if (organization && organization.status === false) {
|
|
41
|
+
let errors = {}
|
|
42
|
+
errors.serverOrganization = organization.serverOrganization
|
|
43
|
+
errors.serverStorage = organization.serverStorage
|
|
44
|
+
errors.organizationBalance = organization.organizationBalance
|
|
45
|
+
errors.error = organization.error
|
|
46
|
+
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
|
|
47
|
+
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
socket.clientId = options.clientId;
|
|
51
|
-
socket.pathname = request.url
|
|
52
|
-
socket.origin = request.headers.origin || request.headers.host
|
|
49
|
+
let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
|
|
50
|
+
options = JSON.parse(options)
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
socket.organization_id = organization_id
|
|
53
|
+
socket.id = options.socketId;
|
|
54
|
+
socket.clientId = options.clientId;
|
|
55
|
+
socket.pathname = request.url
|
|
56
|
+
socket.origin = request.headers.origin || request.headers.host
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
if (socket.origin.startsWith('http'))
|
|
59
|
+
socket.origin = socket.origin.replace('http', 'ws')
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
if (socket.origin.includes('://'))
|
|
61
|
-
socket.host = new URL(socket.origin).host
|
|
62
|
-
else
|
|
63
|
-
socket.host = socket.origin;
|
|
64
|
-
}
|
|
61
|
+
socket.socketUrl = socket.origin + socket.pathname
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
sort: [
|
|
73
|
-
{ key: '_id', direction: 'desc' }
|
|
74
|
-
]
|
|
75
|
-
},
|
|
76
|
-
sync: true,
|
|
77
|
-
organization_id
|
|
78
|
-
}
|
|
63
|
+
if (socket.origin && socket.origin !== 'null') {
|
|
64
|
+
if (socket.origin.includes('://'))
|
|
65
|
+
socket.host = new URL(socket.origin).host
|
|
66
|
+
else
|
|
67
|
+
socket.host = socket.origin;
|
|
68
|
+
}
|
|
79
69
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
70
|
+
// if (!await server.acme.checkCertificate(socket.host, organization_id))
|
|
71
|
+
// return socket.send(JSON.stringify({ method: 'Access Denied', error: 'Host not whitelisted' }))
|
|
72
|
+
|
|
73
|
+
if (!organization || organization && organization.status !== false) {
|
|
74
|
+
let data = {
|
|
75
|
+
socket,
|
|
76
|
+
method: 'object.read',
|
|
77
|
+
array: 'message_log',
|
|
78
|
+
$filter: {
|
|
79
|
+
sort: [
|
|
80
|
+
{ key: '_id', direction: 'desc' }
|
|
83
81
|
]
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (self.authenticate) {
|
|
90
|
-
const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
|
|
91
|
-
const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
|
|
92
|
-
if (user_id) {
|
|
93
|
-
options.user_id = user_id
|
|
94
|
-
socket.user_id = user_id;
|
|
95
|
-
socket.expires = expires;
|
|
96
|
-
userStatus.userStatus = 'on'
|
|
97
|
-
self.emit("notification.user", socket)
|
|
98
|
-
}
|
|
82
|
+
},
|
|
83
|
+
sync: true,
|
|
84
|
+
organization_id
|
|
85
|
+
}
|
|
99
86
|
|
|
100
|
-
|
|
87
|
+
if (options.lastSynced)
|
|
88
|
+
data.$filter.query = [
|
|
89
|
+
{ key: '_id', value: options.lastSynced, operator: '$gt' }
|
|
90
|
+
]
|
|
91
|
+
else
|
|
92
|
+
data.$filter.limit = 1
|
|
93
|
+
|
|
94
|
+
self.emit('object.read', data);
|
|
95
|
+
|
|
96
|
+
if (self.authenticate) {
|
|
97
|
+
const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
|
|
98
|
+
const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
|
|
99
|
+
if (user_id) {
|
|
100
|
+
options.user_id = user_id
|
|
101
|
+
socket.user_id = user_id;
|
|
102
|
+
socket.expires = expires;
|
|
103
|
+
userStatus.userStatus = 'on'
|
|
104
|
+
self.emit("notification.user", socket)
|
|
105
|
+
}
|
|
101
106
|
|
|
102
|
-
|
|
107
|
+
self.emit('userStatus', userStatus);
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
self.onWebSocket(socket);
|
|
106
|
-
}
|
|
109
|
+
self.onWebSocket(socket);
|
|
107
110
|
|
|
108
|
-
|
|
109
|
-
|
|
111
|
+
} else
|
|
112
|
+
self.onWebSocket(socket);
|
|
110
113
|
}
|
|
111
|
-
|
|
112
|
-
|
|
114
|
+
|
|
115
|
+
} else {
|
|
116
|
+
socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
|
|
117
|
+
}
|
|
118
|
+
})
|
|
113
119
|
}
|
|
114
120
|
|
|
115
121
|
onWebSocket(socket) {
|