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.
Files changed (50) hide show
  1. package/README.md +247 -240
  2. package/auth_file.json +13 -13
  3. package/auth_file.mjs +23 -23
  4. package/dist/iosignal.cjs +1 -1
  5. package/dist/iosignal.esm.js.map +1 -1
  6. package/dist/iosignal.min.js.map +1 -1
  7. package/index.js +19 -19
  8. package/indexWebBrowser.js +13 -13
  9. package/package.json +59 -60
  10. package/rollup.config.js +69 -69
  11. package/src/api/RedisAPI.js +68 -68
  12. package/src/api/api_constant.js +7 -7
  13. package/src/api/api_reply.js +34 -34
  14. package/src/api/api_sudo.js +96 -96
  15. package/src/auth/Auth_Env.js +78 -78
  16. package/src/auth/Auth_File.js +75 -75
  17. package/src/auth/Auth_Redis.js +50 -50
  18. package/src/auth/BohoAuth.js +168 -168
  19. package/src/client/CongPacket.js +150 -150
  20. package/src/client/IOCongSocket.js +88 -88
  21. package/src/client/IOCore.js +851 -851
  22. package/src/client/IOWS.js +81 -81
  23. package/src/client/IOWebSocket.js +95 -95
  24. package/src/common/constants.js +159 -159
  25. package/src/common/payload.js +82 -82
  26. package/src/common/quotaTable.js +68 -68
  27. package/src/common/util.js +61 -61
  28. package/src/server/FileLogger.js +30 -30
  29. package/src/server/Manager.js +449 -449
  30. package/src/server/Metric.js +154 -154
  31. package/src/server/Server.js +166 -148
  32. package/src/server/ServerRemote.js +227 -227
  33. package/src/server/ServerRemoteCore.js +467 -467
  34. package/src/server/serverOption.js +56 -55
  35. package/test-browser/favicon.ico +0 -0
  36. package/test-browser/index.html +92 -92
  37. package/test-browser/server.js +11 -11
  38. package/test-client-nodejs/client.cjs +17 -17
  39. package/test-client-nodejs/client.mjs +17 -17
  40. package/test-client-nodejs/client_api_reply.mjs +15 -15
  41. package/test-client-nodejs/stateChange.mjs +19 -19
  42. package/test-server/server-Auth_Env.js +19 -19
  43. package/test-server/server-Auth_File.js +12 -12
  44. package/test-server/server-api_reply.js +6 -6
  45. package/test-server/server-api_sudo.js +7 -7
  46. package/test-server/server-commonjs.cjs +8 -8
  47. package/test-server/server-esm.mjs +19 -19
  48. package/test-server/simple-server.mjs +11 -11
  49. package/test-signal-payloadType/signalPack.js +34 -34
  50. package/test-signal-payloadType/signaling.js +60 -60
@@ -1,227 +1,227 @@
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
- // })
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
+ // })