video-download-server/app.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-12-18 19:56:17 +08:00
const express = require("express");
const request = require("request");
2024-12-18 20:33:58 +08:00
const log = require("./log")
2024-12-18 19:56:17 +08:00
const app = express();
const port = 9000;
const { getDecryptionArray } = require("./decode");
const { Transform } = require("stream");
function xorTransform(decryptionArray) {
let processedBytes = 0;
return new Transform({
transform(chunk, encoding, callback) {
if (processedBytes < decryptionArray.length) {
let remaining = Math.min(
decryptionArray.length - processedBytes,
chunk.length
);
for (let i = 0; i < remaining; i++) {
chunk[i] = chunk[i] ^ decryptionArray[processedBytes + i];
}
processedBytes += remaining;
}
this.push(chunk);
callback();
},
});
}
app.get("/download", (req, res) => {
const { url, decodeKey } = req.query;
2024-12-18 20:33:58 +08:00
log.info(`download url:${url}`);
log.info(`download decodeKey:${decodeKey}`);
2024-12-18 19:56:17 +08:00
let xorStream = xorTransform(
getDecryptionArray(decodeURIComponent(decodeKey))
);
request(decodeURIComponent(url)).pipe(xorStream).pipe(res);
});
app.listen(port, () => {
2024-12-18 20:33:58 +08:00
log.info(`Example app listening at http://localhost:${port}`);
2024-12-18 19:56:17 +08:00
});