video-download-server/app.js

48 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2024-12-18 19:56:17 +08:00
const express = require("express");
const request = require("request");
2024-12-18 20:50:44 +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();
},
});
}
2024-12-18 20:50:44 +08:00
const decode = (url, res, decodeKey) => {
let xorStream = xorTransform(
getDecryptionArray(decodeURIComponent(decodeKey))
);
const req = request(decodeURIComponent(url));
req.on("response", (resp) => {
const headers = resp.headers;
res.writeHead(200, "ok", headers);
});
req.pipe(xorStream).pipe(res);
};
2024-12-18 19:56:17 +08:00
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 20:50:44 +08:00
decode(url, res, decodeKey);
2024-12-18 19:56:17 +08:00
});
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
});