Compare commits

..

22 Commits

Author SHA1 Message Date
d5f4abeeb0 Add list profiles 2023-07-27 09:13:44 +07:00
Sambo Chea
7d17ee38a4 Merge pull request #146 from CUBETIQ/dependabot/npm_and_yarn/types/node-20.4.5
Bump @types/node from 20.4.4 to 20.4.5
2023-07-26 08:33:01 +07:00
dependabot[bot]
d500dac31c Bump @types/node from 20.4.4 to 20.4.5
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 20.4.4 to 20.4.5.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-25 20:53:00 +00:00
Sambo Chea
fbda263da2 Merge pull request #145 from CUBETIQ/dependabot/npm_and_yarn/types/node-20.4.4
Bump @types/node from 20.4.1 to 20.4.4
2023-07-25 21:25:46 +07:00
dependabot[bot]
3d94eae00b Bump @types/node from 20.4.1 to 20.4.4
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 20.4.1 to 20.4.4.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-24 20:49:54 +00:00
56c278d84e Updated 2023-07-14 08:32:27 +07:00
20932e3b12 Merge branch 'main' of https://github.com/CUBETIQ/http-tunnel-client 2023-07-14 08:31:29 +07:00
62db7f8145 Fixed the start port 2023-07-14 08:31:10 +07:00
Sambo Chea
10ce27ecac Merge pull request #141 from CUBETIQ/dependabot/npm_and_yarn/types/express-4.17.17
Bump @types/express from 4.17.15 to 4.17.17
2023-07-12 18:20:29 +07:00
Sambo Chea
43c383c84b Merge pull request #142 from CUBETIQ/dependabot/npm_and_yarn/axios-1.4.0
Bump axios from 1.2.2 to 1.4.0
2023-07-12 18:20:21 +07:00
Sambo Chea
d5724f5c46 Merge pull request #143 from CUBETIQ/dependabot/npm_and_yarn/socket.io-client-4.7.1
Bump socket.io-client from 4.5.4 to 4.7.1
2023-07-12 18:20:14 +07:00
dependabot[bot]
0a48c548db Bump socket.io-client from 4.5.4 to 4.7.1
Bumps [socket.io-client](https://github.com/socketio/socket.io-client) from 4.5.4 to 4.7.1.
- [Release notes](https://github.com/socketio/socket.io-client/releases)
- [Changelog](https://github.com/socketio/socket.io-client/blob/main/CHANGELOG.md)
- [Commits](https://github.com/socketio/socket.io-client/compare/4.5.4...4.7.1)

---
updated-dependencies:
- dependency-name: socket.io-client
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-11 20:53:57 +00:00
dependabot[bot]
07b75a0298 Bump axios from 1.2.2 to 1.4.0
Bumps [axios](https://github.com/axios/axios) from 1.2.2 to 1.4.0.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md)
- [Commits](https://github.com/axios/axios/compare/1.2.2...v1.4.0)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-11 20:53:49 +00:00
dependabot[bot]
d60c6fe57f Bump @types/express from 4.17.15 to 4.17.17
Bumps [@types/express](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/express) from 4.17.15 to 4.17.17.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/express)

---
updated-dependencies:
- dependency-name: "@types/express"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-11 20:53:43 +00:00
775c5da038 Add support for host and port within address 2023-07-11 18:57:49 +07:00
87339a97d5 Add support for proxy with hlt client 2023-07-11 15:04:48 +07:00
0f115da19b Removed 12 and add 18 and 20 2023-07-11 12:43:51 +07:00
656c8e0ef7 Fixed npm install before ci 2023-07-11 12:42:27 +07:00
98929a1e0f Updated 2023-07-11 12:27:49 +07:00
967d06ed1e Fixed import axios 2023-07-11 12:26:38 +07:00
a30c05e942 Updated the package 2023-07-11 12:24:20 +07:00
92312f465f Updated packages 2023-07-11 12:23:23 +07:00
10 changed files with 2319 additions and 33 deletions

View File

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

View File

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

2147
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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": {
@@ -8,9 +8,9 @@
},
"scripts": {
"start": "ts-node-dev --respawn --transpile-only src/cli.ts",
"test": "ts-node-dev --respawn --transpile-only src/test.ts",
"test": "ts-node-dev --respawn --transpile-only test/test.ts",
"local": "ts-node-dev --respawn --transpile-only src/cli.ts start -p local",
"build": "tsc"
"build": "rimraf dist && tsc"
},
"repository": {
"type": "git",
@@ -24,9 +24,9 @@
"license": "ISC",
"dependencies": {
"axios": "^1.2.0",
"commander": "^11.0.0",
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6",
"commander": "^11.0.0",
"https-proxy-agent": "^7.0.0",
"socket.io-client": "^4.5.1"
},
@@ -36,6 +36,7 @@
"devDependencies": {
"@types/express": "^4.17.15",
"@types/node": "^20.0.0",
"rimraf": "^5.0.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.0"
}

View File

@@ -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();

View File

@@ -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();

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

28
src/manage.ts Normal file
View 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}`);
}

View File

@@ -1,4 +1,4 @@
const axios = require("axios");
import axios from "axios";
const getTokenFree = async (baseUrl: string, data: any = {}) => {
const url = `${baseUrl}/__free__/api/get_token`;

View File

@@ -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) => {