forked from redpencilio/mu-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
350 lines (317 loc) · 10.9 KB
/
app.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// see https://github.com/mu-semtech/mu-javascript-template for more info
import { app, uuid } from 'mu';
import * as express from "express";
import fs from 'fs';
import * as pgp from 'openpgp';
import { Buffer } from 'buffer';
import * as http from "http";
import * as https from "https";
import url from 'url';
// load config
import * as config from "/config/config.json";
// Environment
const TUNNEL_LOG_INBOUND = process.env.TUNNEL_LOG_INBOUND == "true" || false;
const TUNNEL_LOG_OUTBOUND = process.env.TUNNEL_LOG_OUTBOUND == "true" || false;
const DISABLE_HTTPS = process.env.DISABLE_HTTPS || false;
const PGP_COMPRESSION_LEVEL = process.env.PGP_COMPRESSION_LEVEL || 0;
console.log(process.version);
console.log(config);
checkConfig()
.catch((err) => {
console.error(err);
process.exit(1);
});
// Enable compression
pgp.config.compressionLevel = PGP_COMPRESSION_LEVEL;
pgp.config.preferredCompressionAlgorithm = pgp.enums.compression.zlib;
// Endpoint for INBOUND messages, e.g. another tunnel service relaying a remote message
app.use("/secure", express.text({ limit: "1000mb" }));
app.use("/secure", express.raw());
app.post('/secure', async (req, res) => {
// Read the PGP message
let message;
try {
switch (req.get('Content-Type')) {
case "text/plain":
message = await pgp.readMessage({ armoredMessage: req.body });
break;
case "application/octet-stream":
message = await pgp.readMessage({ binaryMessage: req.body });
break;
default:
throw `Received message with Content-Type ${req.get('Content-Type')}`;
}
}
catch (err) {
console.error("Bad PGP message.", err);
res.status(400).send("Bad PGP message.");
throw err;
}
// Decrypt the PGP message
const { data: payloadstring, signatures } = await pgp.decrypt({
message,
verificationKeys: config.peer.key,
decryptionKeys: config.self.key
});
// Verify the signature
let sigKeyID;
try {
if (await signatures[0].verified) {
sigKeyID = signatures[0].keyID;
}
else {
throw `Could not verify signature by key ${signatures[0].keyID}`;
}
}
catch (err) {
console.error("Unable to verify signature.", err);
res.status(401).send("Cannot verify key.");
throw err;
}
// Connect the verified signature to a known peer, only one in this case
const peer = config.peer;
// Parse the decrypted message
let payload;
try {
payload = JSON.parse(payloadstring);
}
catch (err) {
console.error(`Could not parse decrypted payload: ${payloadstring}`, err);
res.status(400).send("Unparseable plaintext.");
throw err;
}
if (TUNNEL_LOG_INBOUND) {
console.log(`Received message from ${peer.identity}: ${JSON.stringify(payload, undefined, 2)}`);
}
else {
console.log(`Received message from ${peer.identity}.`);
}
// Set some headers
let headers = payload.headers || {};
headers['mu-call-id'] = uuid();
if (payload.headers && payload.headers['mu-call-id']) {
headers['mu-call-id-trail'] = [...(payload.headers['mu-call-id-trail'] || []), payload.headers['mu-call-id']];
}
headers['mu-tunnel-identity'] = peer.identity;
// Forward the request and construct a response object.
let resobj;
try {
const body = payload.body ? Buffer.from(payload.body, 'base64') : undefined;
const forwardUrl = (new url.URL(payload.path, config.self.stackentry)).toString();
if (TUNNEL_LOG_OUTBOUND)
console.log("Will forward request to ", forwardUrl);
let forwardres = await httpPromise(forwardUrl, headers, payload.method, body);
resobj = await new Promise((resolve, reject) => {
let chunks = [];
forwardres.on('data', chunk => chunks.push(chunk));
forwardres.on('end', () => resolve({
headers: forwardres.headers,
status: forwardres.statusCode,
body: Buffer.concat(chunks).toString('base64')}));
forwardres.on('error', err => reject(err));
});
}
catch (err) {
console.error(`Error while forwarding request: ${err}`);
res.status(502);
throw err;
}
// Encrypt the response object
const encrypted = await pgp.encrypt({
message: await pgp.createMessage({ text: JSON.stringify(resobj) }),
encryptionKeys: peer.key,
signingKeys: config.self.key,
format: 'armored'
});
// Send the response
res.set('Content-Type', 'text/plain')
.status(200)
.send(encrypted);
if (TUNNEL_LOG_OUTBOUND)
console.log(`Succesfully responded to message from ${peer.identity}.`);
});
// Endpoint for OUTBOUND messages, e.g. internal services that want to contact another stack.
app.use("/*", express.raw({ type: "*/*", limit: "1000mb" }));
app.all('/*', async (req, res) => {
//This URL contains the path that needs to be re-sent on the other tunnel end.
const originalPath = req.originalUrl;
if (TUNNEL_LOG_OUTBOUND) {
console.log(`Received message: ${JSON.stringify(req.body, undefined, 2)}`);
}
else {
console.log(`Received message.`);
}
const peer = config.peer;
if(!peer.address) {
console.error(`No address for peer ${peer.identity}.`);
res.status(400).send("No peer address. Does not know how to reach the other tunnel endpoint. Check the tunnel config.json.");
return;
}
//"Boolean" (truthy, falsy) for if the body is empty
const emptyBody = (Object.keys(req.body).length === 0);
// Create an encapsulated JSON object with an accurate representation of the incoming HTTP request to send forward
const requestObj = {
method: req.method,
path: originalPath,
body: Buffer.from((emptyBody ? "" : req.body)).toString("base64"),
headers: req.headers
};
// Encrypt the message
let encrypted;
try {
encrypted = await pgp.encrypt({
message: await pgp.createMessage({ text: JSON.stringify(requestObj) }),
encryptionKeys: peer.key,
signingKeys: config.self.key,
format: 'armored'
});
}
catch (err) {
console.error(`Could not encrypt message: ${err}`);
res.status(500).send("Failed to encrypt.");
throw err;
}
// Send the encrypted message and read the response
let message;
try {
let peerres = await httpPromise(peer.address, { 'Content-Type' : 'text/plain' }, 'POST', encrypted);
message = await new Promise((resolve, reject) => {
let chunks = [];
peerres.on('data', chunk => chunks.push(chunk));
peerres.on('end', () => resolve(Buffer.concat(chunks).toString()));
peerres.on('error', err => reject(err));
});
}
catch (err) {
console.error(`Error while forwarding request: ${err}`);
res.status(502).send("Forwarding failed.");
throw err;
}
// Decrypt the PGP response
let payloadstring;
try {
({ data: payloadstring } = await pgp.decrypt({
message: await pgp.readMessage({ armoredMessage: message }),
verificationKeys: peer.key, // We know the peer, so we know which key to expect.
decryptionKeys: config.self.key,
expectSigned: true
}));
}
catch (err) {
console.error(`Failed to decrypt response: ${err}`);
res.status(502).send("Failed to decrypt.");
throw err;
}
// Parse the response
let payload;
try {
payload = JSON.parse(payloadstring);
}
catch (err) {
console.error(`Failed to parse payload: ${err}`, payload);
res.status(502).send("Unparseable payload.");
throw err;
}
// Forward the response to the requester.
for (let header in payload.headers) {
res.set(header, payload.headers[header]);
}
res.status(payload.status)
.send(Buffer.from(payload.body, 'base64'));
if (TUNNEL_LOG_OUTBOUND)
console.log(`Succesfully handled request to ${peer.identity}.`);
});
// Wrap http(s).request in a promise
function httpPromise(addr, headers, method, body) {
//It seems hacky to select the library based on the protocol string, maybe later use an external library like `fetch`
return new Promise((resolve, reject) => {
let addrUrl = new url.URL(addr);
if (TUNNEL_LOG_OUTBOUND)
console.log(`Sending a request to ${method}: ${addrUrl}`);
let http_s;
if (addrUrl.protocol === "https:") {
if (DISABLE_HTTPS) {
//Not so good, attempting to fix a weird situation
addrUrl = addrUrl.toString().replace(/^https/, "http");
if (TUNNEL_LOG_OUTBOUND)
console.log(`HTTPS disabled, sending a request instead to ${method}: ${addrUrl}`);
http_s = http;
}
else
http_s = https;
}
else
http_s = http;
const req = http_s.request(addrUrl, { headers, method }, forwardres => resolve(forwardres));
req.on('error', err => reject(err));
if (body) {
req.write(body)
}
req.end();
if (TUNNEL_LOG_OUTBOUND)
console.log("Request sent");
});
}
async function loadKeys() {
try {
let peer = config.peer;
const peerkey = await pgp.readKey({ armoredKey: await readKeyFile(peer.keyfile) });
await checkKey(peerkey, peer.identity); // Kind of hacky to only have a function that throws exceptions, but its the simplest
peer.key = peerkey;
console.log(`Loaded peer public key ${peerkey.getKeyID().toHex()} (${peer.identity}) from ${peer.keyfile}.`);
const self = config.self;
const key = await pgp.decryptKey({
privateKey: await pgp.readPrivateKey({ armoredKey: await readKeyFile(self.keyfile) }),
passphrase: config.self.passphrase
});
await checkKey(key, self.identity);
config.self.key = key;
console.log(`Loaded self secret key ${key.getKeyID().toHex()} (${self.identity}) from ${self.keyfile}.`);
}
catch (err) {
console.error("Exception during key loading, aborting.", err)
}
}
// Check expiry and that the key identity matches.
async function checkKey(key, identity) {
const expire = await key.getExpirationTime();
const keyid = key.getKeyID().toHex();
const user = (await key.getPrimaryUser()).user.userID.userID;
if (Date() > expire) {
throw `Key ${keyid} (${user}) expired on ${expire}.`;
}
else if (expire === Infinity) {
console.warn(`Key ${keyid} (${user}) has no expiry.`);
}
if (user !== identity) {
throw `Key ${keyid} (${user}) does not match identity ${identity}.`;
}
}
async function readKeyFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(`/config/keys/${file}`, { encoding: "utf-8" }, (err, data) => {
if (err)
reject(err);
else
resolve(data);
});
});
}
async function checkConfig() {
if (!config) throw new Error("No config found");
const selfConfigMissing = (!config.self
|| !config.self.identity
|| !config.self.keyfile
|| !config.self.passphrase
|| !config.self.stackentry);
const peerConfigMissing = (!config.peer
|| !config.peer.identity
|| !config.peer.keyfile);
if (selfConfigMissing || peerConfigMissing) {
throw new Error("Config incomplete");
}
else {
// Load PGP keys
await loadKeys();
}
}