This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinspect-message
executable file
·89 lines (76 loc) · 3.02 KB
/
inspect-message
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
#!/usr/bin/env node
'use strict';
// This is a proof of concept. The code below is ugly, inefficient and has no tests.
const assert = require('assert').strict;
const fs = require('fs');
const yargs = require('yargs');
const {CARGO_SERIALIZER, PARCEL_SERIALIZER} = require('../core/serialization');
const {Cargo} = require('../core/messages');
const {derCertToPem} = require('../_asn1_utils');
const {deserializeCargoPayload, deserializeServiceMessage} = require('../core/serialization');
const {getAddressFromCert} = require('../core/pki');
const SERIALIZER_BY_TAG = {
C: CARGO_SERIALIZER,
P: PARCEL_SERIALIZER,
};
async function main(argv) {
const messageRaw = fs.readFileSync(fs.openSync('/dev/stdin', 'rs'));
const fileSignature = messageRaw.slice(0, 9).toString();
assert(fileSignature.startsWith('Relaynet'), 'Message must be a Relaynet message');
const fileTag = fileSignature[8];
const serializer = SERIALIZER_BY_TAG[fileTag];
assert(serializer, `Message tag must be either parcel or cargo, not "${fileTag}"`);
const message = await serializer.deserialize(messageRaw);
console.log(`Message type: ${message.constructor.name}`);
console.log(formatMessageInfo(message));
if (argv.recipientKey) {
const payloadDecrypted = await message.decryptPayload(argv.recipientKey);
if (message instanceof Cargo) {
console.log(' Decrypted:');
const parcelsSerialized = deserializeCargoPayload(payloadDecrypted);
for (let parcelSerialized of parcelsSerialized) {
console.log(` Parcel:`);
const parcel = await PARCEL_SERIALIZER.deserialize(parcelSerialized);
console.log(formatMessageInfo(parcel, true))
}
} else {
// It's a parcel
const serviceMessage = deserializeServiceMessage(payloadDecrypted);
console.log(' Service message:');
console.log(` Type: ${serviceMessage.type}`);
if (argv.decodePayload) {
console.log(' Content:', serviceMessage.messageSerialized.toString('utf-8'));
}
}
}
}
function formatMessageInfo(message, indent = false) {
let messageInfo = `Recipient: ${message.recipient}
Sender certificate:
Address: ${getAddressFromCert(derCertToPem(message.senderCert))}
Size: ${message.senderCert.length} octets
Id: ${message.id}
Date: ${message.date}
TTL: ${message.ttl} seconds
Payload:
Size: ${message.payload.length} octets`;
if (indent) {
messageInfo = messageInfo.split('\n').map(line => ` ${line}`).join('\n');
}
return messageInfo;
}
const argv = yargs
.option('recipient-key', {
requiresArg: true,
description: "The path to the recipient's private key, to decrypt payload",
normalize: true,
})
.option('decode-payload', {
description: "Whether to UTF-8 decode the payload after decryption",
boolean: true,
default: false,
})
.help()
.strict()
.argv;
(async () => await main(argv))();