flagtldr / scripts /e2e /mock-x-server.py
botezx's picture
Grok Bot
FlagTLDR 0.5.0 (snapshot of local f98bb37)
043267b
Raw History Blame Contribute Delete
6.91 kB
"""HTTPS mock of x.com for local demos: serves fixtures/mock-x/index.html for every page
and the synthetic TweetDetail JSON for /i/api/graphql/*/TweetDetail; /i/grok* serves a mock Grok
page (fixtures/mock-x/grok.html) with the same framing headers as the real x.com.
GET /__mock/grok-mode?broken|deny|ok switches the Grok page to one without a composer, or to one that refuses
framing (X-Frame-Options: DENY -> iframe refused -> background-tab fallback).
/i/api/graphql/*/AboutAccountQuery serves fixtures/about/<handle>.json with x-rate-limit-* headers;
GET /__mock/about-mode?ok|429|500 switches it, /__mock/about-stats returns {"count": n, "handles": {...}} (test only). Chrome is pointed
here with --host-resolver-rules="MAP x.com 127.0.0.1:8443" --ignore-certificate-errors."""
import http.server, json, ssl, subprocess, sys, time, urllib.parse
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
FIX = ROOT / "extension" / "fixtures"
CERT = ROOT / ".cache" / "mock-x-cert.pem"
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8443
if not CERT.exists():
subprocess.run(["openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "30", "-subj", "/CN=x.com",
"-keyout", str(CERT), "-out", str(CERT)], check=True, capture_output=True)
MODE = {"grok": "ok", "about": "ok"}
GROK_SENT = [] # v0.4.2: prompts the mock Grok received (POST /__mock/grok-sent; GET /__mock/grok-stats[?reset])
ABOUT_STATS = {"count": 0, "handles": {}, "times": []}
ABOUT_LIMIT = {"limit": 50, "remaining": 50, "reset": int(time.time()) + 900}
# Same framing policy as real x.com (checked 2026-10-23): same-origin framing allowed.
FRAME_HEADERS = {"x-frame-options": "SAMEORIGIN",
"content-security-policy": "frame-ancestors 'self' https://x.com https://twitter.com"}
BROKEN = b"<!doctype html><title>Grok</title><main><p>Something went wrong. Try reloading.</p></main>"
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
extra = {}
if self.path.startswith("/__mock/grok-mode"):
MODE["grok"] = "broken" if "broken" in self.path else "deny" if "deny" in self.path else "ok"
body, ctype = MODE["grok"].encode(), "text/plain"
elif self.path.startswith("/__mock/about-mode"):
MODE["about"] = "429" if "429" in self.path else "500" if "500" in self.path else "ok"
if MODE["about"] == "ok":
ABOUT_STATS.update(count=0, handles={}, times=[])
ABOUT_LIMIT.update(remaining=ABOUT_LIMIT["limit"], reset=int(time.time()) + 900)
body, ctype = MODE["about"].encode(), "text/plain"
elif self.path.startswith("/__mock/grok-stats"):
if "reset" in self.path:
GROK_SENT.clear()
body, ctype = json.dumps({"count": len(GROK_SENT), "prompts": GROK_SENT}).encode(), "application/json"
elif self.path.startswith("/__mock/about-stats"):
body, ctype = json.dumps(ABOUT_STATS).encode(), "application/json"
elif "/i/api/graphql/" in self.path and "AboutAccountQuery" in self.path:
q = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
handle = json.loads(q.get("variables", ["{}"])[0]).get("screenName", "")
ABOUT_STATS["count"] += 1
ABOUT_STATS["handles"][handle] = ABOUT_STATS["handles"].get(handle, 0) + 1
ABOUT_STATS["times"].append(round(time.time(), 2))
ABOUT_LIMIT["remaining"] = max(0, ABOUT_LIMIT["remaining"] - 1)
extra = {"x-rate-limit-limit": str(ABOUT_LIMIT["limit"]), "x-rate-limit-remaining": str(ABOUT_LIMIT["remaining"]),
"x-rate-limit-reset": str(ABOUT_LIMIT["reset"])}
f = FIX / "about" / (handle.lower() + ".json")
ctype = "application/json"
if MODE["about"] == "429":
extra["x-rate-limit-remaining"] = "0"
return self._send(429, b'{"errors":[{"code":88,"message":"Rate limit exceeded"}]}', ctype, extra)
if MODE["about"] == "500":
return self._send(500, b'{"errors":[{"message":"oops"}]}', ctype, extra)
body = f.read_bytes() if handle and f.exists() else b'{"data":{"user_result_by_screen_name":{"result":{"__typename":"UserUnavailable"}}}}'
elif self.path.startswith("/i/grok"):
body = BROKEN if MODE["grok"] == "broken" else (FIX / "mock-x" / "grok.html").read_bytes()
ctype, extra = "text/html; charset=utf-8", FRAME_HEADERS
if MODE["grok"] == "deny":
extra = {"x-frame-options": "DENY", "content-security-policy": "frame-ancestors 'none'"}
elif "/i/api/graphql/" in self.path and "TweetDetail" in self.path:
name = "tweetdetail.branch.json" if "branch=1" in self.path else "tweetdetail.sample.json"
body, ctype = (FIX / name).read_bytes(), "application/json"
elif "/i/api/graphql/" in self.path and "HomeTimeline" in self.path: # v0.4.3: unrelated home timeline
body, ctype = (FIX / ("hometimeline.focal2.json" if "tl=focal2" in self.path else "hometimeline.unrelated.json")).read_bytes(), "application/json"
elif self.path.startswith("/__mock/timeline.json"): # v0.4.3: &tl=focal2 = botex-human's 2nd reported case (17:48), invented accounts
body, ctype = (FIX / ("hometimeline.focal2.json" if "tl=focal2" in self.path else "hometimeline.unrelated.json")).read_bytes(), "application/json"
elif self.path.startswith("/__mock/branch.json"): # same data, but not a GraphQL URL (DOM-only fallback test)
body, ctype = (FIX / "tweetdetail.branch.json").read_bytes(), "application/json"
else:
body, ctype = (FIX / "mock-x" / "index.html").read_bytes(), "text/html; charset=utf-8"
extra = FRAME_HEADERS
self._send(200, body, ctype, extra)
def do_POST(self):
n = int(self.headers.get("content-length") or 0)
data = self.rfile.read(n) if n else b""
if self.path.startswith("/__mock/grok-sent"):
GROK_SENT.append(data.decode("utf-8", "replace"))
return self._send(200, b"ok", "text/plain", {})
self._send(404, b"", "text/plain", {})
def _send(self, code, body, ctype, extra):
self.send_response(code)
self.send_header("content-type", ctype)
self.send_header("content-length", str(len(body)))
for k, v in extra.items():
self.send_header(k, v)
self.end_headers()
self.wfile.write(body)
def log_message(self, *a):
pass
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(str(CERT))
srv = http.server.ThreadingHTTPServer(("127.0.0.1", PORT), H)
srv.socket = ctx.wrap_socket(srv.socket, server_side=True)
print(f"mock x.com on https://127.0.0.1:{PORT}", flush=True)
srv.serve_forever()