From 775c5da038c60aa7821e00b070413b4afa43e3e4 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 11 Jul 2023 18:57:49 +0700 Subject: [PATCH] Add support for host and port within address --- CHANGELOG.md | 16 ++++++++++++++++ package.json | 2 +- src/api.ts | 50 ++++++++++++++++++++++++++++++++++++++++++------ src/cli.ts | 13 +++++++++---- src/interface.ts | 1 + test/test.ts | 6 +++--- 6 files changed, 74 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59e6f58..30bb06c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +### 11/07/2023 (1.0.8) + +- Support tunnel with host with port + +* Prev + +``` +hlt start 8080 -h 192.168.1.1 +``` + +- New + +``` +hlt start 192.168.1.1:8080 +``` + ### 11/07/2023 (1.0.7) - Support Client API diff --git a/package.json b/package.json index 69876d4..dee4c82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cubetiq/hlt", - "version": "0.1.7", + "version": "0.1.8", "description": "A lightweight http tunnel client using nodejs and socket.io client", "main": "dist/cli.js", "bin": { diff --git a/src/api.ts b/src/api.ts index f49b549..5ca4a4e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -281,8 +281,49 @@ class HttpTunnelClient implements Client { this.keepAlive(); }; - public start = async (clientOptions: ClientOptions): Promise => { - const { port, options = {} } = clientOptions; + public start = async (clientOptions: Partial): Promise => { + const { port, address, options = {} } = clientOptions; + + // Load host and port check + if (!port) { + if (!address) { + console.error("port or address is required!"); + return; + } + + const [host, portStr] = address.split(":"); + if (!host || !portStr) { + console.error("invalid address!"); + return; + } + + options.host = host; + try { + options.port = parseInt(portStr); + } catch (e) { + console.error("invalid port!"); + return; + } + } else { + if (address) { + const [host, portStr] = address.split(":"); + if (host) { + options.host = host; + } + + if (portStr) { + try { + options.port = parseInt(portStr); + console.log(`default port: ${port} will be ignored and override by port: ${options.port}`); + } catch (e) { + options.port = port; + } + } + } else { + options.port = port; + } + } + const configDir = path.resolve(os.homedir(), PROFILE_PATH); if (!fs.existsSync(configDir)) { @@ -292,7 +333,6 @@ class HttpTunnelClient implements Client { let config: any = {}; const configFilename = `${options.profile || PROFILE_DEFAULT}.json`; const configFilePath = path.resolve(configDir, configFilename); - // console.log(`config file: ${configFilePath}`); if (fs.existsSync(configFilePath)) { config = JSON.parse(fs.readFileSync(configFilePath, "utf8")); @@ -316,7 +356,7 @@ class HttpTunnelClient implements Client { return; } - options.port = port; + // options.port = port; options.token = config.token; options.access = config.access; options.server = config.server; @@ -332,8 +372,6 @@ class HttpTunnelClient implements Client { } await this.initStartClient(options); - // console.log("client started!"); - return this; }; diff --git a/src/cli.ts b/src/cli.ts index bdd2f73..78c5717 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -44,10 +44,14 @@ program program .command("start") .description("start a connection with specific port") - .argument("", "local server port number", (value) => { + .argument(" |
", "local server port number or address", (value) => { + if (isValidHost(value)) { + return value; + } + const port = parseInt(value, 10); if (isNaN(port)) { - throw new InvalidArgumentError("Not a number."); + throw new InvalidArgumentError("Not a number or valid address."); } return port; }) @@ -65,9 +69,10 @@ program .option("-p, --profile ", "profile name", PROFILE_DEFAULT) .option("-h, --host ", "local host value", "localhost") .option("-o, --origin ", "change request origin") - .action((port, options) => { + .action((portOrAddress, options) => { startClient({ - port, + port: portOrAddress, + address: portOrAddress, options, }) }); diff --git a/src/interface.ts b/src/interface.ts index b658f3f..210e34a 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -17,5 +17,6 @@ export interface Options { export interface ClientOptions { port: number; + address?: string; // e.g. localhost:8081 (take if port is not set) options?: Options; } \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index f4f76ac..4e1165a 100644 --- a/test/test.ts +++ b/test/test.ts @@ -2,18 +2,18 @@ import { startClient } from '../src/api'; async function main() { const client = await startClient({ - port: 3000, + // port: 8081, + address: '172.17.0.2:8222', options: { profile: 'mytest', }, }); - console.log('Client started:', client?.getEndpoint()); setTimeout(async () => { client?.stop(); - }, 5000); + }, 10000); } main().catch((err) => {