- vanilla nginx + Rust 动态模块(取代 OpenResty+Lua),共享内存为运行期真源 - 三域名分流 18098 ssl:mdm 管理台 / lm LLM 代理 / *.dm 企业转发,共用 *.mayishangshu.cn 通配符证书(证书不入库) - LLM 统一出口:同 Key 鉴权、上游 Key 替换、白名单、限流配额、SSE - 回归夹具 tests/(网关 55 + LLM 34 = 89 断言,夹具 cp 覆盖重置) - 文档:readme(16 章)+ CLAUDE.md + conf/tls/README(acme.sh 证书管理、hairpin 环回链路)
63 lines
2.2 KiB
Python
63 lines
2.2 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 do_POST(self):
|
|
n = int(self.headers.get("Content-Length") or 0)
|
|
body = self.rfile.read(n)
|
|
rec = {
|
|
"path": self.path,
|
|
"auth": self.headers.get("Authorization", "-"),
|
|
"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")
|
|
|
|
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()
|