diff --git a/.gitignore b/.gitignore index bb1c13f..3e70ac2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ data/* caddy/caddy_data .idea .env -node_modules \ No newline at end of file +node_modules +**/__pycache__ \ No newline at end of file diff --git a/src/mirrorsrun/aria2_api.py b/src/mirrorsrun/aria2_api.py index 8b216c1..f547d1d 100644 --- a/src/mirrorsrun/aria2_api.py +++ b/src/mirrorsrun/aria2_api.py @@ -1,6 +1,7 @@ import json import logging import uuid +from typing import Optional import httpx @@ -33,18 +34,20 @@ async def send_request(method, params=None): raise e -async def add_download(url, save_dir="/app/cache", out_file=None): - logger.info(f"[Aria2] add_download {url=} {save_dir=} {out_file=}") +async def add_download( + url, save_dir="/app/cache", out_file=None, headers: Optional[dict] = None +): + logger.info(f"[Aria2] add_download {url=} {save_dir=} {out_file=} {headers=}") method = "aria2.addUri" options = { "dir": save_dir, - "header": [], - "out": out_file, } if out_file: options["out"] = out_file + if headers: + options["header"] = [f"{k}: {v}" for k, v in headers.items()] params = [[url], options] response = await send_request(method, params) diff --git a/src/mirrorsrun/proxy/file_cache.py b/src/mirrorsrun/proxy/file_cache.py index aff487d..4600cd3 100644 --- a/src/mirrorsrun/proxy/file_cache.py +++ b/src/mirrorsrun/proxy/file_cache.py @@ -100,6 +100,11 @@ async def try_file_based_cache( processed_url, save_dir=cache_file_dir, out_file=os.path.basename(cache_file), + headers={ + key: value + for key, value in request.headers.items() + if key in ["user-agent", "accept", "authorization"] + }, ) except Exception as e: logger.error(f"Download error, return 500 for {target_url}", exc_info=e)