#!/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()
