js-tcp-tunnel 1.2.0 → 1.2.1
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/package.json +1 -1
- package/src/lib.js +52 -18
- package/src/lib.test.js +651 -651
- package/src/types.ts +1 -0
package/package.json
CHANGED
package/src/lib.js
CHANGED
|
@@ -546,10 +546,12 @@ export function pipeSocketDataWithChannel(channelMap, channelId, encodeWriter) {
|
|
|
546
546
|
dstId: channel.dstId,
|
|
547
547
|
dstChannel: channel.dstChannel,
|
|
548
548
|
buffer: buffer,
|
|
549
|
-
})).catch((err) => { console.error('web stream write error', err.message) })
|
|
549
|
+
})).catch((err) => { console.error(channelId, 'web stream write error', err.message) })
|
|
550
550
|
sendPackSize++
|
|
551
551
|
},
|
|
552
552
|
async close() {
|
|
553
|
+
channelMap.delete(channelId)
|
|
554
|
+
socket.destroy()
|
|
553
555
|
encodeWriter.write(buildTcpTunnelData({
|
|
554
556
|
type: TUNNEL_TCP_TYPE_CLOSE,
|
|
555
557
|
srcId: channel.srcId,
|
|
@@ -557,15 +559,24 @@ export function pipeSocketDataWithChannel(channelMap, channelId, encodeWriter) {
|
|
|
557
559
|
dstId: channel.dstId,
|
|
558
560
|
dstChannel: channel.dstChannel,
|
|
559
561
|
buffer: new Uint8Array(0),
|
|
560
|
-
})).catch((err) => { console.error('web stream write error', err.message) })
|
|
561
|
-
channelMap.delete(channelId)
|
|
562
|
-
socket.destroy()
|
|
562
|
+
})).catch((err) => { console.error(channelId, 'pipeSocketDataWithChannel.close web stream write error', err.message) })
|
|
563
563
|
}
|
|
564
564
|
})).catch((err) => {
|
|
565
|
-
console.error('web stream error', err.message)
|
|
565
|
+
console.error(channelId, 'pipeSocketDataWithChannel web stream error', err.message, encodeWriter.desiredSize)
|
|
566
|
+
channelMap.delete(channelId)
|
|
567
|
+
socket.destroy()
|
|
568
|
+
encodeWriter.write(buildTcpTunnelData({
|
|
569
|
+
type: TUNNEL_TCP_TYPE_CLOSE,
|
|
570
|
+
srcId: channel.srcId,
|
|
571
|
+
srcChannel: channel.srcChannel,
|
|
572
|
+
dstId: channel.dstId,
|
|
573
|
+
dstChannel: channel.dstChannel,
|
|
574
|
+
buffer: new Uint8Array(0),
|
|
575
|
+
})).catch((err) => { console.error(channelId, 'web stream write error', err.message) })
|
|
566
576
|
})
|
|
567
577
|
socket.on('error', (err) => {
|
|
568
|
-
console.error('pipeSocketDataWithChannel on error ', err.message)
|
|
578
|
+
console.error(channelId, 'pipeSocketDataWithChannel on error ', err.message)
|
|
579
|
+
channelMap.delete(channelId)
|
|
569
580
|
encodeWriter.write(buildTcpTunnelData({
|
|
570
581
|
type: TUNNEL_TCP_TYPE_ERROR,
|
|
571
582
|
srcId: channel.srcId,
|
|
@@ -573,7 +584,18 @@ export function pipeSocketDataWithChannel(channelMap, channelId, encodeWriter) {
|
|
|
573
584
|
dstId: channel.dstId,
|
|
574
585
|
dstChannel: channel.dstChannel,
|
|
575
586
|
buffer: new Uint8Array(0),
|
|
576
|
-
})).catch((err) => { console.error('web stream write error', err.message) })
|
|
587
|
+
})).catch((err) => { console.error(channelId, 'web stream write error', err.message) })
|
|
588
|
+
})
|
|
589
|
+
socket.on('close', () => {
|
|
590
|
+
console.error(channelId, 'pipeSocketDataWithChannel on close ')
|
|
591
|
+
encodeWriter.write(buildTcpTunnelData({
|
|
592
|
+
type: TUNNEL_TCP_TYPE_CLOSE,
|
|
593
|
+
srcId: channel.srcId,
|
|
594
|
+
srcChannel: channel.srcChannel,
|
|
595
|
+
dstId: channel.dstId,
|
|
596
|
+
dstChannel: channel.dstChannel,
|
|
597
|
+
buffer: new Uint8Array(0),
|
|
598
|
+
})).catch((err) => { console.error(channelId, 'pipeSocketDataWithChannel web stream write error', err.message) })
|
|
577
599
|
channelMap.delete(channelId)
|
|
578
600
|
})
|
|
579
601
|
}
|
|
@@ -677,6 +699,7 @@ async function dispatchClientBufferData(param, setup, listenKeyParamMap, channel
|
|
|
677
699
|
srcChannel: channelId,
|
|
678
700
|
dstChannel: data.srcChannel,
|
|
679
701
|
recvPackSize: 0,
|
|
702
|
+
writableNeedDrainCount: 0,
|
|
680
703
|
key_iv,
|
|
681
704
|
}
|
|
682
705
|
channelMap.set(channelId, channel)
|
|
@@ -769,7 +792,17 @@ async function dispatchClientBufferData(param, setup, listenKeyParamMap, channel
|
|
|
769
792
|
let [clientKey, clientIv] = channel.key_iv
|
|
770
793
|
let buffer = await decrypt(data.buffer, clientKey, clientIv)
|
|
771
794
|
if (buffer.length > 0) {
|
|
772
|
-
|
|
795
|
+
channel.socket.write(buffer)
|
|
796
|
+
if (channel.socket.writableNeedDrain) {
|
|
797
|
+
channel.writableNeedDrainCount++
|
|
798
|
+
} else {
|
|
799
|
+
channel.writableNeedDrainCount = 0
|
|
800
|
+
}
|
|
801
|
+
if (channel.writableNeedDrainCount > 100) {
|
|
802
|
+
console.error(channelId, 'scoket.destroy() on writableNeedDrainCount>100', channel.writableNeedDrainCount)
|
|
803
|
+
channelMap.delete(channelId)
|
|
804
|
+
channel.socket.destroy()
|
|
805
|
+
}
|
|
773
806
|
}
|
|
774
807
|
channel.recvPackSize++
|
|
775
808
|
} else {
|
|
@@ -933,10 +966,7 @@ export class TunnelTcpClientHelper {
|
|
|
933
966
|
*/
|
|
934
967
|
async write(buffer) {
|
|
935
968
|
try {
|
|
936
|
-
await
|
|
937
|
-
sleep(5000),
|
|
938
|
-
dispatchClientBufferData(param, () => thiz.setup(), thiz.listenKeyParamMap, thiz.channelMap, thiz.encodeWriter, buffer),
|
|
939
|
-
])
|
|
969
|
+
await dispatchClientBufferData(param, () => thiz.setup(), thiz.listenKeyParamMap, thiz.channelMap, thiz.encodeWriter, buffer)
|
|
940
970
|
} catch (error) {
|
|
941
971
|
console.error('decode.readable.pipeTo.write', error.message)
|
|
942
972
|
}
|
|
@@ -996,9 +1026,15 @@ export class TunnelTcpClientHelper {
|
|
|
996
1026
|
let server = net.createServer((socket) => {
|
|
997
1027
|
let channelId = this.param.uniqueId++
|
|
998
1028
|
socket.on('error', (err) => {
|
|
999
|
-
console.error('
|
|
1029
|
+
console.error(channelId, 'TunnelTcpClientHelper.connect.onconnect socket error', err.message)
|
|
1000
1030
|
this.channelMap.delete(channelId)
|
|
1001
1031
|
})
|
|
1032
|
+
socket.on('end', () => {
|
|
1033
|
+
console.info(channelId, 'TunnelTcpClientHelper.connect.onconnect socket end')
|
|
1034
|
+
})
|
|
1035
|
+
socket.on('close', () => {
|
|
1036
|
+
console.info(channelId, 'TunnelTcpClientHelper.connect.onconnect socket close')
|
|
1037
|
+
})
|
|
1002
1038
|
/** @type{TUNNEL_TCP_DATA_CONNECT} */
|
|
1003
1039
|
let connectData = { key: sha512(param.tunnelKey) }
|
|
1004
1040
|
/** @type{SocketChannel} */
|
|
@@ -1010,6 +1046,7 @@ export class TunnelTcpClientHelper {
|
|
|
1010
1046
|
dstId: 0,
|
|
1011
1047
|
dstChannel: 0,
|
|
1012
1048
|
recvPackSize: 0,
|
|
1049
|
+
writableNeedDrainCount: 0,
|
|
1013
1050
|
key_iv,
|
|
1014
1051
|
}
|
|
1015
1052
|
this.channelMap.set(channelId, channel)
|
|
@@ -1023,7 +1060,7 @@ export class TunnelTcpClientHelper {
|
|
|
1023
1060
|
}))
|
|
1024
1061
|
}).listen(param.port)
|
|
1025
1062
|
server.on('error', (err) => {
|
|
1026
|
-
console.error('
|
|
1063
|
+
console.error('TunnelTcpClientHelper.connect server error', err.message)
|
|
1027
1064
|
})
|
|
1028
1065
|
this.param.signal.addEventListener('abort', () => { server.close() })
|
|
1029
1066
|
}
|
|
@@ -1220,10 +1257,7 @@ export function createTunnelTcpClientWebSocket(param) {
|
|
|
1220
1257
|
await signal.promise
|
|
1221
1258
|
}
|
|
1222
1259
|
if (!param.signal.aborted) {
|
|
1223
|
-
await
|
|
1224
|
-
sleep(5000),
|
|
1225
|
-
socketWriter.write(chunk),
|
|
1226
|
-
])
|
|
1260
|
+
await socketWriter.write(chunk)
|
|
1227
1261
|
}
|
|
1228
1262
|
}
|
|
1229
1263
|
}))
|