Files
ipass-plugin-cache/tests/cache/mock_upstream.py
T
ssw 43718bd0c4 iPass cache gateway 源码
容量管理: zones.rs 直读 nginx file_cache 共享内存记账(双轴: 磁盘 + keys_zone 索引内存), ?scan=1 扫盘上限 20 万文件; POST /api/admin/cache/purge 整区清理(只删文件不删目录, 根治 [crit] mkdir failed); POST /api/admin/cache/invalidate 按制品精准失效(预览->确认两步式, glob/子串, 删除上限 5000/扫描上限 20 万); GET /api/admin/cache 新增 capacity 块; 前端容量面板 + 按制品失效工具条; 测试 test-cache-capacity.sh 48/48(容量 27 + 精准失效 21, 含 EXPECT_MIN 护栏与前端静态契约); docs 容量管理章节
2026-09-21 09:25:11 +08:00

89 lines
3.8 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# ============================================================================
# 确定性假上游:统一缓存自测用。
#
# - 任何 GET 返回 200,body = "ECHO:<method> <path>?<query>#<body_len>",
# 长度固定且随 path 变化,便于校验「不同生态/路径各存各的」。
# - 支持 Range:返回 206 + Content-Range,验证大层/分片缓存。
# - 记录每个请求到 UPSTREAM_LOG(method path),用于断言「HIT 不再回源」。
# - 不带任何 registry 协议细节(Docker token 舞、cargo 稀疏索引等),
# 那些是生产 conf 的关注点;本靶机只验证「代理缓存」这一层本身。
# ============================================================================
import os
import sys
import urllib.parse
from http.server import BaseHTTPRequestHandler, HTTPServer
PORT = int(os.environ.get("UPSTREAM_PORT", "18999"))
LOG = os.environ.get("UPSTREAM_LOG", "")
def log_line(s: str):
if LOG:
with open(LOG, "a") as f:
f.write(s + "\n")
class H(BaseHTTPRequestHandler):
def _body(self):
# 默认用完整请求行生成确定性内容;长度随 path 变化。
# `?bytes=N` 时返回**恰好 N 字节**(可复现的填充内容,内容随 path 变化),
# 用于验证缓存容量淘汰(max_size 触发 LRU)——没有这个大响应能力,
# 「超容后会不会被淘汰」就没法在本地证明。
path = self.path
n = 0
if "?" in path:
qs = urllib.parse.parse_qs(urllib.parse.urlparse(path).query)
try:
n = int(qs.get("bytes", ["0"])[0])
except ValueError:
n = 0
if n > 0:
seed = f"ECHO:{self.command} {path}\n".encode("utf-8")
return (seed * (n // len(seed) + 1))[:n]
return f"ECHO:{self.command} {path}".encode("utf-8")
def do_GET(self):
# 行首仍是 `GET <path>`(既有断言依赖这个前缀),末尾补一个 auth= 字段:
# 用来证伪「网关把自己的下行 sk_ 透传给了上游」——这是镜像型缓存最要命的
# 一类泄漏,必须在靶机上有一根静态钉子。
log_line(f"GET {self.path} auth={self.headers.get('Authorization', '-')}")
full = self._body()
rng = self.headers.get("Range")
if rng and rng.startswith("bytes="):
# 简单支持单段 range:bytes=start-end
spec = rng[len("bytes="):]
try:
start_s, end_s = spec.split("-")
start = int(start_s) if start_s else 0
end = int(end_s) if end_s else len(full) - 1
end = min(end, len(full) - 1)
chunk = full[start:end + 1]
self.send_response(206)
self.send_header("Content-Range", f"bytes {start}-{end}/{len(full)}")
self.send_header("Content-Length", str(len(chunk)))
self.send_header("Content-Type", "application/octet-stream")
self.send_header("ETag", f'"{len(full)}"')
self.end_headers()
self.wfile.write(chunk)
return
except Exception:
pass
self.send_response(200)
self.send_header("Content-Length", str(len(full)))
self.send_header("Content-Type", "application/octet-stream")
self.send_header("ETag", f'"{len(full)}"')
self.send_header("Cache-Control", "public, max-age=3600")
self.end_headers()
self.wfile.write(full)
# 静默日志,避免刷屏
def log_message(self, *args):
pass
if __name__ == "__main__":
srv = HTTPServer(("127.0.0.1", PORT), H)
print(f"mock upstream on {PORT}", flush=True)
srv.serve_forever()