Fixed and updated for client and lib models and package version and compile for typescript in http tunnel client

This commit is contained in:
Sambo Chea 2022-07-10 20:50:50 +07:00
parent 1af63d5955
commit 4412b6edd6
4 changed files with 17 additions and 27 deletions

View File

@ -1,3 +1,3 @@
#!/usr/bin/env node #!/usr/bin/env node
require('../client'); require('../dist/client');

View File

@ -1,14 +1,14 @@
{ {
"name": "@cubetiq/hlt", "name": "@cubetiq/hlt",
"version": "0.0.9", "version": "0.1.0",
"description": "A lightweight http tunnel client using nodejs and socket.io client", "description": "A lightweight http tunnel client using nodejs and socket.io client",
"main": "client.js", "main": "dist/client.js",
"bin": { "bin": {
"hlt": "bin/hlt" "hlt": "bin/hlt"
}, },
"scripts": { "scripts": {
"start": "ts-node-dev --respawn --transpile-only src/client.ts", "start": "ts-node-dev --respawn --transpile-only src/client.ts",
"build": "rm -rf dist && tsc" "build": "tsc"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -67,7 +67,7 @@ function initClient(options: any) {
release: os.release(), release: os.release(),
}; };
const initParams = { const initParams: any = {
path: "/$cubetiq_http_tunnel", path: "/$cubetiq_http_tunnel",
transports: ["websocket"], transports: ["websocket"],
auth: { auth: {
@ -92,7 +92,7 @@ function initClient(options: any) {
const clientLogPrefix = `client: ${clientId} on profile: ${profile}`; const clientLogPrefix = `client: ${clientId} on profile: ${profile}`;
socket.on("connect", () => { socket.on("connect", () => {
if (socket.connected) { if (socket!.connected) {
console.log(`${clientLogPrefix} is connected to server successfully!`); console.log(`${clientLogPrefix} is connected to server successfully!`);
} }
}); });
@ -113,7 +113,7 @@ function initClient(options: any) {
socket.on("disconnect_exit", (reason) => { socket.on("disconnect_exit", (reason) => {
console.log(`${clientLogPrefix} disconnected and exited ${reason}!`); console.log(`${clientLogPrefix} disconnected and exited ${reason}!`);
socket.disconnect(); socket?.disconnect();
process.exit(1); process.exit(1);
}); });
@ -127,15 +127,12 @@ function initClient(options: any) {
request.headers.host = options.origin; request.headers.host = options.origin;
} }
const tunnelRequest = new TunnelRequest({ const tunnelRequest = new TunnelRequest(socket!, requestId);
requestId,
socket: socket,
});
const localReq = http.request(request); const localReq = http.request(request);
tunnelRequest.pipe(localReq); tunnelRequest.pipe(localReq);
const onTunnelRequestError = (e) => { const onTunnelRequestError = (e: any) => {
tunnelRequest.off("end", onTunnelRequestEnd); tunnelRequest.off("end", onTunnelRequestEnd);
localReq.destroy(e); localReq.destroy(e);
}; };
@ -147,17 +144,14 @@ function initClient(options: any) {
tunnelRequest.once("error", onTunnelRequestError); tunnelRequest.once("error", onTunnelRequestError);
tunnelRequest.once("end", onTunnelRequestEnd); tunnelRequest.once("end", onTunnelRequestEnd);
const onLocalResponse = (localRes) => { const onLocalResponse = (localRes: any) => {
localReq.off("error", onLocalError); localReq.off("error", onLocalError);
if (isWebSocket && localRes.upgrade) { if (isWebSocket && localRes.upgrade) {
return; return;
} }
const tunnelResponse = new TunnelResponse({ const tunnelResponse = new TunnelResponse(socket!, requestId);
responseId: requestId,
socket: socket,
});
tunnelResponse.writeHead( tunnelResponse.writeHead(
localRes.statusCode, localRes.statusCode,
@ -169,23 +163,18 @@ function initClient(options: any) {
localRes.pipe(tunnelResponse); localRes.pipe(tunnelResponse);
}; };
const onLocalError = (error) => { const onLocalError = (error: any) => {
console.log(error); console.log(error);
localReq.off("response", onLocalResponse); localReq.off("response", onLocalResponse);
socket.emit("request-error", requestId, error && error.message); socket?.emit("request-error", requestId, error && error.message);
tunnelRequest.destroy(error); tunnelRequest.destroy(error);
}; };
const onUpgrade = (localRes, localSocket, localHead) => { const onUpgrade = (localRes: any, localSocket: any, localHead: any) => {
// localSocket.once('error', onTunnelRequestError); // localSocket.once('error', onTunnelRequestError);
if (localHead && localHead.length) localSocket.unshift(localHead); if (localHead && localHead.length) localSocket.unshift(localHead);
const tunnelResponse = new TunnelResponse({ const tunnelResponse = new TunnelResponse(socket!, requestId, true);
responseId: requestId,
socket: socket,
duplex: true,
});
tunnelResponse.writeHead(null, null, localRes.headers); tunnelResponse.writeHead(null, null, localRes.headers);
localSocket.pipe(tunnelResponse).pipe(localSocket); localSocket.pipe(tunnelResponse).pipe(localSocket);
}; };
@ -201,7 +190,7 @@ function initClient(options: any) {
// reconnect manually // reconnect manually
const tryReconnect = () => { const tryReconnect = () => {
setTimeout(() => { setTimeout(() => {
socket.io.open((err) => { socket!.io.open((err) => {
if (err) { if (err) {
tryReconnect(); tryReconnect();
} }

View File

@ -100,6 +100,7 @@ class TunnelResponse extends stream.Duplex {
this.socket.on("response-pipe-error", onResponsePipeError); this.socket.on("response-pipe-error", onResponsePipeError);
this.socket.on("response-pipe-end", onResponsePipeEnd); this.socket.on("response-pipe-end", onResponsePipeEnd);
} }
} }
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void) { _write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void) {