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

This commit is contained in:
Sambo Chea 2022-06-26 14:28:37 +07:00
parent bc020825e1
commit 8e3243f86e
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
5 changed files with 75 additions and 11 deletions

View File

@ -2,14 +2,41 @@
A lightweight http tunnel client using nodejs and socket.io client. A lightweight http tunnel client using nodejs and socket.io client.
### Usage ### Installation
- Initialize Client
```shell ```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 - Generate Client Key
```shell ```shell
@ -22,10 +49,16 @@ npx @cubetiq/hlt config client new
npx @cubetiq/hlt config token $TOKEN npx @cubetiq/hlt config token $TOKEN
``` ```
- Custom Server
```shell
npx @cubetiq/hlt config server https://lt.ctdn.net
```
- Start Client - Start Client
```shell ```shell
npx @cubetiq/hlt start npx @cubetiq/hlt start $YOUR_PORT
``` ```
### Contributors ### Contributors

0
bin/hlt Normal file → Executable file
View File

View File

@ -10,6 +10,8 @@ const { generateUUID, addPrefixOnHttpSchema } = require("./util");
const sdk = require("./sdk"); const sdk = require("./sdk");
const packageInfo = require("./package.json");
// constants // constants
const PROFILE_DEFAULT = "default"; const PROFILE_DEFAULT = "default";
const PROFILE_PATH = ".hlt"; const PROFILE_PATH = ".hlt";
@ -36,13 +38,19 @@ function initClient(options) {
const clientId = `${options.clientId || options.apiKey || generateUUID()}`; const clientId = `${options.clientId || options.apiKey || generateUUID()}`;
const clientIdSub = const clientIdSub =
profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`; 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 = { const defaultParams = {
apiKey: options.apiKey, apiKey: options.apiKey,
clientId: options.clientId, clientId: options.clientId,
profile: options.profile, profile: options.profile,
clientIdSub: clientIdSub, clientIdSub: clientIdSub,
clientEndpoint: clientEndpoint,
serverUrl: serverUrl, serverUrl: serverUrl,
access: options.access, access: options.access,
}; };
@ -76,7 +84,13 @@ function initClient(options) {
}); });
socket.on("connect_error", (e) => { 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) => { socket.on("disconnect", (reason) => {
@ -167,11 +181,17 @@ function initClient(options) {
keepAlive(); 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 // init
program program
.command("init") .command("init")
.description("generate a new client and token with free access")
.option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL) .option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL)
.option( .option(
"-t --token <string>", "-t --token <string>",
@ -250,6 +270,7 @@ program
// start // start
program program
.command("start") .command("start")
.description("start a connection with specific port")
.argument("<port>", "local server port number", (value) => { .argument("<port>", "local server port number", (value) => {
const port = parseInt(value, 10); const port = parseInt(value, 10);
if (isNaN(port)) { if (isNaN(port)) {
@ -257,6 +278,7 @@ program
} }
return port; return port;
}) })
.option("-s, --suffix <string>", "setting suffix for client name")
.option("-a, --access <string>", "setting access type (FREE)", TOKEN_FREE) .option("-a, --access <string>", "setting access type (FREE)", TOKEN_FREE)
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT) .option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
.option("-h, --host <string>", "local host value", "localhost") .option("-h, --host <string>", "local host value", "localhost")
@ -304,12 +326,21 @@ program
options.clientId = config.clientId; options.clientId = config.clientId;
options.apiKey = config.apiKey; 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); initClient(options);
}); });
// config // config
program program
.command("config") .command("config")
.description("create and update config file for connection")
.addArgument( .addArgument(
new Argument("<type>", "config type").choices([ new Argument("<type>", "config type").choices([
"access", "access",

View File

@ -1,7 +1,7 @@
{ {
"name": "@cubetiq/hlt", "name": "@cubetiq/hlt",
"version": "0.0.1", "version": "0.0.2",
"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": "client.js",
"bin": { "bin": {
"hlt": "bin/hlt" "hlt": "bin/hlt"

2
sdk.js
View File

@ -1,7 +1,7 @@
const axios = require("axios").default; const axios = require("axios").default;
const getTokenFree = async (baseUrl, data = {}) => { const getTokenFree = async (baseUrl, data = {}) => {
const url = `${baseUrl}/__free__/get_token`; const url = `${baseUrl}/__free__/api/get_token`;
return axios({ return axios({
method: "POST", method: "POST",
url: url, url: url,