Task: Refactoring http-tunnel to hlt and add init command for new client and updated the function for http tunnel client

This commit is contained in:
Sambo Chea 2022-06-26 00:22:33 +07:00
parent 5e8f95b613
commit 4e6158df7e
5 changed files with 95 additions and 18 deletions

View File

@ -4,22 +4,28 @@ A lightweight http tunnel client using nodejs and socket.io client.
### Usage
- Initialize Client
```shell
npx @cubetiq/hlt init
```
- Generate Client Key
```
npx @cubetiq/http-tunnel config client new
```shell
npx @cubetiq/hlt config client new
```
- Set Client Token (Required, contact to vendor)
```
npx @cubetiq/http-tunnel config token $TOKEN
```shell
npx @cubetiq/hlt config token $TOKEN
```
- Start Client
```
npx @cubetiq/http-tunnel start
```shell
npx @cubetiq/hlt start
```
### Contributors

View File

@ -12,6 +12,7 @@ const sdk = require("./sdk");
// constants
const PROFILE_DEFAULT = "default";
const PROFILE_PATH = ".hlt";
const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
// create socket instance
@ -165,8 +166,78 @@ function initClient(options) {
keepAlive();
}
program.name("http-tunnel").description("CUBETIQ HTTP tunnel client");
program.name("hlt").description("CUBETIQ HTTP tunnel client");
// init
program
.command("init")
.option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL)
.option(
"-t --token <string>",
"setting token (default generate FREE access token)",
""
)
.option("-c --client <string>", "setting client (auto generate uuid)", "")
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
.action(async (options) => {
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
console.log(`config file ${configDir} was created`);
}
let config = {};
const configFilename = `${options.profile}.json`;
const configFilePath = path.resolve(configDir, configFilename);
if (fs.existsSync(configFilePath)) {
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
}
if (options.server) {
config.server = options.server;
}
if (options.token) {
config.token = options.token;
}
if (options.client) {
config.clientId = options.clientId;
}
if (!config.server) {
config.server = SERVER_DEFAULT_URL;
}
if (!config.clientId) {
config.clientId = generateUUID();
}
if (!config.token) {
console.log("Generating token...");
await sdk
.getTokenFree(config.server)
.then((resp) => {
if (resp.data?.token) {
config.token = resp.data?.token;
} else {
console.log("free token return with null or empty from server");
return;
}
})
.catch((err) => {
console.error("cannot get free token from server", err);
return;
});
}
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
console.log(`initialized config saved successfully to: ${configFilePath}`);
});
// start
program
.command("start")
.argument("<port>", "local server port number", (value) => {
@ -181,7 +252,7 @@ program
.option("-h, --host <string>", "local host value", "localhost")
.option("-o, --origin <string>", "change request origin")
.action((port, options) => {
const configDir = path.resolve(os.homedir(), ".http-tunnel");
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
@ -200,16 +271,16 @@ program
}
if (!config.token) {
console.info(`Please set token for ${config.server}`);
console.info(`please set token for ${config.server}`);
console.info(
"If you don't have token yet, please contact to: sombochea@cubetiqs.com"
"if you don't have token yet, please contact to: sombochea@cubetiqs.com"
);
return;
}
if (!config.clientId) {
if (!config.apiKey) {
console.info(`Please create client for ${config.server}`);
console.info(`please create client for ${config.server}`);
} else {
config.clientId = config.apiKey;
}
@ -226,6 +297,7 @@ program
initClient(options);
});
// config
program
.command("config")
.addArgument(
@ -245,7 +317,7 @@ program
return;
}
const configDir = path.resolve(os.homedir(), ".http-tunnel");
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
@ -285,7 +357,6 @@ program
await sdk
.getTokenFree(config.server)
.then((resp) => {
console.log("data = ", resp.data);
if (resp.data?.token) {
config.token = resp.data?.token;
} else {

6
package-lock.json generated
View File

@ -1,11 +1,11 @@
{
"name": "@cubetiq/http-tunnel-client",
"name": "@cubetiq/hlt",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@cubetiq/http-tunnel-client",
"name": "@cubetiq/hlt",
"version": "0.0.1",
"license": "ISC",
"dependencies": {
@ -15,7 +15,7 @@
"socket.io-client": "^4.5.1"
},
"bin": {
"http-tunnel": "bin/http-tunnel"
"hlt": "bin/hlt"
}
},
"node_modules/@socket.io/component-emitter": {

View File

@ -1,10 +1,10 @@
{
"name": "@cubetiq/http-tunnel-client",
"name": "@cubetiq/hlt",
"version": "0.0.1",
"description": "A lightweight http tunnel client using nodejs and socket.io client.",
"main": "client.js",
"bin": {
"http-tunnel": "bin/http-tunnel"
"hlt": "bin/hlt"
},
"scripts": {
"start": "node client.js"