Add support for proxy with hlt client
This commit is contained in:
parent
0f115da19b
commit
87339a97d5
@ -1,8 +1,9 @@
|
||||
### 11/07/2023
|
||||
### 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
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.6",
|
||||
"version": "0.1.7",
|
||||
"description": "A lightweight http tunnel client using nodejs and socket.io client",
|
||||
"main": "dist/cli.js",
|
||||
"bin": {
|
||||
|
19
src/api.ts
19
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,7 +281,7 @@ class HttpTunnelClient implements Stoppable {
|
||||
this.keepAlive();
|
||||
};
|
||||
|
||||
public start = async (clientOptions: ClientOptions): Promise<Stoppable | undefined> => {
|
||||
public start = async (clientOptions: ClientOptions): Promise<Client | undefined> => {
|
||||
const { port, options = {} } = clientOptions;
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
@ -289,7 +292,7 @@ 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}`);
|
||||
// console.log(`config file: ${configFilePath}`);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
@ -329,7 +332,7 @@ class HttpTunnelClient implements Stoppable {
|
||||
}
|
||||
|
||||
await this.initStartClient(options);
|
||||
console.log("client started!");
|
||||
// console.log("client started!");
|
||||
|
||||
return this;
|
||||
};
|
||||
@ -341,9 +344,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();
|
||||
|
18
src/cli.ts
18
src/cli.ts
@ -266,7 +266,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 +278,8 @@ program
|
||||
proxyPort: port,
|
||||
});
|
||||
|
||||
onConnectProxy(port, options);
|
||||
|
||||
proxy.on("error", (err) => {
|
||||
console.error("Proxy server error:", err);
|
||||
});
|
||||
@ -284,12 +287,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 +307,14 @@ 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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
program.parse();
|
||||
|
@ -8,6 +8,9 @@ async function main() {
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
console.log('Client started:', client?.getEndpoint());
|
||||
|
||||
setTimeout(async () => {
|
||||
client?.stop();
|
||||
}, 5000);
|
||||
|
Loading…
Reference in New Issue
Block a user