diff --git a/README.md b/README.md index 536317c..cd73b6e 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,28 @@ # CUBETIQ HTTP Tunnel Client + A lightweight http tunnel client using nodejs and socket.io client. ### Usage + - Generate Client Key + ``` npx @cubetiq/http-tunnel config client new ``` +- Set Client Token (Required, contact to vendor) + +``` +npx @cubetiq/http-tunnel config token $TOKEN +``` + +- Start Client + +``` +npx @cubetiq/http-tunnel start +``` + ### Contributors + - Original [web-tunnel](https://github.com/web-tunnel/lite-http-tunnel-client) -- Sambo Chea \ No newline at end of file +- Sambo Chea diff --git a/client.js b/client.js index 3a7db0e..6b20161 100644 --- a/client.js +++ b/client.js @@ -10,6 +10,7 @@ const { generateUUID, addPrefixOnHttpSchema } = require("./util"); // constants const PROFILE_DEFAULT = "default"; +const SERVER_DEFAULT_URL = "https://lt.ctdn.net"; // create socket instance let socket = null; @@ -28,20 +29,28 @@ function initClient(options) { // Current style using sub-domain: https://{{clientId}}-tunnel.myhostingdomain.com // (Original server: https://tunnel.myhostingdomain.com) const profile = options.profile || PROFILE_DEFAULT; - const clientId = `${options.clientId || generateUUID()}`; - const clientIdSub = `${clientId}-${profile}-`; + const clientId = `${options.clientId || options.apiKey || generateUUID()}`; + const clientIdSub = + profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`; const serverUrl = addPrefixOnHttpSchema(options.server, clientIdSub); + const defaultParams = { + apiKey: options.apiKey, + clientId: options.clientId, + profile: options.profile, + clientIdSub: clientIdSub, + serverUrl: serverUrl, + }; + const initParams = { path: "/$cubetiq_http_tunnel", transports: ["websocket"], auth: { token: options.token, - apiKey: options.apiKey, - clientId: options.clientId, - profile: options.profile, - clientIdSub: clientIdSub, - serverUrl: serverUrl, + ...defaultParams, + }, + headers: { + ...defaultParams, }, }; @@ -183,17 +192,23 @@ program } if (!config.server) { - console.error("Please set remote tunnel server firstly!"); - return; + config.server = SERVER_DEFAULT_URL; } if (!config.token) { - console.error(`Please set jwt token for ${config.server} firstly!`); + console.info(`Please set token for ${config.server}!`); + console.info( + "If you don't have token yet, please contact to: sombochea@cubetiqs.com" + ); return; } if (!config.clientId) { - console.error(`Please create client for ${config.server} firstly!`); + if (!config.apiKey) { + console.info(`Please create client for ${config.server}!`); + } else { + config.clientId = config.apiKey; + } return; } @@ -256,6 +271,10 @@ program config[key] = value; } + if (!config.clientId && config.apiKey) { + config.clientId = config.apiKey; + } + fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2)); console.log(`${type} config saved successfully to: ${configFilePath}!`); }); diff --git a/package.json b/package.json index ce9f2b0..3defa4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cubetiq/http-tunnel-client", - "version": "1.0.0", + "version": "0.0.1", "description": "A lightweight http tunnel client using nodejs and socket.io client.", "main": "client.js", "bin": {