-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPunchmoleServer.js
229 lines (219 loc) · 10.4 KB
/
PunchmoleServer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import crypto from "node:crypto";
import express from "express";
import http from "node:http";
import {WebSocketServer} from "ws";
function generateRandomId() {
return crypto.randomBytes(16).toString("hex")
}
export async function PunchmoleServer(
port,
apiKeys,
endpointUrlPath = '/_punchmole',
log = console
) {
if(apiKeys.filter((v) => v !== "").length === 0) {
throw new Error('invalid api keys, please check apiKeys argument')
}
const app = express()
const server = http.createServer(app)
server.on('upgrade', (request, socket) => {
socket.headers = request.headers
socket.origin_url = request.url
})
const wss = new WebSocketServer({
server
})
const domainsToConnections = {}
let openRequests = []
let openWebsocketConnections = []
function getRequestObject(id) {
return openRequests.find((v) => v.id === id)
}
wss.on('error', log.error)
wss.on('connection', (socket) => {
log.info(new Date(), 'client connection open', socket._socket.headers, socket._socket.origin_url)
// if it's a punchmole client connection and not a foreign request connection
if(socket._socket.origin_url === endpointUrlPath) {
socket.on('close', () => {
log.info(new Date(), 'connection closed', socket.domain)
delete domainsToConnections[socket.domain]
})
socket.on('message', async (rawMessage) => {
const message = JSON.parse(rawMessage)
let request = null
if (message.id) {
request = getRequestObject(message.id)
}
switch (message.type) {
case 'register':
if (apiKeys.includes(message.apiKey)) {
log.info(new Date(), 'registering socket for domain', message)
domainsToConnections[message.domain] = {
status: 'alive',
socket: socket
}
socket.domain = message.domain
socket.send(JSON.stringify({type: 'registered', domain: message.domain}))
} else {
log.error(new Date(), 'given api key is wrong/not recognised, stopping connection', message)
await socket.send(JSON.stringify({type: 'error', message: 'invalid api key'}))
socket.close()
}
break
case 'response-start':
log.info(new Date(), 'response start, request id', message.id, message.headers)
if (request) {
request.responseObject.status(message.statusCode)
request.responseObject.statusMessage = message.statusMessage
request.responseObject.set(message.headers)
request.responseObject.on('close', () => {
log.info(new Date(), 'connection closed, stop sending data', message.id)
openRequests = openRequests.filter((v) => v.id !== message.id)
socket.send(JSON.stringify({type: 'request-end', id: message.id}))
})
} else {
log.error(new Date(), 'didnt found response object, probably dead?')
}
break
case 'data':
if (request) {
const data = Buffer.from(message.data, 'binary')
// log.debug(new Date(), 'writing response data to request', message.id, data.length)
try {
request.responseObject.write(data)
} catch(e) {
log.info(new Date(), 'error writing data to response object, request was probably aborted', message.id, e)
}
} else {
log.error(new Date(), 'didnt found response object, unable to send data', message.id)
}
break
case 'data-end':
log.info(new Date(), 'finishing sending data for request', message.id)
if (request) {
request.responseObject.end()
} else {
log.error(new Date(), 'didnt found response object, unable to send data')
}
break
case 'websocket-connection-closed':
if(openWebsocketConnections[message.id]) {
try {
openWebsocketConnections[message.id].close()
} catch(e) {
log.info(new Date(), 'error closing websocket connection, probably already closed', message.id, e)
}
}
break
case 'websocket-message':
const userSocket = openWebsocketConnections[message.id]
if(userSocket) {
log.debug(new Date(), 'sending websocket message received from proxied service to client', message.id)
userSocket.socket.send(message.rawData)
}
break
}
})
} else {
// this part handles incoming websocket connections from user requests and forwards them to the tunneled service
const requestedDomain = socket._socket.headers.host.match(/^(.*?)(:[0-9]{1,}|)$/)[1]
const foreignHost = domainsToConnections[requestedDomain]
if(!foreignHost) {
log.info(new Date(), 'received a websocket connection attempt for a domain not registered (yet), closing it', requestedDomain)
socket.close()
} else {
socket.connectionId = generateRandomId()
log.info(new Date(), 'received a websocket connection from a normal client and not a punchmole client, forwarding...', socket.connectionId, requestedDomain)
openWebsocketConnections[socket.connectionId] = {
date: new Date(),
id: socket.connectionId,
socket: socket
}
foreignHost.socket.send(JSON.stringify({
type: 'websocket-connection',
id: socket.connectionId,
headers: socket._socket.headers,
domain: requestedDomain,
url: socket._socket.origin_url
}))
socket.on('error', (error) => {
log.info(new Date(), 'got error from client websocket connection', socket.connectionId, error)
foreignHost.socket.send(JSON.stringify({
type: 'websocket-error',
id: socket.connectionId,
error: error
}))
})
socket.on('close', () => {
log.info(new Date(), 'client websocket closed', socket.connectionId)
foreignHost.socket.send(JSON.stringify({
type: 'websocket-connection-closed',
id: socket.connectionId,
}))
openWebsocketConnections[socket.connectionId].socket.close()
delete openWebsocketConnections[socket.connectionId]
})
socket.on('message', (rawData) => {
log.info(new Date(), 'received data from client websocket connection, forwarding...', socket.connectionId)
foreignHost.socket.send(JSON.stringify({
type: 'websocket-message',
id: socket.connectionId,
rawData: rawData
}))
})
}
}
})
app.use((req, res) => {
const requestedDomain = req.headers.host.match(/^(.*?)(:[0-9]{1,}|)$/)[1]
const foreignHost = domainsToConnections[requestedDomain]
log.debug(new Date(), 'request started for', requestedDomain, req.method, req.url)
if(foreignHost && foreignHost.status === 'alive') {
log.debug(new Date(), '-> found endpoint', req.url, req.headers, req.body)
const requestForward = {
type: "request-start",
date: new Date(),
domain: requestedDomain,
id: generateRandomId(),
method: req.method,
url: req.url,
headers: req.headers,
body: req.body,
}
log.debug(new Date(), '-> forward to remote client', JSON.stringify(requestForward))
openRequests.push({...requestForward, requestObject: req, responseObject: res})
foreignHost.socket.send(JSON.stringify(requestForward))
req.on('data', (data) => {
log.debug(new Date(), '--> request data received', requestForward.id, data.length)
foreignHost.socket.send(JSON.stringify({
type: 'request-data',
date: new Date(),
id: requestForward.id,
data: data.toString('binary')
}))
})
req.on('end', () => {
log.debug(new Date(), '--> request data reception ended', requestForward.id)
foreignHost.socket.send(JSON.stringify({
type: 'request-data-end',
date: new Date(),
id: requestForward.id
}))
})
} else {
res.status(503)
res.send("no registration for domain and/or remote service not available")
}
})
app.get('/', (req, res) => {
res.send('http server is running')
})
server.listen(port, () => {
log.info(new Date(), `server is listening on port ${port}`)
})
return {
app,
server,
wss
}
}