This repository has been archived on 2023-11-29. You can view files and clone it, but cannot push or open issues or pull requests.
Uloz.to-rychle/webserver.ts

104 lines
5.4 KiB
TypeScript

import express from "express";
import { Downloader } from "./downloader";
import { UrlPool } from "./urlpool";
import { Readable } from 'stream';
let pool = new UrlPool(`https://download.uloz.to/Ps;Hs;up=0;cid=1412447369;uip=89.163.243.88;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=53936164;tm=1645827197;ut=f;rs=0;He;ch=3676c0f11954b17f3b47a432f1ae1ee8;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1412447369&De
https://download.uloz.to/Ps;Hs;up=0;cid=1129985479;uip=185.220.102.249;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=561098867;tm=1645827209;ut=f;rs=0;He;ch=519cfbfcb4efe4af4a94b34ec9644609;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1129985479&De
https://download.uloz.to/Ps;Hs;up=0;cid=114914590;uip=185.220.101.86;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1829476423;tm=1645827209;ut=f;rs=0;He;ch=4cf0db28c8c81955bdc81ccd3b7f92f6;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=114914590&De
https://download.uloz.to/Ps;Hs;up=0;cid=1617820972;uip=107.189.4.253;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=141776455;tm=1645827211;ut=f;rs=0;He;ch=7c1fb7f1a836590c274039c66dfcda99;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1617820972&De
https://download.uloz.to/Ps;Hs;up=0;cid=1469359686;uip=5.2.72.226;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1774386903;tm=1645827212;ut=f;rs=0;He;ch=6b1485bc3e20cc06d0a712ebe4036905;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1469359686&De
https://download.uloz.to/Ps;Hs;up=0;cid=1910168684;uip=107.189.10.63;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1577890352;tm=1645827223;ut=f;rs=0;He;ch=3be975ccb9361af36b61255001a5427b;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1910168684&De
https://download.uloz.to/Ps;Hs;up=0;cid=381158312;uip=89.248.168.41;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=666945992;tm=1645827223;ut=f;rs=0;He;ch=ef7f799ef9521ff132ef82f8c21cd831;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=381158312&De
https://download.uloz.to/Ps;Hs;up=0;cid=335896018;uip=185.195.71.2;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1195420583;tm=1645827224;ut=f;rs=0;He;ch=b77f94898e04485661c3120f1f23e95d;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=335896018&De
https://download.uloz.to/Ps;Hs;up=0;cid=1364037004;uip=185.195.71.244;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1209800903;tm=1645827245;ut=f;rs=0;He;ch=d3a38438c7c8786cb869458f391d6ca3;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1364037004&De
https://download.uloz.to/Ps;Hs;up=0;cid=947300788;uip=185.220.100.243;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1975067348;tm=1645827245;ut=f;rs=0;He;ch=39019f9e27d7a216eb2d2cecffda619c;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=947300788&De
https://download.uloz.to/Ps;Hs;up=0;cid=1608448688;uip=185.220.101.42;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=1643356760;tm=1645827246;ut=f;rs=0;He;ch=1347a9cf2cabad923867347b897924a3;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=1608448688&De
https://download.uloz.to/Ps;Hs;up=0;cid=709281061;uip=185.220.101.20;aff=uloz.to;did=uloz-to;fide=az2Gxzu;fs=pLlZx78VhpLo;hid=JPWTfrY;rid=2092835317;tm=1645827247;ut=f;rs=0;He;ch=254d43fbd221b68723edcb0c6f0d9181;Pe/file/pLlZx78VhpLo/matrix-1999-cz-mp4?bD&c=709281061&De`.split("\n"));
let d = new Downloader(pool);
export class Webserver {
chunk_size = 5*1024*1024;
partial_size = this.chunk_size*10;
constructor() {
const app = express();
app.get("/", async (req, res) => {
let range: Range = {from: 0, to: null};
if(req.headers.range) range = this.parse_range(req.headers.range);
if(!range.from) range.from = 0;
if(!d.ready) await d.init();
let [from, to] = this.from_to([range.from, range.from+this.chunk_size], d.total_size);
let contentLength = this.partial_size + 1;
if(contentLength+from > d.total_size) contentLength = d.total_size-from;
let headers = {
"Content-Range": `bytes ${from}-${from+contentLength}/${d.total_size+1}`,
"Range": `bytes=${from}-${from+contentLength}/${d.total_size+1}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": "application/octet-stream",
};
res.writeHead(206, headers);
const readable = new Readable()
readable._read = async () => {
[from, to] = this.from_to([to+1, to+1+this.chunk_size], d.total_size);
if(from == d.total_size) {
readable.push(null);
//res.end();
}
else {
let stream = await this.download_chunk(from, to);
readable.push(stream);
}
}
let stream = await this.download_chunk(from, to);
readable.push(stream);
readable.pipe(res);
});
app.listen(8000, function () {
console.log("Listening on port 8000!");
});
}
async download_chunk(from: number, to: number) {
console.log("downloading...", from, to);
let stream = await d.download_range(from, to);
console.log("downloaded ", from, to);
return stream;
}
parse_range(input: string): Range {
let [from, to]: (number|null)[] = [null, null];
let parts = input.split("=");
if(parts[1]) [from, to] = parts[1].split("-").map(i => i ? parseInt(i) : null);
return {from, to};
}
from_to(range: [number, number], max: number) {
if(range[0] > max) range[0] = max;
if(range[1] > max) range[1] = max;
return range;
}
}
interface Range {
from: number | null;
to: number | null;
}