js-tcp-tunnel 1.1.1 → 1.2.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/package.json +1 -1
- package/src/lib.js +8 -70
- package/src/lib.test.js +2 -2
- package/src/types.ts +0 -1
package/package.json
CHANGED
package/src/lib.js
CHANGED
|
@@ -308,7 +308,6 @@ export function printTcpTunnelData(data) {
|
|
|
308
308
|
[TUNNEL_TCP_TYPE_CLOSE]: 'TUNNEL_TCP_TYPE_CLOSE ',
|
|
309
309
|
[TUNNEL_TCP_TYPE_PING]: 'TUNNEL_TCP_TYPE_PING ',
|
|
310
310
|
[TUNNEL_TCP_TYPE_PONG]: 'TUNNEL_TCP_TYPE_PONG ',
|
|
311
|
-
[TUNNEL_TCP_TYPE_ACK]: 'TUNNEL_TCP_TYPE_ACK ',
|
|
312
311
|
}[data.type]} recv: ${(tcpTunnelDataRecv)} send: ${(tcpTunnelDataSend)} size:${data.buffer.length}`
|
|
313
312
|
}
|
|
314
313
|
|
|
@@ -421,7 +420,6 @@ export const TUNNEL_TCP_TYPE_ERROR = 0x8117f762
|
|
|
421
420
|
export const TUNNEL_TCP_TYPE_CLOSE = 0x72fd6470
|
|
422
421
|
export const TUNNEL_TCP_TYPE_PING = 0x4768e1ba
|
|
423
422
|
export const TUNNEL_TCP_TYPE_PONG = 0x106f43fb
|
|
424
|
-
export const TUNNEL_TCP_TYPE_ACK = 0xc5870539
|
|
425
423
|
|
|
426
424
|
/**
|
|
427
425
|
* @param {TCP_TUNNEL_DATA} box
|
|
@@ -532,46 +530,15 @@ export function createTimeBufferedTransformStream(bufferTime) {
|
|
|
532
530
|
export function pipeSocketDataWithChannel(channelMap, channelId, encodeWriter) {
|
|
533
531
|
let channel = channelMap.get(channelId)
|
|
534
532
|
let socket = channel.socket
|
|
535
|
-
let signal = Promise_withResolvers()
|
|
536
|
-
signal.resolve()
|
|
537
533
|
let sendPackSize = 0
|
|
538
|
-
let remoteRecvPackSize = 0
|
|
539
|
-
channel.notify = (size) => {
|
|
540
|
-
remoteRecvPackSize = size
|
|
541
|
-
signal.resolve()
|
|
542
|
-
}
|
|
543
534
|
let [clientKey, clientIv] = channel.key_iv
|
|
544
535
|
let bufferedTransform = createTimeBufferedTransformStream(50)
|
|
545
|
-
let backPressureTimer = null
|
|
546
536
|
Readable.toWeb(socket).pipeThrough(bufferedTransform).pipeTo(new WritableStream({
|
|
547
537
|
/**
|
|
548
538
|
* @param {Uint8Array<ArrayBuffer>} chunk
|
|
549
539
|
*/
|
|
550
540
|
async write(chunk) {
|
|
551
541
|
const buffer = await encrypt(chunk, clientKey, clientIv)
|
|
552
|
-
let bufferPackSize = sendPackSize - remoteRecvPackSize
|
|
553
|
-
if (DEBUG_TUNNEL_TCP) {
|
|
554
|
-
console.warn('bufferPackSize:', bufferPackSize)
|
|
555
|
-
}
|
|
556
|
-
if (bufferPackSize > 10) {
|
|
557
|
-
signal.resolve()
|
|
558
|
-
signal = Promise_withResolvers()
|
|
559
|
-
const s = signal
|
|
560
|
-
backPressureTimer = setTimeout(() => {
|
|
561
|
-
s.resolve()
|
|
562
|
-
console.error('pipeSocketDataWithChannel timeout close channel')
|
|
563
|
-
sendPackSize = 0
|
|
564
|
-
this.close()
|
|
565
|
-
}, 10_000).unref()
|
|
566
|
-
if (DEBUG_TUNNEL_TCP) {
|
|
567
|
-
console.info('stop wait signal', ' sendPackSize:', sendPackSize, ' recvPackSize:', remoteRecvPackSize, ' bufferPackSize:', bufferPackSize)
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
await signal.promise
|
|
571
|
-
if (backPressureTimer) {
|
|
572
|
-
clearTimeout(backPressureTimer)
|
|
573
|
-
backPressureTimer = null
|
|
574
|
-
}
|
|
575
542
|
await encodeWriter.write(buildTcpTunnelData({
|
|
576
543
|
type: TUNNEL_TCP_TYPE_DATA,
|
|
577
544
|
srcId: channel.srcId,
|
|
@@ -711,7 +678,6 @@ async function dispatchClientBufferData(param, setup, listenKeyParamMap, channel
|
|
|
711
678
|
dstChannel: data.srcChannel,
|
|
712
679
|
recvPackSize: 0,
|
|
713
680
|
key_iv,
|
|
714
|
-
notify: null,
|
|
715
681
|
}
|
|
716
682
|
channelMap.set(channelId, channel)
|
|
717
683
|
connectSocket.on('connect', () => {
|
|
@@ -806,43 +772,10 @@ async function dispatchClientBufferData(param, setup, listenKeyParamMap, channel
|
|
|
806
772
|
await channel.writer.write(buffer)
|
|
807
773
|
}
|
|
808
774
|
channel.recvPackSize++
|
|
809
|
-
await sendAck(encodeWriter, channel)
|
|
810
775
|
} else {
|
|
811
776
|
await closeRemoteChannel(encodeWriter, data)
|
|
812
777
|
}
|
|
813
778
|
}
|
|
814
|
-
if (data.type == TUNNEL_TCP_TYPE_ACK) {
|
|
815
|
-
let channelId = data.dstChannel
|
|
816
|
-
let channel = channelMap.get(channelId)
|
|
817
|
-
if (channel) {
|
|
818
|
-
channel.srcId = data.dstId
|
|
819
|
-
channel.dstId = data.srcId
|
|
820
|
-
let size = readUInt32LE(data.buffer, 0)
|
|
821
|
-
channel.notify(size)
|
|
822
|
-
} else {
|
|
823
|
-
await closeRemoteChannel(encodeWriter, data)
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
/**
|
|
829
|
-
* @param {WritableStreamDefaultWriter<Uint8Array>} encodeWriter
|
|
830
|
-
* @param {SocketChannel} channel
|
|
831
|
-
*/
|
|
832
|
-
export async function sendAck(encodeWriter, channel) {
|
|
833
|
-
if (channel.recvPackSize % 5 != 0) {
|
|
834
|
-
return
|
|
835
|
-
}
|
|
836
|
-
let sizeBuffer = new Uint8Array(4)
|
|
837
|
-
writeUInt32LE(sizeBuffer, channel.recvPackSize, 0)
|
|
838
|
-
await encodeWriter.write(buildTcpTunnelData({
|
|
839
|
-
type: TUNNEL_TCP_TYPE_ACK,
|
|
840
|
-
srcId: channel.srcId,
|
|
841
|
-
srcChannel: channel.srcChannel,
|
|
842
|
-
dstId: channel.dstId,
|
|
843
|
-
dstChannel: channel.dstChannel,
|
|
844
|
-
buffer: sizeBuffer,
|
|
845
|
-
}))
|
|
846
779
|
}
|
|
847
780
|
|
|
848
781
|
/**
|
|
@@ -1000,7 +933,10 @@ export class TunnelTcpClientHelper {
|
|
|
1000
933
|
*/
|
|
1001
934
|
async write(buffer) {
|
|
1002
935
|
try {
|
|
1003
|
-
await
|
|
936
|
+
await Promise.race([
|
|
937
|
+
sleep(5000),
|
|
938
|
+
dispatchClientBufferData(param, () => thiz.setup(), thiz.listenKeyParamMap, thiz.channelMap, thiz.encodeWriter, buffer),
|
|
939
|
+
])
|
|
1004
940
|
} catch (error) {
|
|
1005
941
|
console.error('decode.readable.pipeTo.write', error.message)
|
|
1006
942
|
}
|
|
@@ -1075,7 +1011,6 @@ export class TunnelTcpClientHelper {
|
|
|
1075
1011
|
dstChannel: 0,
|
|
1076
1012
|
recvPackSize: 0,
|
|
1077
1013
|
key_iv,
|
|
1078
|
-
notify: null,
|
|
1079
1014
|
}
|
|
1080
1015
|
this.channelMap.set(channelId, channel)
|
|
1081
1016
|
this.encodeWriter.write(buildTcpTunnelData({
|
|
@@ -1285,7 +1220,10 @@ export function createTunnelTcpClientWebSocket(param) {
|
|
|
1285
1220
|
await signal.promise
|
|
1286
1221
|
}
|
|
1287
1222
|
if (!param.signal.aborted) {
|
|
1288
|
-
await
|
|
1223
|
+
await Promise.race([
|
|
1224
|
+
sleep(5000),
|
|
1225
|
+
socketWriter.write(chunk),
|
|
1226
|
+
])
|
|
1289
1227
|
}
|
|
1290
1228
|
}
|
|
1291
1229
|
}))
|
package/src/lib.test.js
CHANGED