iosignal 1.4.2 → 1.5.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 +247 -240
- package/auth_file.json +13 -13
- package/auth_file.mjs +23 -23
- package/dist/iosignal.cjs +1 -1
- 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 +59 -60
- package/rollup.config.js +69 -69
- package/src/api/RedisAPI.js +68 -68
- package/src/api/api_constant.js +7 -7
- package/src/api/api_reply.js +34 -34
- package/src/api/api_sudo.js +96 -96
- package/src/auth/Auth_Env.js +78 -78
- package/src/auth/Auth_File.js +75 -75
- package/src/auth/Auth_Redis.js +50 -50
- package/src/auth/BohoAuth.js +168 -168
- package/src/client/CongPacket.js +150 -150
- package/src/client/IOCongSocket.js +88 -88
- package/src/client/IOCore.js +851 -851
- package/src/client/IOWS.js +81 -81
- package/src/client/IOWebSocket.js +95 -95
- package/src/common/constants.js +159 -159
- package/src/common/payload.js +82 -82
- 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 +449 -449
- package/src/server/Metric.js +154 -154
- package/src/server/Server.js +166 -148
- package/src/server/ServerRemote.js +227 -227
- package/src/server/ServerRemoteCore.js +467 -467
- package/src/server/serverOption.js +56 -55
- package/test-browser/favicon.ico +0 -0
- package/test-browser/index.html +92 -92
- 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 +19 -19
- 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/Metric.js
CHANGED
|
@@ -1,154 +1,154 @@
|
|
|
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 Metric{
|
|
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
|
-
getCIdList( prn ){
|
|
76
|
-
let CIdList = Array.from(this.manager.cid2remote.keys())
|
|
77
|
-
if (prn ) console.table(CIdList)
|
|
78
|
-
return CIdList
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getChannelList(prn){
|
|
82
|
-
let chList = Array.from(this.manager.channel_map.keys())
|
|
83
|
-
if (prn ) console.table(chList)
|
|
84
|
-
return chList
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
getSubscribers(ch){
|
|
88
|
-
let list = [];
|
|
89
|
-
if( this.manager.channel_map.has(ch)){
|
|
90
|
-
let subscribers = this.manager.channel_map.get(ch) //set
|
|
91
|
-
subscribers.forEach( c =>{
|
|
92
|
-
list.push(c.cid)
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
// console.log(list)
|
|
96
|
-
return list
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
getRemoteByCId( cid , mode = 1){
|
|
100
|
-
|
|
101
|
-
if( this.manager.cid2remote.has(cid)){
|
|
102
|
-
let remote = this.manager.cid2remote.get(cid)
|
|
103
|
-
if(mode == 1){
|
|
104
|
-
return {
|
|
105
|
-
ip: remote.ip,
|
|
106
|
-
id: remote.cid,
|
|
107
|
-
ssid: remote.ssid,
|
|
108
|
-
cid: remote.cid,
|
|
109
|
-
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
110
|
-
tx: remote.socket.txCounter,
|
|
111
|
-
rx: remote.socket.rxCounter,
|
|
112
|
-
txBytes: remote.socket.bytesWritten || remote.socket._socket?.bytesWritten,
|
|
113
|
-
rxBytes: remote.socket.bytesRead || remote.socket._socket?.bytesRead
|
|
114
|
-
}
|
|
115
|
-
}else if( mode == 2){
|
|
116
|
-
return {
|
|
117
|
-
ip: remote.ip,
|
|
118
|
-
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
119
|
-
nick: remote.nick,
|
|
120
|
-
ssid: remote.ssid,
|
|
121
|
-
echo: remote.lastEchoMessage
|
|
122
|
-
}
|
|
123
|
-
}else if( mode == 3){
|
|
124
|
-
return {
|
|
125
|
-
ip: remote.ip,
|
|
126
|
-
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
127
|
-
nick: remote.nick,
|
|
128
|
-
ssid: remote.ssid,
|
|
129
|
-
isSecure: remote.TLS,
|
|
130
|
-
isAuth: remote.boho.isAuthorized,
|
|
131
|
-
encMode: ENC_MODE[remote.encMode]
|
|
132
|
-
}
|
|
133
|
-
}else if( mode == 4){
|
|
134
|
-
let channels = Array.from( remote.channels.keys() )
|
|
135
|
-
let set_memory = Array.from( remote.memory.keys() )
|
|
136
|
-
let retain_signal = Array.from( remote.retain_signal.keys() )
|
|
137
|
-
return {
|
|
138
|
-
ip: remote.ip,
|
|
139
|
-
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
140
|
-
channels: channels,
|
|
141
|
-
set: set_memory,
|
|
142
|
-
retain: retain_signal
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
}else{
|
|
147
|
-
return { result: "nop" }
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
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 Metric{
|
|
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
|
+
getCIdList( prn ){
|
|
76
|
+
let CIdList = Array.from(this.manager.cid2remote.keys())
|
|
77
|
+
if (prn ) console.table(CIdList)
|
|
78
|
+
return CIdList
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getChannelList(prn){
|
|
82
|
+
let chList = Array.from(this.manager.channel_map.keys())
|
|
83
|
+
if (prn ) console.table(chList)
|
|
84
|
+
return chList
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
getSubscribers(ch){
|
|
88
|
+
let list = [];
|
|
89
|
+
if( this.manager.channel_map.has(ch)){
|
|
90
|
+
let subscribers = this.manager.channel_map.get(ch) //set
|
|
91
|
+
subscribers.forEach( c =>{
|
|
92
|
+
list.push(c.cid)
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
// console.log(list)
|
|
96
|
+
return list
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
getRemoteByCId( cid , mode = 1){
|
|
100
|
+
|
|
101
|
+
if( this.manager.cid2remote.has(cid)){
|
|
102
|
+
let remote = this.manager.cid2remote.get(cid)
|
|
103
|
+
if(mode == 1){
|
|
104
|
+
return {
|
|
105
|
+
ip: remote.ip,
|
|
106
|
+
id: remote.cid,
|
|
107
|
+
ssid: remote.ssid,
|
|
108
|
+
cid: remote.cid,
|
|
109
|
+
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
110
|
+
tx: remote.socket.txCounter,
|
|
111
|
+
rx: remote.socket.rxCounter,
|
|
112
|
+
txBytes: remote.socket.bytesWritten || remote.socket._socket?.bytesWritten,
|
|
113
|
+
rxBytes: remote.socket.bytesRead || remote.socket._socket?.bytesRead
|
|
114
|
+
}
|
|
115
|
+
}else if( mode == 2){
|
|
116
|
+
return {
|
|
117
|
+
ip: remote.ip,
|
|
118
|
+
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
119
|
+
nick: remote.nick,
|
|
120
|
+
ssid: remote.ssid,
|
|
121
|
+
echo: remote.lastEchoMessage
|
|
122
|
+
}
|
|
123
|
+
}else if( mode == 3){
|
|
124
|
+
return {
|
|
125
|
+
ip: remote.ip,
|
|
126
|
+
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
127
|
+
nick: remote.nick,
|
|
128
|
+
ssid: remote.ssid,
|
|
129
|
+
isSecure: remote.TLS,
|
|
130
|
+
isAuth: remote.boho.isAuthorized,
|
|
131
|
+
encMode: ENC_MODE[remote.encMode]
|
|
132
|
+
}
|
|
133
|
+
}else if( mode == 4){
|
|
134
|
+
let channels = Array.from( remote.channels.keys() )
|
|
135
|
+
let set_memory = Array.from( remote.memory.keys() )
|
|
136
|
+
let retain_signal = Array.from( remote.retain_signal.keys() )
|
|
137
|
+
return {
|
|
138
|
+
ip: remote.ip,
|
|
139
|
+
uptime: Math.trunc( ( Date.now() - remote.socket.openTime ) / 1000),
|
|
140
|
+
channels: channels,
|
|
141
|
+
set: set_memory,
|
|
142
|
+
retain: retain_signal
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
}else{
|
|
147
|
+
return { result: "nop" }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
|
package/src/server/Server.js
CHANGED
|
@@ -1,148 +1,166 @@
|
|
|
1
|
-
import net from 'net'
|
|
2
|
-
import EventEmitter from 'events'
|
|
3
|
-
import { WebSocketServer } from 'ws'
|
|
4
|
-
import { Manager } from './Manager.js'
|
|
5
|
-
import { serverOption } from './serverOption.js'
|
|
6
|
-
import { STATUS } from '../api/api_constant.js'
|
|
7
|
-
|
|
8
|
-
export class Server extends EventEmitter {
|
|
9
|
-
|
|
10
|
-
constructor(options, authManager ) {
|
|
11
|
-
super();
|
|
12
|
-
this.apiNames = new Set()
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
console.log('
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
} else {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
1
|
+
import net from 'net'
|
|
2
|
+
import EventEmitter from 'events'
|
|
3
|
+
import { WebSocketServer } from 'ws'
|
|
4
|
+
import { Manager } from './Manager.js'
|
|
5
|
+
import { serverOption } from './serverOption.js'
|
|
6
|
+
import { STATUS } from '../api/api_constant.js'
|
|
7
|
+
|
|
8
|
+
export class Server extends EventEmitter {
|
|
9
|
+
|
|
10
|
+
constructor(options, authManager ) {
|
|
11
|
+
super();
|
|
12
|
+
this.apiNames = new Set()
|
|
13
|
+
this.wss = {};
|
|
14
|
+
|
|
15
|
+
if (options.timeout) {
|
|
16
|
+
let pingT = parseInt(options.timeout)
|
|
17
|
+
if (pingT && pingT >= 1000) serverOption.timeout = pingT
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (options.monitorPeriod) {
|
|
21
|
+
let monitorT = parseInt(options.monitorPeriod)
|
|
22
|
+
if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if (options.showMessage) {
|
|
27
|
+
serverOption.showMessage = options.showMessage
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (options.showMetric) {
|
|
31
|
+
serverOption.showMetric = options.showMetric
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (options.timeout) {
|
|
35
|
+
serverOption.timeout = options.timeout
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (options.port) {
|
|
39
|
+
serverOption.port = parseInt(options.port)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (options.httpServer) {
|
|
43
|
+
serverOption.httpServer = options.httpServer
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (options.congPort) {
|
|
47
|
+
serverOption.congPort = parseInt(options.congPort)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.manager = new Manager(this, authManager)
|
|
51
|
+
|
|
52
|
+
if (serverOption.port || serverOption.httpServer ) this.startWSServer(serverOption)
|
|
53
|
+
if( serverOption.congPort) this.startCongServer(serverOption.congPort)
|
|
54
|
+
|
|
55
|
+
// console.log('serverOption:', serverOption)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
startWSServer(serverOption) {
|
|
60
|
+
|
|
61
|
+
if(serverOption.port ){
|
|
62
|
+
console.log('opening WebSocket Server port:', serverOption.port)
|
|
63
|
+
this.wss = new WebSocketServer({ port: serverOption.port })
|
|
64
|
+
}else if( serverOption.httpServer ){
|
|
65
|
+
console.log('opening WebSocket Server external httpServer:' )
|
|
66
|
+
this.wss = new WebSocketServer({ server: serverOption.httpServer })
|
|
67
|
+
}else{
|
|
68
|
+
throw new TypeError(
|
|
69
|
+
'One and only one of the "port", "httpServer"' +
|
|
70
|
+
'must be specified'
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
// this.wss.setMaxListeners(0)
|
|
76
|
+
|
|
77
|
+
this.wss.on('error', (err) => {
|
|
78
|
+
console.error('### ws server error:', err.message)
|
|
79
|
+
if(err.code == 'EADDRINUSE'){
|
|
80
|
+
process.exit()
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
this.wss.on('close', (err) => {
|
|
85
|
+
console.log('### WS server closed.', err)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
this.wss.on('connection', (ws, req) => {
|
|
89
|
+
ws.socketType = 'websocket'
|
|
90
|
+
this.manager.addRemote(ws, req)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
startCongServer( congPort ) {
|
|
97
|
+
console.log('opening CongSocket Server:', congPort)
|
|
98
|
+
|
|
99
|
+
this.congServer = net.createServer((socket) => {
|
|
100
|
+
this.manager.addRemote(socket)
|
|
101
|
+
})
|
|
102
|
+
.on('error', (err) => {
|
|
103
|
+
console.log('### cong server error:', err.message)
|
|
104
|
+
if(err.code == 'EADDRINUSE'){
|
|
105
|
+
process.exit()
|
|
106
|
+
}
|
|
107
|
+
}).listen( congPort, () => {
|
|
108
|
+
// console.log('congsocket server bound' );
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
api(target, api) {
|
|
115
|
+
|
|
116
|
+
this.apiNames.add(target)
|
|
117
|
+
|
|
118
|
+
// common checkPermission
|
|
119
|
+
if (!api.checkPermission || typeof api.checkPermission != 'function') {
|
|
120
|
+
throw new Error('wrong api interface. no checkPermission function.')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
if(typeof api.request == 'function' && Array.isArray( api.commands )){
|
|
125
|
+
console.log(`API TYPE1. A Class with one request function. target: ${target} list: ${api.commands}` )
|
|
126
|
+
|
|
127
|
+
this.on(target, (remote, req) => {
|
|
128
|
+
if (api.checkPermission(remote, req)) {
|
|
129
|
+
api.request(remote, req)
|
|
130
|
+
} else {
|
|
131
|
+
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
}else{
|
|
136
|
+
let apiList = []
|
|
137
|
+
Object.keys(api).forEach(v => {
|
|
138
|
+
if (typeof api[v] === 'function') apiList.push(v)
|
|
139
|
+
console.log('api list',typeof api[v] ,v )
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
console.log(`API TYPE2. Multiple function list. target: ${target} list:${apiList}`)
|
|
143
|
+
|
|
144
|
+
this.on(target, (remote, req) => {
|
|
145
|
+
let r;
|
|
146
|
+
if (!api.checkPermission(remote, req)) {
|
|
147
|
+
r = "NO_PERMISSION."
|
|
148
|
+
} else {
|
|
149
|
+
// if (apiList.includes(req.topic)) {
|
|
150
|
+
if ( api[req.topic] ) {
|
|
151
|
+
console.log(`API TYPE2. request target: ${target} has topic:${req.topic}`)
|
|
152
|
+
api[req.topic](remote, req)
|
|
153
|
+
return
|
|
154
|
+
} else {
|
|
155
|
+
r = `target: ${req.target} has not topic name: ${req.topic}`
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
remote.response(req.mid, STATUS.ERROR, r)
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return this
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|