iosignal 1.6.1 → 1.7.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/dist/iosignal.cjs +2 -2
- package/dist/iosignal.esm.js.map +1 -1
- package/dist/iosignal.min.js.map +1 -1
- package/indexWebBrowser.js +1 -1
- package/package.json +2 -2
- package/src/api/RedisAPI.js +23 -26
- package/src/api/api_reply.js +12 -15
- package/src/api/api_sudo.js +67 -72
- package/src/auth/Auth_Env.js +7 -9
- package/src/auth/Auth_File.js +31 -35
- package/src/auth/Auth_Redis.js +15 -21
- package/src/auth/BohoAuth.js +68 -72
- package/src/client/CongPacket.js +15 -24
- package/src/client/IOCongSocket.js +26 -33
- package/src/client/IOCore.js +6 -35
- package/src/client/IOWS.js +22 -23
- package/src/client/IOWebSocket.js +4 -5
- package/src/common/constants.js +21 -32
- package/src/common/payload.js +47 -48
- package/src/common/quotaTable.js +10 -10
- package/src/common/util.js +38 -38
- package/src/server/FileLogger.js +9 -9
- package/src/server/Manager.js +39 -55
- package/src/server/Metric.js +54 -53
- package/src/server/Remote.js +207 -0
- package/src/server/{ServerRemoteCore.js → RemoteCore.js} +53 -81
- package/src/server/Server.js +22 -25
- package/src/server/serverOption.js +10 -10
- package/test-browser/index.html +28 -25
- package/test-browser/server.js +1 -1
- package/test-client-nodejs/client.cjs +6 -6
- package/test-client-nodejs/client.mjs +6 -6
- package/test-client-nodejs/client_api_reply.mjs +8 -8
- package/test-client-nodejs/stateChange.mjs +7 -7
- package/test-server/server-Auth_Env.js +2 -2
- package/test-server/server-Auth_File.js +1 -1
- package/test-server/server-api_reply.js +2 -2
- package/test-server/server-api_sudo.js +3 -3
- package/test-server/server-esm.mjs +9 -8
- package/test-server/simple-server.mjs +6 -6
- package/test-signal-payloadType/signalPack.js +1 -1
- package/test-signal-payloadType/signaling.js +4 -4
- package/src/server/ServerRemote.js +0 -227
|
@@ -3,15 +3,15 @@ import { IO } from "iosignal"
|
|
|
3
3
|
|
|
4
4
|
const io = new IO('ws://localhost:7777')
|
|
5
5
|
|
|
6
|
-
io.on('ready', ()=>{
|
|
6
|
+
io.on('ready', () => {
|
|
7
7
|
console.log('ready cid:', io.cid)
|
|
8
|
-
io.signal('#screen','playToggle')
|
|
8
|
+
io.signal('#screen', 'playToggle')
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
-
io.listen('#notify', (...args)=>{
|
|
12
|
-
console.log(
|
|
11
|
+
io.listen('#notify', (...args) => {
|
|
12
|
+
console.log(args)
|
|
13
13
|
})
|
|
14
14
|
|
|
15
|
-
io.on('error',err=>{
|
|
16
|
-
|
|
15
|
+
io.on('error', err => {
|
|
16
|
+
console.log('err', err)
|
|
17
17
|
})
|
|
@@ -3,13 +3,13 @@ import { IO } from "iosignal"
|
|
|
3
3
|
|
|
4
4
|
const io = new IO('ws://localhost:7777')
|
|
5
5
|
|
|
6
|
-
io.on('ready', async ()=>{
|
|
7
|
-
let res_echo = await io.req('reply', 'echo', 'hello'
|
|
8
|
-
let res_date = await io.req('reply', 'date'
|
|
9
|
-
let res_unixtime = await io.req('reply', 'unixtime'
|
|
10
|
-
|
|
11
|
-
if(
|
|
12
|
-
if(
|
|
13
|
-
if(
|
|
6
|
+
io.on('ready', async () => {
|
|
7
|
+
let res_echo = await io.req('reply', 'echo', 'hello')
|
|
8
|
+
let res_date = await io.req('reply', 'date')
|
|
9
|
+
let res_unixtime = await io.req('reply', 'unixtime')
|
|
10
|
+
|
|
11
|
+
if (res_echo.ok) console.log(res_echo.body)
|
|
12
|
+
if (res_date.ok) console.log(res_date.body)
|
|
13
|
+
if (res_unixtime.ok) console.log(res_unixtime.body)
|
|
14
14
|
|
|
15
15
|
});
|
|
@@ -3,17 +3,17 @@ import { IO } from "iosignal"
|
|
|
3
3
|
|
|
4
4
|
const io = new IO('ws://localhost:7777')
|
|
5
5
|
|
|
6
|
-
io.on('ready', ()=>{
|
|
6
|
+
io.on('ready', () => {
|
|
7
7
|
console.log('ready cid:', io.cid)
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
io.on('change', (
|
|
11
|
-
console.log('\nchange event:', evt
|
|
12
|
-
console.log('io.state<Number>', io.state
|
|
13
|
-
console.log('io.stateName<String>:', io.stateName
|
|
10
|
+
io.on('change', (evt) => {
|
|
11
|
+
console.log('\nchange event:', evt)
|
|
12
|
+
console.log('io.state<Number>', io.state)
|
|
13
|
+
console.log('io.stateName<String>:', io.stateName)
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
io.on('error',err=>{
|
|
18
|
-
|
|
17
|
+
io.on('error', err => {
|
|
18
|
+
console.log('err', err)
|
|
19
19
|
})
|
|
@@ -13,7 +13,7 @@ const options = {
|
|
|
13
13
|
// or, as constructor argument.
|
|
14
14
|
let authInfo = 'id1.key1.255,id2.key2.200'
|
|
15
15
|
|
|
16
|
-
let authManager = new Auth_Env(
|
|
17
|
-
const server = new Server(
|
|
16
|
+
let authManager = new Auth_Env(authInfo)
|
|
17
|
+
const server = new Server(options, authManager)
|
|
18
18
|
|
|
19
19
|
|
|
@@ -7,6 +7,6 @@ const options = {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
// const server = new Server( options, new Auth_File('../authInfo.json') ) // JSON version. cannot add comments.
|
|
10
|
-
const server = new Server(
|
|
10
|
+
const server = new Server(options, new Auth_File('../auth_file.mjs')) // JS version. it support comments.
|
|
11
11
|
|
|
12
12
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Server
|
|
1
|
+
import { Server, api_sudo, Auth_Env } from 'iosignal'
|
|
2
2
|
|
|
3
3
|
let authManager = new Auth_Env('admin.adminkey.255')
|
|
4
|
-
const server = new Server(
|
|
4
|
+
const server = new Server({ port: 7777 }, authManager)
|
|
5
5
|
|
|
6
6
|
// api response module
|
|
7
|
-
server.api('sudo', api_sudo
|
|
7
|
+
server.api('sudo', api_sudo)
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { Server } from '../index.js'
|
|
2
2
|
|
|
3
|
-
const server = new Server(
|
|
4
|
-
{
|
|
3
|
+
const server = new Server(
|
|
4
|
+
{
|
|
5
|
+
port: 7777, // WebSocket
|
|
5
6
|
congPort: 8888, // CongSocket
|
|
6
7
|
showMetric: 2,
|
|
7
8
|
showMessage: 'message'
|
|
8
|
-
}
|
|
9
|
+
})
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
// text message handler (No binary message)
|
|
12
13
|
// print text message
|
|
13
14
|
// then reply.
|
|
14
|
-
server.on('text_message', (msg, remote)=>{
|
|
15
|
-
console.log('text_message from(cid): ', remote.cid,
|
|
16
|
-
let result = { msg: msg, yourcid: remote.cid
|
|
17
|
-
let res = JSON.stringify(
|
|
18
|
-
remote.send(
|
|
15
|
+
server.on('text_message', (msg, remote) => {
|
|
16
|
+
console.log('text_message from(cid): ', remote.cid, msg.toString())
|
|
17
|
+
let result = { msg: msg, yourcid: remote.cid, date: new Date().toUTCString() }
|
|
18
|
+
let res = JSON.stringify(result)
|
|
19
|
+
remote.send(res)
|
|
19
20
|
})
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Server
|
|
1
|
+
import { Server } from '../index.js'
|
|
2
2
|
|
|
3
3
|
const server = new Server({
|
|
4
|
-
port: 7777
|
|
4
|
+
port: 7777,
|
|
5
5
|
congPort: 8888,
|
|
6
6
|
showMessage: 'message'
|
|
7
|
-
}
|
|
7
|
+
})
|
|
8
8
|
|
|
9
|
-
server.on('text_message', (data, client)=>{
|
|
10
|
-
console.log(
|
|
11
|
-
client.send('from server. your cid is '+ client.cid + ".")
|
|
9
|
+
server.on('text_message', (data, client) => {
|
|
10
|
+
console.log('text_message', data, 'from', client.cid)
|
|
11
|
+
client.send('from server. your cid is ' + client.cid + ".")
|
|
12
12
|
})
|
|
@@ -23,7 +23,7 @@ console.log(PAYLOAD_TYPE[parsePayload(type5).type], type5) // MBA
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
let signalTag = 'channel'
|
|
26
|
-
let signalPack = getSignalPack(
|
|
26
|
+
let signalPack = getSignalPack(signalTag, ...type5)
|
|
27
27
|
console.log('signalPack', signalPack)
|
|
28
28
|
|
|
29
29
|
let payloadBuffer = getPayloadFromSignalPack(signalPack)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { IO, Server} from 'iosignal'
|
|
1
|
+
import { IO, Server } from 'iosignal'
|
|
2
2
|
|
|
3
3
|
let url = 'ws://localhost:5555'
|
|
4
4
|
let server = new Server({
|
|
5
|
-
|
|
5
|
+
port: 5555
|
|
6
6
|
// ,showMessage: 'message'
|
|
7
|
-
|
|
7
|
+
})
|
|
8
8
|
let done = false;
|
|
9
9
|
|
|
10
10
|
let tx = new IO(url)
|
|
@@ -48,7 +48,7 @@ function sendSignals() {
|
|
|
48
48
|
const arg1 = ['a1']
|
|
49
49
|
const arg2 = ['a2', 222]
|
|
50
50
|
const arg3 = [{ name: 'a3' }, 333]
|
|
51
|
-
const arg4 = [{a:1}, Buffer.from('KKKKK')]
|
|
51
|
+
const arg4 = [{ a: 1 }, Buffer.from('KKKKK')]
|
|
52
52
|
const arg5 = [new Uint8Array(20)]
|
|
53
53
|
|
|
54
54
|
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import { pack, CongRx } from '../client/CongPacket.js'
|
|
2
|
-
import { ServerRemoteCore } from './ServerRemoteCore.js';
|
|
3
|
-
import { serverOption } from './serverOption.js';
|
|
4
|
-
import { IOMsg } from '../common/constants.js';
|
|
5
|
-
import * as util from '../common/util.js'
|
|
6
|
-
|
|
7
|
-
const decoder = new TextDecoder()
|
|
8
|
-
|
|
9
|
-
export class ServerRemote extends ServerRemoteCore {
|
|
10
|
-
constructor(socket, req, manager) {
|
|
11
|
-
super(socket, manager);
|
|
12
|
-
this.socketType = socket.socketType
|
|
13
|
-
|
|
14
|
-
if( this.socketType === 'websocket' ){
|
|
15
|
-
this.TLS = req.headers["x-forwarded-proto"] === 'https' ? true : false;
|
|
16
|
-
this.ip = this.getRemoteIP(req);
|
|
17
|
-
}else{
|
|
18
|
-
this.TLS = false;
|
|
19
|
-
this.ip = this.getRemoteIP( socket.remoteAddress )
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// default channel : HOME_CHANNEL
|
|
24
|
-
// console.log('connected ip:', this.ip)
|
|
25
|
-
|
|
26
|
-
if( util.isPrivateIP(this.ip)){
|
|
27
|
-
// connection from local or private network has default private channel.
|
|
28
|
-
this.HOME_CHANNEL = 'PRIVATE:'
|
|
29
|
-
this.privateNode = true
|
|
30
|
-
}else{
|
|
31
|
-
// client have default channel name as global IP address.
|
|
32
|
-
this.HOME_CHANNEL = 'IP:'+ util.getIPv4HexString(this.ip)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if(this.socketType === 'websocket' ){ // WS
|
|
37
|
-
if(serverOption.debug.slow){
|
|
38
|
-
socket.on("message", this.onTimeDelayMessage.bind(this));
|
|
39
|
-
}else{
|
|
40
|
-
socket.on("message", this.onSocketMessage.bind(this));
|
|
41
|
-
}
|
|
42
|
-
socket.on("pong", this.receiveMonitor.bind(this));
|
|
43
|
-
socket.on("ping", this.receiveMonitor.bind(this));
|
|
44
|
-
socket.on("error", (e) => { console.log('Websocket error',e, e.code) });
|
|
45
|
-
socket.onclose = (e) => {
|
|
46
|
-
this.manager.removeRemote(this);
|
|
47
|
-
};
|
|
48
|
-
}else{ // TCP else
|
|
49
|
-
this.congRx = new CongRx();
|
|
50
|
-
socket.on('data', data=>{ this.congRx.write(data)} )
|
|
51
|
-
|
|
52
|
-
this.congRx.on('wrong',this.onWrongCongPacketMessage.bind(this))
|
|
53
|
-
// readable.pipe( destination )
|
|
54
|
-
// tcpSocket -> congRx(parser) -> onMessage
|
|
55
|
-
// socket.pipe( this.congRx )
|
|
56
|
-
|
|
57
|
-
if(serverOption.debug.slow){
|
|
58
|
-
this.congRx.on('data', this.onTimeDelayMessage.bind(this))
|
|
59
|
-
}else{
|
|
60
|
-
this.congRx.on('data', this.onSocketMessage.bind(this))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
socket.on('error', e=>{ console.log('TCP Socket error',e )})
|
|
64
|
-
socket.on('close', e=>{
|
|
65
|
-
this.manager.removeRemote(this);
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
onWrongCongPacketMessage( message){
|
|
72
|
-
/*
|
|
73
|
-
* three category CongSocket connection.
|
|
74
|
-
1. anonymouse, tcp connection.
|
|
75
|
-
> no cid_req, no auth_req
|
|
76
|
-
> It's attacker. close it.
|
|
77
|
-
2. not authorized but cid ready connnection.
|
|
78
|
-
> anonymouse remote client.
|
|
79
|
-
> allowed some wrong(broken) message. small quota.
|
|
80
|
-
3. authorized remote connection.
|
|
81
|
-
> allowed some wrong(broken) message. large quota.
|
|
82
|
-
*/
|
|
83
|
-
if(this.cid){ // category 2 and 3.
|
|
84
|
-
// temporary broken message.
|
|
85
|
-
console.log('wrong congpack from user:', Date(), this.ip, this.ssid, this.cid )
|
|
86
|
-
}else{
|
|
87
|
-
// attacker
|
|
88
|
-
let msg =""
|
|
89
|
-
try {
|
|
90
|
-
msg = decoder.decode(message)
|
|
91
|
-
} catch (error) {
|
|
92
|
-
msg += 'Len: '+ message.byteLength
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// console.log('attacker message:', attackMsg)
|
|
96
|
-
if(this.manager.attackLogger){
|
|
97
|
-
let attackMsg = `> ${this.ssid} ${this.ip} ${msg}`
|
|
98
|
-
this.manager.attackLogger.log( attackMsg )
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
this.close( true );
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
permissionChecker(){
|
|
107
|
-
// type1.
|
|
108
|
-
// accept some traffic before authorize.
|
|
109
|
-
const limitBytes = 100
|
|
110
|
-
|
|
111
|
-
let txBytes = this.socket.bytesWritten || this.socket._socket?.bytesWritten
|
|
112
|
-
let rxBytes = this.socket.bytesRead || this.socket._socket?.bytesRead
|
|
113
|
-
|
|
114
|
-
if( txBytes == undefined) txBytes = 0
|
|
115
|
-
if( rxBytes == undefined) rxBytes = 0
|
|
116
|
-
// console.log( txBytes, rxBytes )
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// any type of received messages: message, ping, pong
|
|
122
|
-
receiveMonitor(){
|
|
123
|
-
this.socket.rxCounter++
|
|
124
|
-
this.socket.isAlive = true;
|
|
125
|
-
// manager manages connection (ping) check.
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
isConnectionHTTPS(req){
|
|
129
|
-
//In case of reverse proxy environment. i.e. using nginx proxy_pass.
|
|
130
|
-
return req.headers["x-forwarded-proto"]; //https or undefined
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
// nginx https reversproxy , http redirect , direct tcp
|
|
135
|
-
getRemoteIP(req) { //req or ip string
|
|
136
|
-
let ip;
|
|
137
|
-
if( req && req.headers ){
|
|
138
|
-
ip = req.headers["x-forwarded-for"];
|
|
139
|
-
if (ip == undefined ) ip = req.socket.remoteAddress;
|
|
140
|
-
}else{
|
|
141
|
-
ip = req;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (ip) {
|
|
145
|
-
if (ip.indexOf("::ffff:") == 0) ip = ip.substring(7);
|
|
146
|
-
if (ip == "::1") ip = "127.0.0.1";
|
|
147
|
-
} else {
|
|
148
|
-
ip = "0.0.0.0";
|
|
149
|
-
}
|
|
150
|
-
// console.log('getRemoteIP', ip )
|
|
151
|
-
return ip;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
ping(){
|
|
155
|
-
if(this.socketType === 'websocket'){
|
|
156
|
-
this.socket.ping()
|
|
157
|
-
this.socket.txCounter++
|
|
158
|
-
}else{
|
|
159
|
-
this.send( Buffer.from( [ IOMsg.PING ]))
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
pong(){
|
|
164
|
-
if(this.socketType === 'websocket'){
|
|
165
|
-
this.socket.pong()
|
|
166
|
-
this.socket.txCounter++
|
|
167
|
-
}else{
|
|
168
|
-
this.send( Buffer.from( [ IOMsg.PONG ]))
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
getTraffic(){
|
|
173
|
-
let txb = this.socket.bytesWritten || this.socket._socket?.bytesWritten
|
|
174
|
-
let rxb = this.socket.bytesRead || this.socket._socket?.bytesRead
|
|
175
|
-
let tx = this.socket.txCounter
|
|
176
|
-
let rx = this.socket.rxCounter
|
|
177
|
-
let traffic = { tx, txb, rx, rxb }
|
|
178
|
-
// console.log('traffic before close', traffic )
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
close( terminateNow = false){
|
|
182
|
-
this.getTraffic()
|
|
183
|
-
// let info = `call close() [${this.ssid}] socket: readyState:${this.socket.readyState} ${this.socket}`
|
|
184
|
-
// console.log( info )
|
|
185
|
-
if( this.socketType === 'websocket' ){
|
|
186
|
-
if( terminateNow ) this.socket.terminate();
|
|
187
|
-
else this.socket.close();
|
|
188
|
-
}else{
|
|
189
|
-
if( terminateNow ) this.socket.destroy();
|
|
190
|
-
else this.socket.end();
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
send( message, isBinary ){
|
|
195
|
-
this.manager.txBytes += message.byteLength;
|
|
196
|
-
this.socket.txCounter++;
|
|
197
|
-
if(this.socketType === 'websocket' ){ //WebSocket
|
|
198
|
-
if( this.socket.readyState === 1) {
|
|
199
|
-
if( isBinary != undefined ){
|
|
200
|
-
this.socket.send(message, { binary: isBinary });
|
|
201
|
-
}else{
|
|
202
|
-
this.socket.send(message );
|
|
203
|
-
}
|
|
204
|
-
}else{
|
|
205
|
-
// not open
|
|
206
|
-
// console.log('ServerRemote::send(), WS not open. #'+this.ssid + ' cid:', this.cid +":"+ this.getStateName() )
|
|
207
|
-
// console.log('message not sent:', message )
|
|
208
|
-
this.close( true )
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
}else{ //CongSocket
|
|
212
|
-
if( this.socket.readyState == 'open'){
|
|
213
|
-
this.socket.write( pack(message) )
|
|
214
|
-
}else{
|
|
215
|
-
// console.log('ServerRemote::send(), CongSocket not open #'+this.ssid + ' cid:' , this.cid + ':' + this.getStateName() )
|
|
216
|
-
this.close( true )
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
// process.on('uncaughtException', (err, origin) => {
|
|
226
|
-
// console.log('serverRemote::uncaughtException', err,origin)
|
|
227
|
-
// })
|