|
| 1 | +"""mimic CLI — capture any iOS app, generate a client. |
| 2 | +
|
| 3 | + mimic record start the proxy + print iPhone setup steps |
| 4 | + mimic hosts list captured hosts (pick your API host here) |
| 5 | + mimic learn <host> show the endpoints mimic saw for a host |
| 6 | + mimic gen <host> AI-write a Python client for a host |
| 7 | + mimic doctor check your setup |
| 8 | +""" |
| 9 | +import argparse |
| 10 | +import re |
| 11 | +import shutil |
| 12 | +import socket |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | + |
| 16 | +from . import codegen |
| 17 | +from .sources import mitm |
| 18 | + |
| 19 | + |
| 20 | +def _mitm_and_flows(): |
| 21 | + m = mitm.Mitm() |
| 22 | + return m, m.flows() |
| 23 | + |
| 24 | + |
| 25 | +def _lan_ip(): |
| 26 | + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 27 | + try: |
| 28 | + s.connect(("8.8.8.8", 80)) |
| 29 | + return s.getsockname()[0] |
| 30 | + except OSError: |
| 31 | + return "<this-machine-ip>" |
| 32 | + finally: |
| 33 | + s.close() |
| 34 | + |
| 35 | + |
| 36 | +def _mitmweb_cmd(): |
| 37 | + """Prefer mitmweb on PATH; otherwise run it ephemerally through uv.""" |
| 38 | + if shutil.which("mitmweb"): |
| 39 | + return ["mitmweb"] |
| 40 | + if shutil.which("uvx"): |
| 41 | + return ["uvx", "--from", "mitmproxy", "mitmweb"] |
| 42 | + return None |
| 43 | + |
| 44 | + |
| 45 | +def cmd_record(args): |
| 46 | + ip = _lan_ip() |
| 47 | + print( |
| 48 | + f""" |
| 49 | +iPhone capture — do this once, then just reopen the app to add traffic: |
| 50 | +
|
| 51 | + 1. iPhone → Settings → Wi-Fi → (your network) ⓘ → Configure Proxy → Manual |
| 52 | + Server: {ip} Port: 8080 |
| 53 | + 2. Safari → http://mitm.it → download the Apple (.pem) profile |
| 54 | + 3. Settings → General → VPN & Device Management → install the profile |
| 55 | + 4. Settings → General → About → Certificate Trust Settings |
| 56 | + → turn ON full trust for "mitmproxy" ← everyone forgets this |
| 57 | + 5. open the target app and use it normally |
| 58 | + 6. back here: mimic hosts then mimic gen <api-host> |
| 59 | +
|
| 60 | + mitmweb dashboard: http://127.0.0.1:8081 (mimic reads flows from here) |
| 61 | +
|
| 62 | + Some apps (banks, Instagram) pin their certificate, so a proxy sees no |
| 63 | + usable traffic — those aren't supported. Many apps aren't pinned and just |
| 64 | + work; if `mimic hosts` shows the app's API host, you're good. |
| 65 | +""" |
| 66 | + ) |
| 67 | + cmd = _mitmweb_cmd() |
| 68 | + if not cmd: |
| 69 | + sys.exit( |
| 70 | + "no proxy available — install uv (https://astral.sh/uv) so mimic can\n" |
| 71 | + "run mitmproxy for you, or `pipx install mitmproxy` yourself." |
| 72 | + ) |
| 73 | + sys.stdout.flush() # show the steps before the proxy takes over the terminal |
| 74 | + try: |
| 75 | + subprocess.run(cmd) |
| 76 | + except FileNotFoundError: |
| 77 | + sys.exit("failed to launch mitmweb") |
| 78 | + |
| 79 | + |
| 80 | +def cmd_doctor(args): |
| 81 | + ok = True |
| 82 | + |
| 83 | + def check(name, present, fix): |
| 84 | + nonlocal ok |
| 85 | + mark = "ok " if present else "MISSING" |
| 86 | + print(f" [{mark}] {name}") |
| 87 | + if not present: |
| 88 | + ok = False |
| 89 | + print(f" → {fix}") |
| 90 | + |
| 91 | + print("mimic setup check:\n") |
| 92 | + check("proxy (mitmweb or uvx)", _mitmweb_cmd() is not None, |
| 93 | + "install uv: curl -LsSf https://astral.sh/uv/install.sh | sh") |
| 94 | + check("claude CLI (for `mimic gen`)", shutil.which("claude") is not None, |
| 95 | + "install Claude Code, or use `mimic gen --prompt-only`") |
| 96 | + reachable = False |
| 97 | + try: |
| 98 | + mitm.Mitm().flows() |
| 99 | + reachable = True |
| 100 | + except mitm.MitmError: |
| 101 | + pass |
| 102 | + check("mitmweb running + reachable", reachable, |
| 103 | + "run `mimic record` in another terminal") |
| 104 | + print(f"\nLAN IP for the iPhone proxy: {_lan_ip()}:8080") |
| 105 | + sys.exit(0 if ok else 1) |
| 106 | + |
| 107 | + |
| 108 | +def cmd_hosts(args): |
| 109 | + _, flows = _mitm_and_flows() |
| 110 | + rows = mitm.hosts(flows) |
| 111 | + if not rows: |
| 112 | + sys.exit("no traffic captured yet — run `mimic record` and use the app") |
| 113 | + print(f"{'requests':>9} host") |
| 114 | + for host, n in rows: |
| 115 | + print(f"{n:>9} {host}") |
| 116 | + print("\nPick your API host (usually the one with JSON, not media/cdn).") |
| 117 | + |
| 118 | + |
| 119 | +def cmd_learn(args): |
| 120 | + m, flows = _mitm_and_flows() |
| 121 | + eps = mitm.endpoints(m, flows, args.host) |
| 122 | + if not eps: |
| 123 | + sys.exit(f"no requests to {args.host} captured") |
| 124 | + print(f"{args.host}: {len(eps)} endpoints\n") |
| 125 | + for e in eps: |
| 126 | + print(f" {e['method']:5s} {e['path']} -> {e['status']}") |
| 127 | + |
| 128 | + |
| 129 | +def cmd_gen(args): |
| 130 | + m, flows = _mitm_and_flows() |
| 131 | + eps = mitm.endpoints(m, flows, args.host) |
| 132 | + if not eps: |
| 133 | + sys.exit(f"no requests to {args.host} captured") |
| 134 | + |
| 135 | + if args.prompt_only: |
| 136 | + print(codegen.build_prompt(args.host, eps)) |
| 137 | + return |
| 138 | + |
| 139 | + out = args.out or _default_out(args.host) |
| 140 | + print(f"asking claude to write a client from {len(eps)} endpoints…", file=sys.stderr) |
| 141 | + source = codegen.generate(args.host, eps, model=args.model) |
| 142 | + with open(out, "w") as f: |
| 143 | + f.write(source) |
| 144 | + cls = _class_name(source) |
| 145 | + print(f"\nwrote {out}") |
| 146 | + print(f"\n from {out[:-3]} import {cls or 'Client'}") |
| 147 | + print(f" acc = {cls or 'Client'}()") |
| 148 | + print(" # then call the generated methods\n") |
| 149 | + |
| 150 | + |
| 151 | +def _default_out(host): |
| 152 | + stem = re.sub(r"[^a-z0-9]+", "_", host.split(".")[0].lower()).strip("_") |
| 153 | + return f"{stem or 'app'}_client.py" |
| 154 | + |
| 155 | + |
| 156 | +def _class_name(source): |
| 157 | + m = re.search(r"class\s+(\w+)\s*\(", source) |
| 158 | + return m.group(1) if m else None |
| 159 | + |
| 160 | + |
| 161 | +def main(argv=None): |
| 162 | + p = argparse.ArgumentParser(prog="mimic", description=__doc__, |
| 163 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 164 | + sub = p.add_subparsers(dest="cmd", required=True) |
| 165 | + |
| 166 | + sub.add_parser("record", help="start the proxy + iPhone setup steps").set_defaults(func=cmd_record) |
| 167 | + sub.add_parser("doctor", help="check your setup").set_defaults(func=cmd_doctor) |
| 168 | + sub.add_parser("hosts", help="list captured hosts").set_defaults(func=cmd_hosts) |
| 169 | + |
| 170 | + lp = sub.add_parser("learn", help="show endpoints for a host") |
| 171 | + lp.add_argument("host") |
| 172 | + lp.set_defaults(func=cmd_learn) |
| 173 | + |
| 174 | + gp = sub.add_parser("gen", help="AI-generate a client for a host") |
| 175 | + gp.add_argument("host") |
| 176 | + gp.add_argument("-o", "--out", help="output .py path") |
| 177 | + gp.add_argument("--model", default="sonnet", help="claude model (default: sonnet)") |
| 178 | + gp.add_argument("--prompt-only", action="store_true", help="print the prompt instead of calling claude") |
| 179 | + gp.set_defaults(func=cmd_gen) |
| 180 | + |
| 181 | + args = p.parse_args(argv) |
| 182 | + args.func(args) |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + main() |
0 commit comments