iosignal 1.7.0 → 1.8.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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +254 -247
  3. package/auth_file.json +13 -13
  4. package/auth_file.mjs +23 -23
  5. package/dist/iosignal-iife.js +11 -0
  6. package/dist/iosignal-iife.js.map +1 -0
  7. package/dist/iosignal.cjs +1 -2
  8. package/dist/iosignal.js +11 -0
  9. package/dist/iosignal.js.map +1 -0
  10. package/index.js +19 -19
  11. package/indexWebBrowser.js +13 -13
  12. package/package.json +56 -59
  13. package/rollup.config.js +72 -69
  14. package/src/api/RedisAPI.js +65 -65
  15. package/src/api/api_constant.js +7 -7
  16. package/src/api/api_reply.js +31 -31
  17. package/src/api/api_sudo.js +91 -91
  18. package/src/auth/Auth_Env.js +76 -76
  19. package/src/auth/Auth_File.js +71 -71
  20. package/src/auth/Auth_Redis.js +44 -44
  21. package/src/auth/BohoAuth.js +164 -164
  22. package/src/client/CongPacket.js +141 -141
  23. package/src/client/IOCongSocket.js +81 -81
  24. package/src/client/IOCore.js +822 -822
  25. package/src/client/IOWS.js +80 -80
  26. package/src/client/IOWebSocket.js +111 -111
  27. package/src/common/constants.js +148 -148
  28. package/src/common/payload.js +81 -81
  29. package/src/common/quotaTable.js +68 -68
  30. package/src/common/util.js +61 -61
  31. package/src/server/FileLogger.js +30 -30
  32. package/src/server/Manager.js +433 -433
  33. package/src/server/Metric.js +155 -155
  34. package/src/server/Remote.js +207 -207
  35. package/src/server/RemoteCore.js +439 -439
  36. package/src/server/Server.js +163 -163
  37. package/src/server/serverOption.js +56 -56
  38. package/test-browser/favicon.ico +0 -0
  39. package/test-browser/index.html +96 -96
  40. package/test-browser/server.js +11 -11
  41. package/test-client-nodejs/client.cjs +17 -17
  42. package/test-client-nodejs/client.mjs +17 -17
  43. package/test-client-nodejs/client_api_reply.mjs +15 -15
  44. package/test-client-nodejs/stateChange.mjs +19 -19
  45. package/test-server/server-Auth_Env.js +19 -19
  46. package/test-server/server-Auth_File.js +12 -12
  47. package/test-server/server-api_reply.js +6 -6
  48. package/test-server/server-api_sudo.js +7 -7
  49. package/test-server/server-commonjs.cjs +8 -8
  50. package/test-server/server-esm.mjs +20 -20
  51. package/test-server/simple-server.mjs +11 -11
  52. package/test-signal-payloadType/signalPack.js +34 -34
  53. package/test-signal-payloadType/signaling.js +60 -60
  54. package/dist/iosignal.esm.js +0 -3
  55. package/dist/iosignal.esm.js.map +0 -1
  56. package/dist/iosignal.min.js +0 -3
  57. package/dist/iosignal.min.js.map +0 -1
@@ -1,207 +1,207 @@
1
- import { pack, CongRx } from '../client/CongPacket.js'
2
- import { RemoteCore } from './RemoteCore.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 Remote extends RemoteCore {
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
- if (util.isPrivateIP(this.ip)) {
24
- // connection from local or private network has default private channel.
25
- this.HOME_CHANNEL = 'PRIVATE:'
26
- this.privateNode = true
27
- } else {
28
- // The HOME_CHANNEL is defined by the global IP address value.
29
- this.HOME_CHANNEL = 'IP:' + util.getIPv4HexString(this.ip)
30
- }
31
-
32
-
33
- if (this.socketType === 'websocket') {
34
- if (serverOption.debug.slow) {
35
- socket.on("message", this.onTimeDelayMessage.bind(this));
36
- } else {
37
- socket.on("message", this.onSocketMessage.bind(this));
38
- }
39
- socket.on("pong", this.receiveMonitor.bind(this));
40
- socket.on("ping", this.receiveMonitor.bind(this));
41
- socket.on("error", (e) => { console.log('Websocket error', e, e.code) });
42
- socket.onclose = (e) => {
43
- this.manager.removeRemote(this);
44
- };
45
- } else { // TCP else
46
- this.congRx = new CongRx();
47
- socket.on('data', data => { this.congRx.write(data) })
48
-
49
- this.congRx.on('wrong', this.onWrongCongPacketMessage.bind(this))
50
- // readable.pipe( destination )
51
- // tcpSocket -> congRx(parser) -> onMessage
52
- // socket.pipe( this.congRx )
53
-
54
- if (serverOption.debug.slow) {
55
- this.congRx.on('data', this.onTimeDelayMessage.bind(this))
56
- } else {
57
- this.congRx.on('data', this.onSocketMessage.bind(this))
58
- }
59
-
60
- socket.on('error', e => { console.log('TCP Socket error', e) })
61
- socket.on('close', e => {
62
- this.manager.removeRemote(this);
63
- })
64
-
65
- }
66
- }
67
-
68
- onWrongCongPacketMessage(message) {
69
- /*
70
- * three category CongSocket connection.
71
- 1. anonymouse, tcp connection.
72
- > no cid_req, no auth_req
73
- > It's attacker. close it.
74
- 2. not authorized but cid ready connnection.
75
- > anonymouse remote client.
76
- > allowed some wrong(broken) message. small quota.
77
- 3. authorized remote connection.
78
- > allowed some wrong(broken) message. large quota.
79
- */
80
- if (this.cid) { // category 2 and 3.
81
- // temporary broken message.
82
- // console.log('wrong congpack from user:', Date(), this.ip, this.ssid, this.cid)
83
- } else {
84
- // attacker
85
- let msg = ""
86
- try {
87
- msg = decoder.decode(message)
88
- } catch (error) {
89
- msg += 'Len: ' + message.byteLength
90
- }
91
-
92
- // console.log('attacker message:', attackMsg)
93
- if (this.manager.attackLogger) {
94
- let attackMsg = `> ${this.ssid} ${this.ip} ${msg}`
95
- this.manager.attackLogger.log(attackMsg)
96
- }
97
-
98
- this.close(true);
99
- }
100
- }
101
-
102
-
103
- permissionChecker() {
104
- // type1.
105
- // accept some traffic before authorize.
106
- const limitBytes = 100
107
-
108
- let txBytes = this.socket.bytesWritten || this.socket._socket?.bytesWritten
109
- let rxBytes = this.socket.bytesRead || this.socket._socket?.bytesRead
110
-
111
- if (txBytes == undefined) txBytes = 0
112
- if (rxBytes == undefined) rxBytes = 0
113
-
114
- }
115
-
116
-
117
- // any type of received messages: message, ping, pong
118
- receiveMonitor() {
119
- this.socket.rxCounter++
120
- this.socket.isAlive = true;
121
- }
122
-
123
- isConnectionHTTPS(req) {
124
- //In case of reverse proxy environment. i.e. using nginx proxy_pass.
125
- return req.headers["x-forwarded-proto"]; //https or undefined
126
- }
127
-
128
- getRemoteIP(req) { //req or ip string
129
- let ip;
130
- if (req && req.headers) {
131
- ip = req.headers["x-forwarded-for"];
132
- if (ip == undefined) ip = req.socket.remoteAddress;
133
- } else {
134
- ip = req;
135
- }
136
-
137
- if (ip) {
138
- if (ip.indexOf("::ffff:") == 0) ip = ip.substring(7);
139
- if (ip == "::1") ip = "127.0.0.1";
140
- } else {
141
- ip = "0.0.0.0";
142
- }
143
- return ip;
144
- }
145
-
146
- ping() {
147
- if (this.socketType === 'websocket') {
148
- this.socket.ping()
149
- this.socket.txCounter++
150
- } else {
151
- this.send(Buffer.from([IOMsg.PING]))
152
- }
153
- }
154
-
155
- pong() {
156
- if (this.socketType === 'websocket') {
157
- this.socket.pong()
158
- this.socket.txCounter++
159
- } else {
160
- this.send(Buffer.from([IOMsg.PONG]))
161
- }
162
- }
163
-
164
- getTraffic() {
165
- let txb = this.socket.bytesWritten || this.socket._socket?.bytesWritten
166
- let rxb = this.socket.bytesRead || this.socket._socket?.bytesRead
167
- let tx = this.socket.txCounter
168
- let rx = this.socket.rxCounter
169
- let traffic = { tx, txb, rx, rxb }
170
- }
171
-
172
- close(terminateNow = false) {
173
- this.getTraffic()
174
- if (this.socketType === 'websocket') {
175
- if (terminateNow) this.socket.terminate();
176
- else this.socket.close();
177
- } else {
178
- if (terminateNow) this.socket.destroy();
179
- else this.socket.end();
180
- }
181
- }
182
-
183
- send(message, isBinary) {
184
- this.manager.txBytes += message.byteLength;
185
- this.socket.txCounter++;
186
- if (this.socketType === 'websocket') {
187
- if (this.socket.readyState === 1) {
188
- if (isBinary != undefined) {
189
- this.socket.send(message, { binary: isBinary });
190
- } else {
191
- this.socket.send(message);
192
- }
193
- } else {
194
- this.close(true)
195
- }
196
-
197
- } else { //CongSocket
198
- if (this.socket.readyState == 'open') {
199
- this.socket.write(pack(message))
200
- } else {
201
- this.close(true)
202
- }
203
- }
204
-
205
- }
206
-
207
- }
1
+ import { pack, CongRx } from '../client/CongPacket.js'
2
+ import { RemoteCore } from './RemoteCore.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 Remote extends RemoteCore {
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
+ if (util.isPrivateIP(this.ip)) {
24
+ // connection from local or private network has default private channel.
25
+ this.HOME_CHANNEL = 'PRIVATE:'
26
+ this.privateNode = true
27
+ } else {
28
+ // The HOME_CHANNEL is defined by the global IP address value.
29
+ this.HOME_CHANNEL = 'IP:' + util.getIPv4HexString(this.ip)
30
+ }
31
+
32
+
33
+ if (this.socketType === 'websocket') {
34
+ if (serverOption.debug.slow) {
35
+ socket.on("message", this.onTimeDelayMessage.bind(this));
36
+ } else {
37
+ socket.on("message", this.onSocketMessage.bind(this));
38
+ }
39
+ socket.on("pong", this.receiveMonitor.bind(this));
40
+ socket.on("ping", this.receiveMonitor.bind(this));
41
+ socket.on("error", (e) => { console.log('Websocket error', e, e.code) });
42
+ socket.onclose = (e) => {
43
+ this.manager.removeRemote(this);
44
+ };
45
+ } else { // TCP else
46
+ this.congRx = new CongRx();
47
+ socket.on('data', data => { this.congRx.write(data) })
48
+
49
+ this.congRx.on('wrong', this.onWrongCongPacketMessage.bind(this))
50
+ // readable.pipe( destination )
51
+ // tcpSocket -> congRx(parser) -> onMessage
52
+ // socket.pipe( this.congRx )
53
+
54
+ if (serverOption.debug.slow) {
55
+ this.congRx.on('data', this.onTimeDelayMessage.bind(this))
56
+ } else {
57
+ this.congRx.on('data', this.onSocketMessage.bind(this))
58
+ }
59
+
60
+ socket.on('error', e => { console.log('TCP Socket error', e) })
61
+ socket.on('close', e => {
62
+ this.manager.removeRemote(this);
63
+ })
64
+
65
+ }
66
+ }
67
+
68
+ onWrongCongPacketMessage(message) {
69
+ /*
70
+ * three category CongSocket connection.
71
+ 1. anonymouse, tcp connection.
72
+ > no cid_req, no auth_req
73
+ > It's attacker. close it.
74
+ 2. not authorized but cid ready connnection.
75
+ > anonymouse remote client.
76
+ > allowed some wrong(broken) message. small quota.
77
+ 3. authorized remote connection.
78
+ > allowed some wrong(broken) message. large quota.
79
+ */
80
+ if (this.cid) { // category 2 and 3.
81
+ // temporary broken message.
82
+ // console.log('wrong congpack from user:', Date(), this.ip, this.ssid, this.cid)
83
+ } else {
84
+ // attacker
85
+ let msg = ""
86
+ try {
87
+ msg = decoder.decode(message)
88
+ } catch (error) {
89
+ msg += 'Len: ' + message.byteLength
90
+ }
91
+
92
+ // console.log('attacker message:', attackMsg)
93
+ if (this.manager.attackLogger) {
94
+ let attackMsg = `> ${this.ssid} ${this.ip} ${msg}`
95
+ this.manager.attackLogger.log(attackMsg)
96
+ }
97
+
98
+ this.close(true);
99
+ }
100
+ }
101
+
102
+
103
+ permissionChecker() {
104
+ // type1.
105
+ // accept some traffic before authorize.
106
+ const limitBytes = 100
107
+
108
+ let txBytes = this.socket.bytesWritten || this.socket._socket?.bytesWritten
109
+ let rxBytes = this.socket.bytesRead || this.socket._socket?.bytesRead
110
+
111
+ if (txBytes == undefined) txBytes = 0
112
+ if (rxBytes == undefined) rxBytes = 0
113
+
114
+ }
115
+
116
+
117
+ // any type of received messages: message, ping, pong
118
+ receiveMonitor() {
119
+ this.socket.rxCounter++
120
+ this.socket.isAlive = true;
121
+ }
122
+
123
+ isConnectionHTTPS(req) {
124
+ //In case of reverse proxy environment. i.e. using nginx proxy_pass.
125
+ return req.headers["x-forwarded-proto"]; //https or undefined
126
+ }
127
+
128
+ getRemoteIP(req) { //req or ip string
129
+ let ip;
130
+ if (req && req.headers) {
131
+ ip = req.headers["x-forwarded-for"];
132
+ if (ip == undefined) ip = req.socket.remoteAddress;
133
+ } else {
134
+ ip = req;
135
+ }
136
+
137
+ if (ip) {
138
+ if (ip.indexOf("::ffff:") == 0) ip = ip.substring(7);
139
+ if (ip == "::1") ip = "127.0.0.1";
140
+ } else {
141
+ ip = "0.0.0.0";
142
+ }
143
+ return ip;
144
+ }
145
+
146
+ ping() {
147
+ if (this.socketType === 'websocket') {
148
+ this.socket.ping()
149
+ this.socket.txCounter++
150
+ } else {
151
+ this.send(Buffer.from([IOMsg.PING]))
152
+ }
153
+ }
154
+
155
+ pong() {
156
+ if (this.socketType === 'websocket') {
157
+ this.socket.pong()
158
+ this.socket.txCounter++
159
+ } else {
160
+ this.send(Buffer.from([IOMsg.PONG]))
161
+ }
162
+ }
163
+
164
+ getTraffic() {
165
+ let txb = this.socket.bytesWritten || this.socket._socket?.bytesWritten
166
+ let rxb = this.socket.bytesRead || this.socket._socket?.bytesRead
167
+ let tx = this.socket.txCounter
168
+ let rx = this.socket.rxCounter
169
+ let traffic = { tx, txb, rx, rxb }
170
+ }
171
+
172
+ close(terminateNow = false) {
173
+ this.getTraffic()
174
+ if (this.socketType === 'websocket') {
175
+ if (terminateNow) this.socket.terminate();
176
+ else this.socket.close();
177
+ } else {
178
+ if (terminateNow) this.socket.destroy();
179
+ else this.socket.end();
180
+ }
181
+ }
182
+
183
+ send(message, isBinary) {
184
+ this.manager.txBytes += message.byteLength;
185
+ this.socket.txCounter++;
186
+ if (this.socketType === 'websocket') {
187
+ if (this.socket.readyState === 1) {
188
+ if (isBinary != undefined) {
189
+ this.socket.send(message, { binary: isBinary });
190
+ } else {
191
+ this.socket.send(message);
192
+ }
193
+ } else {
194
+ this.close(true)
195
+ }
196
+
197
+ } else { //CongSocket
198
+ if (this.socket.readyState == 'open') {
199
+ this.socket.write(pack(message))
200
+ } else {
201
+ this.close(true)
202
+ }
203
+ }
204
+
205
+ }
206
+
207
+ }