Task: Add console with saved to fullpath of config file for http tunnel client

This commit is contained in:
Sambo Chea 2022-06-25 08:24:54 +07:00
parent 31d65dfe08
commit cf2ad12dac
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
2 changed files with 42 additions and 5 deletions

View File

@ -26,10 +26,12 @@ function initClient(options) {
token: options.token,
},
};
const http_proxy = process.env.https_proxy || process.env.http_proxy;
if (http_proxy) {
initParams.agent = new HttpsProxyAgent(http_proxy);
}
socket = io(options.server, initParams);
socket.on("connect", () => {
@ -51,58 +53,74 @@ function initClient(options) {
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({
requestId,
socket: socket,
});
const localReq = http.request(request);
tunnelRequest.pipe(localReq);
const onTunnelRequestError = (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) => {
localReq.off("error", onLocalError);
if (isWebSocket && localRes.upgrade) {
return;
}
const tunnelResponse = new TunnelResponse({
responseId: requestId,
socket: socket,
});
tunnelResponse.writeHead(
localRes.statusCode,
localRes.statusMessage,
localRes.headers,
localRes.httpVersion
);
localRes.pipe(tunnelResponse);
};
const onLocalError = (error) => {
console.log(error);
localReq.off("response", onLocalResponse);
socket.emit("request-error", requestId, error && error.message);
tunnelRequest.destroy(error);
};
const onUpgrade = (localRes, localSocket, localHead) => {
// localSocket.once('error', onTunnelRequestError);
if (localHead && localHead.length) localSocket.unshift(localHead);
const tunnelResponse = new TunnelResponse({
responseId: requestId,
socket: socket,
duplex: true,
});
tunnelResponse.writeHead(null, null, localRes.headers);
localSocket.pipe(tunnelResponse).pipe(localSocket);
};
localReq.once("error", onLocalError);
localReq.once("response", onLocalResponse);
@ -110,6 +128,7 @@ function initClient(options) {
localReq.on("upgrade", onUpgrade);
}
});
keepAlive();
}
@ -124,30 +143,38 @@ program
}
return port;
})
.option("-p, --profile <string>", "setting profile name", "config")
.option("-p, --profile <string>", "setting profile name", "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(), ".http-tunnel");
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
}
let config = {};
const configFilePath = path.resolve(configDir, `${options.profile}.json`);
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) {
console.log("Please set remote tunnel server firstly");
return;
}
if (!config.token) {
console.log(`Please set jwt token for ${config.server} firstly`);
return;
}
options.port = port;
options.token = config.token;
options.server = config.server;
initClient(options);
});
@ -155,25 +182,32 @@ program
.command("config")
.addArgument(new Argument("<type>", "config type").choices(["jwt", "server"]))
.argument("<value>", "config value")
.option("-p --profile <string>", "setting profile name", "config")
.option("-p --profile <string>", "setting profile name", "default")
.action((type, value, options) => {
const configDir = path.resolve(os.homedir(), ".http-tunnel");
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
}
let config = {};
const configFilePath = path.resolve(configDir, `${options.profile}.json`);
const configFilename = `${options.profile}.json`;
const configFilePath = path.resolve(configDir, configFilename);
if (fs.existsSync(configFilePath)) {
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
}
if (type === "jwt") {
config.token = value;
}
if (type === "server") {
config.server = value;
}
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
console.log(`${type} config saved successfully`);
console.log(`${type} config saved successfully to: ${configFilePath}`);
});
program.parse();

3
package-lock.json generated
View File

@ -12,6 +12,9 @@
"commander": "^9.3.0",
"https-proxy-agent": "^5.0.1",
"socket.io-client": "^4.5.1"
},
"bin": {
"http-tunnel": "bin/http-tunnel"
}
},
"node_modules/@socket.io/component-emitter": {