-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
175 lines (145 loc) · 4.93 KB
/
preload.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const processes = {};
window.addEventListener('DOMContentLoaded', () => {
reload();
document.getElementById("refresh").addEventListener('click', () => {
reload();
});
document.getElementById("edit").addEventListener('click', () => {
edit();
});
})
function reload() {
console.log("=== Reload ===");
document.getElementById("tunnels").innerHTML = "";
// Read the SSH config from ~/.ssh/tunnel
//
const OS = require("node:os");
const FS = require("node:fs");
const SSHConfig = require("ssh-config");
const homedir = OS.homedir();
const config_file = `${homedir}/.ssh/tunnel`;
console.log({ homedir, config_file });
if(!FS.existsSync(config_file)) {
FS.writeFileSync(config_file,
`
# The remote command needs to echo "SSHCONNECT" for tunnel.exe
# to acknowledge the connection
#
Host *
RemoteCommand printf "SSHCONNECT"; sleep infinity
`);
}
let config = FS.readFileSync(config_file, "utf-8");
console.log({ config });
let parsed = SSHConfig.parse(config);
console.log({ parsed });
for(let p of parsed) {
const { type, param, value } = p;
if(type === 1 && param === "Host") {
// .compute will work out the full config for this host
// (e.g. if combined with wildcard / default vaules...)
//
const computed = parsed.compute(value);
const { HostName, LocalForward = [] } = computed;
// Only works for a single local forward rule per Host
//
if(LocalForward.length !== 1) continue;
let [ local_port, remote_port_ ] = LocalForward[0].split(" ");
let [ localhost, remote_port ] = remote_port_.split(":");
// Must be connecting to localhost on the remote side
//
if(localhost !== "localhost") continue;
const process = processes[value];
// status is "" / "disconected", "connecting", "connected"
//
const status = processStatus(process) || "";
console.log({ value, computed, status });
const tunnel = document.createElement("div");
tunnel.innerHTML = `
<div class="tunnel" data-id="${value}" data-status="${status}" title="${status}">
<div class="tunnel-title">
${value}
</div>
<div class="tunnel-details">
<div>:${local_port}</div>
<div class="material-icons">settings_ethernet</div>
<div>${HostName}:${remote_port}</div>
</div>
<div class="tunnel-actions">
<div id="${value}-start" class="material-icons start" title="Start">play_arrow</div>
<div id="${value}-stop" class="material-icons stop" title="Stop">stop</div>
<div class="connecting" title="Connecting"></div>
</div>
<div>
`;
document.getElementById("tunnels").appendChild(tunnel);
document.getElementById(`${value}-start`).addEventListener("click", () => {
console.log(`${value}-start`);
log(`[${value}] Connecting`);
const decoder = new TextDecoder();
const child_process = require("node:child_process");
log(`[${value}] ssh -F ${config_file} ${value}`);
const process = child_process.spawn("ssh", [ "-F", config_file, value ]);
processes[value] = process;
process.stderr.on("data", (data) => {
const text = decoder.decode(data);
log(`[${value}] ${text}`);
});
process.stdout.on("data", (data) => {
const text = decoder.decode(data);
if(text === "SSHCONNECT") {
process.SSHCONNECT = true;
log(`[${value}] Connected`);
reload();
} else {
log(`[${value}] ${text}`);
}
});
process.on("close", (code) => {
log(`[${value}] Exit: ${code}`);
reload();
});
reload();
});
document.getElementById(`${value}-stop`).addEventListener("click", () => {
log(`[${value}] Stop`);
const process = processes[value];
console.log({ process })
if(process) {
process.kill();
setTimeout(() => {
reload();
}, 500)
}
});
}
}
}
// Get the status of any existing ssh process
//
function processStatus(process) {
if(!process) return null;
const { pid, exitCode, killed, SSHCONNECT } = process;
if(!pid) return null;
if(exitCode !== null || killed) return "disconnected";
if(SSHCONNECT) return "connected";
return "connecting";
}
// Log output to the #output div
//
function log(string) {
string = string.trim("\n");
console.log(string);
document.getElementById("output").innerText += `${string}\n`;
document.getElementById("output").scrollTop = 999999999;
}
// Edit the config file
//
async function edit() {
const OS = require("node:os");
const homedir = OS.homedir();
const Open = require("open");
await Open(`${homedir}/.ssh/tunnel`);
}