diff --git a/README.md b/README.md index cd73b6e..bb6d08a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/http-tunnel b/bin/hlt similarity index 100% rename from bin/http-tunnel rename to bin/hlt diff --git a/client.js b/client.js index 8fa9c73..35af258 100644 --- a/client.js +++ b/client.js @@ -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 ", "setting server url", SERVER_DEFAULT_URL) + .option( + "-t --token ", + "setting token (default generate FREE access token)", + "" + ) + .option("-c --client ", "setting client (auto generate uuid)", "") + .option("-p --profile ", "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("", "local server port number", (value) => { @@ -181,7 +252,7 @@ program .option("-h, --host ", "local host value", "localhost") .option("-o, --origin ", "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 { diff --git a/package-lock.json b/package-lock.json index a1708fa..f344992 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { diff --git a/package.json b/package.json index 9144301..487044d 100644 --- a/package.json +++ b/package.json @@ -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"