Spaces:
Paused
Paused
Improve POS proxy diagnostics and token compatibility
Browse files
pages/🎟️ 售票时间集中监控.py
CHANGED
|
@@ -445,7 +445,30 @@ class PosCashierAPI:
|
|
| 445 |
timeout=20,
|
| 446 |
)
|
| 447 |
resp.raise_for_status()
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
def fetch_sessions(self, show_date: str) -> List[dict]:
|
| 451 |
payload = self.request_json(
|
|
|
|
| 445 |
timeout=20,
|
| 446 |
)
|
| 447 |
resp.raise_for_status()
|
| 448 |
+
try:
|
| 449 |
+
return resp.json()
|
| 450 |
+
except ValueError as exc:
|
| 451 |
+
content_type = resp.headers.get("Content-Type", "")
|
| 452 |
+
proxy_status = (
|
| 453 |
+
resp.headers.get("X-POS-Upstream-Status")
|
| 454 |
+
or resp.headers.get("X-TMS-Upstream-Status")
|
| 455 |
+
or "-"
|
| 456 |
+
)
|
| 457 |
+
proxy_error = (
|
| 458 |
+
resp.headers.get("X-POS-Proxy-Error")
|
| 459 |
+
or resp.headers.get("X-TMS-Proxy-Error")
|
| 460 |
+
or resp.headers.get("X-EOP-MSG")
|
| 461 |
+
or "-"
|
| 462 |
+
)
|
| 463 |
+
body_preview = redact_sensitive_text(resp.text[:300].replace("\n", " ").strip())
|
| 464 |
+
raise RuntimeError(
|
| 465 |
+
"POS 接口返回非 JSON:"
|
| 466 |
+
f"HTTP {resp.status_code}; "
|
| 467 |
+
f"代理上游状态={proxy_status}; "
|
| 468 |
+
f"代理错误={proxy_error}; "
|
| 469 |
+
f"Content-Type={content_type or '-'}; "
|
| 470 |
+
f"响应预览={body_preview or '<empty>'}"
|
| 471 |
+
) from exc
|
| 472 |
|
| 473 |
def fetch_sessions(self, show_date: str) -> List[dict]:
|
| 474 |
payload = self.request_json(
|
pos_proxy.py
CHANGED
|
@@ -71,13 +71,13 @@ def build_pos_url(origin_url, proxy_url=None, origin_base_url=None):
|
|
| 71 |
def with_pos_proxy_headers(headers=None, proxy_url=None):
|
| 72 |
headers = dict(headers or {})
|
| 73 |
if get_pos_proxy_base_url(proxy_url):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
or os.getenv("NEW_API_PROXY_TOKEN", "").strip()
|
| 78 |
-
)
|
| 79 |
if proxy_token:
|
| 80 |
headers[POS_PROXY_TOKEN_HEADER] = proxy_token
|
|
|
|
|
|
|
| 81 |
return headers
|
| 82 |
|
| 83 |
|
|
|
|
| 71 |
def with_pos_proxy_headers(headers=None, proxy_url=None):
|
| 72 |
headers = dict(headers or {})
|
| 73 |
if get_pos_proxy_base_url(proxy_url):
|
| 74 |
+
pos_proxy_token = os.getenv(POS_PROXY_TOKEN_ENV, "").strip()
|
| 75 |
+
tms_proxy_token = os.getenv(TMS_PROXY_TOKEN_ENV, "").strip()
|
| 76 |
+
proxy_token = pos_proxy_token or tms_proxy_token or os.getenv("NEW_API_PROXY_TOKEN", "").strip()
|
|
|
|
|
|
|
| 77 |
if proxy_token:
|
| 78 |
headers[POS_PROXY_TOKEN_HEADER] = proxy_token
|
| 79 |
+
if tms_proxy_token:
|
| 80 |
+
headers["X-TMS-Proxy-Token"] = tms_proxy_token
|
| 81 |
return headers
|
| 82 |
|
| 83 |
|
tencent-edgeone-pages/edge-functions/_shared/pos-proxy.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
const POS_ORIGIN = "https://pos.hengdianfilm.com";
|
| 2 |
const PROXY_TOKEN_HEADER = "X-POS-Proxy-Token";
|
|
|
|
| 3 |
|
| 4 |
export function proxyHeaders(extra = {}) {
|
| 5 |
return {
|
|
@@ -39,7 +40,10 @@ export function verifyProxyToken(request, context) {
|
|
| 39 |
headers: proxyHeaders({ "X-POS-Proxy-Error": "missing-token-config" }),
|
| 40 |
});
|
| 41 |
}
|
| 42 |
-
if (
|
|
|
|
|
|
|
|
|
|
| 43 |
return new Response("Proxy token mismatch", {
|
| 44 |
status: 403,
|
| 45 |
headers: proxyHeaders({ "X-POS-Proxy-Error": "token-mismatch" }),
|
|
@@ -51,4 +55,3 @@ export function verifyProxyToken(request, context) {
|
|
| 51 |
export function posUrl(pathAndQuery) {
|
| 52 |
return new URL(pathAndQuery, POS_ORIGIN).toString();
|
| 53 |
}
|
| 54 |
-
|
|
|
|
| 1 |
const POS_ORIGIN = "https://pos.hengdianfilm.com";
|
| 2 |
const PROXY_TOKEN_HEADER = "X-POS-Proxy-Token";
|
| 3 |
+
const TMS_PROXY_TOKEN_HEADER = "X-TMS-Proxy-Token";
|
| 4 |
|
| 5 |
export function proxyHeaders(extra = {}) {
|
| 6 |
return {
|
|
|
|
| 40 |
headers: proxyHeaders({ "X-POS-Proxy-Error": "missing-token-config" }),
|
| 41 |
});
|
| 42 |
}
|
| 43 |
+
if (
|
| 44 |
+
request.headers.get(PROXY_TOKEN_HEADER) !== expectedToken
|
| 45 |
+
&& request.headers.get(TMS_PROXY_TOKEN_HEADER) !== expectedToken
|
| 46 |
+
) {
|
| 47 |
return new Response("Proxy token mismatch", {
|
| 48 |
status: 403,
|
| 49 |
headers: proxyHeaders({ "X-POS-Proxy-Error": "token-mismatch" }),
|
|
|
|
| 55 |
export function posUrl(pathAndQuery) {
|
| 56 |
return new URL(pathAndQuery, POS_ORIGIN).toString();
|
| 57 |
}
|
|
|
tencent-edgeone-pages/edge-functions/pos-api/[[default]].js
CHANGED
|
@@ -16,6 +16,7 @@ export default async function onRequest(context) {
|
|
| 16 |
const targetUrl = posUrl(`${upstreamPath}${requestUrl.search}`);
|
| 17 |
const headers = new Headers(request.headers);
|
| 18 |
headers.delete("X-POS-Proxy-Token");
|
|
|
|
| 19 |
headers.delete("Host");
|
| 20 |
headers.delete("X-Forwarded-For");
|
| 21 |
headers.delete("X-Real-IP");
|
|
|
|
| 16 |
const targetUrl = posUrl(`${upstreamPath}${requestUrl.search}`);
|
| 17 |
const headers = new Headers(request.headers);
|
| 18 |
headers.delete("X-POS-Proxy-Token");
|
| 19 |
+
headers.delete("X-TMS-Proxy-Token");
|
| 20 |
headers.delete("Host");
|
| 21 |
headers.delete("X-Forwarded-For");
|
| 22 |
headers.delete("X-Real-IP");
|