iosignal 1.7.0 → 1.7.1
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/README.md +254 -247
- package/auth_file.json +13 -13
- package/auth_file.mjs +23 -23
- package/dist/iosignal.esm.js.map +1 -1
- package/dist/iosignal.min.js.map +1 -1
- package/index.js +19 -19
- package/indexWebBrowser.js +13 -13
- package/package.json +60 -59
- package/rollup.config.js +69 -69
- package/src/api/RedisAPI.js +65 -65
- package/src/api/api_constant.js +7 -7
- package/src/api/api_reply.js +31 -31
- package/src/api/api_sudo.js +91 -91
- package/src/auth/Auth_Env.js +76 -76
- package/src/auth/Auth_File.js +71 -71
- package/src/auth/Auth_Redis.js +44 -44
- package/src/auth/BohoAuth.js +164 -164
- package/src/client/CongPacket.js +141 -141
- package/src/client/IOCongSocket.js +81 -81
- package/src/client/IOCore.js +822 -822
- package/src/client/IOWS.js +80 -80
- package/src/client/IOWebSocket.js +111 -111
- package/src/common/constants.js +148 -148
- package/src/common/payload.js +81 -81
- package/src/common/quotaTable.js +68 -68
- package/src/common/util.js +61 -61
- package/src/server/FileLogger.js +30 -30
- package/src/server/Manager.js +433 -433
- package/src/server/Metric.js +155 -155
- package/src/server/Remote.js +207 -207
- package/src/server/RemoteCore.js +439 -439
- package/src/server/Server.js +163 -163
- package/src/server/serverOption.js +56 -56
- package/test-browser/favicon.ico +0 -0
- package/test-browser/index.html +96 -96
- package/test-browser/server.js +11 -11
- package/test-client-nodejs/client.cjs +17 -17
- package/test-client-nodejs/client.mjs +17 -17
- package/test-client-nodejs/client_api_reply.mjs +15 -15
- package/test-client-nodejs/stateChange.mjs +19 -19
- package/test-server/server-Auth_Env.js +19 -19
- package/test-server/server-Auth_File.js +12 -12
- package/test-server/server-api_reply.js +6 -6
- package/test-server/server-api_sudo.js +7 -7
- package/test-server/server-commonjs.cjs +8 -8
- package/test-server/server-esm.mjs +20 -20
- package/test-server/simple-server.mjs +11 -11
- package/test-signal-payloadType/signalPack.js +34 -34
- package/test-signal-payloadType/signaling.js +60 -60
package/src/server/Manager.js
CHANGED
|
@@ -1,433 +1,433 @@
|
|
|
1
|
-
import { MBP } from 'meta-buffer-pack'
|
|
2
|
-
import { Remote } from './Remote.js'
|
|
3
|
-
import { serverOption } from './serverOption.js'
|
|
4
|
-
import { IOMsg, CLIENT_STATE } from '../common/constants.js'
|
|
5
|
-
import { getSignalPack } from '../common/payload.js';
|
|
6
|
-
import { FileLogger } from './FileLogger.js'
|
|
7
|
-
import { Metric } from './Metric.js'
|
|
8
|
-
|
|
9
|
-
const encoder = new TextEncoder()
|
|
10
|
-
const decoder = new TextDecoder()
|
|
11
|
-
|
|
12
|
-
export class Manager {
|
|
13
|
-
|
|
14
|
-
constructor(server, authManager) {
|
|
15
|
-
this.server = server;
|
|
16
|
-
this.txBytes = 0;
|
|
17
|
-
this.rxBytes = 0;
|
|
18
|
-
|
|
19
|
-
this.authManager = authManager;
|
|
20
|
-
|
|
21
|
-
this.connectionLogger;
|
|
22
|
-
if (serverOption.fileLogger.connection.use) {
|
|
23
|
-
this.connectionLogger = new FileLogger(serverOption.fileLogger.connection.path)
|
|
24
|
-
// console.log('Manager: begin file logger [connection]')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
this.attackLogger;
|
|
28
|
-
if (serverOption.fileLogger.attack.use) {
|
|
29
|
-
this.attackLogger = new FileLogger(serverOption.fileLogger.attack.path)
|
|
30
|
-
// console.log('Manager: begin file logger.[attack]')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
this.remotes = new Set(); // total remotes
|
|
34
|
-
this.cid2remote = new Map(); // key: cid -> value: remote
|
|
35
|
-
this.retain_messages = new Map() // key: tag, data: signalMessage
|
|
36
|
-
this.channel_map = new Map() // key: channelName -> value: < remote : Set >
|
|
37
|
-
this.CHANNEL_PREFIX = 'CH:'
|
|
38
|
-
this.CID_PREFIX = 'CID:'
|
|
39
|
-
this.metric = new Metric(this)
|
|
40
|
-
this.lastSSID = 0;
|
|
41
|
-
|
|
42
|
-
this.pingIntervalID = setInterval(e => {
|
|
43
|
-
this.remotes.forEach(function each(remo) {
|
|
44
|
-
let socket = remo.socket;
|
|
45
|
-
if (socket.isAlive === false) {
|
|
46
|
-
console.log('## timeout. cid:', remo.cid)
|
|
47
|
-
remo.close();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
socket.txCounter++;
|
|
51
|
-
socket.isAlive = false;
|
|
52
|
-
if (remo.socketType === 'websocket') {
|
|
53
|
-
socket.ping();
|
|
54
|
-
} else {
|
|
55
|
-
remo.ping()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
}, serverOption.timeout);
|
|
60
|
-
|
|
61
|
-
if (serverOption.showMetric) {
|
|
62
|
-
this.monitIntervalID = setInterval((e) => {
|
|
63
|
-
this.monitor()
|
|
64
|
-
}, serverOption.monitorPeriod);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
addRemote(socket, req) {
|
|
72
|
-
socket.isAlive = true;
|
|
73
|
-
let remote = new Remote(socket, req, this)
|
|
74
|
-
this.remotes.add(remote)
|
|
75
|
-
remote.send(Buffer.from([IOMsg.SERVER_READY]))
|
|
76
|
-
remote.setState(CLIENT_STATE.SENT_SERVER_READY)
|
|
77
|
-
this.lastSSID = remote.ssid;
|
|
78
|
-
let connectionInfo = `+ IP:${remote.ip} #${remote.ssid} ${socket.socketType === 'websocket' ? "WS" : "CS"} `;
|
|
79
|
-
// console.log( connectionInfo)
|
|
80
|
-
if (this.connectionLogger) this.connectionLogger.log(connectionInfo)
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
removeRemote(remote) {
|
|
85
|
-
|
|
86
|
-
let remoteInfo = `- IP:${remote.ip} #${remote.ssid} cid:${remote.cid} ${remote?.socket.socketType === 'websocket' ? "WS" : "CS"} `
|
|
87
|
-
if (this.connectionLogger) this.connectionLogger.log(remoteInfo)
|
|
88
|
-
this.deligateSignal(remote, '@state', 'close')
|
|
89
|
-
this.deligateSignal(remote, '@$state', 'close')
|
|
90
|
-
|
|
91
|
-
// remove subscriber
|
|
92
|
-
for (let ch of remote.channels.keys()) {
|
|
93
|
-
if (this.channel_map.has(ch)) {
|
|
94
|
-
const remotes = this.channel_map.get(ch)
|
|
95
|
-
// console.log(`-- channel [ ${ch} ] removes subscriber id: ${remote.cid} `)
|
|
96
|
-
remotes.delete(remote)
|
|
97
|
-
if (remotes.size == 0) this.channel_map.delete(ch)
|
|
98
|
-
} else {
|
|
99
|
-
// console.log('no such a ch: ', ch )
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// retain message policy
|
|
104
|
-
// type 1. ( isAuthorizedUser) ? keep : delete.
|
|
105
|
-
if (remote.boho.isAuthorized) {
|
|
106
|
-
//keep retain signal.
|
|
107
|
-
} else {
|
|
108
|
-
// anonymouse: remove the cid publish channel
|
|
109
|
-
for (let ch of remote.retain_signal.keys()) {
|
|
110
|
-
// console.log('cid retain_signal.keys:', ch )
|
|
111
|
-
ch = this.CID_PREFIX + remote.cid + '@' + ch
|
|
112
|
-
if (this.channel_map.has(ch)) {
|
|
113
|
-
this.channel_map.delete(ch)
|
|
114
|
-
// console.log(`-- deleted cid pub ch [ ${ch} ]. cid publisher id: ${remote.cid} `)
|
|
115
|
-
} else {
|
|
116
|
-
// console.log('no such a retain_signal tag: ', ch )
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (remote.uid && this.server.apiNames.has('account')) {
|
|
123
|
-
let req = { topic: 'detachUserRemote' }
|
|
124
|
-
req.args = ['caller:manager.removeRemote']
|
|
125
|
-
this.server.emit('account', remote, req)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
this.remotes.delete(remote)
|
|
129
|
-
this.cid2remote.delete(remote.cid)
|
|
130
|
-
remote = null
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
// it's like unix wall command.
|
|
136
|
-
serverSignal(obj) {
|
|
137
|
-
let sigPack = MBP.pack(
|
|
138
|
-
MBP.MB('#MsgType', '8', IOMsg.SERVER_SIGNAL),
|
|
139
|
-
MBP.MB('#signalObject', obj)
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
this.remotes.forEach(remote => {
|
|
143
|
-
remote.send_enc_mode(sigPack)
|
|
144
|
-
})
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
getSignalTag(buffer) {
|
|
148
|
-
let tagLen = buffer.readUint8(1)
|
|
149
|
-
let tagBuf = buffer.subarray(2, 2 + tagLen)
|
|
150
|
-
let tag = decoder.decode(tagBuf)
|
|
151
|
-
return tag
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
getNewSignalTagMessage(buffer, newTag) {
|
|
155
|
-
let msgType = buffer[0]
|
|
156
|
-
let tagLen = buffer.readUint8(1)
|
|
157
|
-
let newTagBuf = encoder.encode(newTag)
|
|
158
|
-
let newTagLen = newTagBuf.byteLength;
|
|
159
|
-
let payloadCunk = buffer.subarray(2 + tagLen)
|
|
160
|
-
let newBuffer = Buffer.concat([Buffer.from([msgType, newTagLen]), newTagBuf, payloadCunk])
|
|
161
|
-
return newBuffer
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// signaling to the cid subscribers.
|
|
166
|
-
// example. sending cid close signal.
|
|
167
|
-
deligateSignal(remote, tag, ...args) {
|
|
168
|
-
let sigPack = getSignalPack(tag, ...args)
|
|
169
|
-
this.sender(tag, remote, sigPack)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
adminSignal(cid, message) {
|
|
173
|
-
if (this.cid2remote.has(cid)) {
|
|
174
|
-
this.cid2remote.get(cid).send_enc_mode(message)
|
|
175
|
-
return
|
|
176
|
-
} else {
|
|
177
|
-
return "no cid"
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
serverSignalTo(tag, ...args) {
|
|
182
|
-
let cid = tag.split('@')[0]
|
|
183
|
-
let topic = tag.split('@')[1]
|
|
184
|
-
let sigPack = getSignalPack('@' + topic, ...args)
|
|
185
|
-
if (cid && this.cid2remote.has(cid)) {
|
|
186
|
-
let targetRemote = this.cid2remote.get(cid)
|
|
187
|
-
console.log('target', targetRemote.state, targetRemote.cid)
|
|
188
|
-
targetRemote.send_enc_mode(sigPack)
|
|
189
|
-
return
|
|
190
|
-
} else {
|
|
191
|
-
return "no cid"
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
sender(tag, remote, message) {
|
|
196
|
-
if (serverOption.memberOnly && !remote.boho.isAuthorized) {
|
|
197
|
-
// console.log("## MemberOnly, reject unAuthorized remote." )
|
|
198
|
-
remote.send(Buffer.from([IOMsg.SERVER_CLEAR_AUTH]))
|
|
199
|
-
remote.close()
|
|
200
|
-
return ['err', 'not autrized']
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (tag.indexOf('$') == 0) return ['err', 'prefix $ is reserved for userSet tag.']
|
|
204
|
-
|
|
205
|
-
let cidIndex = tag.indexOf('@');
|
|
206
|
-
if (cidIndex === 0) {
|
|
207
|
-
// ** CID_PUB multi-cast
|
|
208
|
-
// [cid_pub] @topic , @$retainTopic
|
|
209
|
-
// modify signalpack with cid_appneded tag.
|
|
210
|
-
tag = remote.cid + tag;
|
|
211
|
-
message = this.getNewSignalTagMessage(message, tag)
|
|
212
|
-
|
|
213
|
-
} else if (cidIndex > 0) {
|
|
214
|
-
// uni-cast.
|
|
215
|
-
let targetCId = tag.split('@')[0];
|
|
216
|
-
if (this.cid2remote.has(targetCId)) {
|
|
217
|
-
//rm cid from tag.
|
|
218
|
-
let ommitCIdTag = '@' + tag.split('@')[1]
|
|
219
|
-
// console.log('uni-cast tag change from', tag, 'to' , ommitCIdTag )
|
|
220
|
-
message = this.getNewSignalTagMessage(message, ommitCIdTag)
|
|
221
|
-
// console.log(`origin tag: ${tag} omitTag: ${this.getSignalTag(message)}`)
|
|
222
|
-
this.cid2remote.get(targetCId).send_enc_mode(message)
|
|
223
|
-
return ['ok', 1]
|
|
224
|
-
}
|
|
225
|
-
return ['err', 'Invalid cid']
|
|
226
|
-
} else {
|
|
227
|
-
// console.log('ch_pub tag:', tag)
|
|
228
|
-
//else channel publish message
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* multi-cast:
|
|
234
|
-
* channel_publish
|
|
235
|
-
* or cid_publish
|
|
236
|
-
*/
|
|
237
|
-
|
|
238
|
-
// HOME_CHANNEL substitution.
|
|
239
|
-
// (blank)#topic -> home_channel#topic.
|
|
240
|
-
if (tag.indexOf('#') === 0) {
|
|
241
|
-
tag = remote.HOME_CHANNEL + tag;
|
|
242
|
-
} else if (tag.includes('@')) {
|
|
243
|
-
tag = this.CID_PREFIX + tag;
|
|
244
|
-
} else {
|
|
245
|
-
tag = this.CHANNEL_PREFIX + tag;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// retain signal message
|
|
249
|
-
if (tag.includes('$')
|
|
250
|
-
// && remote.boho.isAuthorized
|
|
251
|
-
&& serverOption.retain.isAvailable
|
|
252
|
-
&& serverOption.retain.limitCounter > this.retain_messages.size
|
|
253
|
-
&& serverOption.retain.limitSize > message.byteLength
|
|
254
|
-
) {
|
|
255
|
-
// cid retain signal
|
|
256
|
-
if (tag.includes('@')) {
|
|
257
|
-
// cid retain signal stored inside remote.
|
|
258
|
-
let retainTag = tag.split('@')[1]
|
|
259
|
-
remote.retain_signal.set(retainTag, message)
|
|
260
|
-
} else {
|
|
261
|
-
// channel retain signal stored inside manager.
|
|
262
|
-
this.retain_messages.set(tag, message)
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
} else {
|
|
266
|
-
// console.log('no retain. serverOption.retain:', serverOption.retain )
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
let sentCounter = 0;
|
|
270
|
-
if (this.channel_map.has(tag)) {
|
|
271
|
-
// console.log('raw channel_map matched tag:', tag)
|
|
272
|
-
let remotes = this.channel_map.get(tag);
|
|
273
|
-
if (remotes.size >= 1) {
|
|
274
|
-
let limit;
|
|
275
|
-
if (serverOption.useQuota.publishCounter) {
|
|
276
|
-
limit = Math.min(remote.quota.publishCounter, remotes.size)
|
|
277
|
-
} else {
|
|
278
|
-
limit = remotes.size;
|
|
279
|
-
}
|
|
280
|
-
let peers = remotes.values();
|
|
281
|
-
while (sentCounter < limit) {
|
|
282
|
-
let c = peers.next().value
|
|
283
|
-
if (!c) {
|
|
284
|
-
// console.log('## null remote' )
|
|
285
|
-
break;
|
|
286
|
-
}
|
|
287
|
-
if (c !== remote) {
|
|
288
|
-
c.send_enc_mode(message);
|
|
289
|
-
sentCounter++;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (serverOption.useQuota.publishCounter) {
|
|
294
|
-
// console.log(`pub >> [${tag}] sent: ${sentCounter} [use quota limit: ${remote.quota.publishCounter }] total: ${remotes.size} subscribers. ` )
|
|
295
|
-
} else {
|
|
296
|
-
// console.log(`pub >> [${tag}] sent: ${sentCounter} [no quota limit] total: ${remotes.size} subscribers. ` )
|
|
297
|
-
}
|
|
298
|
-
return ['ok', sentCounter]
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
// console.log('-- No subscriber. ch: ' , tag )
|
|
302
|
-
return ['err', 'No subscriber.']
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
subscribe(chArr, remote) {
|
|
308
|
-
chArr.forEach(tag => {
|
|
309
|
-
if (tag.indexOf('#') === 0) {
|
|
310
|
-
tag = remote.HOME_CHANNEL + tag
|
|
311
|
-
} else if (tag.includes('@')) {
|
|
312
|
-
tag = this.CID_PREFIX + tag;
|
|
313
|
-
} else {
|
|
314
|
-
tag = this.CHANNEL_PREFIX + tag
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// 1. set channel map
|
|
318
|
-
if (this.channel_map.has(tag)) {
|
|
319
|
-
this.channel_map.get(tag).add(remote)
|
|
320
|
-
} else {
|
|
321
|
-
this.channel_map.set(tag, new Set([remote]))
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// 2.add to remote channels.
|
|
325
|
-
remote.channels.add(tag)
|
|
326
|
-
|
|
327
|
-
// 3. send retain message if available.
|
|
328
|
-
if (tag.includes('$')
|
|
329
|
-
&& serverOption.retain.isAvailable
|
|
330
|
-
) {
|
|
331
|
-
let retainMessage;
|
|
332
|
-
if (tag.includes('@')) {
|
|
333
|
-
let cid = tag.split('@')[0]
|
|
334
|
-
cid = cid.split(this.CID_PREFIX)[1]
|
|
335
|
-
let retainTag = tag.split('@')[1]
|
|
336
|
-
if (this.cid2remote.has(cid)) {
|
|
337
|
-
retainMessage = this.cid2remote.get(cid).retain_signal.get(retainTag)
|
|
338
|
-
} else {
|
|
339
|
-
// console.log('cid subscribe is rejected. not online:', cid)
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
} else if (this.retain_messages.has(tag)) {
|
|
344
|
-
retainMessage = this.retain_messages.get(tag)
|
|
345
|
-
}
|
|
346
|
-
if (retainMessage) remote.send_enc_mode(retainMessage)
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
})
|
|
351
|
-
return remote.channels.size;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
unsubscribe(chArr, remote) {
|
|
355
|
-
//unsubscribe all channels of the remote.
|
|
356
|
-
if (chArr.length == 1 && chArr[0] == "") {
|
|
357
|
-
|
|
358
|
-
remote.channels.forEach(ch => {
|
|
359
|
-
if (this.channel_map.has(ch)) {
|
|
360
|
-
let remotes = this.channel_map.get(ch)
|
|
361
|
-
remotes.delete(remote)
|
|
362
|
-
if (remotes.size == 0) this.channel_map.delete(ch)
|
|
363
|
-
}
|
|
364
|
-
})
|
|
365
|
-
remote.channels.clear();
|
|
366
|
-
|
|
367
|
-
} else {
|
|
368
|
-
// unsubscribe each channels.
|
|
369
|
-
chArr.forEach(ch => {
|
|
370
|
-
// substitution home_channel
|
|
371
|
-
if (ch.indexOf('#') === 0) {
|
|
372
|
-
ch = remote.HOME_CHANNEL + ch;
|
|
373
|
-
} else if (ch.includes('@')) {
|
|
374
|
-
ch = this.CID_PREFIX + ch;
|
|
375
|
-
} else {
|
|
376
|
-
ch = this.CHANNEL_PREFIX + ch;
|
|
377
|
-
}
|
|
378
|
-
// console.log('-- Manager::unsubscribe:', ch )
|
|
379
|
-
|
|
380
|
-
// delete manager map.
|
|
381
|
-
if (this.channel_map.has(ch)) {
|
|
382
|
-
let remotes = this.channel_map.get(ch)
|
|
383
|
-
remotes.delete(remote)
|
|
384
|
-
if (remotes.size == 0) this.channel_map.delete(ch)
|
|
385
|
-
} else {
|
|
386
|
-
// console.log('##no such a ch', ch )
|
|
387
|
-
}
|
|
388
|
-
remote.channels.delete(ch)
|
|
389
|
-
})
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
// console.log( '-- result unsub:', remote.channels )
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
monitor() {
|
|
398
|
-
if (process.stdout.isTTY) {
|
|
399
|
-
process.stdout.cursorTo(0, 0)
|
|
400
|
-
process.stdout.clearScreenDown()
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
if (serverOption.showChannel > 0) {
|
|
404
|
-
this.metric.channels(serverOption.showChannel);
|
|
405
|
-
}
|
|
406
|
-
let mode = parseInt(serverOption.showMetric)
|
|
407
|
-
switch (mode) {
|
|
408
|
-
case 1:
|
|
409
|
-
this.metric.oneline(true);
|
|
410
|
-
break;
|
|
411
|
-
case 2:
|
|
412
|
-
this.metric.getRemotes(true);
|
|
413
|
-
break;
|
|
414
|
-
case 3:
|
|
415
|
-
this.metric.getChannelList(true);
|
|
416
|
-
break;
|
|
417
|
-
default:
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
closeRemoteByCId(cid) {
|
|
424
|
-
if (this.cid2remote.has(cid)) {
|
|
425
|
-
this.cid2remote.get(cid).close()
|
|
426
|
-
return 1
|
|
427
|
-
} else {
|
|
428
|
-
return 0
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
}
|
|
1
|
+
import { MBP } from 'meta-buffer-pack'
|
|
2
|
+
import { Remote } from './Remote.js'
|
|
3
|
+
import { serverOption } from './serverOption.js'
|
|
4
|
+
import { IOMsg, CLIENT_STATE } from '../common/constants.js'
|
|
5
|
+
import { getSignalPack } from '../common/payload.js';
|
|
6
|
+
import { FileLogger } from './FileLogger.js'
|
|
7
|
+
import { Metric } from './Metric.js'
|
|
8
|
+
|
|
9
|
+
const encoder = new TextEncoder()
|
|
10
|
+
const decoder = new TextDecoder()
|
|
11
|
+
|
|
12
|
+
export class Manager {
|
|
13
|
+
|
|
14
|
+
constructor(server, authManager) {
|
|
15
|
+
this.server = server;
|
|
16
|
+
this.txBytes = 0;
|
|
17
|
+
this.rxBytes = 0;
|
|
18
|
+
|
|
19
|
+
this.authManager = authManager;
|
|
20
|
+
|
|
21
|
+
this.connectionLogger;
|
|
22
|
+
if (serverOption.fileLogger.connection.use) {
|
|
23
|
+
this.connectionLogger = new FileLogger(serverOption.fileLogger.connection.path)
|
|
24
|
+
// console.log('Manager: begin file logger [connection]')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.attackLogger;
|
|
28
|
+
if (serverOption.fileLogger.attack.use) {
|
|
29
|
+
this.attackLogger = new FileLogger(serverOption.fileLogger.attack.path)
|
|
30
|
+
// console.log('Manager: begin file logger.[attack]')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.remotes = new Set(); // total remotes
|
|
34
|
+
this.cid2remote = new Map(); // key: cid -> value: remote
|
|
35
|
+
this.retain_messages = new Map() // key: tag, data: signalMessage
|
|
36
|
+
this.channel_map = new Map() // key: channelName -> value: < remote : Set >
|
|
37
|
+
this.CHANNEL_PREFIX = 'CH:'
|
|
38
|
+
this.CID_PREFIX = 'CID:'
|
|
39
|
+
this.metric = new Metric(this)
|
|
40
|
+
this.lastSSID = 0;
|
|
41
|
+
|
|
42
|
+
this.pingIntervalID = setInterval(e => {
|
|
43
|
+
this.remotes.forEach(function each(remo) {
|
|
44
|
+
let socket = remo.socket;
|
|
45
|
+
if (socket.isAlive === false) {
|
|
46
|
+
console.log('## timeout. cid:', remo.cid)
|
|
47
|
+
remo.close();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
socket.txCounter++;
|
|
51
|
+
socket.isAlive = false;
|
|
52
|
+
if (remo.socketType === 'websocket') {
|
|
53
|
+
socket.ping();
|
|
54
|
+
} else {
|
|
55
|
+
remo.ping()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
});
|
|
59
|
+
}, serverOption.timeout);
|
|
60
|
+
|
|
61
|
+
if (serverOption.showMetric) {
|
|
62
|
+
this.monitIntervalID = setInterval((e) => {
|
|
63
|
+
this.monitor()
|
|
64
|
+
}, serverOption.monitorPeriod);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
addRemote(socket, req) {
|
|
72
|
+
socket.isAlive = true;
|
|
73
|
+
let remote = new Remote(socket, req, this)
|
|
74
|
+
this.remotes.add(remote)
|
|
75
|
+
remote.send(Buffer.from([IOMsg.SERVER_READY]))
|
|
76
|
+
remote.setState(CLIENT_STATE.SENT_SERVER_READY)
|
|
77
|
+
this.lastSSID = remote.ssid;
|
|
78
|
+
let connectionInfo = `+ IP:${remote.ip} #${remote.ssid} ${socket.socketType === 'websocket' ? "WS" : "CS"} `;
|
|
79
|
+
// console.log( connectionInfo)
|
|
80
|
+
if (this.connectionLogger) this.connectionLogger.log(connectionInfo)
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
removeRemote(remote) {
|
|
85
|
+
|
|
86
|
+
let remoteInfo = `- IP:${remote.ip} #${remote.ssid} cid:${remote.cid} ${remote?.socket.socketType === 'websocket' ? "WS" : "CS"} `
|
|
87
|
+
if (this.connectionLogger) this.connectionLogger.log(remoteInfo)
|
|
88
|
+
this.deligateSignal(remote, '@state', 'close')
|
|
89
|
+
this.deligateSignal(remote, '@$state', 'close')
|
|
90
|
+
|
|
91
|
+
// remove subscriber
|
|
92
|
+
for (let ch of remote.channels.keys()) {
|
|
93
|
+
if (this.channel_map.has(ch)) {
|
|
94
|
+
const remotes = this.channel_map.get(ch)
|
|
95
|
+
// console.log(`-- channel [ ${ch} ] removes subscriber id: ${remote.cid} `)
|
|
96
|
+
remotes.delete(remote)
|
|
97
|
+
if (remotes.size == 0) this.channel_map.delete(ch)
|
|
98
|
+
} else {
|
|
99
|
+
// console.log('no such a ch: ', ch )
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// retain message policy
|
|
104
|
+
// type 1. ( isAuthorizedUser) ? keep : delete.
|
|
105
|
+
if (remote.boho.isAuthorized) {
|
|
106
|
+
//keep retain signal.
|
|
107
|
+
} else {
|
|
108
|
+
// anonymouse: remove the cid publish channel
|
|
109
|
+
for (let ch of remote.retain_signal.keys()) {
|
|
110
|
+
// console.log('cid retain_signal.keys:', ch )
|
|
111
|
+
ch = this.CID_PREFIX + remote.cid + '@' + ch
|
|
112
|
+
if (this.channel_map.has(ch)) {
|
|
113
|
+
this.channel_map.delete(ch)
|
|
114
|
+
// console.log(`-- deleted cid pub ch [ ${ch} ]. cid publisher id: ${remote.cid} `)
|
|
115
|
+
} else {
|
|
116
|
+
// console.log('no such a retain_signal tag: ', ch )
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if (remote.uid && this.server.apiNames.has('account')) {
|
|
123
|
+
let req = { topic: 'detachUserRemote' }
|
|
124
|
+
req.args = ['caller:manager.removeRemote']
|
|
125
|
+
this.server.emit('account', remote, req)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
this.remotes.delete(remote)
|
|
129
|
+
this.cid2remote.delete(remote.cid)
|
|
130
|
+
remote = null
|
|
131
|
+
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
// it's like unix wall command.
|
|
136
|
+
serverSignal(obj) {
|
|
137
|
+
let sigPack = MBP.pack(
|
|
138
|
+
MBP.MB('#MsgType', '8', IOMsg.SERVER_SIGNAL),
|
|
139
|
+
MBP.MB('#signalObject', obj)
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
this.remotes.forEach(remote => {
|
|
143
|
+
remote.send_enc_mode(sigPack)
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
getSignalTag(buffer) {
|
|
148
|
+
let tagLen = buffer.readUint8(1)
|
|
149
|
+
let tagBuf = buffer.subarray(2, 2 + tagLen)
|
|
150
|
+
let tag = decoder.decode(tagBuf)
|
|
151
|
+
return tag
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
getNewSignalTagMessage(buffer, newTag) {
|
|
155
|
+
let msgType = buffer[0]
|
|
156
|
+
let tagLen = buffer.readUint8(1)
|
|
157
|
+
let newTagBuf = encoder.encode(newTag)
|
|
158
|
+
let newTagLen = newTagBuf.byteLength;
|
|
159
|
+
let payloadCunk = buffer.subarray(2 + tagLen)
|
|
160
|
+
let newBuffer = Buffer.concat([Buffer.from([msgType, newTagLen]), newTagBuf, payloadCunk])
|
|
161
|
+
return newBuffer
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
// signaling to the cid subscribers.
|
|
166
|
+
// example. sending cid close signal.
|
|
167
|
+
deligateSignal(remote, tag, ...args) {
|
|
168
|
+
let sigPack = getSignalPack(tag, ...args)
|
|
169
|
+
this.sender(tag, remote, sigPack)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
adminSignal(cid, message) {
|
|
173
|
+
if (this.cid2remote.has(cid)) {
|
|
174
|
+
this.cid2remote.get(cid).send_enc_mode(message)
|
|
175
|
+
return
|
|
176
|
+
} else {
|
|
177
|
+
return "no cid"
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
serverSignalTo(tag, ...args) {
|
|
182
|
+
let cid = tag.split('@')[0]
|
|
183
|
+
let topic = tag.split('@')[1]
|
|
184
|
+
let sigPack = getSignalPack('@' + topic, ...args)
|
|
185
|
+
if (cid && this.cid2remote.has(cid)) {
|
|
186
|
+
let targetRemote = this.cid2remote.get(cid)
|
|
187
|
+
console.log('target', targetRemote.state, targetRemote.cid)
|
|
188
|
+
targetRemote.send_enc_mode(sigPack)
|
|
189
|
+
return
|
|
190
|
+
} else {
|
|
191
|
+
return "no cid"
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
sender(tag, remote, message) {
|
|
196
|
+
if (serverOption.memberOnly && !remote.boho.isAuthorized) {
|
|
197
|
+
// console.log("## MemberOnly, reject unAuthorized remote." )
|
|
198
|
+
remote.send(Buffer.from([IOMsg.SERVER_CLEAR_AUTH]))
|
|
199
|
+
remote.close()
|
|
200
|
+
return ['err', 'not autrized']
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (tag.indexOf('$') == 0) return ['err', 'prefix $ is reserved for userSet tag.']
|
|
204
|
+
|
|
205
|
+
let cidIndex = tag.indexOf('@');
|
|
206
|
+
if (cidIndex === 0) {
|
|
207
|
+
// ** CID_PUB multi-cast
|
|
208
|
+
// [cid_pub] @topic , @$retainTopic
|
|
209
|
+
// modify signalpack with cid_appneded tag.
|
|
210
|
+
tag = remote.cid + tag;
|
|
211
|
+
message = this.getNewSignalTagMessage(message, tag)
|
|
212
|
+
|
|
213
|
+
} else if (cidIndex > 0) {
|
|
214
|
+
// uni-cast.
|
|
215
|
+
let targetCId = tag.split('@')[0];
|
|
216
|
+
if (this.cid2remote.has(targetCId)) {
|
|
217
|
+
//rm cid from tag.
|
|
218
|
+
let ommitCIdTag = '@' + tag.split('@')[1]
|
|
219
|
+
// console.log('uni-cast tag change from', tag, 'to' , ommitCIdTag )
|
|
220
|
+
message = this.getNewSignalTagMessage(message, ommitCIdTag)
|
|
221
|
+
// console.log(`origin tag: ${tag} omitTag: ${this.getSignalTag(message)}`)
|
|
222
|
+
this.cid2remote.get(targetCId).send_enc_mode(message)
|
|
223
|
+
return ['ok', 1]
|
|
224
|
+
}
|
|
225
|
+
return ['err', 'Invalid cid']
|
|
226
|
+
} else {
|
|
227
|
+
// console.log('ch_pub tag:', tag)
|
|
228
|
+
//else channel publish message
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* multi-cast:
|
|
234
|
+
* channel_publish
|
|
235
|
+
* or cid_publish
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
// HOME_CHANNEL substitution.
|
|
239
|
+
// (blank)#topic -> home_channel#topic.
|
|
240
|
+
if (tag.indexOf('#') === 0) {
|
|
241
|
+
tag = remote.HOME_CHANNEL + tag;
|
|
242
|
+
} else if (tag.includes('@')) {
|
|
243
|
+
tag = this.CID_PREFIX + tag;
|
|
244
|
+
} else {
|
|
245
|
+
tag = this.CHANNEL_PREFIX + tag;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// retain signal message
|
|
249
|
+
if (tag.includes('$')
|
|
250
|
+
// && remote.boho.isAuthorized
|
|
251
|
+
&& serverOption.retain.isAvailable
|
|
252
|
+
&& serverOption.retain.limitCounter > this.retain_messages.size
|
|
253
|
+
&& serverOption.retain.limitSize > message.byteLength
|
|
254
|
+
) {
|
|
255
|
+
// cid retain signal
|
|
256
|
+
if (tag.includes('@')) {
|
|
257
|
+
// cid retain signal stored inside remote.
|
|
258
|
+
let retainTag = tag.split('@')[1]
|
|
259
|
+
remote.retain_signal.set(retainTag, message)
|
|
260
|
+
} else {
|
|
261
|
+
// channel retain signal stored inside manager.
|
|
262
|
+
this.retain_messages.set(tag, message)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
} else {
|
|
266
|
+
// console.log('no retain. serverOption.retain:', serverOption.retain )
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let sentCounter = 0;
|
|
270
|
+
if (this.channel_map.has(tag)) {
|
|
271
|
+
// console.log('raw channel_map matched tag:', tag)
|
|
272
|
+
let remotes = this.channel_map.get(tag);
|
|
273
|
+
if (remotes.size >= 1) {
|
|
274
|
+
let limit;
|
|
275
|
+
if (serverOption.useQuota.publishCounter) {
|
|
276
|
+
limit = Math.min(remote.quota.publishCounter, remotes.size)
|
|
277
|
+
} else {
|
|
278
|
+
limit = remotes.size;
|
|
279
|
+
}
|
|
280
|
+
let peers = remotes.values();
|
|
281
|
+
while (sentCounter < limit) {
|
|
282
|
+
let c = peers.next().value
|
|
283
|
+
if (!c) {
|
|
284
|
+
// console.log('## null remote' )
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
if (c !== remote) {
|
|
288
|
+
c.send_enc_mode(message);
|
|
289
|
+
sentCounter++;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (serverOption.useQuota.publishCounter) {
|
|
294
|
+
// console.log(`pub >> [${tag}] sent: ${sentCounter} [use quota limit: ${remote.quota.publishCounter }] total: ${remotes.size} subscribers. ` )
|
|
295
|
+
} else {
|
|
296
|
+
// console.log(`pub >> [${tag}] sent: ${sentCounter} [no quota limit] total: ${remotes.size} subscribers. ` )
|
|
297
|
+
}
|
|
298
|
+
return ['ok', sentCounter]
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// console.log('-- No subscriber. ch: ' , tag )
|
|
302
|
+
return ['err', 'No subscriber.']
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
subscribe(chArr, remote) {
|
|
308
|
+
chArr.forEach(tag => {
|
|
309
|
+
if (tag.indexOf('#') === 0) {
|
|
310
|
+
tag = remote.HOME_CHANNEL + tag
|
|
311
|
+
} else if (tag.includes('@')) {
|
|
312
|
+
tag = this.CID_PREFIX + tag;
|
|
313
|
+
} else {
|
|
314
|
+
tag = this.CHANNEL_PREFIX + tag
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// 1. set channel map
|
|
318
|
+
if (this.channel_map.has(tag)) {
|
|
319
|
+
this.channel_map.get(tag).add(remote)
|
|
320
|
+
} else {
|
|
321
|
+
this.channel_map.set(tag, new Set([remote]))
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// 2.add to remote channels.
|
|
325
|
+
remote.channels.add(tag)
|
|
326
|
+
|
|
327
|
+
// 3. send retain message if available.
|
|
328
|
+
if (tag.includes('$')
|
|
329
|
+
&& serverOption.retain.isAvailable
|
|
330
|
+
) {
|
|
331
|
+
let retainMessage;
|
|
332
|
+
if (tag.includes('@')) {
|
|
333
|
+
let cid = tag.split('@')[0]
|
|
334
|
+
cid = cid.split(this.CID_PREFIX)[1]
|
|
335
|
+
let retainTag = tag.split('@')[1]
|
|
336
|
+
if (this.cid2remote.has(cid)) {
|
|
337
|
+
retainMessage = this.cid2remote.get(cid).retain_signal.get(retainTag)
|
|
338
|
+
} else {
|
|
339
|
+
// console.log('cid subscribe is rejected. not online:', cid)
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
} else if (this.retain_messages.has(tag)) {
|
|
344
|
+
retainMessage = this.retain_messages.get(tag)
|
|
345
|
+
}
|
|
346
|
+
if (retainMessage) remote.send_enc_mode(retainMessage)
|
|
347
|
+
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
})
|
|
351
|
+
return remote.channels.size;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
unsubscribe(chArr, remote) {
|
|
355
|
+
//unsubscribe all channels of the remote.
|
|
356
|
+
if (chArr.length == 1 && chArr[0] == "") {
|
|
357
|
+
|
|
358
|
+
remote.channels.forEach(ch => {
|
|
359
|
+
if (this.channel_map.has(ch)) {
|
|
360
|
+
let remotes = this.channel_map.get(ch)
|
|
361
|
+
remotes.delete(remote)
|
|
362
|
+
if (remotes.size == 0) this.channel_map.delete(ch)
|
|
363
|
+
}
|
|
364
|
+
})
|
|
365
|
+
remote.channels.clear();
|
|
366
|
+
|
|
367
|
+
} else {
|
|
368
|
+
// unsubscribe each channels.
|
|
369
|
+
chArr.forEach(ch => {
|
|
370
|
+
// substitution home_channel
|
|
371
|
+
if (ch.indexOf('#') === 0) {
|
|
372
|
+
ch = remote.HOME_CHANNEL + ch;
|
|
373
|
+
} else if (ch.includes('@')) {
|
|
374
|
+
ch = this.CID_PREFIX + ch;
|
|
375
|
+
} else {
|
|
376
|
+
ch = this.CHANNEL_PREFIX + ch;
|
|
377
|
+
}
|
|
378
|
+
// console.log('-- Manager::unsubscribe:', ch )
|
|
379
|
+
|
|
380
|
+
// delete manager map.
|
|
381
|
+
if (this.channel_map.has(ch)) {
|
|
382
|
+
let remotes = this.channel_map.get(ch)
|
|
383
|
+
remotes.delete(remote)
|
|
384
|
+
if (remotes.size == 0) this.channel_map.delete(ch)
|
|
385
|
+
} else {
|
|
386
|
+
// console.log('##no such a ch', ch )
|
|
387
|
+
}
|
|
388
|
+
remote.channels.delete(ch)
|
|
389
|
+
})
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// console.log( '-- result unsub:', remote.channels )
|
|
393
|
+
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
monitor() {
|
|
398
|
+
if (process.stdout.isTTY) {
|
|
399
|
+
process.stdout.cursorTo(0, 0)
|
|
400
|
+
process.stdout.clearScreenDown()
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (serverOption.showChannel > 0) {
|
|
404
|
+
this.metric.channels(serverOption.showChannel);
|
|
405
|
+
}
|
|
406
|
+
let mode = parseInt(serverOption.showMetric)
|
|
407
|
+
switch (mode) {
|
|
408
|
+
case 1:
|
|
409
|
+
this.metric.oneline(true);
|
|
410
|
+
break;
|
|
411
|
+
case 2:
|
|
412
|
+
this.metric.getRemotes(true);
|
|
413
|
+
break;
|
|
414
|
+
case 3:
|
|
415
|
+
this.metric.getChannelList(true);
|
|
416
|
+
break;
|
|
417
|
+
default:
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
closeRemoteByCId(cid) {
|
|
424
|
+
if (this.cid2remote.has(cid)) {
|
|
425
|
+
this.cid2remote.get(cid).close()
|
|
426
|
+
return 1
|
|
427
|
+
} else {
|
|
428
|
+
return 0
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
}
|