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

70 lines
2.3 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
export default new class E2EE extends BuiltinModule {
get settingPath() {
return ['security', 'default', 'e2ee'];
}
2018-08-10 15:22:30 +02:00
get master() {
return 'temporarymasterkey';
}
get database() {
return Settings.getSet('security').settings.find(s => s.id === 'e2eedb').settings[0].value;
}
encrypt(key, content, prefix = '$:') {
return prefix + aes256.encrypt(key, content);
}
decrypt(key, content, prefix = '$:') {
return aes256.decrypt(key, content.substr(2));
}
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) {
const ctaComponent = await ReactComponents.getComponent('ChannelTextArea');
MonkeyPatch('BD:E2EE', ctaComponent.component.prototype).after('render', this.render);
2018-08-10 15:22:30 +02:00
MonkeyPatch('BD:E2EE', ctaComponent.component.prototype).before('handleSubmit', this.handleSubmit.bind(this));
2018-08-09 09:56:44 +02:00
}
2018-08-10 14:08:21 +02:00
render(component, args, retVal) {
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 14:30:24 +02:00
inner.props.children.splice(0, 0, VueInjector.createReactElement(E2EEComponent, {}, true));
2018-08-10 14:08:21 +02:00
}
2018-08-09 09:56:44 +02:00
2018-08-10 14:08:21 +02:00
handleSubmit(component, args, retVal) {
2018-08-10 15:22:30 +02:00
const key = this.getKey(DiscordApi.currentChannel.id);
if (!key) return;
component.props.value = this.encrypt(this.decrypt(this.master, key), component.props.value);
2018-08-10 14:08:21 +02:00
}
disabled(e) {
for (const patch of Patcher.getPatchesByCaller('BD:E2EE')) patch.unpatch();
2018-08-09 09:56:44 +02:00
}
}