-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPunchmoleClient.js
211 lines (206 loc) · 10.1 KB
/
PunchmoleClient.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { EventEmitter } from 'node:events'
import { WebSocket } from 'ws'
import http from 'node:http'
export function PunchmoleClient(apiKey, domain, targetUrl, endpointUrl, log = console) {
const eventEmitter = new EventEmitter()
const ws = new WebSocket(endpointUrl)
ws.on('open', () => {
log.info(new Date(), 'connection with upstream server opened to forward url', targetUrl)
ws.send(JSON.stringify({'type': 'register', 'domain': domain, 'apiKey': apiKey}))
ws.isAlive = true
})
const interval = setInterval(() => {
ws.ping()
}, 10000)
ws.on('ping', () => {
ws.pong()
})
ws.on('pong', () => {
})
ws.on('close', () => {
log.info(new Date(), 'connection with upstream server closed')
clearInterval(interval)
eventEmitter.emit('close')
})
ws.on('error', (err) => {
log.error(new Date(), 'websocket error', err)
eventEmitter.emit('error', err)
})
const controllers = {}
const requests = {}
const websocketConnections = {}
// opening a new websocket may take some time and the remote server has already sent the first message
// for that purpose we push messages to a buffer (in order until the buffer is empty) which is sent then
// automatically once the websocket connection for one connection id (request id) is open,
let websocketMessageBuffer = []
// run through all buffered websocket messages in 30ms intervals and check if they're sendable
setInterval(() => {
for(let message of websocketMessageBuffer) {
if(
websocketConnections[message.request.id] &&
websocketConnections[message.request.id].readyState === WebSocket.OPEN
) {
log.info('sending buffered message to finally opened websocket connection', message)
websocketConnections[message.request.id].send(message.request.rawData)
websocketMessageBuffer = websocketMessageBuffer.filter((v) => v.request.id !== message.request.id)
}
}
}, 30)
ws.on('message', async (rawMessage) => {
const request = JSON.parse(rawMessage)
let targetRequest = requests[request.id]
switch(request.type) {
case 'error':
log.error(new Date(), 'error received from upstream server', request)
eventEmitter.emit('error', request)
break
case 'registered':
log.info(new Date(), 'registration successful', request)
eventEmitter.emit('registered', request)
break
case 'request-end':
// if the foreign host stops the connection we need to stop streaming data (if we're streaming...)
log.debug(new Date(), 'request end received, stopping data if there is any', request.id)
// cleaning up status controllers
if(controllers[request.id]) {
controllers[request.id].controller.abort()
delete controllers[request.id].controller
delete controllers[request.id]
}
// cleaning up requests
if(requests[request.id]) {
requests[request.id].request.destroy()
delete requests[request.id]
}
eventEmitter.emit('request-end', request)
break
case 'request-start':
controllers[request.id] = {date: new Date(), controller: new AbortController()}
const url = targetUrl + request.url
log.info(new Date(), 'received request', JSON.stringify(request))
log.debug(new Date(), 'forwarding url', JSON.stringify(url))
requests[request.id] = {
responseStarted: false,
request: http.request(url, {
method: request.method,
headers: request.headers,
signal: controllers[request.id].controller.signal
}, (response) => {
requests[request.id].responseStarted = true
ws.send(JSON.stringify({
type: 'response-start',
id: request.id,
statusCode: response.statusCode,
statusMessage: response.statusMessage,
headers: response.headers
}))
response.on('data', data => {
ws.send(JSON.stringify({
type: 'data',
id: request.id,
data: data.toString('binary')
}))
})
response.on('end', async () => {
await ws.send(JSON.stringify({
type: 'data-end',
id: request.id
}))
})
controllers[request.id].controller.signal.addEventListener('abort', () => {
log.info(new Date(), 'ending stream, remote client closed connection', request.id)
response.destroy()
}, { once: true })
})
}
requests[request.id].request.on('error', (err) => {
log.error(new Date(), 'request error', request.id, err);
if(!requests[request.id] || requests[request.id].responseStarted === false) {
ws.send(JSON.stringify({
type: 'response-start',
id: request.id,
statusCode: 503,
statusMessage: 'Service Unavailable',
headers: {}
}))
ws.send(JSON.stringify({
type: 'data',
id: request.id,
data: err.toString('binary')
}))
}
ws.send(JSON.stringify({
type: 'data-end',
id: request.id
}))
if(requests[request.id]) {
requests[request.id].request.destroy()
delete requests[request.id]
}
})
eventEmitter.emit('request', request)
break
case 'request-data':
if(targetRequest && targetRequest.request) {
log.debug(new Date(), 'request data has been received forwarding to remote service', request.id, Buffer.from(request.data, 'binary').length)
targetRequest.request.write(Buffer.from(request.data, 'binary'))
}
break
case 'request-data-end':
if(targetRequest && targetRequest.request) {
log.debug(new Date(), 'request has been finished, ending request to remote service', request.id)
targetRequest.request.end()
}
break
case 'websocket-connection':
log.info(new Date(), 'new websocket connection requested', request, targetUrl)
const websocketConnectionUrl = targetUrl.replace(/^http/, 'ws') + request.url
log.debug(new Date(), '-> opening websocket connection...', websocketConnectionUrl)
websocketConnections[request.id] = new WebSocket(websocketConnectionUrl)
websocketConnections[request.id].on('message', (rawData) => {
ws.send(JSON.stringify({
type: 'websocket-message',
id: request.id,
rawData: rawData
}))
})
websocketConnections[request.id].on('close', () => {
log.info(new Date(), 'service side closed websocket connection, sending forward and cleaning up...', request.id)
ws.send(JSON.stringify({
type: 'websocket-connection-closed',
id: request.id
}))
delete websocketConnections[request.id]
})
websocketConnections[request.id].on('error', (error) => {
log.warn(new Date(), 'websocket connected to service got an error', error)
})
break
case 'websocket-connection-closed':
if(websocketConnections[request.id]) {
log.debug(new Date(), 'remote client closed connection to websocket, closing websocket connection', request.id)
websocketConnections[request.id].close()
}
break
case 'websocket-message':
if(
websocketConnections[request.id] &&
websocketConnections[request.id].readyState === WebSocket.OPEN &&
// check if the websocket message buffer has messages for the given connection (request id) and if it
// has enqueue new messages rather then sending it directly to ensure the order is given
// (some protocols require separate authentication and/or handshake procedures which keep account for here)
!websocketMessageBuffer.find((v) => v.request.id === request.id)
) {
log.debug(new Date(), 'websocket message received and ready to send forward', request.id)
websocketConnections[request.id].send(request.rawData)
} else {
log.debug(new Date(), 'websocket message received but not ready to send forward yet, buffering...', request.id)
websocketMessageBuffer.push({
date: new Date(),
request: request,
})
}
}
})
return eventEmitter
}