-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproxy.js
47 lines (38 loc) · 1.39 KB
/
proxy.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
'use strict';
const { Socket } = require("net");
const { write, listen, shutdown, connect, close } = require("./proxyfunctions");
// handles only a single client
// client -> server (Proxy) -> remote (QuestDB)
// client <- server (Proxy) <- remote (QuestDB)
class Proxy {
constructor() {
this.remote = new Socket();
this.remote.on("data", async data => {
console.info(`received from remote, forwarding to client: ${data}`);
await write(this.client, data);
});
this.remote.on("close", () => {
console.info("remote connection closed");
});
this.remote.on("error", err => {
console.error(`remote connection: ${err}`);
});
}
async start(listenPort, remotePort, remoteHost, tlsOptions = undefined) {
return new Promise(resolve => {
this.remote.on("ready", async () => {
console.info("remote connection ready");
await listen(this, listenPort, async data => {
console.info(`received from client, forwarding to remote: ${data}`);
await write(this.remote, data);
}, tlsOptions);
resolve();
});
connect(this, remotePort, remoteHost);
});
}
async stop() {
await shutdown(this, async () => await close(this));
}
}
exports.Proxy = Proxy;