Task: Add client sdk for get token from server and add functions for enhance free usage for http tunnel client

This commit is contained in:
Sambo Chea 2022-06-25 21:38:09 +07:00
parent e5db37bf5d
commit 2a3a74fade
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
4 changed files with 195 additions and 6 deletions

View File

@ -8,6 +8,8 @@ const { program, InvalidArgumentError, Argument } = require("commander");
const { TunnelRequest, TunnelResponse } = require("./lib");
const { generateUUID, addPrefixOnHttpSchema } = require("./util");
const sdk = require("./sdk");
// constants
const PROFILE_DEFAULT = "default";
const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
@ -47,6 +49,7 @@ function initClient(options) {
transports: ["websocket"],
auth: {
token: options.token,
token_free: options.token_free,
...defaultParams,
},
headers: {
@ -173,6 +176,7 @@ program
}
return port;
})
.option("-a, --access <string>", "setting access type (FREE)", "FREE")
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
.option("-h, --host <string>", "local host value", "localhost")
.option("-o, --origin <string>", "change request origin")
@ -195,7 +199,7 @@ program
config.server = SERVER_DEFAULT_URL;
}
if (!config.token) {
if (!config.token && !config.token_free) {
console.info(`Please set token for ${config.server}!`);
console.info(
"If you don't have token yet, please contact to: sombochea@cubetiqs.com"
@ -214,6 +218,7 @@ program
options.port = port;
options.token = config.token;
options.token_free = config.token_free;
options.server = config.server;
options.clientId = config.clientId;
options.apiKey = config.apiKey;
@ -225,6 +230,7 @@ program
.command("config")
.addArgument(
new Argument("<type>", "config type").choices([
"access",
"token",
"server",
"client",
@ -233,7 +239,7 @@ program
)
.argument("<value>", "config value")
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
.action((type, value, options) => {
.action(async (type, value, options) => {
if (!type) {
console.error("type config is required!");
return;
@ -254,6 +260,10 @@ program
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
}
if (!config.server) {
config.server = SERVER_DEFAULT_URL;
}
if (type === "token" || type === "jwt") {
config.token = value;
} else if (type === "server") {
@ -267,8 +277,27 @@ program
console.log(`client: ${config.clientId} was set to config!`);
} else if (type === "apiKey" || type === "key") {
config.apiKey = value;
} else {
config[key] = value;
} else if (type === "access") {
config.access = (value && value.toUpperCase().trim()) || "FREE";
// FREE
if (config.access === "FREE") {
await sdk
.getTokenFree(config.server)
.then((resp) => {
console.log("data = ", resp.data);
if (resp.data?.token) {
config.token_free = resp.data?.token;
} else {
console.log("free token return with null or empty from server!");
return;
}
})
.catch((err) => {
console.log("cannot get free token from server!", err);
return;
});
}
}
if (!config.clientId && config.apiKey) {

144
package-lock.json generated
View File

@ -1,14 +1,15 @@
{
"name": "@cubetiq/http-tunnel-client",
"version": "1.0.0",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@cubetiq/http-tunnel-client",
"version": "1.0.0",
"version": "0.0.1",
"license": "ISC",
"dependencies": {
"axios": "^0.27.2",
"commander": "^9.3.0",
"https-proxy-agent": "^5.0.1",
"socket.io-client": "^4.5.1"
@ -33,6 +34,31 @@
"node": ">= 6.0.0"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "0.27.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
"dependencies": {
"follow-redirects": "^1.14.9",
"form-data": "^4.0.0"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/commander": {
"version": "9.3.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-9.3.0.tgz",
@ -57,6 +83,14 @@
}
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/engine.io-client": {
"version": "6.2.2",
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.2.tgz",
@ -77,6 +111,38 @@
"node": ">=10.0.0"
}
},
"node_modules/follow-redirects": {
"version": "1.15.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz",
"integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/https-proxy-agent": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz",
@ -89,6 +155,25 @@
"node": ">= 6"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@ -163,6 +248,28 @@
"debug": "4"
}
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"axios": {
"version": "0.27.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
"requires": {
"follow-redirects": "^1.14.9",
"form-data": "^4.0.0"
}
},
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"requires": {
"delayed-stream": "~1.0.0"
}
},
"commander": {
"version": "9.3.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-9.3.0.tgz",
@ -176,6 +283,11 @@
"ms": "2.1.2"
}
},
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ=="
},
"engine.io-client": {
"version": "6.2.2",
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.2.tgz",
@ -193,6 +305,21 @@
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz",
"integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg=="
},
"follow-redirects": {
"version": "1.15.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz",
"integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA=="
},
"form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
},
"https-proxy-agent": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz",
@ -202,6 +329,19 @@
"debug": "4"
}
},
"mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg=="
},
"mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"requires": {
"mime-db": "1.52.0"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",

View File

@ -20,6 +20,7 @@
"author": "Sambo Chea <sombochea@cubetiqs.com>",
"license": "ISC",
"dependencies": {
"axios": "^0.27.2",
"commander": "^9.3.0",
"https-proxy-agent": "^5.0.1",
"socket.io-client": "^4.5.1"

19
sdk.js Normal file
View File

@ -0,0 +1,19 @@
const axios = require("axios").default;
const getTokenFree = async (baseUrl, data = {}) => {
const url = `${baseUrl}/__free__/get_token`;
return axios({
method: "POST",
url: url,
data: {
...data,
},
headers: {
"x-access-type": "FREE",
},
});
};
module.exports = {
getTokenFree,
};