Task: Add client headers and auth for tunnel connection and updated the configs load for init params for http tunnel client

This commit is contained in:
Sambo Chea 2022-06-25 20:32:38 +07:00
parent e925d37e1b
commit e5db37bf5d
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
3 changed files with 48 additions and 13 deletions

View File

@ -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 <sombochea@cubetiqs.com>
- Sambo Chea <sombochea@cubetiqs.com>

View File

@ -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}!`);
});

View File

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