Task: Add console with saved to fullpath of config file for http tunnel client
This commit is contained in:
parent
31d65dfe08
commit
cf2ad12dac
44
client.js
44
client.js
@ -26,10 +26,12 @@ function initClient(options) {
|
|||||||
token: options.token,
|
token: options.token,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const http_proxy = process.env.https_proxy || process.env.http_proxy;
|
const http_proxy = process.env.https_proxy || process.env.http_proxy;
|
||||||
if (http_proxy) {
|
if (http_proxy) {
|
||||||
initParams.agent = new HttpsProxyAgent(http_proxy);
|
initParams.agent = new HttpsProxyAgent(http_proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket = io(options.server, initParams);
|
socket = io(options.server, initParams);
|
||||||
|
|
||||||
socket.on("connect", () => {
|
socket.on("connect", () => {
|
||||||
@ -51,58 +53,74 @@ function initClient(options) {
|
|||||||
console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
|
console.log(`${isWebSocket ? "WS" : request.method}: `, request.path);
|
||||||
request.port = options.port;
|
request.port = options.port;
|
||||||
request.hostname = options.host;
|
request.hostname = options.host;
|
||||||
|
|
||||||
if (options.origin) {
|
if (options.origin) {
|
||||||
request.headers.host = options.origin;
|
request.headers.host = options.origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tunnelRequest = new TunnelRequest({
|
const tunnelRequest = new TunnelRequest({
|
||||||
requestId,
|
requestId,
|
||||||
socket: socket,
|
socket: socket,
|
||||||
});
|
});
|
||||||
|
|
||||||
const localReq = http.request(request);
|
const localReq = http.request(request);
|
||||||
tunnelRequest.pipe(localReq);
|
tunnelRequest.pipe(localReq);
|
||||||
|
|
||||||
const onTunnelRequestError = (e) => {
|
const onTunnelRequestError = (e) => {
|
||||||
tunnelRequest.off("end", onTunnelRequestEnd);
|
tunnelRequest.off("end", onTunnelRequestEnd);
|
||||||
localReq.destroy(e);
|
localReq.destroy(e);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onTunnelRequestEnd = () => {
|
const onTunnelRequestEnd = () => {
|
||||||
tunnelRequest.off("error", onTunnelRequestError);
|
tunnelRequest.off("error", onTunnelRequestError);
|
||||||
};
|
};
|
||||||
|
|
||||||
tunnelRequest.once("error", onTunnelRequestError);
|
tunnelRequest.once("error", onTunnelRequestError);
|
||||||
tunnelRequest.once("end", onTunnelRequestEnd);
|
tunnelRequest.once("end", onTunnelRequestEnd);
|
||||||
|
|
||||||
const onLocalResponse = (localRes) => {
|
const onLocalResponse = (localRes) => {
|
||||||
localReq.off("error", onLocalError);
|
localReq.off("error", onLocalError);
|
||||||
|
|
||||||
if (isWebSocket && localRes.upgrade) {
|
if (isWebSocket && localRes.upgrade) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tunnelResponse = new TunnelResponse({
|
const tunnelResponse = new TunnelResponse({
|
||||||
responseId: requestId,
|
responseId: requestId,
|
||||||
socket: socket,
|
socket: socket,
|
||||||
});
|
});
|
||||||
|
|
||||||
tunnelResponse.writeHead(
|
tunnelResponse.writeHead(
|
||||||
localRes.statusCode,
|
localRes.statusCode,
|
||||||
localRes.statusMessage,
|
localRes.statusMessage,
|
||||||
localRes.headers,
|
localRes.headers,
|
||||||
localRes.httpVersion
|
localRes.httpVersion
|
||||||
);
|
);
|
||||||
|
|
||||||
localRes.pipe(tunnelResponse);
|
localRes.pipe(tunnelResponse);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onLocalError = (error) => {
|
const onLocalError = (error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
localReq.off("response", onLocalResponse);
|
localReq.off("response", onLocalResponse);
|
||||||
socket.emit("request-error", requestId, error && error.message);
|
socket.emit("request-error", requestId, error && error.message);
|
||||||
tunnelRequest.destroy(error);
|
tunnelRequest.destroy(error);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onUpgrade = (localRes, localSocket, localHead) => {
|
const onUpgrade = (localRes, localSocket, localHead) => {
|
||||||
// localSocket.once('error', onTunnelRequestError);
|
// localSocket.once('error', onTunnelRequestError);
|
||||||
if (localHead && localHead.length) localSocket.unshift(localHead);
|
if (localHead && localHead.length) localSocket.unshift(localHead);
|
||||||
|
|
||||||
const tunnelResponse = new TunnelResponse({
|
const tunnelResponse = new TunnelResponse({
|
||||||
responseId: requestId,
|
responseId: requestId,
|
||||||
socket: socket,
|
socket: socket,
|
||||||
duplex: true,
|
duplex: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
tunnelResponse.writeHead(null, null, localRes.headers);
|
tunnelResponse.writeHead(null, null, localRes.headers);
|
||||||
localSocket.pipe(tunnelResponse).pipe(localSocket);
|
localSocket.pipe(tunnelResponse).pipe(localSocket);
|
||||||
};
|
};
|
||||||
|
|
||||||
localReq.once("error", onLocalError);
|
localReq.once("error", onLocalError);
|
||||||
localReq.once("response", onLocalResponse);
|
localReq.once("response", onLocalResponse);
|
||||||
|
|
||||||
@ -110,6 +128,7 @@ function initClient(options) {
|
|||||||
localReq.on("upgrade", onUpgrade);
|
localReq.on("upgrade", onUpgrade);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
keepAlive();
|
keepAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,30 +143,38 @@ program
|
|||||||
}
|
}
|
||||||
return port;
|
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("-h, --host <string>", "local host value", "localhost")
|
||||||
.option("-o, --origin <string>", "change request origin")
|
.option("-o, --origin <string>", "change request origin")
|
||||||
.action((port, options) => {
|
.action((port, options) => {
|
||||||
const configDir = path.resolve(os.homedir(), ".http-tunnel");
|
const configDir = path.resolve(os.homedir(), ".http-tunnel");
|
||||||
|
|
||||||
if (!fs.existsSync(configDir)) {
|
if (!fs.existsSync(configDir)) {
|
||||||
fs.mkdirSync(configDir);
|
fs.mkdirSync(configDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = {};
|
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)) {
|
if (fs.existsSync(configFilePath)) {
|
||||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.server) {
|
if (!config.server) {
|
||||||
console.log("Please set remote tunnel server firstly");
|
console.log("Please set remote tunnel server firstly");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.token) {
|
if (!config.token) {
|
||||||
console.log(`Please set jwt token for ${config.server} firstly`);
|
console.log(`Please set jwt token for ${config.server} firstly`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
options.port = port;
|
options.port = port;
|
||||||
options.token = config.token;
|
options.token = config.token;
|
||||||
options.server = config.server;
|
options.server = config.server;
|
||||||
|
|
||||||
initClient(options);
|
initClient(options);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -155,25 +182,32 @@ program
|
|||||||
.command("config")
|
.command("config")
|
||||||
.addArgument(new Argument("<type>", "config type").choices(["jwt", "server"]))
|
.addArgument(new Argument("<type>", "config type").choices(["jwt", "server"]))
|
||||||
.argument("<value>", "config value")
|
.argument("<value>", "config value")
|
||||||
.option("-p --profile <string>", "setting profile name", "config")
|
.option("-p --profile <string>", "setting profile name", "default")
|
||||||
.action((type, value, options) => {
|
.action((type, value, options) => {
|
||||||
const configDir = path.resolve(os.homedir(), ".http-tunnel");
|
const configDir = path.resolve(os.homedir(), ".http-tunnel");
|
||||||
|
|
||||||
if (!fs.existsSync(configDir)) {
|
if (!fs.existsSync(configDir)) {
|
||||||
fs.mkdirSync(configDir);
|
fs.mkdirSync(configDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = {};
|
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)) {
|
if (fs.existsSync(configFilePath)) {
|
||||||
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
config = JSON.parse(fs.readFileSync(configFilePath, "utf8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === "jwt") {
|
if (type === "jwt") {
|
||||||
config.token = value;
|
config.token = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === "server") {
|
if (type === "server") {
|
||||||
config.server = value;
|
config.server = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
|
||||||
console.log(`${type} config saved successfully`);
|
console.log(`${type} config saved successfully to: ${configFilePath}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
program.parse();
|
program.parse();
|
||||||
|
3
package-lock.json
generated
3
package-lock.json
generated
@ -12,6 +12,9 @@
|
|||||||
"commander": "^9.3.0",
|
"commander": "^9.3.0",
|
||||||
"https-proxy-agent": "^5.0.1",
|
"https-proxy-agent": "^5.0.1",
|
||||||
"socket.io-client": "^4.5.1"
|
"socket.io-client": "^4.5.1"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"http-tunnel": "bin/http-tunnel"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@socket.io/component-emitter": {
|
"node_modules/@socket.io/component-emitter": {
|
||||||
|
Loading…
Reference in New Issue
Block a user