Archived
1
0
This repository has been archived on 2024-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
code-server/packages/tunnel/src/server.ts
Kyle Carberry 85d2225e0c Featureful (#31)
* Fix loading within the CLI

* Remove app

* Remove promise handle

* Add initial travis file

* Add libxkbfile dependency

* Add libxkbfile-dev

* Add build script

* Fix malformed bash statement

* Remove yarn from script

* Improve build script

* Extract upx before usage

* Only run upx if on linux

* Ensure resource directory exists

* Pack runnable binary

* Export binary with platform

* Improve build process

* Install upx before running install script

* Update typescript version before running nexe

* Add os.release() function for multi-platform support

* Update travis.yml to improve deployment

* Add on CI

* Update to v1.31.0

* Add libsecret

* Update build target

* Skip cleanup

* Fix built-in extensions

* Add basics for apps

* Create custom DNS server

* Fix forking within CLI. Fixes TS language features

* Fix filename resolve

* Fix default extensions path

* Add custom dialog

* Store workspace path

* Remove outfiles

* Cleanup

* Always authed outside of CLI

* Use location.host for client

* Remove useless app interface

* Remove debug file for building wordlist

* Use chromes tcp host

* Update patch

* Build browser app before packaging

* Replace all css containing file:// URLs, fix webviews

* Fix save

* Fix mkdir
2019-02-21 11:55:42 -06:00

54 lines
1.5 KiB
TypeScript

import * as net from "net";
import { TunnelCloseCode } from "./common";
export interface WS {
addEventListener(event: "message", cb: (event: {
// tslint:disable-next-line:no-any
readonly data: any;
}) => void): void;
addEventListener(event: "close", cb: () => void): void;
binaryType: string;
close(code: number, reason?: string): void;
// tslint:disable-next-line:no-any
send(data: any): void;
}
export const handle = async (socket: WS, port: number): Promise<void> => {
const hosts = [
"127.0.0.1",
"::", // localhost
];
let localSocket: net.Socket | undefined;
for (let i = 0; i < hosts.length; i++) {
if (localSocket) {
break;
}
localSocket = await new Promise((resolve, reject): void => {
const socket = net.connect({
host: hosts[i],
port,
}, () => {
// Connected
resolve(socket);
});
socket.on("error", (err: Error & { readonly code: string }) => {
if (err.code === "ECONNREFUSED") {
resolve(undefined);
}
});
});
}
if (!localSocket) {
socket.close(TunnelCloseCode.ConnectionRefused);
return;
}
socket.binaryType = "arraybuffer";
socket.addEventListener("message", (event) => localSocket!.write(Buffer.from(event.data)));
socket.addEventListener("close", () => localSocket!.end());
localSocket.on("data", (data) => socket.send(data));
localSocket.on("error", (err) => socket.close(TunnelCloseCode.Error, err.toString()));
localSocket.on("close", () => socket.close(TunnelCloseCode.Normal));
};