Task: Upgrading the http tunnel client to typescript language and updated the response and request models and updated the types

This commit is contained in:
Sambo Chea 2022-07-09 21:07:55 +07:00
parent 1b96f430e8
commit 23c7bda220
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
9 changed files with 1425 additions and 179 deletions

2
.npmignore Normal file
View File

@ -0,0 +1,2 @@
src/
.github/

156
lib.js
View File

@ -1,156 +0,0 @@
const stream = require("stream");
class TunnelRequest extends stream.Readable {
constructor({ socket, requestId }) {
super();
this._socket = socket;
this._requestId = requestId;
const onRequestPipe = (requestId, data) => {
if (this._requestId === requestId) {
this.push(data);
}
};
const onRequestPipes = (requestId, data) => {
if (this._requestId === requestId) {
data.forEach((chunk) => {
this.push(chunk);
});
}
};
const onRequestPipeError = (requestId, error) => {
if (this._requestId === requestId) {
this._socket.off("request-pipe", onRequestPipe);
this._socket.off("request-pipes", onRequestPipes);
this._socket.off("request-pipe-error", onRequestPipeError);
this._socket.off("request-pipe-end", onRequestPipeEnd);
this.destroy(new Error(error));
}
};
const onRequestPipeEnd = (requestId, data) => {
if (this._requestId === requestId) {
this._socket.off("request-pipe", onRequestPipe);
this._socket.off("request-pipes", onRequestPipes);
this._socket.off("request-pipe-error", onRequestPipeError);
this._socket.off("request-pipe-end", onRequestPipeEnd);
if (data) {
this.push(data);
}
this.push(null);
}
};
this._socket.on("request-pipe", onRequestPipe);
this._socket.on("request-pipes", onRequestPipes);
this._socket.on("request-pipe-error", onRequestPipeError);
this._socket.on("request-pipe-end", onRequestPipeEnd);
}
_read() { }
}
class TunnelResponse extends stream.Duplex {
constructor({ socket, responseId, duplex }) {
super();
this._socket = socket;
this._responseId = responseId;
if (duplex) {
// for websocket request bidirection
const onResponsePipe = (responseId, data) => {
if (this._responseId === responseId) {
this.push(data);
}
};
const onResponsePipes = (responseId, data) => {
if (this._responseId === responseId) {
data.forEach((chunk) => {
this.push(chunk);
});
}
};
const onResponsePipeError = (responseId, error) => {
if (this._responseId === responseId) {
this._socket.off("response-pipe", onResponsePipe);
this._socket.off("response-pipes", onResponsePipes);
this._socket.off("response-pipe-error", onResponsePipeError);
this._socket.off("response-pipe-end", onResponsePipeEnd);
this.destroy(new Error(error));
}
};
const onResponsePipeEnd = (responseId, data) => {
if (this._responseId === responseId) {
this._socket.off("response-pipe", onResponsePipe);
this._socket.off("response-pipes", onResponsePipes);
this._socket.off("response-pipe-error", onResponsePipeError);
this._socket.off("response-pipe-end", onResponsePipeEnd);
if (data) {
this.push(data);
}
this.push(null);
}
};
this._socket.on("response-pipe", onResponsePipe);
this._socket.on("response-pipes", onResponsePipes);
this._socket.on("response-pipe-error", onResponsePipeError);
this._socket.on("response-pipe-end", onResponsePipeEnd);
}
}
_write(chunk, encoding, callback) {
this._socket.emit("response-pipe", this._responseId, chunk);
this._socket.io.engine.once("drain", () => {
callback();
});
}
_writev(chunks, callback) {
this._socket.emit("response-pipes", this._responseId, chunks);
this._socket.io.engine.once("drain", () => {
callback();
});
}
_final(callback) {
this._socket.emit("response-pipe-end", this._responseId);
this._socket.io.engine.once("drain", () => {
callback();
});
}
_destroy(e, callback) {
if (e) {
this._socket.emit(
"response-pipe-error",
this._responseId,
e && e.message
);
this._socket.io.engine.once("drain", () => {
callback();
});
return;
}
callback();
}
writeHead(statusCode, statusMessage, headers, httpVersion) {
this._socket.emit("response", this._responseId, {
statusCode,
statusMessage,
headers,
httpVersion,
});
}
_read(size) { }
}
exports.TunnelRequest = TunnelRequest;
exports.TunnelResponse = TunnelResponse;

1219
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,8 @@
"hlt": "bin/hlt"
},
"scripts": {
"start": "node client.js"
"start": "ts-node-dev --respawn --transpile-only src/client.ts",
"build": "rm -rf dist && tsc"
},
"repository": {
"type": "git",
@ -27,5 +28,10 @@
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/node": "^18.0.3",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
}
}

View File

@ -1,16 +1,17 @@
const os = require("os");
const fs = require("fs");
const path = require("path");
const http = require("http");
const { io } = require("socket.io-client");
const HttpsProxyAgent = require("https-proxy-agent");
const { program, InvalidArgumentError, Argument } = require("commander");
const { TunnelRequest, TunnelResponse } = require("./lib");
const { generateUUID, addPrefixOnHttpSchema } = require("./util");
import * as os from "os";
import * as fs from "fs";
import * as path from "path";
import * as http from "http";
import { io } from "socket.io-client";
import { HttpsProxyAgent } from "https-proxy-agent";
import { program, InvalidArgumentError, Argument } from "commander";
import { TunnelRequest, TunnelResponse } from "./lib";
import { generateUUID, addPrefixOnHttpSchema } from "./util";
import { Socket } from 'socket.io-client';
const sdk = require("./sdk");
const sdk = require("../sdk");
const packageInfo = require("./package.json");
const packageInfo = require("../package.json");
// constants
const PROFILE_DEFAULT = "default";
@ -19,7 +20,7 @@ const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
const TOKEN_FREE = "FREE";
// create socket instance
let socket = null;
let socket: Socket | null = null;
function keepAlive() {
setTimeout(() => {
@ -30,7 +31,7 @@ function keepAlive() {
}, 5000);
}
function initClient(options) {
function initClient(options: any) {
// Please change this if your domain goes wrong here
// Current style using sub-domain: https://{{clientId}}-tunnel.myhostingdomain.com
// (Original server: https://tunnel.myhostingdomain.com)
@ -245,7 +246,7 @@ program
console.log(`config file ${configDir} was created`);
}
let config = {};
let config: any = {};
const configFilename = `${options.profile}.json`;
const configFilePath = path.resolve(configDir, configFilename);
@ -327,7 +328,7 @@ program
fs.mkdirSync(configDir);
}
let config = {};
let config: any = {};
const configFilename = `${options.profile}.json`;
const configFilePath = path.resolve(configDir, configFilename);
@ -399,7 +400,7 @@ program
console.log(`config file ${configDir} was created`);
}
let config = {};
let config: any = {};
const configFilename = `${options.profile}.json`;
const configFilePath = path.resolve(configDir, configFilename);
@ -480,7 +481,7 @@ program
return;
}
let config = {};
let config: any = {};
const configFilename = `${options.profile}.json`;
const configFilePath = path.resolve(configDir, configFilename);

164
src/lib.ts Normal file
View File

@ -0,0 +1,164 @@
import * as stream from "stream";
import { Socket } from 'socket.io-client';
class TunnelRequest extends stream.Readable {
constructor(private socket: Socket, private requestId: string) {
super();
const onRequestPipe = (requestId: string, data: any) => {
if (this.requestId === requestId) {
this.push(data);
}
};
const onRequestPipes = (requestId: string, data: any) => {
if (!data) return;
if (this.requestId === requestId) {
data.forEach((chunk: any) => {
this.push(chunk);
});
}
};
const onRequestPipeError = (requestId: string, error?: any) => {
if (this.requestId === requestId) {
this.socket.off("request-pipe", onRequestPipe);
this.socket.off("request-pipes", onRequestPipes);
this.socket.off("request-pipe-error", onRequestPipeError);
this.socket.off("request-pipe-end", onRequestPipeEnd);
this.destroy(new Error(error));
}
};
const onRequestPipeEnd = (requestId: string, data: any) => {
if (this.requestId === requestId) {
this.socket.off("request-pipe", onRequestPipe);
this.socket.off("request-pipes", onRequestPipes);
this.socket.off("request-pipe-error", onRequestPipeError);
this.socket.off("request-pipe-end", onRequestPipeEnd);
if (data) {
this.push(data);
}
this.push(null);
}
};
this.socket.on("request-pipe", onRequestPipe);
this.socket.on("request-pipes", onRequestPipes);
this.socket.on("request-pipe-error", onRequestPipeError);
this.socket.on("request-pipe-end", onRequestPipeEnd);
}
_read() { }
}
class TunnelResponse extends stream.Duplex {
constructor(private socket: Socket, private responseId: string, duplex?: boolean) {
super();
if (duplex) {
// for websocket request bidirection
const onResponsePipe = (responseId: string, data: any) => {
if (this.responseId === responseId) {
this.push(data);
}
};
const onResponsePipes = (responseId: string, data: any) => {
if (this.responseId === responseId) {
data.forEach((chunk: any) => {
this.push(chunk);
});
}
};
const onResponsePipeError = (responseId: string, error?: any) => {
if (this.responseId === responseId) {
this.socket.off("response-pipe", onResponsePipe);
this.socket.off("response-pipes", onResponsePipes);
this.socket.off("response-pipe-error", onResponsePipeError);
this.socket.off("response-pipe-end", onResponsePipeEnd);
this.destroy(new Error(error));
}
};
const onResponsePipeEnd = (responseId: string, data: any) => {
if (this.responseId === responseId) {
this.socket.off("response-pipe", onResponsePipe);
this.socket.off("response-pipes", onResponsePipes);
this.socket.off("response-pipe-error", onResponsePipeError);
this.socket.off("response-pipe-end", onResponsePipeEnd);
if (data) {
this.push(data);
}
this.push(null);
}
};
this.socket.on("response-pipe", onResponsePipe);
this.socket.on("response-pipes", onResponsePipes);
this.socket.on("response-pipe-error", onResponsePipeError);
this.socket.on("response-pipe-end", onResponsePipeEnd);
}
}
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void) {
this.socket.emit("response-pipe", this.responseId, chunk);
this.socket.io.engine.once("drain", () => {
callback && callback();
});
}
_writev(
chunks: Array<{
chunk: any;
encoding: BufferEncoding;
}>,
callback: (error?: Error | null) => void) {
this.socket.emit("response-pipes", this.responseId, chunks);
this.socket.io.engine.once("drain", () => {
callback();
});
}
_final(callback: (error?: Error | null) => void) {
this.socket.emit("response-pipe-end", this.responseId);
this.socket.io.engine.once("drain", () => {
callback();
});
}
_destroy(error: Error | null, callback: (error: Error | null) => void) {
if (error) {
this.socket.emit(
"response-pipe-error",
this.responseId,
error && error.message
);
this.socket.io.engine.once("drain", () => {
callback(error);
});
return;
}
callback(null);
}
writeHead(statusCode: any, statusMessage?: any, headers?: any, httpVersion?: any) {
this.socket.emit("response", this.responseId, {
statusCode,
statusMessage,
headers,
httpVersion,
});
}
_read(size: number) { }
}
export {
TunnelRequest,
TunnelResponse
}

View File

View File

@ -1,6 +1,6 @@
const crypto = require("crypto");
import * as crypto from "crypto";
const addPrefixOnHttpSchema = (url, prefixDomain) => {
const addPrefixOnHttpSchema = (url: string, prefixDomain: string) => {
let prefixSubDomain = prefixDomain;
const prefixSchema = url.substring(0, url.indexOf("://") + 3);
const splitDomain = url.substring(url.indexOf("://") + 3);
@ -16,4 +16,4 @@ const generateUUID = () => {
return crypto.randomUUID();
};
module.exports = { addPrefixOnHttpSchema, generateUUID };
export { addPrefixOnHttpSchema, generateUUID };

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["ES2015"],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": false,
"outDir": "dist",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}