- LiteLLM:/anthropic/* 路径前缀路由;clean_headers 的 authenticated_with_header 语义 + 默认摘凭据头 + 双层 allowlist + 日志/转发两副本分离 - one-api:根地址 + 路径天然区分,model 只选 channel,出站三头齐设 - Higress:Automatic Protocol Detection;v2.2.3 把按模型名宽泛匹配改成路径后缀判断 - Anthropic 官方:走网关用 ANTHROPIC_AUTH_TOKEN(发 Authorization),直连才用 ANTHROPIC_API_KEY(发 X-Api-Key) - 靶机改为记录全部收到的头:手挑字段会让未替换的头在回归里全绿通过 结论:协议由客户端路径表达,不需要网关分发;统一注册的实体是 base 写到 host。 鉴权头两种修法与 off 模式的必注入清单(漏 Content-Type 会让所有 POST 废)见文档。
91 lines
3.5 KiB
Python
91 lines
3.5 KiB
Python
"""假上游:记录它**实际收到**的 path / auth / host / body。
|
||
|
||
这是验证「下游 Key 是否真被换成上游真 Key」的唯一可靠手段 ——
|
||
只看网关自己的响应是看不出来的(网关返回 200 不代表上游收到了正确的头)。
|
||
|
||
UPSTREAM_PORT=18999 UPSTREAM_LOG=/path/to/upstream.log python3 upstream.py
|
||
|
||
带 `"stream": true` 的请求体会回 SSE,分 3 块、每块间隔 0.4s,
|
||
用来验证 nginx 侧 `proxy_buffering off` 是否生效(缓冲住的话会攒完一次性返回)。
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import time
|
||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||
|
||
LOG = os.environ.get("UPSTREAM_LOG", "/tmp/upstream.log")
|
||
PORT = int(os.environ.get("UPSTREAM_PORT", "18999"))
|
||
|
||
|
||
class H(BaseHTTPRequestHandler):
|
||
protocol_version = "HTTP/1.1"
|
||
|
||
def _record(self, body: bytes):
|
||
# 记录**全部**收到的头,而不是手挑几个字段。
|
||
# 教训:手挑字段会漏掉"没被替换的那个头"——第一版只记 Authorization,
|
||
# 于是 x-api-key 泄漏在整轮回归里全绿通过,完全看不见。
|
||
rec = {
|
||
"path": self.path,
|
||
"method": self.command,
|
||
"headers": {k.lower(): v for k, v in self.headers.items()},
|
||
# 下面几个是长期被断言的字段,保留以免旧断言失效
|
||
"auth": self.headers.get("Authorization", "-"),
|
||
"x_api_key": self.headers.get("x-api-key", "-"),
|
||
"anthropic_version": self.headers.get("anthropic-version", "-"),
|
||
"host": self.headers.get("Host", "-"),
|
||
"body": body.decode("utf-8", "replace"),
|
||
}
|
||
with open(LOG, "a") as f:
|
||
f.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||
|
||
def do_GET(self):
|
||
"""`GET /<slot>/v1/models` 必须原样透传到上游——这里验的就是这条。
|
||
|
||
网关自己的 `GET /v1/models`(无 slot 段)在本地应答、不会打到这里,
|
||
两条路互不干扰。
|
||
"""
|
||
self._record(b"")
|
||
payload = json.dumps({
|
||
"object": "list",
|
||
"data": [{"id": "upstream-model", "object": "model"}],
|
||
}).encode()
|
||
self.send_response(200)
|
||
self.send_header("Content-Type", "application/json")
|
||
self.send_header("Content-Length", str(len(payload)))
|
||
self.end_headers()
|
||
self.wfile.write(payload)
|
||
|
||
def do_POST(self):
|
||
n = int(self.headers.get("Content-Length") or 0)
|
||
body = self.rfile.read(n)
|
||
self._record(body)
|
||
|
||
if b'"stream"' in body and b"true" in body:
|
||
self.send_response(200)
|
||
self.send_header("Content-Type", "text/event-stream")
|
||
self.send_header("Cache-Control", "no-cache")
|
||
self.send_header("Connection", "close")
|
||
self.end_headers()
|
||
for i in range(3):
|
||
self.wfile.write(('data: {"i":%d}\n\n' % i).encode())
|
||
self.wfile.flush()
|
||
time.sleep(0.4)
|
||
self.close_connection = True
|
||
else:
|
||
payload = json.dumps({
|
||
"id": "chatcmpl-x",
|
||
"choices": [{"message": {"role": "assistant", "content": "pong"}}],
|
||
}).encode()
|
||
self.send_response(200)
|
||
self.send_header("Content-Type", "application/json")
|
||
self.send_header("Content-Length", str(len(payload)))
|
||
self.end_headers()
|
||
self.wfile.write(payload)
|
||
|
||
def log_message(self, *a):
|
||
pass
|
||
|
||
|
||
ThreadingHTTPServer(("127.0.0.1", PORT), H).serve_forever()
|