Skip to content

Commit 693e612

Browse files
committed
main.ts: Proxy VNC connections to support HTTPS and prevent viewing other users' virutal displays
1 parent 1a37284 commit 693e612

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

main.ts

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,39 @@ async function handler(request: Request): Promise<Response> {
153153

154154
async function proxy(request: Request, token: string): Promise<Response> {
155155
const editor = tokenToEditor[token];
156+
let shutdown = async function() {
157+
console.log("Shutting down port " + editor.port + " VSCode instance");
158+
delete tokenToEditor[token];
159+
editor.process.kill("SIGHUP");
160+
await editor.process.status();
161+
nextDisplay.push(editor.display);
162+
};
163+
let decrRefCount = function() {
164+
--editor.refCount;
165+
if(editor.refCount == 0) {
166+
console.log("Last client closed for port " + editor.port + " VSCode instance");
167+
editor.shutdownHandle = setTimeout(shutdown, SHUTDOWN_TIMEOUT_S * 1000);
168+
}
169+
};
170+
let incrRefCount = function() {
171+
if(editor.refCount == 0 && editor.shutdownHandle) {
172+
console.log("Client reopened for port " + editor.port + " VSCode instance");
173+
clearTimeout(editor.shutdownHandle);
174+
delete editor.shutdownHandle;
175+
}
176+
++editor.refCount;
177+
};
178+
156179
const url = new URL(request.url);
157180
url.hostname = "localhost";
158-
url.port = String(editor.port);
181+
if(url.pathname.startsWith("/vnc/") || url.pathname == "/websockify") {
182+
url.port = String(editor.displayPort);
183+
url.pathname = url.pathname.replace("/vnc/", "/");
184+
shutdown = nop;
185+
decrRefCount = nop;
186+
incrRefCount = nop;
187+
} else
188+
url.port = String(editor.port);
159189

160190
if(request.headers.get("upgrade") != "websocket") {
161191
let response = await fetch(new Request(String(url), request), {redirect: "manual"});
@@ -166,9 +196,7 @@ async function proxy(request: Request, token: string): Promise<Response> {
166196
+ "location.search.startsWith(\"?folder=\")"
167197
+ "|| location.search.startsWith(\"?workspace=\")"
168198
+ ")"
169-
+ "\nopen(\"http://\" + location.hostname + \":"
170-
+ editor.displayPort
171-
+ "/vnc_lite.html\", \"_blank\", \"popup\");"
199+
+ "\nopen(\"vnc/vnc_lite.html\", \"_blank\", \"popup\");"
172200
,
173201
response,
174202
);
@@ -178,28 +206,6 @@ async function proxy(request: Request, token: string): Promise<Response> {
178206

179207
const serverSocket = new WebSocket(String(url));
180208
const {response, socket} = Deno.upgradeWebSocket(request);
181-
const shutdown = async function() {
182-
console.log("Shutting down port " + editor.port + " VSCode instance");
183-
delete tokenToEditor[token];
184-
editor.process.kill("SIGHUP");
185-
await editor.process.status();
186-
nextDisplay.push(editor.display);
187-
};
188-
const decrRefCount = function() {
189-
--editor.refCount;
190-
if(editor.refCount == 0) {
191-
console.log("Last client closed for port " + editor.port + " VSCode instance");
192-
editor.shutdownHandle = setTimeout(shutdown, SHUTDOWN_TIMEOUT_S * 1000);
193-
}
194-
};
195-
const incrRefCount = function() {
196-
if(editor.refCount == 0 && editor.shutdownHandle) {
197-
console.log("Client reopened for port " + editor.port + " VSCode instance");
198-
clearTimeout(editor.shutdownHandle);
199-
delete editor.shutdownHandle;
200-
}
201-
++editor.refCount;
202-
};
203209

204210
const serverBootstrap = deferred();
205211
serverSocket.onopen = function() {
@@ -259,6 +265,10 @@ async function parsePortsAndToken(
259265
};
260266
}
261267

268+
function nop(): Promise<void> {
269+
return Promise.resolve();
270+
}
271+
262272
const address: Partial<Deno.ListenOptions> = {
263273
hostname: "localhost",
264274
};

0 commit comments

Comments
 (0)