/
index.js
executable file
·90 lines (73 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
const { exec } = require("child_process");
const { spawn } = require("child_process");
const { createInterface } = require("readline");
const runCommand = (command) => {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.log("Error \n");
reject(error);
return;
}
if (stderr) {
console.log(stderr);
}
resolve(stdout);
});
});
};
const runInteractiveCommand = (command, args) => {
const spawned = spawn(command, args, { stdio: "inherit" });
spawned.on("error", (error) => {
console.error(`Error: ${error}`);
});
};
const readline = createInterface({
input: process.stdin,
output: process.stdout,
});
const askQuestion = (question) => {
return new Promise((resolve) => {
readline.question(question, resolve);
});
};
const runScript = async () => {
try {
await runCommand("brew install mkcert");
await runCommand("mkcert -install");
await runCommand("mkcert localhost");
console.log("Installing local-ssl-proxy...");
await runCommand("npm install -g local-ssl-proxy");
console.log("Installed local-ssl-proxy ✅", "\n");
const source = await askQuestion("Enter the source (e.g 3010): ");
console.log("Source:", source, "\n");
if (!source) {
console.log("Source is required");
readline.close();
return;
}
const target = await askQuestion("Enter the target (e.g 3000): ");
console.log("Target:", target, "\n");
if (!target) {
console.log("Target is required!");
readline.close();
return;
}
readline.close();
console.log("Proxying traffic from port", source, "to port", target, "\n");
runInteractiveCommand("local-ssl-proxy", [
`--source`,
source,
`--target`,
target,
"--cert",
"localhost.pem",
"--key",
"localhost-key.pem",
]);
} catch (error) {
console.error("Error:", error);
}
};
runScript();