-
Notifications
You must be signed in to change notification settings - Fork 41
/
websocket.ts
95 lines (80 loc) · 3.23 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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';
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) {
try {
const json = JSON.stringify(object);
websocket.send(json);
} catch (error) {
console.error("Caught error when sending a websocket message to the client");
console.error(error);
}
}
/**
* Find the handler for a message and run it
**/
websocket.on('message', (text : string) => {
try {
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);
} catch (error) {
console.error("Caught error when processing websocket message");
console.error(error);
}
});
// Log messages if debug is enabled
websocket.on('message', (text: string) => {
try {
const message = JSON.parse(text);
log(Date.now() + " Received " + message.type + " message:", "info");
log(message, 'info');
} catch (error) {
console.error("Caught error when logging websocket message for debug mode");
console.error(error);
}
});
websocket.on('error', (code: number, reason: string) => {
console.info("Caught an error. Error code: " + code + " Reason: " + reason);
});
websocket.on('close', (code: number, reason: string) => {
try {
websocket.terminate();
const proxy = Proxy.getInstance();
proxy.deleteConnection(websocket.tunnelmoleClientId);
console.info("Connection Closed. Code: " + code + " Reason: " + reason);
} catch (error) {
console.error("Caught error when closing websocket connection");
console.error(error);
}
});
}