Add support for host and port within address

This commit is contained in:
Sambo Chea 2023-07-11 18:57:49 +07:00
parent 87339a97d5
commit 775c5da038
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
6 changed files with 74 additions and 14 deletions

View File

@ -1,3 +1,19 @@
### 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

View File

@ -1,6 +1,6 @@
{
"name": "@cubetiq/hlt",
"version": "0.1.7",
"version": "0.1.8",
"description": "A lightweight http tunnel client using nodejs and socket.io client",
"main": "dist/cli.js",
"bin": {

View File

@ -281,8 +281,49 @@ class HttpTunnelClient implements Client {
this.keepAlive();
};
public start = async (clientOptions: ClientOptions): Promise<Client | 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 (address) {
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;
}
}
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
if (!fs.existsSync(configDir)) {
@ -292,7 +333,6 @@ class HttpTunnelClient implements Client {
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"));
@ -316,7 +356,7 @@ class HttpTunnelClient implements Client {
return;
}
options.port = port;
// options.port = port;
options.token = config.token;
options.access = config.access;
options.server = config.server;
@ -332,8 +372,6 @@ class HttpTunnelClient implements Client {
}
await this.initStartClient(options);
// console.log("client started!");
return this;
};

View File

@ -44,10 +44,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 +69,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,
})
});

View File

@ -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;
}

View File

@ -2,18 +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) => {