This commit is contained in:
Anonymous 2024-06-09 21:00:36 +08:00
parent 19c63bba60
commit d63f28ba3a
2 changed files with 24 additions and 20 deletions

View File

@ -28,18 +28,11 @@ LightMirrors是一个开源的缓存镜像站服务用于加速软件包下
由于国内访问国外软件源的速度较慢特别是DockerHub缺少国内镜像站 由于国内访问国外软件源的速度较慢特别是DockerHub缺少国内镜像站
因此我们在本地部署镜像站来加速网络访问和节省外网带宽。 因此我们在本地部署镜像站来加速网络访问和节省外网带宽。
---
## Quick Start ## Quick Start
### Prerequisites 执行以下命令试用LightMirrors
- docker + docker-compose.
- 一个域名,设置 `*.yourdomain` 的A记录指向您服务器的IP.
- `*.local.homeinfra.org` 默认指向 `127.0.0.1`,本地测试可以直接使用。
- 代理服务器(如有必要).
> 如果需要使用HTTPS可以在外层新增一个HTTP网关如Caddy请参考后续章节。
执行以下命令以启动LightMirrors
```bash ```bash
@ -61,6 +54,18 @@ pip3 download -i http://torch.local.homeinfra.org/whl/ torch --trusted-host torc
### Deployment ### Deployment
### Prerequisites
- docker + docker-compose.
- 一个域名,设置 `*.yourdomain` 的A记录指向您服务器的IP.
- `*.local.homeinfra.org` 默认指向 `127.0.0.1`,本地测试可以直接使用。
- 代理服务器(如有必要).
> 如果需要使用HTTPS可以在外层新增一个HTTP网关如Caddy请参考后续章节。
> **对于DockerHub镜像我们强烈建议启用HTTPS**
修改 `.env` 文件,设置下列参数: 修改 `.env` 文件,设置下列参数:
- `BASE_DOMAIN`: 基础域名,如 `local.homeinfra.org`,可以通过 `*.local.homeinfra.org` 访问镜像站。 - `BASE_DOMAIN`: 基础域名,如 `local.homeinfra.org`,可以通过 `*.local.homeinfra.org` 访问镜像站。

View File

@ -1,5 +1,6 @@
import base64 import base64
import json import json
import logging
import re import re
import time import time
from typing import Dict from typing import Dict
@ -11,6 +12,8 @@ from starlette.responses import Response
from proxy.direct import direct_proxy from proxy.direct import direct_proxy
from proxy.file_cache import try_file_based_cache from proxy.file_cache import try_file_based_cache
logger = logging.getLogger(__name__)
BASE_URL = "https://registry-1.docker.io" BASE_URL = "https://registry-1.docker.io"
@ -36,11 +39,11 @@ def try_extract_image_name(path):
if match: if match:
assert len(match.groups()) == 3 assert len(match.groups()) == 3
name, operation, reference = match.groups() name, resource, reference = match.groups()
assert re.match(name_regex, name) assert re.match(name_regex, name)
assert re.match(reference_regex, reference) assert re.match(reference_regex, reference)
assert operation in ["manifests", "blobs"] assert resource in ["manifests", "blobs", "tags"]
return name, operation, reference return name, resource, reference
return None, None, None return None, None, None
@ -84,8 +87,6 @@ def inject_token(name: str, req: Request, httpx_req: httpx.Request):
async def post_process(request: Request, response: Response): async def post_process(request: Request, response: Response):
if response.status_code == 307: if response.status_code == 307:
location = response.headers["location"] location = response.headers["location"]
# TODO: logger
print("[redirect]", location)
return await try_file_based_cache(request, location) return await try_file_based_cache(request, location)
return response return response
@ -93,7 +94,6 @@ async def post_process(request: Request, response: Response):
async def docker(request: Request): async def docker(request: Request):
path = request.url.path path = request.url.path
print("[request]", request.method, request.url)
if not path.startswith("/v2/"): if not path.startswith("/v2/"):
return Response(content="Not Found", status_code=404) return Response(content="Not Found", status_code=404)
@ -101,7 +101,7 @@ async def docker(request: Request):
return Response(content="OK") return Response(content="OK")
# return await direct_proxy(request, BASE_URL + '/v2/') # return await direct_proxy(request, BASE_URL + '/v2/')
name, operation, reference = try_extract_image_name(path) name, resource, reference = try_extract_image_name(path)
if not name: if not name:
return Response(content="404 Not Found", status_code=404) return Response(content="404 Not Found", status_code=404)
@ -110,10 +110,9 @@ async def docker(request: Request):
if "/" not in name: if "/" not in name:
name = f"library/{name}" name = f"library/{name}"
target_url = BASE_URL + f"/v2/{name}/{operation}/{reference}" target_url = BASE_URL + f"/v2/{name}/{resource}/{reference}"
# logger logger.info(f"got docker request, {path=} {name=} {resource=} {reference=} {target_url=}")
print("[PARSED]", path, name, operation, reference, target_url)
return await direct_proxy( return await direct_proxy(
request, request,