from mitmproxy import http


ORIGIN_HEADER = "X-V8CTF-Origin-Host"
BACKEND_HOST = "localhost"
BACKEND_PORT = 1234


class Redirector:
    def request(self, flow: http.HTTPFlow) -> None:
        flow.request.headers[ORIGIN_HEADER] = flow.request.host
        flow.request.host = BACKEND_HOST
        flow.request.port = BACKEND_PORT
        flow.request.scheme = "http"

    def http_connect(self, flow: http.HTTPFlow) -> None:
        flow.server_conn.address = (BACKEND_HOST, BACKEND_PORT)


addons = [Redirector()]
