Files
memind/scripts/wechat-egress-proxy-105.py
T
john 229805a070 Improve WeChat MP replies and ship MindSpace/H5 production updates.
Add WeChat service account routing with sync acks, connectivity tests, and context isolation; document deploy runbooks; and bundle related MindSpace, voice, Plaza, and server gateway changes for production rollout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-19 23:06:43 +08:00

68 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import urllib.error
import urllib.parse
import urllib.request
ALLOWED_PATHS = {
"/cgi-bin/stable_token",
"/cgi-bin/message/custom/send",
"/cgi-bin/menu/create",
"/cgi-bin/menu/get",
"/cgi-bin/ticket/getticket",
}
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/healthz":
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(b"ok")
return
self._proxy("GET")
def do_POST(self):
self._proxy("POST")
def _proxy(self, method):
parsed = urllib.parse.urlsplit(self.path)
if parsed.path not in ALLOWED_PATHS:
self.send_error(403)
return
length = int(self.headers.get("Content-Length", "0")) if method == "POST" else 0
body = self.rfile.read(length) if length else None
url = "https://api.weixin.qq.com" + self.path
headers = {
"Content-Type": self.headers.get("Content-Type", "application/json"),
}
req = urllib.request.Request(url, data=body, headers=headers, method=method)
try:
with urllib.request.urlopen(req, timeout=20) as resp:
payload = resp.read()
self.send_response(resp.status)
self.send_header("Content-Type", resp.headers.get("Content-Type", "application/json"))
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
except urllib.error.HTTPError as err:
payload = err.read()
self.send_response(err.code)
self.send_header("Content-Type", err.headers.get("Content-Type", "application/json"))
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def log_message(self, fmt, *args):
return
if __name__ == "__main__":
host = os.environ.get("WECHAT_EGRESS_HOST", "127.0.0.1")
port = int(os.environ.get("WECHAT_EGRESS_PORT", "19090"))
HTTPServer((host, port), Handler).serve_forever()