From 8e3243f86e2797b928b6e6360681752bb80be67e Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sun, 26 Jun 2022 14:28:37 +0700 Subject: [PATCH] Task: Add client initialize with quick create client id and token and also start with custom suffix and default suffix can be port or uuid and add more features for http tunnel client --- README.md | 43 ++++++++++++++++++++++++++++++++++++++----- bin/hlt | 0 client.js | 37 ++++++++++++++++++++++++++++++++++--- package.json | 4 ++-- sdk.js | 2 +- 5 files changed, 75 insertions(+), 11 deletions(-) mode change 100644 => 100755 bin/hlt diff --git a/README.md b/README.md index bb6d08a..33d971f 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,41 @@ A lightweight http tunnel client using nodejs and socket.io client. -### Usage - -- Initialize Client +### Installation ```shell -npx @cubetiq/hlt init +npm i -g @cubetiq/hlt + +OR + +npx @cubetiq/hlt + ``` +### Usage + +- Initialize Client and Start (Quick) + +```shell +# Initialize a client and token for connect (default's profile) +npx @cubetiq/hlt init + +# Start port 3000 to remote server +npx @cubetiq/hlt start 3000 +``` + +- Initialize Client and Start (Quick with custom's profile) + +```shell +# Initialize a client and token for connect (mytest's profile) +npx @cubetiq/hlt init -p mytest + +# Start port 3000 to remote server (mytest's profile) +npx @cubetiq/hlt start 3000 -p mytest +``` + +### Custom Config + - Generate Client Key ```shell @@ -22,10 +49,16 @@ npx @cubetiq/hlt config client new npx @cubetiq/hlt config token $TOKEN ``` +- Custom Server + +```shell +npx @cubetiq/hlt config server https://lt.ctdn.net +``` + - Start Client ```shell -npx @cubetiq/hlt start +npx @cubetiq/hlt start $YOUR_PORT ``` ### Contributors diff --git a/bin/hlt b/bin/hlt old mode 100644 new mode 100755 diff --git a/client.js b/client.js index 7f05d84..443e32c 100644 --- a/client.js +++ b/client.js @@ -10,6 +10,8 @@ const { generateUUID, addPrefixOnHttpSchema } = require("./util"); const sdk = require("./sdk"); +const packageInfo = require("./package.json"); + // constants const PROFILE_DEFAULT = "default"; const PROFILE_PATH = ".hlt"; @@ -36,13 +38,19 @@ function initClient(options) { const clientId = `${options.clientId || options.apiKey || generateUUID()}`; const clientIdSub = profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`; - const serverUrl = addPrefixOnHttpSchema(options.server, clientIdSub); + const clientEndpoint = ( + options.suffix ? `${clientIdSub}${options.suffix}-` : clientIdSub + ) + .toLowerCase() + .trim(); + const serverUrl = addPrefixOnHttpSchema(options.server, clientEndpoint); const defaultParams = { apiKey: options.apiKey, clientId: options.clientId, profile: options.profile, clientIdSub: clientIdSub, + clientEndpoint: clientEndpoint, serverUrl: serverUrl, access: options.access, }; @@ -76,7 +84,13 @@ function initClient(options) { }); socket.on("connect_error", (e) => { - console.log(`${clientLogPrefix} connect error!`, e && e.message); + console.log( + `${clientLogPrefix} connect error:`, + (e && e.message) || "something wrong" + ); + if (e && e.message && e.message.startsWith("[40")) { + process.exit(1); + } }); socket.on("disconnect", (reason) => { @@ -167,11 +181,17 @@ function initClient(options) { keepAlive(); } -program.name("hlt").description("CUBETIQ HTTP tunnel client"); +program + .name("hlt") + .description( + "CUBETIQ HTTP tunnel client with free access for local tunneling" + ) + .version(`v${packageInfo.version}`); // init program .command("init") + .description("generate a new client and token with free access") .option("-s --server ", "setting server url", SERVER_DEFAULT_URL) .option( "-t --token ", @@ -250,6 +270,7 @@ program // start program .command("start") + .description("start a connection with specific port") .argument("", "local server port number", (value) => { const port = parseInt(value, 10); if (isNaN(port)) { @@ -257,6 +278,7 @@ program } return port; }) + .option("-s, --suffix ", "setting suffix for client name") .option("-a, --access ", "setting access type (FREE)", TOKEN_FREE) .option("-p, --profile ", "setting profile name", PROFILE_DEFAULT) .option("-h, --host ", "local host value", "localhost") @@ -304,12 +326,21 @@ program options.clientId = config.clientId; options.apiKey = config.apiKey; + if (options.suffix === "port" || options.suffix === "true") { + options.suffix = `${port}`; + } else if (options.suffix === "false") { + options.suffix = undefined; + } else if (options.suffix === "gen" || options.suffix === "uuid") { + options.suffix = generateUUID(); + } + initClient(options); }); // config program .command("config") + .description("create and update config file for connection") .addArgument( new Argument("", "config type").choices([ "access", diff --git a/package.json b/package.json index 879c246..90d0806 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@cubetiq/hlt", - "version": "0.0.1", - "description": "A lightweight http tunnel client using nodejs and socket.io client.", + "version": "0.0.2", + "description": "A lightweight http tunnel client using nodejs and socket.io client", "main": "client.js", "bin": { "hlt": "bin/hlt" diff --git a/sdk.js b/sdk.js index 342404c..a7cad0b 100644 --- a/sdk.js +++ b/sdk.js @@ -1,7 +1,7 @@ const axios = require("axios").default; const getTokenFree = async (baseUrl, data = {}) => { - const url = `${baseUrl}/__free__/get_token`; + const url = `${baseUrl}/__free__/api/get_token`; return axios({ method: "POST", url: url,