BetterDiscordApp-v2/client/src/builtin/E2EE.js

124 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-08-09 09:56:44 +02:00
/**
* BetterDiscord E2EE Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
2018-08-10 15:22:30 +02:00
import { Settings } from 'modules';
2018-08-09 09:56:44 +02:00
import BuiltinModule from './BuiltinModule';
2018-08-10 15:22:30 +02:00
import { WebpackModules, ReactComponents, MonkeyPatch, Patcher, DiscordApi } from 'modules';
2018-08-10 14:08:21 +02:00
import { VueInjector, Reflection } from 'ui';
2018-08-10 14:30:24 +02:00
import E2EEComponent from './E2EEComponent.vue';
2018-08-10 14:08:21 +02:00
import aes256 from 'aes256';
2018-08-09 09:56:44 +02:00
2018-08-10 16:02:41 +02:00
let seed = Math.random().toString(36).replace(/[^a-z]+/g, '');
2018-08-10 16:01:07 +02:00
2018-08-09 09:56:44 +02:00
export default new class E2EE extends BuiltinModule {
2018-08-10 16:01:07 +02:00
constructor() {
super();
this.master = this.encrypt(seed, 'temporarymasterkey');
this.encryptNewMessages = true;
2018-08-09 09:56:44 +02:00
}
2018-08-10 16:02:41 +02:00
setMaster(key) {
seed = Math.random().toString(36).replace(/[^a-z]+/g, '');
2018-08-10 16:04:09 +02:00
const newMaster = this.encrypt(seed, key);
// TODO re-encrypt everything with new master
return (this.master = newMaster);
2018-08-10 16:02:41 +02:00
}
2018-08-10 16:01:07 +02:00
get settingPath() {
return ['security', 'default', 'e2ee'];
2018-08-10 15:22:30 +02:00
}
get database() {
return Settings.getSetting('security', 'e2eedb', 'e2ekvps').value;
2018-08-10 15:22:30 +02:00
}
2018-08-10 20:17:52 +02:00
encrypt(key, content, prefix = '') {
2018-08-10 15:22:30 +02:00
return prefix + aes256.encrypt(key, content);
}
2018-08-10 20:17:52 +02:00
decrypt(key, content, prefix = '') {
return aes256.decrypt(key, content.replace(prefix, ''));
2018-08-10 15:22:30 +02:00
}
getKey(channelId) {
const haveKey = this.database.find(kvp => kvp.value.key === channelId);
if (!haveKey) return null;
return haveKey.value.value;
}
2018-08-10 14:08:21 +02:00
async enabled(e) {
2018-08-10 19:38:02 +02:00
this.patchMessageContent();
const selector = '.' + WebpackModules.getClassName('channelTextArea', 'emojiButton');
2018-08-11 02:39:13 +02:00
const cta = await ReactComponents.getComponent('ChannelTextArea', { selector });
this.patchChannelTextArea(cta);
this.patchChannelTextAreaSubmit(cta);
cta.forceUpdateAll();
}
async patchMessageContent() {
const selector = '.' + WebpackModules.getClassName('container', 'containerCozy', 'containerCompact', 'edited');
const MessageContent = await ReactComponents.getComponent('MessageContent', { selector });
MonkeyPatch('BD:E2EE', MessageContent.component.prototype).before('render', this.renderMessageContent.bind(this));
2018-08-09 09:56:44 +02:00
}
2018-08-10 19:38:02 +02:00
renderMessageContent(component, args, retVal) {
const key = this.getKey(DiscordApi.currentChannel.id);
2018-08-11 02:39:13 +02:00
if (!key) return; // We don't have a key for this channel
const Message = WebpackModules.getModuleByPrototypes(['isMentioned']);
const MessageParser = WebpackModules.getModuleByName('MessageParser');
const currentChannel = DiscordApi.currentChannel.discordObject;
if (!component.props || !component.props.message) return;
const { content } = component.props.message;
if (typeof content !== 'string') return; // Ignore any non string content
if (!content.startsWith('$:')) return; // Not an encrypted string
let decrypt;
try {
decrypt = this.decrypt(this.decrypt(this.decrypt(seed, this.master), key), component.props.message.content);
} catch (err) { return } // Ignore errors such as non empty
// Create a new message to parse it properly
const create = Message.create(MessageParser.createMessage(currentChannel, MessageParser.parse(currentChannel, decrypt).content));
if (!create.content || !create.contentParsed) return;
component.props.message.content = create.content;
component.props.message.contentParsed = create.contentParsed;
2018-08-10 19:38:02 +02:00
}
2018-08-11 02:39:13 +02:00
patchChannelTextArea(cta) {
MonkeyPatch('BD:E2EE', cta.component.prototype).after('render', this.renderChannelTextArea);
}
renderChannelTextArea(component, args, retVal) {
2018-08-10 14:08:21 +02:00
if (!(retVal.props.children instanceof Array)) retVal.props.children = [retVal.props.children];
const inner = retVal.props.children.find(child => child.props.className && child.props.className.includes('inner'));
2018-08-10 19:38:02 +02:00
inner.props.children.splice(0, 0, VueInjector.createReactElement(E2EEComponent, {}, false));
2018-08-10 14:08:21 +02:00
}
2018-08-09 09:56:44 +02:00
2018-08-11 02:39:13 +02:00
patchChannelTextAreaSubmit(cta) {
MonkeyPatch('BD:E2EE', cta.component.prototype).before('handleSubmit', this.handleChannelTextAreaSubmit.bind(this));
}
handleChannelTextAreaSubmit(component, args, retVal) {
2018-08-10 15:22:30 +02:00
const key = this.getKey(DiscordApi.currentChannel.id);
if (!this.encryptNewMessages || !key) return;
2018-08-10 20:17:52 +02:00
component.props.value = this.encrypt(this.decrypt(this.decrypt(seed, this.master), key), component.props.value, '$:');
2018-08-10 14:08:21 +02:00
}
2018-08-10 19:38:02 +02:00
async disabled(e) {
2018-08-10 14:08:21 +02:00
for (const patch of Patcher.getPatchesByCaller('BD:E2EE')) patch.unpatch();
2018-08-10 19:38:02 +02:00
const ctaComponent = await ReactComponents.getComponent('ChannelTextArea');
ctaComponent.forceUpdateAll();
2018-08-09 09:56:44 +02:00
}
}