• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

synchronizing / mitm / 23581258074

26 Mar 2026 06:45AM UTC coverage: 86.264% (-7.7%) from 93.96%
23581258074

Pull #41

github

web-flow
Merge 78cf0ad5c into a189234b3
Pull Request #41: Modernize project: CLI, dependency removal, restructure

401 of 457 new or added lines in 12 files covered. (87.75%)

22 existing lines in 1 file now uncovered.

628 of 728 relevant lines covered (86.26%)

4.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/mitm/cli.py
1
"""
2
Command-line interface.
3
"""
4

NEW
5
from __future__ import annotations
×
6

NEW
7
import asyncio
×
NEW
8
import logging
×
NEW
9
import os
×
NEW
10
import sys
×
11

NEW
12
import click
×
13

NEW
14
from mitm import __data__
×
NEW
15
from mitm.extension.middleware import CLILog
×
NEW
16
from mitm.proxy import MITM
×
17

NEW
18
PROXY_ENV_KEYS = [
×
19
    "HTTP_PROXY",
20
    "HTTPS_PROXY",
21
    "http_proxy",
22
    "https_proxy",
23
]
24

NEW
25
CERT_ENV_KEYS = [
×
26
    "SSL_CERT_FILE",
27
    "REQUESTS_CA_BUNDLE",
28
    "NODE_EXTRA_CA_CERTS",
29
    "CURL_CA_BUNDLE",
30
]
31

32

NEW
33
@click.command(context_settings={"ignore_unknown_options": True})
×
NEW
34
@click.option("--host", default="127.0.0.1", show_default=True, help="Host to listen on.")
×
NEW
35
@click.option("-p", "--port", default=8888, show_default=True, type=int, help="Port to listen on.")
×
NEW
36
@click.argument("command", nargs=-1, type=click.UNPROCESSED)
×
NEW
37
def main(host: str, port: int, command: tuple[str, ...]):
×
38
    """
39
    Man-in-the-middle proxy.
40

41
    Run standalone as a proxy server, or wrap a COMMAND to capture its traffic.
42

43
    \b
44
    Examples:
45
        mitm                              Start proxy on default port.
46
        mitm -p 9999                      Start proxy on port 9999.
47
        mitm -- curl https://example.com  Capture curl's traffic.
48
        mitm -- python my_script.py       Capture a script's traffic.
49
    """
NEW
50
    logging.getLogger("mitm").setLevel(logging.CRITICAL)
×
51

NEW
52
    if command:
×
NEW
53
        code = asyncio.run(wrap(host, port, command))
×
NEW
54
        sys.exit(code)
×
55
    else:
NEW
56
        MITM(host=host, port=port, middlewares=[CLILog]).run()
×
57

58

NEW
59
async def wrap(host: str, port: int, command: tuple[str, ...]) -> int:
×
60
    """
61
    Start the proxy, run a command with traffic routed through it, then shut down.
62

63
    Args:
64
        host: Proxy bind host.
65
        port: Proxy bind port.
66
        command: The command and its arguments.
67

68
    Returns:
69
        The child process exit code.
70
    """
NEW
71
    async with MITM(host=host, port=port, middlewares=[CLILog]):
×
NEW
72
        env = build_env(host, port)
×
73

NEW
74
        try:
×
NEW
75
            proc = await asyncio.create_subprocess_exec(
×
76
                *command,
77
                env=env,
78
                stdout=sys.stdout,
79
                stderr=sys.stderr,
80
            )
NEW
81
            await proc.wait()
×
NEW
82
            return proc.returncode or 0
×
NEW
83
        except FileNotFoundError:
×
NEW
84
            click.echo(f"mitm: command not found: {command[0]}", err=True)
×
NEW
85
            return 127
×
86

87

NEW
88
def build_env(host: str, port: int) -> dict[str, str]:
×
89
    """
90
    Build an environment dict that routes traffic through the proxy.
91

92
    Args:
93
        host: Proxy host.
94
        port: Proxy port.
95

96
    Returns:
97
        A copy of the current environment with proxy and CA cert variables set.
98
    """
NEW
99
    env = os.environ.copy()
×
100

NEW
101
    proxy_url = f"http://{host}:{port}"
×
NEW
102
    for key in PROXY_ENV_KEYS:
×
NEW
103
        env[key] = proxy_url
×
104

NEW
105
    cert_path = str(__data__ / "mitm.crt")
×
NEW
106
    for key in CERT_ENV_KEYS:
×
NEW
107
        env[key] = cert_path
×
108

NEW
109
    return env
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc