How to Use mitmproxy With a Residential Proxy (Upstream Mode)

Run mitmproxy in upstream mode, and every request leaves through your residential gateway while you watch it in full: mitmproxy --mode upstream:http://gateway:port --set upstream_auth=username:password. Point your client at 127.0.0.1:8080 and install mitmproxy's CA so HTTPS works. A config.yaml makes the setup permanent, one mitmproxy per session gives you two exits side by side, and a short Python addon rotates sessions on every new connection without restarts. Apps with no proxy settings go through Proxifier on Windows or proxychains in WSL. Buy an HTTP or HTTPS gateway with sticky sessions, because SOCKS does not work in upstream mode.
Mitmproxy with a residential proxy helps you keep tabs on what your client sends and what IP the target gets. This pairing lets you watch a request from your machine go through an intermediary to the target and back to you, with every header and body in view to inspect and debug.
So this guide covers five different ways to chain mitmproxy to a residential gateway, their failures, and what proxies to buy.
How to chain mitmproxy to a residential proxy
There are multiple ways to do it: using a config file, plugging in a Python script, or deploying proxychains for apps without native proxy settings.
Before you proceed, have your residential proxy credentials ready. If you don't have them, you can claim 1GB of free residential data from us by signing up and completing a short KYC. The commands below use our gateway’s host and port, so replace them with whatever your dashboard provides, which may be a different port per session, so copy the exact endpoint your dashboard shows.
Also, install mitmproxy and choose your tool. Mitmproxy ships with a terminal UI, mitmweb has a browser UI, and mitmdump suits scripts and servers. They pretty much work similarly, and the only difference is the interface. We have primarily used mitmproxy (for greater visibility) in this tutorial and mitmdump when a log was enough.
Note: Everything below was run on mitmproxy 12.2.2 on Windows 11, except Method 5, which is Unix-only and was run on 12.3.1 under WSL.
Method 1: mitmproxy upstream mode with upstream_auth
Upstream mode tells mitmproxy not to reach the target site by itself and instead to hand off every request to the second proxy, which is a residential gateway in our case. This method is the baseline of every other method and proves that the chain works. In a nutshell, a request flows from the client to mitmproxy on the local machine, via the proxy gateway, and then to the IP echo site.
Here, you can see mitmproxy record traffic and the IP echo record a residential IP, since the proxy gateway is the preceding hop. We do this with just two settings. --mode tells mitmproxy where to forward everything, and upstream_auth carries the residential username and password. The client points at the mitmproxy instance and trusts its certificate for this to work.
mitmproxy --mode upstream:http://residential.byteful.com:8309 --set upstream_auth=username:passwordNote that credentials go separately from the gateway URL. Here, --mode takes the host and port only, and the username and password go with upstream_auth. So remember to split credentials from the URL since most proxy dashboards give them combined. Also, mitmweb and mitmdump take the same flags, so you can swap the first words and leave the rest of the command as-is.

The terminal interface confirms the chain in its status bar, with the upstream hop on the bottom left and the active port on the far right. This implies that the flags work without us sending a single request. However, with mitmdump, you'll get a startup line instead since it has no interface.
The next step is to point the client at 127.0.0.1:8080 and install mitmproxy’s CA for HTTPS. In our testing, curl’s Schannel backend accepted --cacert but still rejected the chain with CERT_TRUST_IS_UNTRUSTED_ROOT. Besides, curl ignores CURL_CA_BUNDLE entirely when the backend is Schannel. Therefore, we put the CA in the Windows certificate store instead:
certutil -addstore -user Root "$env:USERPROFILE\.mitmproxy\mitmproxy-ca-cert.cer"Finally, we hit ipinfo to check the flow:
curl.exe -x "http://127.0.0.1:8080" "https://ipinfo.io/json"
We have used Byteful's sticky sessions to keep the same exit for the entire run. You can check that the chained request returned the same IP and ASN. Please note that the middle call uses a placeholder instead of real credentials, so the gateway returned a 407. However, the two calls on either side of it have the same exit IP. One goes through the gateway alone with curl -x, and the other goes through the full mitmproxy chain.
Keep in mind that mitmproxy sends credentials as HTTP basic authentication on every request it forwards, including every CONNECT, and your shell history has them in plain text.
Besides, this article uses an HTTP endpoint since that's most commonly supplied. However, we recommend using an HTTPS gateway (it's generally a different port) if your provider supports it (Byteful does) so the credentials remain invisible to anyone snooping in the middle.
Note: Our residential network blocks requests to bare IP addresses. This guide is unaffected because mitmproxy passes the hostname to the gateway, but a request to a literal IP URL will fail through the chain.
Method 2: mitmproxy config.yaml for a persistent residential upstream

This is a convenience upgrade that relieves you from typing the whole command every time and instead uses one config file that mitmproxy reads at startup. In addition, this keeps credentials out of your shell history. You can create it at ~/.mitmproxy/config.yaml, or %USERPROFILE%.mitmproxy\config.yaml on Windows.

You can skip listen_port if 8080 is free. Now start mitmproxy without any arguments, and the status bar will still show the upstream, proving it read the configuration file.
Importantly, mode must be a list. The single-line form turns up in GitHub issues and in other guides, and on mitmproxy 12.2.2 it crashes at startup right away. Additionally, you must acknowledge that the file itself is plain text, so it doesn't protect your credentials beyond keeping them out of shell history.
One config file serves one session, which is enough for a standing setup. However, the next section covers how to compare two exits at the same time.
Method 3: One mitmproxy instance per residential exit
Sometimes you need multiple exits. For instance, to compare what a German and a US visitor see. This means two residential sessions running concurrently, sending the same request side by side.
For this, you need to run one mitmproxy per session. One process won't do, since upstream_auth works singularly for the whole process. Therefore, two listeners would share the same credentials and thus the same exit node. Here, we use mitmdump, so each process logs to its terminal instead of taking over the window with a full-screen interface, and each gets a separate port and session.
This helps the client pick an exit by selecting a port.
mitmdump --mode upstream:http://residential.byteful.com:8309@8080 --set upstream_auth=us_session_username:passwordmitmdump --mode upstream:http://residential.byteful.com:8309@8081 --set upstream_auth=de_session_username:passwordThe following images show a Maryland IP on 8080 and a Bavarian one on 8081.

Two things if you're continuing from the previous method. First, rename the previous config.yaml, because mitmproxy still reads it alongside your flags. Your --mode flag replaces the mode in the file, but the file's upstream_auth and listen_port still load, and listen_port applies whenever your --mode has no @port.
Second, free any port still held by an earlier mitmproxy instance, or you will get a bind error at startup. This looks like a client pointing to the wrong port, but it's actually the listener never coming up.
Method 4: Per-flow upstream selection with a mitmproxy addon
The previous method works fine for a few exits. However, it becomes tiresome when you need a dozen, since you need to stop mitmproxy and start it again for a different exit.
A short Python addon can fix this. Mitmproxy lets you run code at defined points for request handling, and one of those stages is at the start of the connection to the upstream proxy. This allows you to pick the session per connection without requiring a restart.
At Byteful, host and port don't change between sessions, since targeting is username-based. So, we used the same upstream address with two different usernames, one for Germany and one for the US.
import itertools
from base64 import b64encode
from mitmproxy import http
SESSIONS = [
"us_session_username",
"de_session_username",
]
PASSWORD = "your_password"
_sessions = itertools.cycle(SESSIONS)
def _basic(username: str) -> str:
token = b64encode(f"{username}:{PASSWORD}".encode()).decode()
return f"Basic {token}"
def http_connect_upstream(flow: http.HTTPFlow) -> None:
flow.request.headers["Proxy-Authorization"] = _basic(next(_sessions))Remember to run it without the upstream_auth flag, because this, along with the preceding Python script, writes to the same header, causing the rotation to silently stop. Also, ensure no config.yaml supplies the same option either.
mitmdump --mode upstream:http://residential.byteful.com:8309 -s rotate.pyThis confirms rotation, as four consecutive requests through port 8080 came back alternately from Maryland, Bavaria, Maryland, and Bavaria. The log shows one upstream connection per request and varying response sizes as each session hits a different exit. Here are the first two of the four:
[11:22:49] client connect
[11:22:49] server connect residential.byteful.com:8309 (103.106.231.96:8309)
GET https://ipinfo.io/json
200 OK 308b
[11:22:54] client connect
[11:22:54] server connect residential.byteful.com:8309 (103.106.231.96:8309)
GET https://ipinfo.io/json
200 OK 291bThe rotation here happens per upstream connection rather than per request, since the hook fires on each CONNECT. So if your client reuses connections, the exit IP remains the same until a new connection. Also note that this only works with HTTPS since plain HTTP doesn't produce a CONNECT and you're likely to get a 407 instead.
Every method till now assumes the client has proxy settings. However, the next method covers cases where the program has none.
Method 5: Use proxychains for apps without proxy settings
Many apps don’t come with proxy configurations, such as compiled binaries and game clients. And though Mitmproxy can catch traffic if the client doesn’t cooperate, you can’t chain it to a residential gateway.
Apps such as Proxifier can handle this natively for Windows. Alternatively, you can use proxychains inside WSL, as explained below. Proxychains only hooks programs running in WSL, so for this method we ran both mitmproxy and the client inside WSL.
First, write a config pointing at mitmproxy's listener:
strict_chain
proxy_dns
[ProxyList]
http 127.0.0.1 8080Then run the program through it, with no proxy flags of its own:
proxychains4 -f ~/proxychains.conf curl -sL http://icanhazip.comIt prints the chain line first, which shows the hop through 127.0.0.1:8080, and then the command output shows the residential IP. The residential hop and everything else stay where they are, quite similar to Method 1. Proxychain works by preloading a library that intercepts the client’s socket calls on the fly, so a statically linked binary escapes it.
The following figure shows one, BusyBox, fetching over plain HTTP and returning the machine's own IP with and without proxychains, while curl below it goes through the chain to the residential exit.

However, this method has a caveat: a failed hook doesn't always look like one. In our test, the same static BusyBox fetch over HTTPS came out at the residential exit even though proxychains never hooked BusyBox itself, because BusyBox shells out to a separate helper for TLS, and that helper went through the dynamic linker.
So, check the chain line rather than the IP, since it's the only way to confirm proxychains actually hooked the client.
Which residential proxy setup to use with mitmproxy
The methods above cover how to link mitmproxy to a residential gateway. This section, however, covers what to buy, with every point directly related to mitmproxy.
- Endpoint: Upstream mode takes an http:// or https:// gateway and nothing else. SOCKS won't work.
- Session: Look for sticky and rotating residential proxies. Our sticky sessions hold one IP for up to 24 hours, which helps during long debugging runs.
- Targeting: upstream_auth drives targeting alone since mitmproxy has no separate settings for it. It suits Byteful since our targeting is username-led, but check if you're elsewhere.
- Authentication: Supports username/password with upstream_auth, or you can whitelist the machine's IP to keep credentials out of config files and shell history.
Limits of mitmproxy’s upstream mode
Before you commit to mitmproxy, here are the limitations of this approach.
Per-host exceptions: Upstream mode has no configuration to exempt a specific host since it forwards everything. The only option in this case is to write code, as mitmproxy's own change_upstream_proxy.py example does.
No chaining: You can't chain Transparent, TUN, WireGuard, and local redirect modes, since they have no native upstream option. The savior in this case can be an addon setting server_conn.via or a method similar to using proxychains.
No DNS capture: Mitmproxy doesn't resolve the hostname, and it simply sends CONNECT host:port to the gateway, so there is no local DNS query to capture.
TLS handshake: Mitmproxy terminates TLS in the middle, so the handshake your target sees doesn't belong to your client. This restricts room for TLS testing against anti-bot security systems.
HTTP versions: Mitmproxy speaks HTTP/1.1 to the upstream proxy, with HTTP/2 negotiated separately on the client-facing and server-facing connections. HTTP/3 cannot travel through the chain because the CONNECT tunnel is TCP. Mitmproxy itself supports HTTP/3 since version 11, but only in the reverse, WireGuard, local, and transparent modes, which are the same modes that cannot take an upstream.
Troubleshooting mitmproxy with a residential proxy
Before going through the following table, test the gateway on its own with curl -x, so that you know what to troubleshoot.
| Symptom | Cause | Fix |
|---|---|---|
| 502 from mitmproxy naming a 407 from the gateway | Wrong gateway host, port, or credentials | Test the gateway alone with curl -x. Mitmproxy’s 502 names the upstream and the reason, which a bare 407 from curl doesn’t |
| No flows at all | The client isn’t pointed at mitmproxy, or the listener never came up | Check mitmproxy’s startup output for a bind error. A port still held by an earlier instance looks identical to a misconfigured client |
| HTTPS fails after HTTP works | The CA isn’t installed in that client | Install it. This has nothing to do with the residential hop. Browsers take it from mitm.it. On Windows, curl’s Schannel backend accepts --cacert but still failed chain validation in our test, so put the CA in the certificate store instead |
| CRYPT_E_REVOCATION_OFFLINE on Windows | Schannel is checking revocation and can’t reach the responder | Add --ssl-revoke-best-effort, which relaxes only the revocation check. --ssl-no-revoke also works but disables revocation checking entirely |
| Mitmproxy crashes at startup with a TypeError about mode | mode in config.yaml is a string, not a list | Make it a list. The traceback names the type, not the file, so it doesn’t point at the config |
| 407 on plain HTTP while HTTPS works, with the Method 4 addon | No CONNECT is sent for plain HTTP, so the hook never runs and no credentials are attached | Use HTTPS, or set the same header in a requestheaders hook |
| The addon loads but every request leaves on one session | upstream_auth is set as well, from a flag or a leftover config.yaml, and overwrites the script’s header | Remove it from both places |
| The exit IP changes between requests | A rotating endpoint | Switch to a sticky session for the length of the capture |
| The IP is right, but the location is wrong | The session expired, or the targeting parameters in the username are off | Check both. On Byteful’s gateway, a malformed targeting parameter failed authentication outright rather than returning the wrong place |
| Two lookups place the same exit in different towns | Geolocation databases disagree | Expect it. Country-level results are more reliable than city-level, and neither proves where a device sits |
| Everything is slow | Two hops plus a residential exit | Expect it, and keep captures small. In our run the first request through a new session was roughly three times slower than the ones after it |


