diff --git a/CHANGELOG.md b/CHANGELOG.md index 683eaa6..59e6f58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ -### 11/07/2023 +### 11/07/2023 (1.0.7) - Support Client API - Support Proxy (HTTP/HTTPS) and TCP - Improvements and Bugs fixed +- Support auto proxy with hlt client ### 30/11/2022 diff --git a/package.json b/package.json index 10f9187..69876d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cubetiq/hlt", - "version": "0.1.6", + "version": "0.1.7", "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 23592c9..f49b549 100644 --- a/src/api.ts +++ b/src/api.ts @@ -11,15 +11,17 @@ import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from ". import { ClientOptions, Options } from "./interface"; import { getTokenFree } from './sdk'; -interface Stoppable { +interface Client { + getEndpoint(): string | null; stop(): void; } -class HttpTunnelClient implements Stoppable { +class HttpTunnelClient implements Client { // create socket instance private socket: Socket | null = null; private keepAliveTimer: NodeJS.Timeout | null = null; private keepAliveTimeout: number | null = null; + private endpoint: string | null = null; private keepAlive() { if (!this.socket) { @@ -119,6 +121,7 @@ class HttpTunnelClient implements Stoppable { .toLowerCase() .trim(); const serverUrl = addPrefixOnHttpSchema(options.server || SERVER_DEFAULT_URL, clientEndpoint); + this.endpoint = serverUrl // extra options for socket to identify the client (authentication and options of tunnel) const defaultParams = { @@ -278,7 +281,7 @@ class HttpTunnelClient implements Stoppable { this.keepAlive(); }; - public start = async (clientOptions: ClientOptions): Promise => { + public start = async (clientOptions: ClientOptions): Promise => { const { port, options = {} } = clientOptions; const configDir = path.resolve(os.homedir(), PROFILE_PATH); @@ -289,7 +292,7 @@ class HttpTunnelClient implements Stoppable { let config: any = {}; const configFilename = `${options.profile || PROFILE_DEFAULT}.json`; const configFilePath = path.resolve(configDir, configFilename); - console.log(`config file: ${configFilePath}`); + // console.log(`config file: ${configFilePath}`); if (fs.existsSync(configFilePath)) { config = JSON.parse(fs.readFileSync(configFilePath, "utf8")); @@ -329,7 +332,7 @@ class HttpTunnelClient implements Stoppable { } await this.initStartClient(options); - console.log("client started!"); + // console.log("client started!"); return this; }; @@ -341,9 +344,13 @@ class HttpTunnelClient implements Stoppable { this.socket = null; this.keepAliveTimer && clearInterval(this.keepAliveTimer); - console.log("client stopped!"); + console.log("client stopped from server:", this.endpoint); } }; + + public getEndpoint = () => { + return this.endpoint; + } } export const client = new HttpTunnelClient(); diff --git a/src/cli.ts b/src/cli.ts index 52f8fe8..bdd2f73 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -266,7 +266,8 @@ program throw new InvalidArgumentError("Target is not a url or host with port."); }) - .action((port, target) => { + .option("-p --profile ", "setting profile name for connect with hlt server (proxy with current local port)") + .action((port, target, options) => { const isTcp = target.indexOf("tcp") === 0; if (isTcp) { console.log("[TCP] Start proxy server with port:", port, "and target:", target); @@ -277,6 +278,8 @@ program proxyPort: port, }); + onConnectProxy(port, options); + proxy.on("error", (err) => { console.error("Proxy server error:", err); }); @@ -284,12 +287,15 @@ program proxy.on("close", () => { console.log("Proxy server closed"); }); + } else { console.log("[HTTP/HTTPS] Start proxy server with port:", port, "and target:", target); const proxy = createProxyServer(target, { proxyPort: port, }); + onConnectProxy(port, options); + proxy.on("error", (err) => { console.error("Proxy server error:", err); }); @@ -301,4 +307,14 @@ program }); +const onConnectProxy = (port: number, options: any) => { + if (options?.profile) { + console.log(`Start proxy: ${port} via hlt client with profile: ${options.profile}`); + startClient({ + port, + options, + }) + } +} + program.parse(); diff --git a/test/test.ts b/test/test.ts index 9a0b741..f4f76ac 100644 --- a/test/test.ts +++ b/test/test.ts @@ -8,6 +8,9 @@ async function main() { }, }); + + console.log('Client started:', client?.getEndpoint()); + setTimeout(async () => { client?.stop(); }, 5000);