Add support client API and support proxy http/https and tcp

This commit is contained in:
Sambo Chea 2023-07-11 12:20:03 +07:00
parent afbf3097f1
commit 9f61c8aebf
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
9 changed files with 1765 additions and 343 deletions

View File

@ -1,2 +1,3 @@
src/ src/
.github/ .github/
test/

View File

@ -1,3 +1,9 @@
### 11/07/2023
- Support Client API
- Support Proxy (HTTP/HTTPS) and TCP
- Improvements and Bugs fixed
### 30/11/2022 ### 30/11/2022
- Fixed response data in axios request with accept-encoding to identity - Fixed response data in axios request with accept-encoding to identity

1333
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,8 @@
"dependencies": { "dependencies": {
"axios": "^1.2.0", "axios": "^1.2.0",
"commander": "^9.3.0", "commander": "^9.3.0",
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6",
"https-proxy-agent": "^5.0.1", "https-proxy-agent": "^5.0.1",
"socket.io-client": "^4.5.1" "socket.io-client": "^4.5.1"
}, },
@ -32,6 +34,7 @@
"access": "public" "access": "public"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.15",
"@types/node": "^18.0.3", "@types/node": "^18.0.3",
"ts-node-dev": "^2.0.0", "ts-node-dev": "^2.0.0",
"typescript": "^4.7.4" "typescript": "^4.7.4"

View File

@ -11,326 +11,343 @@ import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from ".
import { ClientOptions, Options } from "./interface"; import { ClientOptions, Options } from "./interface";
import { getTokenFree } from './sdk'; import { getTokenFree } from './sdk';
// create socket instance interface Stoppable {
let socket: Socket | null = null; stop(): void;
let keepAliveTimer: NodeJS.Timeout | null = null;
function keepAlive() {
keepAliveTimer = setTimeout(() => {
if (socket && socket.connected) {
socket.send("ping");
}
keepAlive();
}, 5000);
} }
// Init the Client class HttpTunnelClient implements Stoppable {
const initClient = async (options: any) => { // create socket instance
const configDir = path.resolve(os.homedir(), PROFILE_PATH); private socket: Socket | null = null;
private keepAliveTimer: NodeJS.Timeout | null = null;
private keepAliveTimeout: number | null = null;
if (!fs.existsSync(configDir)) { private keepAlive() {
fs.mkdirSync(configDir); if (!this.socket) {
console.log(`config file ${configDir} was created`); return;
}
this.keepAliveTimer = setTimeout(() => {
if (this.socket && this.socket.connected) {
this.socket.send("ping");
}
this.keepAlive();
}, this.keepAliveTimeout || 5000);
} }
let config: any = {}; // Init the Client for config file
const configFilename = `${options.profile}.json`; public initConfigFile = async (options: any) => {
const configFilePath = path.resolve(configDir, configFilename); const profile = options.profile || PROFILE_DEFAULT;
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
if (fs.existsSync(configFilePath)) { if (!fs.existsSync(configDir)) {
config = JSON.parse(fs.readFileSync(configFilePath, "utf8")); fs.mkdirSync(configDir);
} console.log(`config file ${configDir} was created`);
}
// Force reset config server from client init let config: any = {};
if (!config.server || options.force) { const configFilename = `${profile}.json`;
config.server = options.server || SERVER_DEFAULT_URL; const configFilePath = path.resolve(configDir, configFilename);
}
if (!config.token && options.token) { if (fs.existsSync(configFilePath)) {
config.token = options.token; config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
} }
if (!config.access) { // Force reset config server from client init
config.access = options.access || TOKEN_FREE; if (!config.server || options.force) {
} config.server = options.server || SERVER_DEFAULT_URL;
}
if (!config.clientId) { if (!config.token && options.token) {
config.clientId = options.client || generateUUID(); config.token = options.token;
} }
if (!config.apiKey && options.key) { if (!config.access) {
config.apiKey = options.key; config.access = options.access || TOKEN_FREE;
} }
let errorCode = 0; if (!config.clientId) {
if (!config.token || options.force) { config.clientId = options.client || generateUUID();
console.log(`Generating token from server: ${config.server}`); }
await getTokenFree(config.server, {
timestamp: (new Date().getTime()), if (!config.apiKey && options.key) {
clientId: config.clientId, config.apiKey = options.key;
apiKey: config.apiKey, }
})
.then((resp: any) => { let errorCode = 0;
if (resp.data?.token) { if (!config.token || options.force) {
console.log("Token generated successfully!"); console.log(`Generating token from server: ${config.server}`);
config.token = resp.data?.token; await getTokenFree(config.server, {
} else { timestamp: (new Date().getTime()),
errorCode = 1; clientId: config.clientId,
console.error("Generate free token failed, return with null or empty from server!", resp); apiKey: config.apiKey,
return;
}
}) })
.catch((err: any) => { .then((resp: any) => {
errorCode = 1; if (resp.data?.token) {
console.error("cannot get free token from server", err); console.log("Token generated successfully!");
return; config.token = resp.data?.token;
}); } else {
} errorCode = 1;
console.error("Generate free token failed, return with null or empty from server!", resp);
if (errorCode === 0) { return;
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2)); }
console.log(`initialized config saved successfully to: ${configFilePath}`); })
} .catch((err: any) => {
}; errorCode = 1;
console.error("cannot get free token from server", err);
// Start Client return;
const 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);
// 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) => { if (errorCode === 0) {
console.error( fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
`${clientLogPrefix} connect error:`, console.log(`initialized config saved successfully to: ${configFilePath}`);
(e && e.message) || "something wrong" }
); };
if (e && e.message && e.message.startsWith("[40")) {
// 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);
// 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); process.exit(1);
} });
});
socket.on("disconnect", (reason) => { this.socket.on("request", (requestId, request) => {
console.warn(`${clientLogPrefix} disconnected: ${reason}!`); const isWebSocket = request.headers.upgrade === "websocket";
}); console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
request.port = options.port;
request.hostname = options.host;
socket.on("disconnect_exit", (reason) => { if (options.origin) {
console.warn(`${clientLogPrefix} disconnected and exited ${reason}!`); request.headers.host = options.origin;
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) => {
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(socket!, requestId); const tunnelRequest = new TunnelRequest(this.socket!, requestId);
tunnelResponse.writeHead( const localReq = http.request(request);
localRes.statusCode, tunnelRequest.pipe(localReq);
localRes.statusMessage,
localRes.headers,
localRes.httpVersion
);
localRes.pipe(tunnelResponse); const onTunnelRequestError = (e: any) => {
}; console.error("tunnel request error: ", e);
tunnelRequest.off("end", onTunnelRequestEnd);
localReq.destroy(e);
};
const onLocalError = (error: any) => { const onTunnelRequestEnd = () => {
console.error("local error:", error); tunnelRequest.off("error", onTunnelRequestError);
localReq.off("response", onLocalResponse); };
socket?.emit("request-error", requestId, error && error.message);
tunnelRequest.destroy(error);
};
const onUpgrade = (localRes: any, localSocket: any, localHead: any) => { tunnelRequest.once("error", onTunnelRequestError);
// localSocket.once('error', onTunnelRequestError); tunnelRequest.once("end", onTunnelRequestEnd);
if (localHead && localHead.length) localSocket.unshift(localHead);
const tunnelResponse = new TunnelResponse(socket!, requestId, true); const onLocalResponse = (localRes: any) => {
tunnelResponse.writeHead(null, null, localRes.headers); localReq.off("error", onLocalError);
localSocket.pipe(tunnelResponse).pipe(localSocket);
};
localReq.once("error", onLocalError); if (isWebSocket && localRes.upgrade) {
localReq.once("response", onLocalResponse); return;
}
if (isWebSocket) { const tunnelResponse = new TunnelResponse(this.socket!, requestId);
localReq.on("upgrade", onUpgrade);
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: ClientOptions): Promise<Stoppable | undefined> => {
const { port, options = {} } = clientOptions;
const configDir = path.resolve(os.homedir(), PROFILE_PATH);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
} }
});
// reconnect manually let config: any = {};
// const tryReconnect = () => { const configFilename = `${options.profile || PROFILE_DEFAULT}.json`;
// setTimeout(() => { const configFilePath = path.resolve(configDir, configFilename);
// socket!.io.open((err) => { console.log(`config file: ${configFilePath}`);
// if (err) {
// tryReconnect();
// }
// });
// }, 2000);
// };
// socket.io.on("close", tryReconnect);
keepAlive(); if (fs.existsSync(configFilePath)) {
}; config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
const startClient = (clientOptions: ClientOptions) => {
const { port, options = {} } = clientOptions;
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; if (!config.server) {
options.token = config.token; config.server = SERVER_DEFAULT_URL;
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") { if (!config.token) {
options.suffix = `${port}`; console.info(`please init or set token for ${config.server}`);
} else if (options.suffix === "false") { return;
options.suffix = undefined; }
} else if (options.suffix === "gen" || options.suffix === "uuid") {
options.suffix = generateUUID();
}
initStartClient(options); if (!config.clientId) {
}; if (!config.apiKey) {
console.info(`please init or create a client for ${config.server}`);
} else {
config.clientId = config.apiKey;
}
return;
}
const stopClient = () => { options.port = port;
if (socket) { options.token = config.token;
socket.disconnect(); options.access = config.access;
socket.close(); options.server = config.server;
socket = null; options.clientId = config.clientId;
keepAliveTimer && clearInterval(keepAliveTimer); options.apiKey = options.key || config.apiKey;
console.log("client stopped"); 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();
}
export { await this.initStartClient(options);
initClient, console.log("client started!");
startClient,
stopClient, 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!");
}
};
}
export const client = new HttpTunnelClient();
export const initConfigFileClient = client.initConfigFile;
export const startClient = client.start;
export const stopClient = client.stop;

View File

@ -2,10 +2,12 @@ import { Argument, InvalidArgumentError, program } from "commander";
import * as fs from "fs"; import * as fs from "fs";
import * as os from "os"; import * as os from "os";
import * as path from "path"; import * as path from "path";
import { initClient, startClient } from "./api"; import { initConfigFileClient, startClient } from "./api";
import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant"; import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant";
import { createProxyServer } from "./proxy";
import { createProxyServer as createProxyTCPServer } from "./proxy_tcp";
import { getTokenFree } from './sdk'; import { getTokenFree } from './sdk';
import { generateUUID } from "./util"; import { generateUUID, isValidHost, isValidUrl } from "./util";
const packageInfo = require("../package.json"); const packageInfo = require("../package.json");
@ -35,7 +37,7 @@ program
.option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT) .option("-p --profile <string>", "setting profile name", PROFILE_DEFAULT)
.option("-f --force", "force to generate new client and token", false) .option("-f --force", "force to generate new client and token", false)
.action(async (options) => { .action(async (options) => {
initClient(options); initConfigFileClient(options);
}); });
// start // start
@ -211,4 +213,92 @@ program
} }
}); });
// 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.");
})
.action((port, target) => {
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,
});
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,
});
proxy.on("error", (err) => {
console.error("Proxy server error:", err);
});
proxy.on("close", () => {
console.log("Proxy server closed");
});
}
});
program.parse(); program.parse();

View File

@ -1,9 +0,0 @@
import { startClient, stopClient } from './api';
startClient({
port: 3001,
});
setTimeout(() => {
stopClient();
}, 5000);

View File

@ -16,4 +16,49 @@ const generateUUID = () => {
return crypto.randomUUID(); 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 }; export { addPrefixOnHttpSchema, generateUUID };

View File

@ -1,14 +1,16 @@
{ {
"compilerOptions": { "compilerOptions": {
"lib": ["ES2015"], "lib": ["ES2015"],
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"sourceMap": false, "sourceMap": false,
"outDir": "dist", "outDir": "dist",
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"skipLibCheck": true "skipLibCheck": true,
} "esModuleInterop": true
},
"exclude": ["node_modules", "dist", "test", "src/**/*.spec.ts"]
} }