# Could mitmproxy be used for reverse proxy for multi sites?

**URL:** <https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253>\
**Category:** help\
**Created:** [December 7, 2016, 2:04pm UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253 "2016-12-07T14:04:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingbase](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kingbase](https://discourse.mitmproxy.org/u/kingbase)\
**Post date:** [December 7, 2016, 2:04pm UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/1 "2016-12-07T14:04:26Z")

</div>

I saw mitmproxy could be used as reverse proxy for one single site using the following command:

```
mitmproxy -R http://www.test.com -p 80

```

But I’m not sure could this be used for multi site in one command. For example, I want it as reverse proxy for [www.test.com](http://www.test.com) and [img.test.com](http://img.test.com) and [static.test.com](http://static.test.com), but when I read the docs, it says: only one site could be used one time.

So, may I do this by the script of mitmproxy?

Thanks very much.

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [December 8, 2016, 2:22pm UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/2 "2016-12-08T14:22:40Z")

</div>

You can re-route requests to different servers in reverse proxy mode. That being said, most of the time you probably want to run mitmproxy as a regular proxy then. If you are unsure, please take a look at [http://docs.mitmproxy.org/en/stable/modes.html](http://docs.mitmproxy.org/en/stable/modes.html). 🙂

If you know what you are doing and you still want to run as a reverse proxy, take a look at this:

> <https://github.com/mitmproxy/mitmproxy/blob/master/examples/complex/dns_spoofing.py>

---

<div class="post-metadata">

**Author:** ![kingbase](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kingbase](https://discourse.mitmproxy.org/u/kingbase)\
**Post date:** [December 9, 2016, 1:29am UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/3 "2016-12-09T01:29:14Z")

</div>

Hi, Hils.

Thanks for your reply.

The reason I use reverse proxy is that suppose Evil and Victim is in under same route, Evil is able to change the DNS setting of the route. So, Victim got the wrong DNS result of some website like \*.test.com point to the same IP: Evil’s IP.

So now it’s turn of mitmproxy. I want mitmproxy serve as a website which could response the request of [a.test.com](http://a.test.com), [b.test.com](http://b.test.com), [c.test.com](http://c.test.com) and so on. So I think I should use reverse proxy mode as the other modes could not act as a webserver. But I’m not sure whether I am understanding correctly.

---

<div class="post-metadata">

**Author:** ![kingbase](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kingbase](https://discourse.mitmproxy.org/u/kingbase)\
**Post date:** [December 9, 2016, 1:56am UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/4 "2016-12-09T01:56:43Z")

</div>

The command I run is:

```
mitmdump -R http://us.yahoo.com -p 80 -s redirect.py

```

I run it on my computer for experiment. And I set the DNS on my computer:

```
127.0.0.1 it.yahoo.com
127.0.0.1 news.yahoo.com

```

Then I just visit [news.yahoo.com](http://news.yahoo.com) on my computer, and I was getting the result of [us.yahoo.com](http://us.yahoo.com)

What I want is I visit [it.yahoo.com](http://it.yahoo.com), mitmproxy give me the result of [it.yahoo.com](http://it.yahoo.com). And when I visit [news.yahoo.com](http://news.yahoo.com), mitmproxy give me the result of [news.yahoo.com](http://news.yahoo.com).

I tried [https://github.com/mitmproxy/mitmproxy/blob/master/examples/complex/dns\_spoofing.py](https://github.com/mitmproxy/mitmproxy/blob/master/examples/complex/dns_spoofing.py) and found that when I was visiting [news.yahoo.com](http://news.yahoo.com), the script shows that in request flow.request.pretty\_host is not [news.yahoo.com](http://news.yahoo.com) but [us.yahoo.com](http://us.yahoo.com) in the command line.

I tried read the source and found in mitmproxy\protocol\[http.py:194](http://http.py:194)

```
        # update host header in reverse proxy mode
        if self.config.options.mode == "reverse":
            if six.PY2:
                flow.request.headers["Host"] = self.config.upstream_server.address.host.encode()
            else:
                flow.request.headers["Host"] = self.config.upstream_server.address.host
        # set upstream auth
        if self.mode == "upstream" and self.config.upstream_auth is not None:
            flow.request.headers["Proxy-Authorization"] = self.config.upstream_auth
        self.process_request_hook(flow)

```

This means before process\_request\_hook, the host in header was replace by self.config.upstream\_server.address.host which is [us.yahoo.com](http://us.yahoo.com). So I don’t think the script dns\_spoofing.py will work.

So, could I read the raw header in request stub in scripting? So I can replace the host header correctly. I didn’t found a proper way to do this. I’m not sure if flow.client\_conn could be used for this.

Thanks very much!

---

<div class="post-metadata">

**Author:** ![kingbase](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kingbase](https://discourse.mitmproxy.org/u/kingbase)\
**Post date:** [December 13, 2016, 3:59am UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/5 "2016-12-13T03:59:16Z")

</div>

hello @mhils could you help to have a look?

Thanks very much.

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [December 13, 2016, 12:16pm UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/6 "2016-12-13T12:16:34Z")

</div>

Thanks for investigating this. You should still be able to see the original header in `requestheaders` instead of `request`. If that’s the case, I’d be more than happy if you could send a quick PR that fixes the example!

Thanks,  
Max

---

<div class="post-metadata">

**Author:** ![kingbase](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kingbase](https://discourse.mitmproxy.org/u/kingbase)\
**Post date:** [December 21, 2016, 8:09am UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/7 "2016-12-21T08:09:02Z")

</div>

Sorry for the late reply.

I searched “requestheaders” in mitmproxy, found the following:

Occurrences of ‘requestheaders’ in Directory Lib\site-packages\mitmproxy with mask '\*.py’  
Lib\site-packages\mitmproxy\[controller.py](http://controller.py)  
"requestheaders",  
Lib\site-packages\mitmproxy\flow\[master.py](http://master.py)  
yield “requestheaders”, f  
def requestheaders(self, f):  
Lib\site-packages\mitmproxy\protocol\[http.py](http://http.py)  
self.channel.ask(“requestheaders”, flow)

But requestheaders could not be visited in the context of request function in my custom script.

So is there another way to find the raw header from client in the context of request function?

My script is very simple and looks just like this:

def request(flow):  
print(flow.requestheaders)

---

<div class="post-metadata">

**Author:** ![kingbase](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@kingbase](https://discourse.mitmproxy.org/u/kingbase)\
**Post date:** [December 26, 2016, 2:29am UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/8 "2016-12-26T02:29:10Z")

</div>

hello @mhils could you please have a look? Thanks very much.

---

<div class="post-metadata">

**Author:** ![amm0nite](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@amm0nite](https://discourse.mitmproxy.org/u/amm0nite)\
**Post date:** [February 1, 2017, 8:33pm UTC](https://discourse.mitmproxy.org/t/could-mitmproxy-be-used-for-reverse-proxy-for-multi-sites/253/9 "2017-02-01T20:33:54Z")

</div>

Hello, `dns_spoofing.py` is now fixed on mitmproxy master branch 🕶
