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

ScenariomitmproxyNotes
A: Force new IPClient controls connectionsmitmproxy passes through
B: Keep IPClient reuses connectionsmitmproxy preserves upstream
C: Proxy-TunnelAddon injects headerApplied to all requests automatically

Troubleshooting

IssueCauseFix
Start failedmitmdump not installedpip install mitmproxy
Auth failed--upstream-auth wrongVerify credentials
TimeoutProxy unreachableCheck t.16yun.cn:31111

Need an enterprise proxy plan?

We can tailor architecture to your target domains, concurrency, and reliability goals.