2021-01-13 02:53:12 +08:00
|
|
|
import { field, logger } from "@coder/logger"
|
2021-03-16 05:15:24 +08:00
|
|
|
import { promises as fs } from "fs"
|
2020-02-05 03:27:46 +08:00
|
|
|
import * as net from "net"
|
|
|
|
import * as path from "path"
|
|
|
|
import * as tls from "tls"
|
2021-03-10 07:32:31 +08:00
|
|
|
import { Emitter } from "../../src/common/emitter"
|
2021-04-22 04:22:11 +08:00
|
|
|
import { tmpdir } from "../../src/node/constants"
|
2021-03-10 07:32:31 +08:00
|
|
|
import { SocketProxyProvider } from "../../src/node/socket"
|
2021-04-22 04:22:11 +08:00
|
|
|
import { generateCertificate } from "../../src/node/util"
|
2020-02-05 03:27:46 +08:00
|
|
|
|
|
|
|
describe("SocketProxyProvider", () => {
|
|
|
|
const provider = new SocketProxyProvider()
|
|
|
|
|
|
|
|
const onServerError = new Emitter<{ event: string; error: Error }>()
|
|
|
|
const onClientError = new Emitter<{ event: string; error: Error }>()
|
|
|
|
const onProxyError = new Emitter<{ event: string; error: Error }>()
|
2021-01-26 01:34:51 +08:00
|
|
|
const fromServerToClient = new Emitter<Buffer>()
|
|
|
|
const fromClientToServer = new Emitter<Buffer>()
|
2020-02-05 03:27:46 +08:00
|
|
|
const fromClientToProxy = new Emitter<Buffer>()
|
|
|
|
|
|
|
|
let errors = 0
|
|
|
|
let close = false
|
|
|
|
const onError = ({ event, error }: { event: string; error: Error }): void => {
|
|
|
|
if (!close || event === "error") {
|
|
|
|
logger.error(event, field("error", error.message))
|
|
|
|
++errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onServerError.event(onError)
|
|
|
|
onClientError.event(onError)
|
|
|
|
onProxyError.event(onError)
|
|
|
|
|
|
|
|
let server: tls.TLSSocket
|
|
|
|
let proxy: net.Socket
|
|
|
|
let client: tls.TLSSocket
|
|
|
|
|
|
|
|
const getData = <T>(emitter: Emitter<T>): Promise<T> => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const d = emitter.event((t) => {
|
|
|
|
d.dispose()
|
|
|
|
resolve(t)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-09 04:55:47 +08:00
|
|
|
beforeAll(async () => {
|
2020-10-30 17:26:40 +08:00
|
|
|
const cert = await generateCertificate("localhost")
|
2020-02-05 03:27:46 +08:00
|
|
|
const options = {
|
2021-03-16 05:15:24 +08:00
|
|
|
cert: await fs.readFile(cert.cert),
|
|
|
|
key: await fs.readFile(cert.certKey),
|
2020-02-05 03:27:46 +08:00
|
|
|
rejectUnauthorized: false,
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:15:24 +08:00
|
|
|
await fs.mkdir(path.join(tmpdir, "tests"), { recursive: true })
|
2020-02-05 03:27:46 +08:00
|
|
|
const socketPath = await provider.findFreeSocketPath(path.join(tmpdir, "tests/tls-socket-proxy"))
|
2021-03-16 05:15:24 +08:00
|
|
|
await fs.rmdir(socketPath, { recursive: true })
|
2020-02-05 03:27:46 +08:00
|
|
|
|
2021-01-21 03:59:12 +08:00
|
|
|
return new Promise<void>((_resolve) => {
|
2020-02-05 03:27:46 +08:00
|
|
|
const resolved: { [key: string]: boolean } = { client: false, server: false }
|
|
|
|
const resolve = (type: "client" | "server"): void => {
|
|
|
|
resolved[type] = true
|
|
|
|
if (resolved.client && resolved.server) {
|
|
|
|
// We don't need any more connections.
|
|
|
|
main.close() // eslint-disable-line @typescript-eslint/no-use-before-define
|
|
|
|
_resolve()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const main = tls
|
|
|
|
.createServer(options, (s) => {
|
|
|
|
server = s
|
|
|
|
server
|
|
|
|
.on("data", (d) => fromClientToServer.emit(d))
|
|
|
|
.on("error", (error) => onServerError.emit({ event: "error", error }))
|
|
|
|
.on("end", () => onServerError.emit({ event: "end", error: new Error("unexpected end") }))
|
|
|
|
.on("close", () => onServerError.emit({ event: "close", error: new Error("unexpected close") }))
|
|
|
|
resolve("server")
|
|
|
|
})
|
|
|
|
.on("error", (error) => onServerError.emit({ event: "error", error }))
|
|
|
|
.on("end", () => onServerError.emit({ event: "end", error: new Error("unexpected end") }))
|
|
|
|
.on("close", () => onServerError.emit({ event: "close", error: new Error("unexpected close") }))
|
|
|
|
.listen(socketPath, () => {
|
|
|
|
client = tls
|
|
|
|
.connect({ ...options, path: socketPath })
|
|
|
|
.on("data", (d) => fromServerToClient.emit(d))
|
|
|
|
.on("error", (error) => onClientError.emit({ event: "error", error }))
|
|
|
|
.on("end", () => onClientError.emit({ event: "end", error: new Error("unexpected end") }))
|
|
|
|
.on("close", () => onClientError.emit({ event: "close", error: new Error("unexpected close") }))
|
|
|
|
.once("connect", () => resolve("client"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should work without a proxy", async () => {
|
|
|
|
server.write("server->client")
|
2021-01-26 01:34:51 +08:00
|
|
|
const dataFromServerToClient = (await getData(fromServerToClient)).toString()
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(dataFromServerToClient).toBe("server->client")
|
2020-02-05 03:27:46 +08:00
|
|
|
client.write("client->server")
|
2021-01-26 01:34:51 +08:00
|
|
|
const dataFromClientToServer = (await getData(fromClientToServer)).toString()
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(dataFromClientToServer).toBe("client->server")
|
|
|
|
expect(errors).toEqual(0)
|
2020-02-05 03:27:46 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should work with a proxy", async () => {
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(server instanceof tls.TLSSocket).toBe(true)
|
2020-02-05 03:27:46 +08:00
|
|
|
proxy = (await provider.createProxy(server))
|
|
|
|
.on("data", (d) => fromClientToProxy.emit(d))
|
|
|
|
.on("error", (error) => onProxyError.emit({ event: "error", error }))
|
|
|
|
.on("end", () => onProxyError.emit({ event: "end", error: new Error("unexpected end") }))
|
|
|
|
.on("close", () => onProxyError.emit({ event: "close", error: new Error("unexpected close") }))
|
|
|
|
|
|
|
|
provider.stop() // We don't need more proxies.
|
|
|
|
|
|
|
|
proxy.write("server proxy->client")
|
2021-03-16 04:31:19 +08:00
|
|
|
const dataFromServerToClient = (await getData(fromServerToClient)).toString()
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(dataFromServerToClient).toBe("server proxy->client")
|
2020-02-05 03:27:46 +08:00
|
|
|
client.write("client->server proxy")
|
2021-03-16 04:31:19 +08:00
|
|
|
const dataFromClientToProxy = (await getData(fromClientToProxy)).toString()
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(dataFromClientToProxy).toBe("client->server proxy")
|
|
|
|
expect(errors).toEqual(0)
|
2020-02-05 03:27:46 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should close", async () => {
|
|
|
|
close = true
|
|
|
|
client.end()
|
|
|
|
proxy.end()
|
|
|
|
})
|
|
|
|
})
|