diff --git a/links.ts b/links.ts index 24fdf1b..9c64f73 100644 --- a/links.ts +++ b/links.ts @@ -13,7 +13,7 @@ export class Links { inst = axios.create({ httpAgent: this.tor.httpAgent(), - httpsAgent: this.tor.httpsAgent(), + httpsAgent: this.tor.httpsAgent() }); parse_cookie(cookies: string[] | undefined) { @@ -35,10 +35,22 @@ export class Links { return match![group] ? match![group] : undefined; } - async link(id: string) { + async direct_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); + let redirect = await this.inst.get(download_link, {maxRedirects: 0, validateStatus: null}); + 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}); + + if(captcha_page.status !== 200) throw new Error("Status code is not 200"); let cookies = this.parse_cookie(captcha_page.headers["set-cookie"]); let url = this.regex_parse(//gm, captcha_page.data, 1); diff --git a/urlpool.ts b/urlpool.ts index 4db3e41..0c02787 100644 --- a/urlpool.ts +++ b/urlpool.ts @@ -1,3 +1,4 @@ +import axios from "axios"; import { Downloader } from "./downloader"; import { Links } from "./links"; @@ -9,16 +10,25 @@ function sleep(ms: number) { export class UrlPool { - urls: string[]; + id: string; + is_direct = false; + urls: string[] = []; used: boolean[] = []; generating = false; downloader?: Downloader; + valid = false; - constructor(urls?: string[]) { - this.urls = urls ? urls : []; - for(let i=0; i { + try { + let link = await (this.is_direct ? links.direct_link(this.id) : links.captcha_link(this.id)); + if(link) this.add(link); + console.log(link); + return link; + } catch { + return undefined; + } + } + get_downloader() { if(this.downloader) return this.downloader; this.downloader = new Downloader(this) @@ -87,13 +104,15 @@ export class UrlPoolStorage { pools: {[U: string]: UrlPool} = {}; - get(id: string) { - return this.pools[id] ? this.pools[id] : this.new(id); + async get(id: string): Promise { + return this.pools[id] ? this.pools[id] : await this.new(id); } - new(id: string) { - let pool = new UrlPool; - pool.generate(id); + async new(id: string): Promise { + let pool = new UrlPool(id); + let check = await pool.init(); + if(!check) return undefined; + pool.start_generation(); this.pools[id] = pool; return pool; } diff --git a/webserver.ts b/webserver.ts index 2ea2f40..381e6db 100644 --- a/webserver.ts +++ b/webserver.ts @@ -17,7 +17,12 @@ export class Webserver { app.get("/status/:id", async (req, res) => { - let p = storage.get(req.params.id); + let p = await storage.get(req.params.id); + if(!p) { + res.status(404); + res.end(); + return; + } res.write(JSON.stringify({ streams: { @@ -32,7 +37,12 @@ export class Webserver { app.get("/stream/:id", async (req, res) => { - let p = storage.get(req.params.id); + let p = await storage.get(req.params.id); + if(!p) { + res.status(404); + res.end(); + return; + } let d = p.get_downloader(); let range: Range = {from: 0, to: null};