diff --git a/comet/api/stream.py b/comet/api/stream.py index 573e5c8..44fca59 100644 --- a/comet/api/stream.py +++ b/comet/api/stream.py @@ -303,6 +303,7 @@ async def playback(request: Request, b64config: str, hash: str, index: str): async with aiohttp.ClientSession() as session: debrid = getDebrid(session, config) download_link = await debrid.generate_download_link(hash, index) + proxy = debrid.proxy if config["debridService"] == "alldebrid" else None # proxy is not needed to proxy realdebrid stream if ( settings.PROXY_DEBRID_STREAM @@ -312,7 +313,7 @@ async def playback(request: Request, b64config: str, hash: str, index: str): async def stream_content(headers: dict): async with aiohttp.ClientSession() as session: - async with session.get(download_link, headers=headers) as response: + async with session.get(download_link, headers=headers, proxy=proxy) as response: while True: chunk = await response.content.read( settings.PROXY_DEBRID_STREAM_BYTES_PER_CHUNK @@ -331,7 +332,7 @@ async def stream_content(headers: dict): range = f"bytes={start}-{end}" async with await session.get( - download_link, headers={"Range": f"bytes={start}-{end}"} + download_link, headers={"Range": f"bytes={start}-{end}"}, proxy=proxy ) as response: await session.close() diff --git a/comet/debrid/alldebrid.py b/comet/debrid/alldebrid.py index 23d3ee0..c594e96 100644 --- a/comet/debrid/alldebrid.py +++ b/comet/debrid/alldebrid.py @@ -11,6 +11,7 @@ class AllDebrid: def __init__(self, session: aiohttp.ClientSession, debrid_api_key: str): session.headers["Authorization"] = f"Bearer {debrid_api_key}" self.session = session + self.proxy = None self.api_url = "http://api.alldebrid.com/v4" self.agent = "comet" @@ -91,33 +92,32 @@ async def generate_download_link(self, hash: str, index: str): f"{self.api_url}/magnet/upload?agent=comet&magnets[]={hash}" ) check_blacklisted = await check_blacklisted.text() - proxy = None if "NO_SERVER" in check_blacklisted: - proxy = settings.DEBRID_PROXY_URL - if not proxy: + self.proxy = settings.DEBRID_PROXY_URL + if not self.proxy: logger.warning( "All-Debrid blacklisted server's IP. No proxy found." ) else: logger.warning( - f"All-Debrid blacklisted server's IP. Switching to proxy {proxy} for {hash}|{index}" + f"All-Debrid blacklisted server's IP. Switching to proxy {self.proxy} for {hash}|{index}" ) upload_magnet = await self.session.get( f"{self.api_url}/magnet/upload?agent=comet&magnets[]={hash}", - proxy=proxy, + proxy=self.proxy, ) upload_magnet = await upload_magnet.json() get_magnet_status = await self.session.get( f"{self.api_url}/magnet/status?agent=comet&id={upload_magnet['data']['magnets'][0]['id']}", - proxy=proxy, + proxy=self.proxy, ) get_magnet_status = await get_magnet_status.json() unlock_link = await self.session.get( f"{self.api_url}/link/unlock?agent=comet&link={get_magnet_status['data']['magnets']['links'][int(index)]['link']}", - proxy=proxy, + proxy=self.proxy, ) unlock_link = await unlock_link.json()