Compare commits
No commits in common. "main" and "ts" have entirely different histories.
30
.github/workflows/node.js.yml
vendored
30
.github/workflows/node.js.yml
vendored
@ -1,30 +0,0 @@
|
||||
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
||||
|
||||
name: Node.js CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [14.x, 16.x, 18.x, 20.x]
|
||||
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: 'npm'
|
||||
- run: npm i && npm ci
|
||||
- run: npm run build --if-present
|
@ -1,3 +1,2 @@
|
||||
src/
|
||||
.github/
|
||||
test/
|
52
CHANGELOG.md
52
CHANGELOG.md
@ -1,52 +0,0 @@
|
||||
### 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
|
||||
|
||||
- Fixed response data in axios request with accept-encoding to identity
|
||||
- Allow to force change server url for init
|
||||
|
||||
### 11/11/2022
|
||||
|
||||
- Fixed axios is not a function
|
||||
- Add generate token with data (clientId, apiKey) to server
|
||||
- Add generate token with options (-f | --force) to regenerate and override
|
||||
|
||||
### 20/10/2022
|
||||
|
||||
- Upgraded packages and fixed some bugs
|
||||
|
||||
### Initialized
|
||||
|
||||
- HTTP tunnel between server and client via https link
|
||||
- Custom profile (using `-p myprofile`)
|
||||
- Suffix url (using `-s`)
|
||||
- Config (get/set token,access,server,client,key configs)
|
48
README.md
48
README.md
@ -9,71 +9,59 @@ npm i -g @cubetiq/hlt
|
||||
|
||||
OR
|
||||
|
||||
npx -y @cubetiq/hlt
|
||||
npx @cubetiq/hlt
|
||||
|
||||
```
|
||||
|
||||
### Usages
|
||||
### Usage
|
||||
|
||||
- If installed to global (bin), please using cli
|
||||
|
||||
```shell
|
||||
hlt [command] [options]
|
||||
```
|
||||
|
||||
- Initialize Client and Start (Quick)
|
||||
- Initialize Client and Start (Quick)
|
||||
|
||||
```shell
|
||||
# Initialize a client and token for connect (default's profile)
|
||||
npx -y @cubetiq/hlt init
|
||||
npx @cubetiq/hlt init
|
||||
|
||||
# Start port 3000 to remote server
|
||||
npx -y @cubetiq/hlt start 3000
|
||||
npx @cubetiq/hlt start 3000
|
||||
```
|
||||
|
||||
# Start port 3000 with suffix to remote server
|
||||
|
||||
npx -y @cubetiq/hlt start 3000 -s mytest
|
||||
|
||||
````
|
||||
|
||||
- Initialize Client and Start (Quick with custom's profile)
|
||||
|
||||
```shell
|
||||
# Initialize a client and token for connect (mytest's profile)
|
||||
npx -y @cubetiq/hlt init -p mytest
|
||||
npx @cubetiq/hlt init -p mytest
|
||||
|
||||
# Start port 3000 to remote server (mytest's profile)
|
||||
npx -y @cubetiq/hlt start 3000 -p mytest
|
||||
````
|
||||
npx @cubetiq/hlt start 3000 -p mytest
|
||||
```
|
||||
|
||||
### Custom Config
|
||||
|
||||
- Generate Client Key
|
||||
- Generate Client Key
|
||||
|
||||
```shell
|
||||
npx -y @cubetiq/hlt config client new
|
||||
npx @cubetiq/hlt config client new
|
||||
```
|
||||
|
||||
- Set Client Token (Required, contact to vendor)
|
||||
- Set Client Token (Required, contact to vendor)
|
||||
|
||||
```shell
|
||||
npx -y @cubetiq/hlt config token $TOKEN
|
||||
npx @cubetiq/hlt config token $TOKEN
|
||||
```
|
||||
|
||||
- Set Custom Server
|
||||
- Custom Server
|
||||
|
||||
```shell
|
||||
npx -y @cubetiq/hlt config server https://lt.ctdn.net
|
||||
npx @cubetiq/hlt config server https://lt.ctdn.net
|
||||
```
|
||||
|
||||
- Start Client
|
||||
- Start Client
|
||||
|
||||
```shell
|
||||
npx -y @cubetiq/hlt start $YOUR_PORT
|
||||
npx @cubetiq/hlt start $YOUR_PORT
|
||||
```
|
||||
|
||||
### Contributors
|
||||
|
||||
- Original [web-tunnel](https://github.com/web-tunnel/lite-http-tunnel-client)
|
||||
- Sambo Chea <sombochea@cubetiqs.com>
|
||||
- Original [web-tunnel](https://github.com/web-tunnel/lite-http-tunnel-client)
|
||||
- Sambo Chea <sombochea@cubetiqs.com>
|
||||
|
2
bin/hlt
2
bin/hlt
@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('../dist/cli');
|
||||
require('../dist/client');
|
2080
package-lock.json
generated
2080
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
24
package.json
24
package.json
@ -1,16 +1,14 @@
|
||||
{
|
||||
"name": "@cubetiq/hlt",
|
||||
"version": "0.1.10",
|
||||
"version": "0.1.0",
|
||||
"description": "A lightweight http tunnel client using nodejs and socket.io client",
|
||||
"main": "dist/cli.js",
|
||||
"main": "dist/client.js",
|
||||
"bin": {
|
||||
"hlt": "bin/hlt"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "ts-node-dev --respawn --transpile-only src/cli.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": "rimraf dist && tsc"
|
||||
"start": "ts-node-dev --respawn --transpile-only src/client.ts",
|
||||
"build": "tsc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -23,21 +21,17 @@
|
||||
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^1.2.0",
|
||||
"commander": "^11.0.0",
|
||||
"express": "^4.18.2",
|
||||
"http-proxy-middleware": "^2.0.6",
|
||||
"https-proxy-agent": "^7.0.0",
|
||||
"axios": "^0.27.2",
|
||||
"commander": "^9.3.0",
|
||||
"https-proxy-agent": "^5.0.1",
|
||||
"socket.io-client": "^4.5.1"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.15",
|
||||
"@types/node": "^20.0.0",
|
||||
"rimraf": "^5.0.1",
|
||||
"@types/node": "^18.0.3",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^5.0.0"
|
||||
"typescript": "^4.7.4"
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"extends": [
|
||||
"config:base"
|
||||
]
|
||||
}
|
399
src/api.ts
399
src/api.ts
@ -1,399 +0,0 @@
|
||||
import * as fs from "fs";
|
||||
import * as http from "http";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { Socket, io } from "socket.io-client";
|
||||
import { TunnelRequest, TunnelResponse } from "./lib";
|
||||
import { addPrefixOnHttpSchema, generateUUID } from "./util";
|
||||
|
||||
import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant";
|
||||
import { ClientOptions, Options } from "./interface";
|
||||
import { getTokenFree } from './sdk';
|
||||
|
||||
interface Client {
|
||||
getEndpoint(): string | null;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.keepAliveTimer = setTimeout(() => {
|
||||
if (this.socket && this.socket.connected) {
|
||||
this.socket.send("ping");
|
||||
}
|
||||
this.keepAlive();
|
||||
}, this.keepAliveTimeout || 5000);
|
||||
}
|
||||
|
||||
// Init the Client for config file
|
||||
public initConfigFile = async (options: any) => {
|
||||
const profile = options.profile || PROFILE_DEFAULT;
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
// Force reset config server from client init
|
||||
if (!config.server || options.force) {
|
||||
config.server = options.server || SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token && options.token) {
|
||||
config.token = options.token;
|
||||
}
|
||||
|
||||
if (!config.access) {
|
||||
config.access = options.access || TOKEN_FREE;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
config.clientId = options.client || generateUUID();
|
||||
}
|
||||
|
||||
if (!config.apiKey && options.key) {
|
||||
config.apiKey = options.key;
|
||||
}
|
||||
|
||||
let errorCode = 0;
|
||||
if (!config.token || options.force) {
|
||||
console.log(`Generating token from server: ${config.server}`);
|
||||
await getTokenFree(config.server, {
|
||||
timestamp: (new Date().getTime()),
|
||||
clientId: config.clientId,
|
||||
apiKey: config.apiKey,
|
||||
})
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
console.log("Token generated successfully!");
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
errorCode = 1;
|
||||
console.error("Generate free token failed, return with null or empty from server!", resp);
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
errorCode = 1;
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
if (errorCode === 0) {
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`initialized config saved successfully to: ${configFilePath}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Start Client
|
||||
public initStartClient = async (options: Options) => {
|
||||
// Please change this if your domain goes wrong here
|
||||
// Current style using sub-domain: https://{{clientId}}-tunnel.myhostingdomain.com
|
||||
// (Original server: https://tunnel.myhostingdomain.com)
|
||||
const profile = options.profile || PROFILE_DEFAULT;
|
||||
const clientId = `${options.apiKey || options.clientId || generateUUID()}`;
|
||||
const clientIdSub =
|
||||
profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`;
|
||||
const clientEndpoint = (
|
||||
options.suffix ? `${clientIdSub}${options.suffix}-` : clientIdSub
|
||||
)
|
||||
.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 = {
|
||||
apiKey: options.apiKey,
|
||||
clientId: options.clientId,
|
||||
profile: profile,
|
||||
clientIdSub: clientIdSub,
|
||||
clientEndpoint: clientEndpoint,
|
||||
serverUrl: serverUrl,
|
||||
access: options.access,
|
||||
keep_connection: options.keep_connection || true,
|
||||
};
|
||||
|
||||
// extra info for notify about the running of the tunnel (it's private info, other platfom cannot access this)
|
||||
// this using for internal only (don't worry about this)
|
||||
const osInfo = {
|
||||
hostname: os.hostname(),
|
||||
platform: os.platform(),
|
||||
arch: os.arch(),
|
||||
release: os.release(),
|
||||
timestamp: new Date().getTime(),
|
||||
};
|
||||
|
||||
const initParams: any = {
|
||||
path: "/$cubetiq_http_tunnel",
|
||||
transports: ["websocket"],
|
||||
auth: {
|
||||
token: options.token,
|
||||
...defaultParams,
|
||||
},
|
||||
headers: {
|
||||
...defaultParams,
|
||||
os: osInfo,
|
||||
},
|
||||
// reconnection: true,
|
||||
};
|
||||
|
||||
const http_proxy = process.env.https_proxy || process.env.http_proxy;
|
||||
if (http_proxy) {
|
||||
initParams.agent = new HttpsProxyAgent(http_proxy);
|
||||
}
|
||||
|
||||
// Connecting to socket server and agent here...
|
||||
console.log(`client connecting to server: ${serverUrl}`);
|
||||
this.socket = io(serverUrl, initParams);
|
||||
|
||||
const clientLogPrefix = `client: ${clientId} on profile: ${profile}`;
|
||||
this.socket.on("connect", () => {
|
||||
if (this.socket!.connected) {
|
||||
console.log(`${clientLogPrefix} is connected to server successfully!`);
|
||||
}
|
||||
});
|
||||
|
||||
this.socket.on("connect_error", (e) => {
|
||||
console.error(
|
||||
`${clientLogPrefix} connect error:`,
|
||||
(e && e.message) || "something wrong"
|
||||
);
|
||||
if (e && e.message && e.message.startsWith("[40")) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
this.socket.on("disconnect", (reason) => {
|
||||
console.warn(`${clientLogPrefix} disconnected: ${reason}!`);
|
||||
});
|
||||
|
||||
this.socket.on("disconnect_exit", (reason) => {
|
||||
console.warn(`${clientLogPrefix} disconnected and exited ${reason}!`);
|
||||
this.socket?.disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
this.socket.on("request", (requestId, request) => {
|
||||
const isWebSocket = request.headers.upgrade === "websocket";
|
||||
console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
|
||||
request.port = options.port;
|
||||
request.hostname = options.host;
|
||||
|
||||
if (options.origin) {
|
||||
request.headers.host = options.origin;
|
||||
}
|
||||
|
||||
const tunnelRequest = new TunnelRequest(this.socket!, requestId);
|
||||
|
||||
const localReq = http.request(request);
|
||||
tunnelRequest.pipe(localReq);
|
||||
|
||||
const onTunnelRequestError = (e: any) => {
|
||||
console.error("tunnel request error: ", e);
|
||||
tunnelRequest.off("end", onTunnelRequestEnd);
|
||||
localReq.destroy(e);
|
||||
};
|
||||
|
||||
const onTunnelRequestEnd = () => {
|
||||
tunnelRequest.off("error", onTunnelRequestError);
|
||||
};
|
||||
|
||||
tunnelRequest.once("error", onTunnelRequestError);
|
||||
tunnelRequest.once("end", onTunnelRequestEnd);
|
||||
|
||||
const onLocalResponse = (localRes: any) => {
|
||||
localReq.off("error", onLocalError);
|
||||
|
||||
if (isWebSocket && localRes.upgrade) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tunnelResponse = new TunnelResponse(this.socket!, requestId);
|
||||
|
||||
tunnelResponse.writeHead(
|
||||
localRes.statusCode,
|
||||
localRes.statusMessage,
|
||||
localRes.headers,
|
||||
localRes.httpVersion
|
||||
);
|
||||
|
||||
localRes.pipe(tunnelResponse);
|
||||
};
|
||||
|
||||
const onLocalError = (error: any) => {
|
||||
console.error("local error:", error);
|
||||
localReq.off("response", onLocalResponse);
|
||||
this.socket?.emit("request-error", requestId, error && error.message);
|
||||
tunnelRequest.destroy(error);
|
||||
};
|
||||
|
||||
const onUpgrade = (localRes: any, localSocket: any, localHead: any) => {
|
||||
// localSocket.once('error', onTunnelRequestError);
|
||||
if (localHead && localHead.length) localSocket.unshift(localHead);
|
||||
|
||||
const tunnelResponse = new TunnelResponse(this.socket!, requestId, true);
|
||||
tunnelResponse.writeHead(null, null, localRes.headers);
|
||||
localSocket.pipe(tunnelResponse).pipe(localSocket);
|
||||
};
|
||||
|
||||
localReq.once("error", onLocalError);
|
||||
localReq.once("response", onLocalResponse);
|
||||
|
||||
if (isWebSocket) {
|
||||
localReq.on("upgrade", onUpgrade);
|
||||
}
|
||||
});
|
||||
|
||||
// reconnect manually
|
||||
// const tryReconnect = () => {
|
||||
// setTimeout(() => {
|
||||
// socket!.io.open((err) => {
|
||||
// if (err) {
|
||||
// tryReconnect();
|
||||
// }
|
||||
// });
|
||||
// }, 2000);
|
||||
// };
|
||||
// socket.io.on("close", tryReconnect);
|
||||
|
||||
this.keepAlive();
|
||||
};
|
||||
|
||||
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)) {
|
||||
fs.mkdirSync(configDir);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile || PROFILE_DEFAULT}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token) {
|
||||
console.info(`please init or set token for ${config.server}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
if (!config.apiKey) {
|
||||
console.info(`please init or create a client for ${config.server}`);
|
||||
} else {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// options.port = port;
|
||||
options.token = config.token;
|
||||
options.access = config.access;
|
||||
options.server = config.server;
|
||||
options.clientId = config.clientId;
|
||||
options.apiKey = options.key || config.apiKey;
|
||||
|
||||
if (options.suffix === "port" || options.suffix === "true") {
|
||||
options.suffix = `${port}`;
|
||||
} else if (options.suffix === "false") {
|
||||
options.suffix = undefined;
|
||||
} else if (options.suffix === "gen" || options.suffix === "uuid") {
|
||||
options.suffix = generateUUID();
|
||||
}
|
||||
|
||||
await this.initStartClient(options);
|
||||
return this;
|
||||
};
|
||||
|
||||
public stop = () => {
|
||||
if (this.socket) {
|
||||
this.socket.disconnect();
|
||||
this.socket.close();
|
||||
this.socket = null;
|
||||
this.keepAliveTimer && clearInterval(this.keepAliveTimer);
|
||||
|
||||
console.log("client stopped from server:", this.endpoint);
|
||||
}
|
||||
};
|
||||
|
||||
public getEndpoint = () => {
|
||||
return this.endpoint;
|
||||
}
|
||||
}
|
||||
|
||||
export const client = new HttpTunnelClient();
|
||||
|
||||
export const initConfigFileClient = client.initConfigFile;
|
||||
export const startClient = client.start;
|
||||
export const stopClient = client.stop;
|
339
src/cli.ts
339
src/cli.ts
@ -1,339 +0,0 @@
|
||||
import { Argument, InvalidArgumentError, program } from "commander";
|
||||
import * as fs from "fs";
|
||||
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';
|
||||
import { generateUUID, isValidHost, isValidUrl } from "./util";
|
||||
|
||||
const packageInfo = require("../package.json");
|
||||
|
||||
program
|
||||
.name("hlt")
|
||||
.description(
|
||||
"CUBETIQ HTTP tunnel client with free access for local tunneling"
|
||||
)
|
||||
.version(`v${packageInfo.version}`);
|
||||
|
||||
// init
|
||||
program
|
||||
.command("init")
|
||||
.description("generate a new client and token with free access")
|
||||
.option("-s, --server <string>", "setting server url", SERVER_DEFAULT_URL)
|
||||
.option(
|
||||
"-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(
|
||||
"-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)
|
||||
.action(async (options) => {
|
||||
initConfigFileClient(options);
|
||||
});
|
||||
|
||||
// start
|
||||
program
|
||||
.command("start")
|
||||
.description("start a connection with specific port")
|
||||
.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 or valid address.");
|
||||
}
|
||||
return port;
|
||||
})
|
||||
.option("-s, --suffix <string>", "suffix for client name")
|
||||
.option(
|
||||
"-K, --keep_connection <boolean>",
|
||||
"keep connection for client and old connection will be closed (override connection)",
|
||||
true
|
||||
)
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-a, --access <string>", "access type (FREE)", TOKEN_FREE)
|
||||
.option("-p, --profile <string>", "profile name", PROFILE_DEFAULT)
|
||||
.option("-h, --host <string>", "local host value", "localhost")
|
||||
.option("-o, --origin <string>", "change request origin")
|
||||
.action((portOrAddress, options) => {
|
||||
startClient({
|
||||
port: portOrAddress,
|
||||
address: portOrAddress,
|
||||
options,
|
||||
})
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config")
|
||||
.description("create and update config file for connection")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.argument("<value>", "config value")
|
||||
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, value, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
// Error Code status
|
||||
let errorCode = 0;
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
config.token = value;
|
||||
} else if (type === "server") {
|
||||
config.server = value;
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
if (!value || value === "" || value === "new") {
|
||||
config.clientId = generateUUID();
|
||||
} else {
|
||||
config.clientId = value;
|
||||
}
|
||||
console.log(`client: ${config.clientId} was set to config`);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
config.apiKey = value;
|
||||
} else if (type === "access") {
|
||||
config.access = (value && value.toUpperCase().trim()) || TOKEN_FREE;
|
||||
|
||||
// FREE
|
||||
if (config.access === TOKEN_FREE) {
|
||||
await getTokenFree(config.server)
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
errorCode = 1;
|
||||
console.error("Generate free token failed, return with null or empty from server!", resp);
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
errorCode = 1;
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!config.clientId && config.apiKey) {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
|
||||
if (errorCode === 0) {
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`${type} config saved successfully to: ${configFilePath}`);
|
||||
}
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config-get")
|
||||
.description("get type from config file")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.option("-p, --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
if (!fs.existsSync(configDir)) {
|
||||
console.log(`config file ${configDir} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
} else {
|
||||
console.log(`config file ${configFilePath} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
console.log(config.token);
|
||||
} else if (type === "server") {
|
||||
console.log(config.server);
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
console.log(config.clientId);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
console.log(config.apiKey);
|
||||
} else if (type === "access") {
|
||||
console.log(config.access);
|
||||
} else {
|
||||
console.log('no config found for type: "' + type + '"');
|
||||
}
|
||||
});
|
||||
|
||||
// proxy
|
||||
program
|
||||
.command("proxy")
|
||||
.description("start a proxy server with specific port")
|
||||
.argument("<port>", "local server port number", (value) => {
|
||||
const port = parseInt(value, 10);
|
||||
if (isNaN(port)) {
|
||||
throw new InvalidArgumentError("Not a number.");
|
||||
}
|
||||
return port;
|
||||
})
|
||||
.argument("<target>", "target server url (https://google.com) or tcp (tcp://127.0.0.1:8080 or 127.0.0.1:8080)", (value) => {
|
||||
// Validate target
|
||||
if (!value) {
|
||||
throw new InvalidArgumentError("Target is required.");
|
||||
}
|
||||
|
||||
// Check if target is url
|
||||
if (value.indexOf("http") === 0 || value.indexOf("https") === 0) {
|
||||
if (isValidUrl(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentError("Target is not a valid url.");
|
||||
}
|
||||
|
||||
if (value.indexOf("tcp") === 0) {
|
||||
// Remove tcp prefix from target
|
||||
const t = value.substring(6); // remove tcp prefix (tcp://)
|
||||
if (isValidHost(t)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentError("Target is not a valid tcp host.");
|
||||
}
|
||||
|
||||
// Check if target is host with port (tcp)
|
||||
const target = value.split(":");
|
||||
if (target.length === 2) {
|
||||
const port = parseInt(target[1], 10);
|
||||
if (isNaN(port)) {
|
||||
throw new InvalidArgumentError("Target port is not a number.");
|
||||
}
|
||||
|
||||
return `tcp://${value}`;
|
||||
}
|
||||
|
||||
if (isValidHost(value)) {
|
||||
return `tcp://${value}`
|
||||
}
|
||||
|
||||
throw new InvalidArgumentError("Target is not a url or host with port.");
|
||||
})
|
||||
.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);
|
||||
const t = target.substring(6); // remove tcp prefix (tcp://)
|
||||
const targetHost = t.split(":")[0];
|
||||
const targetPort = parseInt(t.split(":")[1], 10);
|
||||
const proxy = createProxyTCPServer(targetHost, targetPort, {
|
||||
proxyPort: port,
|
||||
});
|
||||
|
||||
onConnectProxy(port, options);
|
||||
|
||||
proxy.on("error", (err) => {
|
||||
console.error("Proxy server error:", err);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
proxy.on("close", () => {
|
||||
console.log("Proxy server closed");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
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();
|
497
src/client.ts
Normal file
497
src/client.ts
Normal file
@ -0,0 +1,497 @@
|
||||
import * as os from "os";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as http from "http";
|
||||
import { io } from "socket.io-client";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
import { program, InvalidArgumentError, Argument } from "commander";
|
||||
import { TunnelRequest, TunnelResponse } from "./lib";
|
||||
import { generateUUID, addPrefixOnHttpSchema } from "./util";
|
||||
import { Socket } from 'socket.io-client';
|
||||
|
||||
import { getTokenFree } from './sdk'
|
||||
|
||||
const packageInfo = require("../package.json");
|
||||
|
||||
// constants
|
||||
const PROFILE_DEFAULT = "default";
|
||||
const PROFILE_PATH = ".hlt";
|
||||
const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
|
||||
const TOKEN_FREE = "FREE";
|
||||
|
||||
// create socket instance
|
||||
let socket: Socket | null = null;
|
||||
|
||||
function keepAlive() {
|
||||
setTimeout(() => {
|
||||
if (socket && socket.connected) {
|
||||
socket.send("ping");
|
||||
}
|
||||
keepAlive();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
function initClient(options: any) {
|
||||
// Please change this if your domain goes wrong here
|
||||
// Current style using sub-domain: https://{{clientId}}-tunnel.myhostingdomain.com
|
||||
// (Original server: https://tunnel.myhostingdomain.com)
|
||||
const profile = options.profile || PROFILE_DEFAULT;
|
||||
const clientId = `${options.apiKey || options.clientId || generateUUID()}`;
|
||||
const clientIdSub =
|
||||
profile === PROFILE_DEFAULT ? `${clientId}-` : `${clientId}-${profile}-`;
|
||||
const clientEndpoint = (
|
||||
options.suffix ? `${clientIdSub}${options.suffix}-` : clientIdSub
|
||||
)
|
||||
.toLowerCase()
|
||||
.trim();
|
||||
const serverUrl = addPrefixOnHttpSchema(options.server, clientEndpoint);
|
||||
|
||||
// extra options for socket to identify the client (authentication and options of tunnel)
|
||||
const defaultParams = {
|
||||
apiKey: options.apiKey,
|
||||
clientId: options.clientId,
|
||||
profile: options.profile,
|
||||
clientIdSub: clientIdSub,
|
||||
clientEndpoint: clientEndpoint,
|
||||
serverUrl: serverUrl,
|
||||
access: options.access,
|
||||
keep_connection: options.keep_connection || true,
|
||||
};
|
||||
|
||||
// extra info for notify about the running of the tunnel (it's private info, other platfom cannot access this)
|
||||
// this using for internal only (don't worry about this)
|
||||
const osInfo = {
|
||||
hostname: os.hostname(),
|
||||
platform: os.platform(),
|
||||
arch: os.arch(),
|
||||
release: os.release(),
|
||||
};
|
||||
|
||||
const initParams: any = {
|
||||
path: "/$cubetiq_http_tunnel",
|
||||
transports: ["websocket"],
|
||||
auth: {
|
||||
token: options.token,
|
||||
...defaultParams,
|
||||
},
|
||||
headers: {
|
||||
...defaultParams,
|
||||
os: osInfo,
|
||||
},
|
||||
// reconnection: true,
|
||||
};
|
||||
|
||||
const http_proxy = process.env.https_proxy || process.env.http_proxy;
|
||||
if (http_proxy) {
|
||||
initParams.agent = new HttpsProxyAgent(http_proxy);
|
||||
}
|
||||
|
||||
// Connecting to socket server and agent here...
|
||||
console.log(`client connecting to server: ${serverUrl}`);
|
||||
socket = io(serverUrl, initParams);
|
||||
|
||||
const clientLogPrefix = `client: ${clientId} on profile: ${profile}`;
|
||||
socket.on("connect", () => {
|
||||
if (socket!.connected) {
|
||||
console.log(`${clientLogPrefix} is connected to server successfully!`);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("connect_error", (e) => {
|
||||
console.log(
|
||||
`${clientLogPrefix} connect error:`,
|
||||
(e && e.message) || "something wrong"
|
||||
);
|
||||
if (e && e.message && e.message.startsWith("[40")) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnect", (reason) => {
|
||||
console.log(`${clientLogPrefix} disconnected: ${reason}!`);
|
||||
});
|
||||
|
||||
socket.on("disconnect_exit", (reason) => {
|
||||
console.log(`${clientLogPrefix} disconnected and exited ${reason}!`);
|
||||
socket?.disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
socket.on("request", (requestId, request) => {
|
||||
const isWebSocket = request.headers.upgrade === "websocket";
|
||||
console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
|
||||
request.port = options.port;
|
||||
request.hostname = options.host;
|
||||
|
||||
if (options.origin) {
|
||||
request.headers.host = options.origin;
|
||||
}
|
||||
|
||||
const tunnelRequest = new TunnelRequest(socket!, requestId);
|
||||
|
||||
const localReq = http.request(request);
|
||||
tunnelRequest.pipe(localReq);
|
||||
|
||||
const onTunnelRequestError = (e: any) => {
|
||||
tunnelRequest.off("end", onTunnelRequestEnd);
|
||||
localReq.destroy(e);
|
||||
};
|
||||
|
||||
const onTunnelRequestEnd = () => {
|
||||
tunnelRequest.off("error", onTunnelRequestError);
|
||||
};
|
||||
|
||||
tunnelRequest.once("error", onTunnelRequestError);
|
||||
tunnelRequest.once("end", onTunnelRequestEnd);
|
||||
|
||||
const onLocalResponse = (localRes: any) => {
|
||||
localReq.off("error", onLocalError);
|
||||
|
||||
if (isWebSocket && localRes.upgrade) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tunnelResponse = new TunnelResponse(socket!, requestId);
|
||||
|
||||
tunnelResponse.writeHead(
|
||||
localRes.statusCode,
|
||||
localRes.statusMessage,
|
||||
localRes.headers,
|
||||
localRes.httpVersion
|
||||
);
|
||||
|
||||
localRes.pipe(tunnelResponse);
|
||||
};
|
||||
|
||||
const onLocalError = (error: any) => {
|
||||
console.log(error);
|
||||
localReq.off("response", onLocalResponse);
|
||||
socket?.emit("request-error", requestId, error && error.message);
|
||||
tunnelRequest.destroy(error);
|
||||
};
|
||||
|
||||
const onUpgrade = (localRes: any, localSocket: any, localHead: any) => {
|
||||
// localSocket.once('error', onTunnelRequestError);
|
||||
if (localHead && localHead.length) localSocket.unshift(localHead);
|
||||
|
||||
const tunnelResponse = new TunnelResponse(socket!, requestId, true);
|
||||
tunnelResponse.writeHead(null, null, localRes.headers);
|
||||
localSocket.pipe(tunnelResponse).pipe(localSocket);
|
||||
};
|
||||
|
||||
localReq.once("error", onLocalError);
|
||||
localReq.once("response", onLocalResponse);
|
||||
|
||||
if (isWebSocket) {
|
||||
localReq.on("upgrade", onUpgrade);
|
||||
}
|
||||
});
|
||||
|
||||
// reconnect manually
|
||||
const tryReconnect = () => {
|
||||
setTimeout(() => {
|
||||
socket!.io.open((err) => {
|
||||
if (err) {
|
||||
tryReconnect();
|
||||
}
|
||||
});
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
// socket.io.on("close", tryReconnect);
|
||||
|
||||
keepAlive();
|
||||
}
|
||||
|
||||
program
|
||||
.name("hlt")
|
||||
.description(
|
||||
"CUBETIQ HTTP tunnel client with free access for local tunneling"
|
||||
)
|
||||
.version(`v${packageInfo.version}`);
|
||||
|
||||
// init
|
||||
program
|
||||
.command("init")
|
||||
.description("generate a new client and token with free access")
|
||||
.option("-s --server <string>", "setting server url", SERVER_DEFAULT_URL)
|
||||
.option(
|
||||
"-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(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (options) => {
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = options.server || SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token && options.token) {
|
||||
config.token = options.token;
|
||||
}
|
||||
|
||||
if (!config.access) {
|
||||
config.access = options.access || TOKEN_FREE;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
config.clientId = options.client || generateUUID();
|
||||
}
|
||||
|
||||
if (!config.apiKey && options.key) {
|
||||
config.apiKey = options.key;
|
||||
}
|
||||
|
||||
if (!config.token) {
|
||||
console.log("Generating token...");
|
||||
await getTokenFree(config.server)
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
console.log("free token return with null or empty from server");
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`initialized config saved successfully to: ${configFilePath}`);
|
||||
});
|
||||
|
||||
// start
|
||||
program
|
||||
.command("start")
|
||||
.description("start a connection with specific port")
|
||||
.argument("<port>", "local server port number", (value) => {
|
||||
const port = parseInt(value, 10);
|
||||
if (isNaN(port)) {
|
||||
throw new InvalidArgumentError("Not a number.");
|
||||
}
|
||||
return port;
|
||||
})
|
||||
.option("-s, --suffix <string>", "suffix for client name")
|
||||
.option(
|
||||
"-K, --keep_connection <boolean>",
|
||||
"keep connection for client and old connection will be closed (override connection)",
|
||||
true
|
||||
)
|
||||
.option(
|
||||
"-k --key <string>",
|
||||
"setting client api key for authentication access"
|
||||
)
|
||||
.option("-a, --access <string>", "access type (FREE)", TOKEN_FREE)
|
||||
.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) => {
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
}
|
||||
|
||||
if (!config.server) {
|
||||
config.server = SERVER_DEFAULT_URL;
|
||||
}
|
||||
|
||||
if (!config.token) {
|
||||
console.info(`please init or set token for ${config.server}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.clientId) {
|
||||
if (!config.apiKey) {
|
||||
console.info(`please init or create a client for ${config.server}`);
|
||||
} else {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
options.port = port;
|
||||
options.token = config.token;
|
||||
options.access = config.access;
|
||||
options.server = config.server;
|
||||
options.clientId = config.clientId;
|
||||
options.apiKey = options.key || config.apiKey;
|
||||
|
||||
if (options.suffix === "port" || options.suffix === "true") {
|
||||
options.suffix = `${port}`;
|
||||
} else if (options.suffix === "false") {
|
||||
options.suffix = undefined;
|
||||
} else if (options.suffix === "gen" || options.suffix === "uuid") {
|
||||
options.suffix = generateUUID();
|
||||
}
|
||||
|
||||
initClient(options);
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config")
|
||||
.description("create and update config file for connection")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.argument("<value>", "config value")
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, value, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir);
|
||||
console.log(`config file ${configDir} was created`);
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
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") {
|
||||
config.server = value;
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
if (!value || value === "" || value === "new") {
|
||||
config.clientId = generateUUID();
|
||||
} else {
|
||||
config.clientId = value;
|
||||
}
|
||||
console.log(`client: ${config.clientId} was set to config`);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
config.apiKey = value;
|
||||
} else if (type === "access") {
|
||||
config.access = (value && value.toUpperCase().trim()) || TOKEN_FREE;
|
||||
|
||||
// FREE
|
||||
if (config.access === TOKEN_FREE) {
|
||||
await getTokenFree(config.server)
|
||||
.then((resp: any) => {
|
||||
if (resp.data?.token) {
|
||||
config.token = resp.data?.token;
|
||||
} else {
|
||||
console.log("free token return with null or empty from server");
|
||||
return;
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
console.error("cannot get free token from server", err);
|
||||
return;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!config.clientId && config.apiKey) {
|
||||
config.clientId = config.apiKey;
|
||||
}
|
||||
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||
console.log(`${type} config saved successfully to: ${configFilePath}`);
|
||||
});
|
||||
|
||||
// config
|
||||
program
|
||||
.command("config-get")
|
||||
.description("get type from config file")
|
||||
.addArgument(
|
||||
new Argument("<type>", "config type").choices([
|
||||
"access",
|
||||
"token",
|
||||
"server",
|
||||
"client",
|
||||
"key",
|
||||
])
|
||||
)
|
||||
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
|
||||
.action(async (type, options) => {
|
||||
if (!type) {
|
||||
console.error("type config is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
|
||||
if (!fs.existsSync(configDir)) {
|
||||
console.log(`config file ${configDir} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
let config: any = {};
|
||||
const configFilename = `${options.profile}.json`;
|
||||
const configFilePath = path.resolve(configDir, configFilename);
|
||||
|
||||
if (fs.existsSync(configFilePath)) {
|
||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||
} else {
|
||||
console.log(`config file ${configFilePath} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "token" || type === "jwt") {
|
||||
console.log(config.token);
|
||||
} else if (type === "server") {
|
||||
console.log(config.server);
|
||||
} else if (type === "clientId" || type === "client") {
|
||||
console.log(config.clientId);
|
||||
} else if (type === "apiKey" || type === "key") {
|
||||
console.log(config.apiKey);
|
||||
} else if (type === "access") {
|
||||
console.log(config.access);
|
||||
} else {
|
||||
console.log('no config found for type: "' + type + '"');
|
||||
}
|
||||
});
|
||||
|
||||
program.parse();
|
@ -1,5 +0,0 @@
|
||||
// constants
|
||||
export const PROFILE_DEFAULT = "default";
|
||||
export const PROFILE_PATH = ".hlt";
|
||||
export const SERVER_DEFAULT_URL = "https://lt.ctdn.net";
|
||||
export const TOKEN_FREE = "FREE";
|
@ -1,22 +0,0 @@
|
||||
export interface Options {
|
||||
server?: string;
|
||||
profile?: string;
|
||||
key?: string;
|
||||
apiKey?: string;
|
||||
access?: string;
|
||||
suffix?: string;
|
||||
clientId?: string;
|
||||
keep_connection?: boolean;
|
||||
token?: string;
|
||||
origin?: string;
|
||||
port?: number;
|
||||
host?: string;
|
||||
|
||||
// [key: string]: any;
|
||||
}
|
||||
|
||||
export interface ClientOptions {
|
||||
port: number;
|
||||
address?: string; // e.g. localhost:8081 (take if port is not set)
|
||||
options?: Options;
|
||||
}
|
@ -57,7 +57,7 @@ class TunnelResponse extends stream.Duplex {
|
||||
super();
|
||||
|
||||
if (duplex) {
|
||||
// for websocket request: bidirection
|
||||
// for websocket request: bidirection
|
||||
const onResponsePipe = (responseId: string, data: any) => {
|
||||
if (this.responseId === responseId) {
|
||||
this.push(data);
|
||||
|
@ -1,28 +0,0 @@
|
||||
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}`);
|
||||
}
|
47
src/proxy.ts
47
src/proxy.ts
@ -1,47 +0,0 @@
|
||||
import express from 'express';
|
||||
import { createProxyMiddleware, Options as ProxyOpts } from 'http-proxy-middleware';
|
||||
|
||||
interface ProxyOptions extends ProxyOpts {
|
||||
basePath?: string;
|
||||
proxyPort?: number | string;
|
||||
}
|
||||
|
||||
interface SSLProxyOptions extends ProxyOptions {
|
||||
ssl?: {
|
||||
key: string;
|
||||
cert: string;
|
||||
};
|
||||
}
|
||||
|
||||
const port = process.env.PROXY_PORT || 3000;
|
||||
const app = express();
|
||||
|
||||
export const createProxyServer = (target: string, opts?: ProxyOptions) => {
|
||||
if (!target) {
|
||||
throw new Error('Proxy target is required');
|
||||
}
|
||||
|
||||
app.use(
|
||||
opts?.basePath || '',
|
||||
createProxyMiddleware({
|
||||
target,
|
||||
changeOrigin: true,
|
||||
ws: true,
|
||||
onProxyReq: (proxyReq, req, res) => {
|
||||
console.log(`[HTTP] Proxying ${req.method} ${req.url} to ${target}`);
|
||||
},
|
||||
onProxyReqWs: (proxyReq, req, socket, options, head) => {
|
||||
console.log(`[WS] Proxying ${req.method} ${req.url} to ${target}`);
|
||||
},
|
||||
onError: (err, req, res) => {
|
||||
console.error(`Proxy error: ${err.message}`);
|
||||
},
|
||||
...opts
|
||||
})
|
||||
);
|
||||
|
||||
const proxyPort = opts?.proxyPort || port;
|
||||
return app.listen(proxyPort, () => {
|
||||
console.log(`Proxy listening on port ${proxyPort} and forwarding to ${target}`);
|
||||
});
|
||||
};
|
@ -1,67 +0,0 @@
|
||||
import * as net from 'net'
|
||||
|
||||
const port = process.env.PROXY_PORT || 3000;
|
||||
|
||||
interface ProxyOptions extends net.TcpNetConnectOpts {
|
||||
proxyPort?: number | string;
|
||||
}
|
||||
|
||||
// Create a TCP server that acts as a proxy
|
||||
export const createProxyServer = (targetHost: string, targetPort: number, opts?: Partial<ProxyOptions>) => {
|
||||
const server = net.createServer((clientSocket) => {
|
||||
const targetSocket = net.createConnection({
|
||||
host: targetHost,
|
||||
port: targetPort,
|
||||
...opts,
|
||||
});
|
||||
|
||||
clientSocket.pipe(targetSocket);
|
||||
targetSocket.pipe(clientSocket);
|
||||
|
||||
// Listen for target socket connection
|
||||
targetSocket.on('connect', () => {
|
||||
console.log(`Target socket connected to ${targetHost}:${targetPort}`);
|
||||
});
|
||||
|
||||
// targetSocket.on('data', (data) => {
|
||||
// console.log('Target socket received data length:', data.length);
|
||||
// });
|
||||
|
||||
// Listen for client socket requests
|
||||
clientSocket.on('data', (data) => {
|
||||
console.log('Client socket request data', data.toString());
|
||||
});
|
||||
|
||||
clientSocket.on('end', () => {
|
||||
targetSocket.end();
|
||||
});
|
||||
|
||||
targetSocket.on('end', () => {
|
||||
clientSocket.end();
|
||||
});
|
||||
|
||||
clientSocket.on('error', (err) => {
|
||||
console.error('Client socket error:', err);
|
||||
});
|
||||
|
||||
targetSocket.on('error', (err) => {
|
||||
console.error('Target socket error:', err);
|
||||
});
|
||||
|
||||
targetSocket.on('timeout', () => {
|
||||
console.log('Target socket timeout');
|
||||
});
|
||||
|
||||
clientSocket.on('timeout', () => {
|
||||
console.log('Client socket timeout');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
const proxyPort = opts?.proxyPort || port;
|
||||
server.listen(proxyPort, () => {
|
||||
console.log(`TCP proxy server listening on port ${proxyPort} and forwarding to ${targetHost}:${targetPort}`);
|
||||
});
|
||||
|
||||
return server;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import axios from "axios";
|
||||
const axios = require("axios").default;
|
||||
|
||||
const getTokenFree = async (baseUrl: string, data: any = {}) => {
|
||||
const url = `${baseUrl}/__free__/api/get_token`;
|
||||
@ -10,7 +10,6 @@ const getTokenFree = async (baseUrl: string, data: any = {}) => {
|
||||
},
|
||||
headers: {
|
||||
"x-access-type": "FREE",
|
||||
"Accept-Encoding": "identity",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
45
src/util.ts
45
src/util.ts
@ -16,49 +16,4 @@ const generateUUID = () => {
|
||||
return crypto.randomUUID();
|
||||
};
|
||||
|
||||
export const isValidUrl = (url: string) => {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const isValidIP = (ip: string) => {
|
||||
const regex = new RegExp(
|
||||
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$"
|
||||
);
|
||||
return regex.test(ip);
|
||||
};
|
||||
|
||||
export const isValidHostname = (hostname: string) => {
|
||||
const regex = new RegExp(
|
||||
"^(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.?)+(?:(?:[a-z]{2,}\\.)?[a-z]{2,})|localhost)$",
|
||||
"i"
|
||||
);
|
||||
return regex.test(hostname) || isValidIP(hostname);
|
||||
};
|
||||
|
||||
export const isValidPort = (port: number) => {
|
||||
return port > 0 && port < 65536;
|
||||
};
|
||||
|
||||
export const isValidHost = (host: string) => {
|
||||
const [hostname, port] = host.split(":");
|
||||
return isValidHostname(hostname) && isValidPort(parseInt(port, 10));
|
||||
};
|
||||
|
||||
export const isValidTarget = (target: string) => {
|
||||
if (isValidUrl(target)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isValidHost(target)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export { addPrefixOnHttpSchema, generateUUID };
|
||||
|
34
test/test.ts
34
test/test.ts
@ -1,34 +0,0 @@
|
||||
import { startClient } from '../src/api';
|
||||
|
||||
async function main() {
|
||||
const client = await startClient({
|
||||
// port: 8081,
|
||||
address: '172.17.0.2:8222',
|
||||
options: {
|
||||
profile: 'mytest',
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Client started:', client?.getEndpoint());
|
||||
|
||||
setTimeout(async () => {
|
||||
client?.stop();
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// import { createProxyServer } from './proxy';
|
||||
// import { createProxyServer } from './proxy_tcp';
|
||||
|
||||
// const proxy = createProxyServer('https://git.cubetiqs.com', {
|
||||
// proxyPort: 3005,
|
||||
// basePath: '/',
|
||||
// });
|
||||
|
||||
// const proxy = createProxyServer('192.168.0.202', 8081, {
|
||||
// proxyPort: 3005,
|
||||
// });
|
@ -1,16 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2015"],
|
||||
"target": "es5",
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2015"],
|
||||
"target": "es5",
|
||||
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": false,
|
||||
"outDir": "dist",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"exclude": ["node_modules", "dist", "test", "src/**/*.spec.ts"]
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": false,
|
||||
"outDir": "dist",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user