summaryrefslogtreecommitdiff
path: root/backend/data-aggregate.cjs
diff options
context:
space:
mode:
authoraura <nw@moneybot.cc>2026-02-17 22:39:42 +0100
committeraura <nw@moneybot.cc>2026-02-17 22:39:42 +0100
commit636b0323075225c584b62719ed51e75521bb7ffb (patch)
tree61b02271b6d0695a4beffc23fb6eb062a7da22c3 /backend/data-aggregate.cjs
push source
Diffstat (limited to 'backend/data-aggregate.cjs')
-rw-r--r--backend/data-aggregate.cjs65
1 files changed, 65 insertions, 0 deletions
diff --git a/backend/data-aggregate.cjs b/backend/data-aggregate.cjs
new file mode 100644
index 0000000..974f1e1
--- /dev/null
+++ b/backend/data-aggregate.cjs
@@ -0,0 +1,65 @@
+const archiver = require('archiver');
+const fs = require('fs');
+const crypto = require('crypto');
+
+function jwt_secret() {
+ let file = fs.readFileSync( '../data/jwt_secret.txt', 'utf8' );
+ if( file.endsWith( '\n' ) ) {
+ file = file.slice( 0, -1 );
+ }
+
+ return file;
+}
+
+function read( filename ) {
+ const contents = fs.readFileSync( filename, 'utf8' );
+ const salt = Buffer.from( contents.slice( 0, 32 ), 'hex' );
+ const iv = Buffer.from( contents.slice( 32, 56 ), 'hex' );
+ const authTag = Buffer.from( contents.slice( 56, 88 ), 'hex' );
+ const data = contents.slice( 88 );
+
+ const key = crypto.pbkdf2Sync( jwt_secret(), salt, 100000, 32, 'sha512' );
+ const decipher = crypto.createDecipheriv( 'aes-256-gcm', key, iv );
+ decipher.setAuthTag( authTag );
+
+ let decrypted = decipher.update( data, 'hex', 'utf8' );
+ decrypted += decipher.final( 'utf8' );
+
+ return decrypted;
+}
+
+async function main() {
+ try {
+ const userdata = JSON.parse( JSON.parse( process.argv[2] ) );
+ const archive = archiver( 'zip', {
+ zlib: { level: 9 }
+ } );
+
+ archive.pipe( process.stdout );
+ archive.append( process.argv[2], { name: '000-user.json' } );
+
+ const chats = userdata.chat_files;
+ for( let chat of chats.files ) {
+ if( !chat.id )
+ continue;
+
+ try {
+ let chatFile = read( `../data/chats/${chat.id}.json` );
+ archive.append( chatFile, { name: chat.id + '.json' } );
+ } catch( e ) {}
+ }
+ try {
+ let notesFile = fs.readFileSync( `../data/notes/${userdata.uuid}-notes.json` );
+ archive.append( notesFile, { name: `${userdata.uuid}-notes.json` } );
+ } catch( e ) {
+ archive.append( "{}", { name: `${userdata.uuid}-notes.json` } );
+ }
+
+ archive.finalize();
+ } catch (e) {
+ console.log( '0' );
+ console.log( e );
+ }
+}
+
+main();