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