-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
264 lines (218 loc) · 8.17 KB
/
server.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict';
const express = require('express');
const formidable = require('formidable');
const path = require('path');
const fs = require('fs-extra');
const util = require('util');
const {spawn} = require('child_process');
const process = require('process');
const webSocket = require('ws');
const rimraf = require('rimraf');
const glob = require('glob');
const server = require('http').createServer();
const PORT = process.env.AAX_TO_MP3_PORT || 80;
const HOST = '0.0.0.0';
const TMP_DIR = '/tmp/aax2mp3/';
const WORKDIR = '/usr/src/app';
const SERVER_LOG_PATH = path.join(TMP_DIR, "server.log")
const getDirectories = source => fs
.readdirSync(source, {withFileTypes: true})
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
let globalWs = null;
process.on('SIGINT', function () {
process.exit();
});
if (fs.existsSync(TMP_DIR)) {
rimraf.sync(TMP_DIR);
}
fs.mkdirSync(TMP_DIR);
const stream = fs.createWriteStream(SERVER_LOG_PATH);
const app = express();
app.use('/static', express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/output/', (req, res) => {
res.sendFile(path.join(__dirname, 'output.html'));
});
app.get('/download/', (req, res) => {
res.download(path.join(TMP_DIR, "audiobook.mp3"));
});
function output(string) {
console.log(string);
stream.write(string + "\n");
if (globalWs !== null) {
globalWs.send(`${string}`);
}
}
function getChecksum(path) {
return new Promise((resolve, reject) => {
output(util.format("Processing file:", path));
const ffprobe = spawn('ffprobe', ['-loglevel', 'debug', path]);
const grep = spawn('grep', ['checksum']);
const sed = spawn('sed', ['s/.*checksum == \\(\\w\\+\\)/\\1/']);
ffprobe.stderr.pipe(grep.stdin);
grep.stdout.pipe(sed.stdin);
let checksum;
sed.stdout.on('data', function (data) {
output(util.format("sed stdout:"));
checksum = data;
});
sed.on('close', function (code) {
output(util.format("sed closed with exit code", code));
resolve(checksum.toString().trim());
});
sed.on('error', function (err) {
output(util.format("sed encountered an error"));
reject(err);
});
});
}
async function getActivationBytes(checksum) {
return new Promise((resolve) => {
const rcrack = spawn('./rcrack', ['.', '-h', checksum]);
const grep = spawn('grep', ['hex:']);
const sed = spawn('sed', ['s/.*hex:\\(\\w\\+\\)/\\1/']);
rcrack.stdout.pipe(grep.stdin);
grep.stdout.pipe(sed.stdin);
rcrack.stdout.on('data', function (data) {
output(util.format('rcrack stdout:', data));
});
rcrack.stderr.on('data', function (data) {
output(util.format('rcrack stderr:', data.toString()));
});
rcrack.on('close', function (code) {
output(util.format('rcrack closed with exit code', code));
});
let activationBytes;
sed.stdout.on('data', function (data) {
activationBytes = data;
});
sed.on('close', function (code) {
output(util.format('sed code', code));
resolve(activationBytes.toString().trim());
});
});
}
async function processMp3Files() {
const outdir = getDirectories(TMP_DIR)[0];
output(util.format("Outdir:", outdir));
const author = fs.readdirSync(path.join(TMP_DIR, outdir))[0];
const title = fs.readdirSync(path.join(TMP_DIR, outdir, author))[0];
process.chdir(path.join(TMP_DIR, outdir, author, title));
const mp3Files = glob.sync("*.mp3");
const inputstring = "concat:" + mp3Files.join("|");
const outputfile = "audiobook.mp3";
output(util.format("Concatenating mp3 files..."));
const ffmpeg = spawn('ffmpeg', ['-i', inputstring, '-acodec', 'copy', outputfile]);
ffmpeg.stdout.on('data', function (data) {
output(util.format('ffmpeg stdout:', data.toString()));
});
ffmpeg.stderr.on('data', function (data) {
output(util.format('ffmpeg stderr:', data.toString()));
});
ffmpeg.on('close', function (code) {
output(util.format('ffmpeg closed with exit code', code));
fs.copyFileSync(outputfile, path.join(TMP_DIR, outputfile), 0);
output('link /download');
});
}
async function processActivationBytes(activationBytes, path) {
process.chdir('../AAXtoMP3');
output(util.format("Running AAXtoMp3..."));
const aaxtomp3 = spawn('./AAXtoMP3', ['--authcode', activationBytes, path]);
aaxtomp3.stdout.on('data', function (data) {
output(util.format('aaxtomp3 stdout:', data.toString()));
});
aaxtomp3.stderr.on('data', function (data) {
output(util.format('aaxtomp3 stderr:', data.toString()));
});
aaxtomp3.on('close', function (code) {
output(util.format('aaxtomp3 closed with exit code', code));
processMp3Files();
});
}
async function processWithoutActivationBytes(path) {
process.chdir('./AAXtoMP3');
output(util.format("Running AAXtoMp3 without activation bytes..."));
const aaxtomp3 = spawn('./AAXtoMP3', [path]);
aaxtomp3.stdout.on('data', function (data) {
output(util.format('aaxtomp3 stdout:', data.toString()));
});
aaxtomp3.stderr.on('data', function (data) {
output(util.format('aaxtomp3 stderr:', data.toString()));
});
aaxtomp3.on('close', function (code) {
output(util.format('aaxtomp3 closed with exit code', code));
processMp3Files();
});
}
async function processChecksum(checksum, path) {
process.chdir('tables');
output(util.format("Computing activation bytes -- please wait..."));
const activationBytesPromise = getActivationBytes(checksum);
activationBytesPromise.then(function (activationBytes) {
output(util.format(`Activation bytes: ${activationBytes}`));
processActivationBytes(activationBytes, path);
});
}
async function processFile(file) {
// Give WebSockets some time to connect
await new Promise(done => setTimeout(done, 100));
process.chdir(WORKDIR); // Switch back to the workdir in case pwd has changed
const newPath = path.join(TMP_DIR, file.name);
fs.copyFileSync(file.path, newPath, 0);
const extension = path.extname(file.name)
if (extension === ".aaxc") {
output("Processing AAXC file...");
processWithoutActivationBytes(newPath);
} else if (extension === ".voucher" || extension === ".json" || extension === ".jpg") {
output(util.format(`Got ${extension} file. I am going to assume it will be used for converting an AAXC file.`));
} else if (extension === ".aax") {
output("Processing AAX file...");
const checksumPromise = getChecksum(newPath);
checksumPromise.then(function (checksum) {
output(util.format("Checksum from ffprobe:", checksum));
processChecksum(checksum, newPath);
});
} else {
output(util.format(`Expected a file with any of the extensions ['.aax', '.aaxc', '.voucher', 'json', 'voucher'], got: ${extension}`));
}
}
const wss = new webSocket.Server({
server: server
});
server.on('request', app);
wss.on('connection', ws => {
output('Server: Connection established');
ws.on('message', message => {
console.log(`Received message => ${message}`);
});
ws.send('Server: Hello.');
globalWs = ws;
});
app.post('/submit-form', (req, res) => {
const options = {
maxFileSize: 20 * 1024 * 1024 * 1024 // 20GB
}
new formidable.IncomingForm(options).parse(req)
.on('file', (name, file) => {
output(util.format('Uploaded file', name, file.name));
processFile(file);
})
.on('aborted', () => {
console.error('Request aborted by user');
})
.on('error', (err) => {
console.error('Error', err);
throw err
})
.on('end', () => {
res.redirect("/output/")
})
});
server.listen(PORT, function() {
console.log(`http/ws server listening on ${PORT}`);
});
output(util.format(`Running on http://${HOST}:${PORT}`));