You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
5.2 KiB

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const rncryptor_1 = require("../utils/rncryptor");
const fetch = require("node-fetch");
const ldat_1 = require("../utils/ldat");
const rsa = require("../crypto/rsa");
const crypto = require("crypto");
const meme = require("../utils/meme");
const FormData = require("form-data");
5 years ago
// import { models } from '../models'
const constants = require(path.join(__dirname, '../../config/constants.json'));
const msgtypes = constants.message_types;
function modifyPayloadAndSaveMediaKey(payload, chat, sender) {
return __awaiter(this, void 0, void 0, function* () {
if (payload.type === msgtypes.attachment) {
const mt = payload.message && payload.message.mediaToken;
const key = payload.message && payload.message.mediaKey;
const typ = payload.message && payload.message.mediaType;
if (!mt || !key)
return payload;
const terms = ldat_1.parseLDAT(mt);
if (!terms.host)
return payload;
try {
const r = yield fetch(`https://${terms.host}/file/${mt}`, {
headers: { 'Authorization': `Bearer ${meme.mediaToken}` }
});
const buf = yield r.buffer();
const decMediaKey = rsa.decrypt(chat.groupPrivateKey, key);
const imgBase64 = rncryptor_1.default.Decrypt(decMediaKey, buf.toString('base64'));
const newKey = crypto.randomBytes(20).toString('hex');
const encImg = rncryptor_1.default.Encrypt(newKey, imgBase64);
var encImgBuffer = Buffer.from(encImg, 'base64');
5 years ago
const form = new FormData();
form.append('file', encImgBuffer, {
contentType: typ || 'image/jpg',
filename: 'Image.jpg',
knownLength: encImgBuffer.length,
});
5 years ago
const formHeaders = form.getHeaders();
const resp = yield fetch(`https://${terms.host}/file`, {
method: 'POST',
5 years ago
headers: Object.assign(Object.assign({}, formHeaders), { 'Authorization': `Bearer ${meme.mediaToken}` }),
5 years ago
body: form
});
5 years ago
let json = yield resp.json();
if (!json.muid)
return payload;
// PUT NEW TERMS, to finish in personalizeMessage
const amt = terms.meta && terms.meta.amt;
const ttl = terms.meta && terms.meta.ttl;
const mediaTerms = {
muid: json.muid, ttl: ttl || 31536000, host: '',
meta: Object.assign({}, amt && { amt }),
skipSigning: amt ? true : false // only sign if its free
};
5 years ago
const keyToEncrypt = newKey.slice();
const encKey = rsa.encrypt(chat.groupKey, keyToEncrypt);
const keyToSave = encKey.slice();
var date = new Date();
date.setMilliseconds(0);
5 years ago
console.log('[modify] save media key!', {
muid: json.muid,
chatId: chat.id,
5 years ago
key: keyToSave,
5 years ago
messageId: (payload.message && payload.message.id) || 0,
receiver: 0,
sender: sender.id,
createdAt: date,
});
5 years ago
// await sleep(1)
// await models.MediaKey.create({
// muid:json.muid,
// chatId:chat.id,
// key:encKey,
// messageId: (payload.message&&payload.message.id)||0,
// receiver: 0,
// sender: sender.id, // the og sender
// createdAt: date,
// })
return fillmsg(payload, { mediaTerms, mediaKey: encKey }); // key is re-encrypted later
}
catch (e) {
console.log("[modify] error", e);
return payload;
}
// how to link w og msg? ogMediaToken?
}
else {
return payload;
}
});
}
exports.modifyPayloadAndSaveMediaKey = modifyPayloadAndSaveMediaKey;
function fillmsg(full, props) {
return Object.assign(Object.assign({}, full), { message: Object.assign(Object.assign({}, full.message), props) });
}
5 years ago
// async function sleep(ms) {
// return new Promise(resolve => setTimeout(resolve, ms))
// }
//# sourceMappingURL=modify.js.map