forked from robbie-cahill/tunnelmole-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket.ts
78 lines (63 loc) · 2.35 KB
/
websocket.ts
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
import { messageHandlers } from "./message-handlers";
import HostipWebSocket from "./src/websocket/host-ip-websocket";
import log from "./src/logging/log";
import moment from "moment";
import Proxy from "./src/proxy";
import { IncomingMessage } from "http";
import config from "./config";
const inArray = require("in_array");
/**
* Callback to initialise new websocket connections
*/
export default function websocket(
websocket: HostipWebSocket,
request: IncomingMessage,
) {
websocket.isAlive = true;
websocket.connectionStart = moment().unix();
//@ts-ignore
websocket.ipAddress = request.headers["x-forwarded-for"] ?? "127.0.0.1";
// Hack: Punch in HostipWebsocket sendMessage
websocket.sendMessage = function sendMessage(object: any) {
const json = JSON.stringify(object);
websocket.send(json);
};
/**
* Find the handler for a message and run it
**/
websocket.on("message", (text: string) => {
const message = JSON.parse(text);
if (typeof message.type !== "string") {
console.error("Invalid message, type is missing or invalid");
return;
}
// Skip any messages that are handled dynamically using other explicitly defined 'message' callbacks
// Example: forwardedResponse handler in handleRequest that is set dynamically for every request
const dynamicallyHandledMessageTypes = ["forwardedResponse"];
if (inArray(message.type, dynamicallyHandledMessageTypes)) {
return;
}
if (typeof messageHandlers[message.type] !== "function") {
console.error("Handler not found for message type " + message.type);
return;
}
const handler = messageHandlers[message.type];
handler(message, websocket);
});
// Log messages if debug is enabled
if (config.runtime.debug)
websocket.on("message", (text: string) => {
const message = JSON.parse(text);
log(Date.now() + " Received " + message.type + " message:", "info");
log(message, "info");
});
websocket.on("error", (code: number, reason: string) => {
console.info("Caught an error. Error code: " + code + " Reason: " + reason);
});
websocket.on("close", (code: number, reason: string) => {
websocket.terminate();
const proxy = Proxy.getInstance();
proxy.deleteConnection(websocket.tunnelmoleClientId);
console.info("Connection Closed. Code: " + code + " Reason: " + reason);
});
}