Python mitmproxy Tunnel Proxy: Forward to Crawler Proxy via Upstream Mode
mitmproxy upstream mode forwarding to 16Yun Crawler Proxy with addon-based Proxy-Tunnel injection.
16Yun Engineering TeamMay 31, 20261 min read
Architecture
Client → mitmproxy (localhost:38080) → 16Yun Crawler Proxy (t.16yun.cn:31111) → Target
Clients don't need proxy credentials — all proxy logic is centralized in the mitmproxy layer.
Start mitmproxy
mitmdump \
--listen-host 127.0.0.1 \
--listen-port 38080 \
--mode upstream:http://t.16yun.cn:31111 \
--upstream-auth username:password \
-s addon.py
Addon Script
# addon.py
import os
from mitmproxy import http
SEED = os.getenv("PROXY_TUNNEL", "mitmproxy-demo-1")
class UpstreamProxy:
def request(self, flow: http.HTTPFlow) -> None:
flow.request.headers.setdefault("Proxy-Tunnel", SEED)
addons = [UpstreamProxy()]
Client Usage
Clients point to local mitmproxy only:
import requests
session = requests.Session()
session.proxies = {
"http": "http://127.0.0.1:38080",
"https": "http://127.0.0.1:38080",
}
resp = session.get("https://httpbin.org/ip", timeout=30)
print(resp.json()['origin'])
Scenario Reference
| Scenario | mitmproxy | Notes |
|---|---|---|
| A: Force new IP | Client controls connections | mitmproxy passes through |
| B: Keep IP | Client reuses connections | mitmproxy preserves upstream |
| C: Proxy-Tunnel | Addon injects header | Applied to all requests automatically |
Troubleshooting
| Issue | Cause | Fix |
|---|---|---|
| Start failed | mitmdump not installed | pip install mitmproxy |
| Auth failed | --upstream-auth wrong | Verify credentials |
| Timeout | Proxy unreachable | Check t.16yun.cn:31111 |
Need an enterprise proxy plan?
We can tailor architecture to your target domains, concurrency, and reliability goals.