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:
parent
5e8f95b613
commit
4e6158df7e
18
README.md
18
README.md
@ -4,22 +4,28 @@ A lightweight http tunnel client using nodejs and socket.io client.
|
|||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
|
- Initialize Client
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npx @cubetiq/hlt init
|
||||||
|
```
|
||||||
|
|
||||||
- Generate Client Key
|
- Generate Client Key
|
||||||
|
|
||||||
```
|
```shell
|
||||||
npx @cubetiq/http-tunnel config client new
|
npx @cubetiq/hlt config client new
|
||||||
```
|
```
|
||||||
|
|
||||||
- Set Client Token (Required, contact to vendor)
|
- Set Client Token (Required, contact to vendor)
|
||||||
|
|
||||||
```
|
```shell
|
||||||
npx @cubetiq/http-tunnel config token $TOKEN
|
npx @cubetiq/hlt config token $TOKEN
|
||||||
```
|
```
|
||||||
|
|
||||||
- Start Client
|
- Start Client
|
||||||
|
|
||||||
```
|
```shell
|
||||||
npx @cubetiq/http-tunnel start
|
npx @cubetiq/hlt start
|
||||||
```
|
```
|
||||||
|
|
||||||
### Contributors
|
### Contributors
|
||||||
|
85
client.js
85
client.js
@ -12,6 +12,7 @@ const sdk = require("./sdk");
|
|||||||
|
|
||||||
// constants
|
// constants
|
||||||
const PROFILE_DEFAULT = "default";
|
const PROFILE_DEFAULT = "default";
|
||||||
|
const PROFILE_PATH = ".hlt";
|
||||||
const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
|
const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
|
||||||
|
|
||||||
// create socket instance
|
// create socket instance
|
||||||
@ -165,8 +166,78 @@ function initClient(options) {
|
|||||||
keepAlive();
|
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
|
program
|
||||||
.command("start")
|
.command("start")
|
||||||
.argument("<port>", "local server port number", (value) => {
|
.argument("<port>", "local server port number", (value) => {
|
||||||
@ -181,7 +252,7 @@ program
|
|||||||
.option("-h, --host <string>", "local host value", "localhost")
|
.option("-h, --host <string>", "local host value", "localhost")
|
||||||
.option("-o, --origin <string>", "change request origin")
|
.option("-o, --origin <string>", "change request origin")
|
||||||
.action((port, options) => {
|
.action((port, options) => {
|
||||||
const configDir = path.resolve(os.homedir(), ".http-tunnel");
|
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||||
|
|
||||||
if (!fs.existsSync(configDir)) {
|
if (!fs.existsSync(configDir)) {
|
||||||
fs.mkdirSync(configDir);
|
fs.mkdirSync(configDir);
|
||||||
@ -200,16 +271,16 @@ program
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!config.token) {
|
if (!config.token) {
|
||||||
console.info(`Please set token for ${config.server}`);
|
console.info(`please set token for ${config.server}`);
|
||||||
console.info(
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.clientId) {
|
if (!config.clientId) {
|
||||||
if (!config.apiKey) {
|
if (!config.apiKey) {
|
||||||
console.info(`Please create client for ${config.server}`);
|
console.info(`please create client for ${config.server}`);
|
||||||
} else {
|
} else {
|
||||||
config.clientId = config.apiKey;
|
config.clientId = config.apiKey;
|
||||||
}
|
}
|
||||||
@ -226,6 +297,7 @@ program
|
|||||||
initClient(options);
|
initClient(options);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// config
|
||||||
program
|
program
|
||||||
.command("config")
|
.command("config")
|
||||||
.addArgument(
|
.addArgument(
|
||||||
@ -245,7 +317,7 @@ program
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const configDir = path.resolve(os.homedir(), ".http-tunnel");
|
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||||
|
|
||||||
if (!fs.existsSync(configDir)) {
|
if (!fs.existsSync(configDir)) {
|
||||||
fs.mkdirSync(configDir);
|
fs.mkdirSync(configDir);
|
||||||
@ -285,7 +357,6 @@ program
|
|||||||
await sdk
|
await sdk
|
||||||
.getTokenFree(config.server)
|
.getTokenFree(config.server)
|
||||||
.then((resp) => {
|
.then((resp) => {
|
||||||
console.log("data = ", resp.data);
|
|
||||||
if (resp.data?.token) {
|
if (resp.data?.token) {
|
||||||
config.token = resp.data?.token;
|
config.token = resp.data?.token;
|
||||||
} else {
|
} else {
|
||||||
|
6
package-lock.json
generated
6
package-lock.json
generated
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@cubetiq/http-tunnel-client",
|
"name": "@cubetiq/hlt",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@cubetiq/http-tunnel-client",
|
"name": "@cubetiq/hlt",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -15,7 +15,7 @@
|
|||||||
"socket.io-client": "^4.5.1"
|
"socket.io-client": "^4.5.1"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"http-tunnel": "bin/http-tunnel"
|
"hlt": "bin/hlt"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@socket.io/component-emitter": {
|
"node_modules/@socket.io/component-emitter": {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@cubetiq/http-tunnel-client",
|
"name": "@cubetiq/hlt",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"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": {
|
||||||
"http-tunnel": "bin/http-tunnel"
|
"hlt": "bin/hlt"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node client.js"
|
"start": "node client.js"
|
||||||
|
Loading…
Reference in New Issue
Block a user