From 78f5fe5dc4a4f47187db5d92bbe07c1f974de9c4 Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Thu, 9 Feb 2023 01:10:55 +0100 Subject: [PATCH] Fixed broken file size and better direct link detection --- links.ts | 18 +++++++----------- urlpool.ts | 4 +--- webserver.ts | 6 +++--- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/links.ts b/links.ts index 7486b71..1d25235 100644 --- a/links.ts +++ b/links.ts @@ -36,22 +36,18 @@ export class Links { return match![group] ? match![group] : undefined; } - async direct_link(id: string): Promise { - - let download_link = "https://uloz.to/download-dialog/free/download?fileSlug="+id; - let redirect = await this.inst.get(download_link, {maxRedirects: 0, validateStatus: null}); - if(redirect.status !== 302) throw new Error(`Status code: ${redirect.status}`); - this.tor.torNewSession(); - if(redirect.headers.location && redirect.headers.location.startsWith("https://download.uloz.to")) - return redirect.headers.location; - - } - async captcha_link(id: string): Promise { let download_link = "https://uloz.to/download-dialog/free/download?fileSlug="+id; let captcha_page = await this.inst.get(download_link, {maxRedirects: 0, validateStatus: null}); + // Direct link detection + if(captcha_page.status == 302) { + this.tor.torNewSession(); + if(captcha_page.headers.location && captcha_page.headers.location.startsWith("https://download")) + return captcha_page.headers.location; + } + if(captcha_page.status !== 200) throw new Error(`Status code: ${captcha_page.status}`); let cookies = this.parse_cookie(captcha_page.headers["set-cookie"]); diff --git a/urlpool.ts b/urlpool.ts index 3c4c46a..627cd19 100644 --- a/urlpool.ts +++ b/urlpool.ts @@ -26,7 +26,6 @@ export class UrlPool { file_name?: string; total_size: number = 0; - is_direct = false; constructor(storage: UrlPoolStorage, id: string) { this.valid_to = new Date(); @@ -42,7 +41,6 @@ export class UrlPool { let dom = new JSDOM(page.data); // TODO: Add quick download support // let quick_dl_url = links.regex_parse(new RegExp('href="(/quickDownload/[^"]*)"'), page.data, 1); - this.is_direct = 'js-free-download-button-direct' == links.regex_parse(new RegExp('data-href="/download-dialog/free/[^"]+" +class=".+(js-free-download-button-direct).+"'), page.data, 1); let filename = dom.window.document.querySelector(".jsFileTitle"); if(filename && filename.textContent) { @@ -117,7 +115,7 @@ export class UrlPool { async generate(): Promise { this.last_generated = unixtime(); try { - let link = await (this.is_direct ? links.direct_link(this.id) : links.captcha_link(this.id)); + let link = await links.captcha_link(this.id); if(link) this.add(link); return link; } catch(e) { diff --git a/webserver.ts b/webserver.ts index c492ac2..035156c 100644 --- a/webserver.ts +++ b/webserver.ts @@ -57,10 +57,10 @@ export class Webserver { let d = p.get_downloader(range.from); - let contentLength = p.total_size-1-range.from; + let contentLength = p.total_size-range.from; let headers = { - "Content-Range": `bytes ${range.from}-${p.total_size-1}/${p.total_size-1}`, - "Range": `bytes=${range.from}-${p.total_size-1}/${p.total_size-1}`, + "Content-Range": `bytes ${range.from}-${p.total_size-1}/${p.total_size}`, + "Range": `bytes=${range.from}-${p.total_size-1}/${p.total_size}`, "Accept-Ranges": "bytes", "Content-Length": contentLength, "Content-Type": "application/octet-stream",