Compare commits
18 Commits
98929a1e0f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
d5f4abeeb0
|
|||
|
|
7d17ee38a4 | ||
|
|
d500dac31c | ||
|
|
fbda263da2 | ||
|
|
3d94eae00b | ||
|
56c278d84e
|
|||
|
20932e3b12
|
|||
|
62db7f8145
|
|||
|
|
10ce27ecac | ||
|
|
43c383c84b | ||
|
|
d5724f5c46 | ||
|
|
0a48c548db | ||
|
|
07b75a0298 | ||
|
|
d60c6fe57f | ||
|
775c5da038
|
|||
|
87339a97d5
|
|||
|
0f115da19b
|
|||
|
656c8e0ef7
|
4
.github/workflows/node.js.yml
vendored
4
.github/workflows/node.js.yml
vendored
@@ -16,7 +16,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x, 14.x, 16.x]
|
||||
node-version: [14.x, 16.x, 18.x, 20.x]
|
||||
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
||||
|
||||
steps:
|
||||
@@ -26,5 +26,5 @@ jobs:
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: 'npm'
|
||||
- run: npm ci
|
||||
- run: npm i && npm ci
|
||||
- run: npm run build --if-present
|
||||
|
||||
27
CHANGELOG.md
27
CHANGELOG.md
@@ -1,8 +1,33 @@
|
||||
### 11/07/2023
|
||||
### 27/07/2023 (1.0.10)
|
||||
|
||||
- Add list profiles
|
||||
|
||||
### 14/07/2023 (1.0.9)
|
||||
|
||||
- Fixed the start with port unable to start
|
||||
|
||||
### 11/07/2023 (1.0.8)
|
||||
|
||||
- Support tunnel with host with port
|
||||
|
||||
* Prev
|
||||
|
||||
```
|
||||
hlt start 8080 -h 192.168.1.1
|
||||
```
|
||||
|
||||
- New
|
||||
|
||||
```
|
||||
hlt start 192.168.1.1:8080
|
||||
```
|
||||
|
||||
### 11/07/2023 (1.0.7)
|
||||
|
||||
- Support Client API
|
||||
- Support Proxy (HTTP/HTTPS) and TCP
|
||||
- Improvements and Bugs fixed
|
||||
- Support auto proxy with hlt client
|
||||
|
||||
### 30/11/2022
|
||||
|
||||
|
||||
81
package-lock.json
generated
81
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.6",
|
||||
"version": "0.1.9",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.6",
|
||||
"version": "0.1.9",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^1.2.0",
|
||||
@@ -140,26 +140,27 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@types/express": {
|
||||
"version": "4.17.15",
|
||||
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.15.tgz",
|
||||
"integrity": "sha512-Yv0k4bXGOH+8a+7bELd2PqHQsuiANB+A8a4gnQrkRWzrkKlb6KHaVvyXhqs04sVW/OWlbPyYxRgYlIXLfrufMQ==",
|
||||
"version": "4.17.17",
|
||||
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz",
|
||||
"integrity": "sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==",
|
||||
"devOptional": true,
|
||||
"dependencies": {
|
||||
"@types/body-parser": "*",
|
||||
"@types/express-serve-static-core": "^4.17.31",
|
||||
"@types/express-serve-static-core": "^4.17.33",
|
||||
"@types/qs": "*",
|
||||
"@types/serve-static": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/express-serve-static-core": {
|
||||
"version": "4.17.32",
|
||||
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.32.tgz",
|
||||
"integrity": "sha512-aI5h/VOkxOF2Z1saPy0Zsxs5avets/iaiAJYznQFm5By/pamU31xWKL//epiF4OfUA2qTOc9PV6tCUjhO8wlZA==",
|
||||
"version": "4.17.35",
|
||||
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.35.tgz",
|
||||
"integrity": "sha512-wALWQwrgiB2AWTT91CB62b6Yt0sNHpznUXeZEcnPU3DRdlDIz74x8Qg1UUYKSVFi+va5vKOLYRBI1bRKiLLKIg==",
|
||||
"devOptional": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*",
|
||||
"@types/qs": "*",
|
||||
"@types/range-parser": "*"
|
||||
"@types/range-parser": "*",
|
||||
"@types/send": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/http-proxy": {
|
||||
@@ -177,9 +178,9 @@
|
||||
"devOptional": true
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "20.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.4.1.tgz",
|
||||
"integrity": "sha512-JIzsAvJeA/5iY6Y/OxZbv1lUcc8dNSE77lb2gnBH+/PJ3lFR1Ccvgwl5JWnHAkNHcRsT0TbpVOsiMKZ1F/yyJg=="
|
||||
"version": "20.4.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.4.5.tgz",
|
||||
"integrity": "sha512-rt40Nk13II9JwQBdeYqmbn2Q6IVTA5uPhvSO+JVqdXw/6/4glI6oR9ezty/A9Hg5u7JH4OmYmuQ+XvjKm0Datg=="
|
||||
},
|
||||
"node_modules/@types/qs": {
|
||||
"version": "6.9.7",
|
||||
@@ -193,6 +194,22 @@
|
||||
"integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==",
|
||||
"devOptional": true
|
||||
},
|
||||
"node_modules/@types/send": {
|
||||
"version": "0.17.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz",
|
||||
"integrity": "sha512-Cwo8LE/0rnvX7kIIa3QHCkcuF21c05Ayb0ZfxPiv0W8VRiZiNW/WuRupHKpqqGVGf7SUA44QSOUKaEd9lIrd/Q==",
|
||||
"devOptional": true,
|
||||
"dependencies": {
|
||||
"@types/mime": "^1",
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/send/node_modules/@types/mime": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
|
||||
"integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==",
|
||||
"devOptional": true
|
||||
},
|
||||
"node_modules/@types/serve-static": {
|
||||
"version": "1.15.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.0.tgz",
|
||||
@@ -313,9 +330,9 @@
|
||||
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.2.2.tgz",
|
||||
"integrity": "sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q==",
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz",
|
||||
"integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.15.0",
|
||||
"form-data": "^4.0.0",
|
||||
@@ -626,21 +643,21 @@
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io-client": {
|
||||
"version": "6.2.3",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.3.tgz",
|
||||
"integrity": "sha512-aXPtgF1JS3RuuKcpSrBtimSjYvrbhKW9froICH4s0F3XQWLxsKNxqzG39nnvQZQnva4CMvUK63T7shevxRyYHw==",
|
||||
"version": "6.5.1",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.5.1.tgz",
|
||||
"integrity": "sha512-hE5wKXH8Ru4L19MbM1GgYV/2Qo54JSMh1rlJbfpa40bEWkCKNo3ol2eOtGmowcr+ysgbI7+SGL+by42Q3pt/Ng==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"ws": "~8.2.3",
|
||||
"engine.io-parser": "~5.1.0",
|
||||
"ws": "~8.11.0",
|
||||
"xmlhttprequest-ssl": "~2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io-parser": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz",
|
||||
"integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg==",
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.1.0.tgz",
|
||||
"integrity": "sha512-enySgNiK5tyZFynt3z7iqBR+Bto9EVVVvDFuTT0ioHCGbzirZVGDGiQjZzEp8hWl6hd5FSVytJGuScX1C1C35w==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
@@ -1564,14 +1581,14 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-client": {
|
||||
"version": "4.5.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.4.tgz",
|
||||
"integrity": "sha512-ZpKteoA06RzkD32IbqILZ+Cnst4xewU7ZYK12aS1mzHftFFjpoMz69IuhP/nL25pJfao/amoPI527KnuhFm01g==",
|
||||
"version": "4.7.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.7.1.tgz",
|
||||
"integrity": "sha512-Qk3Xj8ekbnzKu3faejo4wk2MzXA029XppiXtTF/PkbTg+fcwaTw1PlDrTrrrU4mKoYC4dvlApOnSeyLCKwek2w==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.2.3",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
"engine.io-client": "~6.5.1",
|
||||
"socket.io-parser": "~4.2.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
@@ -2081,9 +2098,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.2.3",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz",
|
||||
"integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==",
|
||||
"version": "8.11.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz",
|
||||
"integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.6",
|
||||
"version": "0.1.10",
|
||||
"description": "A lightweight http tunnel client using nodejs and socket.io client",
|
||||
"main": "dist/cli.js",
|
||||
"bin": {
|
||||
|
||||
64
src/api.ts
64
src/api.ts
@@ -11,15 +11,17 @@ import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from ".
|
||||
import { ClientOptions, Options } from "./interface";
|
||||
import { getTokenFree } from './sdk';
|
||||
|
||||
interface Stoppable {
|
||||
interface Client {
|
||||
getEndpoint(): string | null;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
class HttpTunnelClient implements Stoppable {
|
||||
class HttpTunnelClient implements Client {
|
||||
// create socket instance
|
||||
private socket: Socket | null = null;
|
||||
private keepAliveTimer: NodeJS.Timeout | null = null;
|
||||
private keepAliveTimeout: number | null = null;
|
||||
private endpoint: string | null = null;
|
||||
|
||||
private keepAlive() {
|
||||
if (!this.socket) {
|
||||
@@ -119,6 +121,7 @@ class HttpTunnelClient implements Stoppable {
|
||||
.toLowerCase()
|
||||
.trim();
|
||||
const serverUrl = addPrefixOnHttpSchema(options.server || SERVER_DEFAULT_URL, clientEndpoint);
|
||||
this.endpoint = serverUrl
|
||||
|
||||
// extra options for socket to identify the client (authentication and options of tunnel)
|
||||
const defaultParams = {
|
||||
@@ -278,8 +281,50 @@ class HttpTunnelClient implements Stoppable {
|
||||
this.keepAlive();
|
||||
};
|
||||
|
||||
public start = async (clientOptions: ClientOptions): Promise<Stoppable | undefined> => {
|
||||
const { port, options = {} } = clientOptions;
|
||||
public start = async (clientOptions: Partial<ClientOptions>): Promise<Client | undefined> => {
|
||||
const { port, address, options = {} } = clientOptions;
|
||||
|
||||
// Load host and port check
|
||||
if (!port) {
|
||||
if (!address) {
|
||||
console.error("port or address is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const [host, portStr] = address.split(":");
|
||||
if (!host || !portStr) {
|
||||
console.error("invalid address!");
|
||||
return;
|
||||
}
|
||||
|
||||
options.host = host;
|
||||
try {
|
||||
options.port = parseInt(portStr);
|
||||
} catch (e) {
|
||||
console.error("invalid port!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (typeof address !== "number" && address && address.includes(":")) {
|
||||
const [host, portStr] = address.split(":");
|
||||
if (host) {
|
||||
options.host = host;
|
||||
}
|
||||
|
||||
if (portStr) {
|
||||
try {
|
||||
options.port = parseInt(portStr);
|
||||
console.log(`default port: ${port} will be ignored and override by port: ${options.port}`);
|
||||
} catch (e) {
|
||||
options.port = port;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
options.port = port;
|
||||
console.log(`default port: ${port} will be forwared`);
|
||||
}
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
@@ -289,7 +334,6 @@ class HttpTunnelClient implements Stoppable {
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile || PROFILE_DEFAULT}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
console.log(`config file: ${configFilePath}`);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
@@ -313,7 +357,7 @@ class HttpTunnelClient implements Stoppable {
|
||||
return;
|
||||
}
|
||||
|
||||
options.port = port;
|
||||
// options.port = port;
|
||||
options.token = config.token;
|
||||
options.access = config.access;
|
||||
options.server = config.server;
|
||||
@@ -329,8 +373,6 @@ class HttpTunnelClient implements Stoppable {
|
||||
}
|
||||
|
||||
await this.initStartClient(options);
|
||||
console.log("client started!");
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
@@ -341,9 +383,13 @@ class HttpTunnelClient implements Stoppable {
|
||||
this.socket = null;
|
||||
this.keepAliveTimer && clearInterval(this.keepAliveTimer);
|
||||
|
||||
console.log("client stopped!");
|
||||
console.log("client stopped from server:", this.endpoint);
|
||||
}
|
||||
};
|
||||
|
||||
public getEndpoint = () => {
|
||||
return this.endpoint;
|
||||
}
|
||||
}
|
||||
|
||||
export const client = new HttpTunnelClient();
|
||||
|
||||
63
src/cli.ts
63
src/cli.ts
@@ -4,6 +4,7 @@ import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { initConfigFileClient, startClient } from "./api";
|
||||
import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant";
|
||||
import { listProfile } from "./manage";
|
||||
import { createProxyServer } from "./proxy";
|
||||
import { createProxyServer as createProxyTCPServer } from "./proxy_tcp";
|
||||
import { getTokenFree } from './sdk';
|
||||
@@ -22,20 +23,20 @@ program
|
||||
program
|
||||
.command("init")
|
||||
.description("generate a new client and token with free access")
|
||||
.option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL)
|
||||
.option("-s, --server <string>", "setting server url", SERVER_DEFAULT_URL)
|
||||
.option(
|
||||
"-t --token <string>",
|
||||
"-t, --token <string>",
|
||||
"setting token (default generate FREE access token)",
|
||||
""
|
||||
)
|
||||
.option("-a --access <string>", "setting token access type", TOKEN_FREE)
|
||||
.option("-c --client <string>", "setting client (auto generate uuid)")
|
||||
.option("-a, --access <string>", "setting token access type", TOKEN_FREE)
|
||||
.option("-c, --client <string>", "setting client (auto generate uuid)")
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"-k, --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.option("-f --force", "force to generate new client and token", false)
|
||||
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.option("-f, --force", "force to generate new client and token", false)
|
||||
.action(async (options) => {
|
||||
initConfigFileClient(options);
|
||||
});
|
||||
@@ -44,10 +45,14 @@ program
|
||||
program
|
||||
.command("start")
|
||||
.description("start a connection with specific port")
|
||||
.argument("<port>", "local server port number", (value) => {
|
||||
.argument("<port> | <address>", "local server port number or address", (value) => {
|
||||
if (isValidHost(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const port = parseInt(value, 10);
|
||||
if (isNaN(port)) {
|
||||
throw new InvalidArgumentError("Not a number.");
|
||||
throw new InvalidArgumentError("Not a number or valid address.");
|
||||
}
|
||||
return port;
|
||||
})
|
||||
@@ -65,9 +70,10 @@ program
|
||||
.option("-p, --profile <string>", "profile name", PROFILE_DEFAULT)
|
||||
.option("-h, --host <string>", "local host value", "localhost")
|
||||
.option("-o, --origin <string>", "change request origin")
|
||||
.action((port, options) => {
|
||||
.action((portOrAddress, options) => {
|
||||
startClient({
|
||||
port,
|
||||
port: portOrAddress,
|
||||
address: portOrAddress,
|
||||
options,
|
||||
})
|
||||
});
|
||||
@@ -86,7 +92,7 @@ program
|
||||
])
|
||||
)
|
||||
.argument("<value>", "config value")
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, value, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
@@ -174,7 +180,7 @@ program
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
@@ -266,7 +272,8 @@ program
|
||||
|
||||
throw new InvalidArgumentError("Target is not a url or host with port.");
|
||||
})
|
||||
.action((port, target) => {
|
||||
.option("-p, --profile <string>", "setting profile name for connect with hlt server (proxy with current local port)")
|
||||
.action((port, target, options) => {
|
||||
const isTcp = target.indexOf("tcp") === 0;
|
||||
if (isTcp) {
|
||||
console.log("[TCP] Start proxy server with port:", port, "and target:", target);
|
||||
@@ -277,6 +284,8 @@ program
|
||||
proxyPort: port,
|
||||
});
|
||||
|
||||
onConnectProxy(port, options);
|
||||
|
||||
proxy.on("error", (err) => {
|
||||
console.error("Proxy server error:", err);
|
||||
});
|
||||
@@ -284,12 +293,15 @@ program
|
||||
proxy.on("close", () => {
|
||||
console.log("Proxy server closed");
|
||||
});
|
||||
|
||||
} else {
|
||||
console.log("[HTTP/HTTPS] Start proxy server with port:", port, "and target:", target);
|
||||
const proxy = createProxyServer(target, {
|
||||
proxyPort: port,
|
||||
});
|
||||
|
||||
onConnectProxy(port, options);
|
||||
|
||||
proxy.on("error", (err) => {
|
||||
console.error("Proxy server error:", err);
|
||||
});
|
||||
@@ -301,4 +313,27 @@ program
|
||||
});
|
||||
|
||||
|
||||
const onConnectProxy = (port: number, options: any) => {
|
||||
if (options?.profile) {
|
||||
console.log(`Start proxy: ${port} via hlt client with profile: ${options.profile}`);
|
||||
startClient({
|
||||
port,
|
||||
options,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// profile
|
||||
program
|
||||
.command("profile")
|
||||
.description("manage profile")
|
||||
.option("-l, --list", "list all profiles", false)
|
||||
.action((options) => {
|
||||
if (options.list) {
|
||||
listProfile();
|
||||
} else {
|
||||
console.log("profile command is required");
|
||||
}
|
||||
});
|
||||
|
||||
program.parse();
|
||||
|
||||
@@ -17,5 +17,6 @@ export interface Options {
|
||||
|
||||
export interface ClientOptions {
|
||||
port: number;
|
||||
address?: string; // e.g. localhost:8081 (take if port is not set)
|
||||
options?: Options;
|
||||
}
|
||||
28
src/manage.ts
Normal file
28
src/manage.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { PROFILE_DEFAULT, PROFILE_PATH } from "./constant";
|
||||
|
||||
export const listProfile = () => {
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
if (!fs.existsSync(configDir)) {
|
||||
console.log(`config file ${configDir} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
const configFiles = fs.readdirSync(configDir);
|
||||
if (configFiles.length === 0) {
|
||||
console.log(`config file ${configDir} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("List of profile:");
|
||||
configFiles.forEach((file) => {
|
||||
const configFilePath = path.resolve(configDir, file);
|
||||
const config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
const name = file.replace(".json", "");
|
||||
console.log(`- ${name} (${config.clientId})`);
|
||||
});
|
||||
|
||||
console.log(`\nCurrent profile: ${PROFILE_DEFAULT}`);
|
||||
}
|
||||
@@ -2,15 +2,18 @@ import { startClient } from '../src/api';
|
||||
|
||||
async function main() {
|
||||
const client = await startClient({
|
||||
port: 3000,
|
||||
// port: 8081,
|
||||
address: '172.17.0.2:8222',
|
||||
options: {
|
||||
profile: 'mytest',
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Client started:', client?.getEndpoint());
|
||||
|
||||
setTimeout(async () => {
|
||||
client?.stop();
|
||||
}, 5000);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
|
||||
Reference in New Issue
Block a user