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.
### 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

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 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 <string>", "setting server url", SERVER_DEFAULT_URL)
.option(
"-t --token <string>",
@ -250,6 +270,7 @@ program
// start
program
.command("start")
.description("start a connection with specific port")
.argument("<port>", "local server port number", (value) => {
const port = parseInt(value, 10);
if (isNaN(port)) {
@ -257,6 +278,7 @@ program
}
return port;
})
.option("-s, --suffix <string>", "setting suffix for client name")
.option("-a, --access <string>", "setting access type (FREE)", TOKEN_FREE)
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
.option("-h, --host <string>", "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("<type>", "config type").choices([
"access",

View File

@ -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"

2
sdk.js
View File

@ -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,