# Use reverse proxy mode to do real-time replay

**URL:** <https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369>\
**Category:** Uncategorized\
**Created:** [February 20, 2017, 4:04pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369 "2017-02-20T16:04:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ppsun](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@ppsun](https://discourse.mitmproxy.org/u/ppsun)\
**Post date:** [February 20, 2017, 4:04pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/1 "2017-02-20T16:04:11Z")

</div>

Suppose we have a real server A and a shadow server B, both of which are behind a mitmproxy instance that works in reverse mode. All the service requests are destined for server A through HTTPS originally.

Is it possible that, for each request destined for server A, we duplicate and replay that request to server B through an HTTPS connection simultaneously? Then the response from server A is redirected by mitmproxy to the original client; but the response from B will be dropped automatically. Here I want to do real-time replay which is different from other posts I have seen that record a log and then replay. Also, the mitmproxy needs to establish two https connections to A and B concurrently.

Please let me know if mitmproxy could do that using its scripting interface or how to extend it to achieve that.

---

<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:** [February 21, 2017, 2:03pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/2 "2017-02-21T14:03:37Z")

</div>

Hi @ppsun,

You are likely looking for something similar to [https://github.com/mitmproxy/mitmproxy/blob/master/examples/complex/dup\_and\_replay.py](https://github.com/mitmproxy/mitmproxy/blob/master/examples/complex/dup_and_replay.py) 🙂  
Does that help?

---

<div class="post-metadata">

**Author:** ![ppsun](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@ppsun](https://discourse.mitmproxy.org/u/ppsun)\
**Post date:** [February 21, 2017, 3:23pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/3 "2017-02-21T15:23:36Z")

</div>

Thanks. Actually I have noticed this script. However, it is replaying HTTP request. What if the proxy also talks to the shadow server B using HTTPS? Then the mitmproxy would also need to negotiate the TLS crypto parameters with B through the TLS handshake process. Will mitmproxy do TLS handshake with B automatically?

---

<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:** [February 21, 2017, 3:36pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/4 "2017-02-21T15:36:15Z")

</div>

Yes. Replay is fairly isolated from the actual proxy mechanism actually: [https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/proxy/protocol/http\_replay.py](https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/proxy/protocol/http_replay.py)

---

<div class="post-metadata">

**Author:** ![ppsun](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@ppsun](https://discourse.mitmproxy.org/u/ppsun)\
**Post date:** [February 21, 2017, 3:57pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/5 "2017-02-21T15:57:33Z")

</div>

Got it. I’ll check the source code and do a real-time replay test. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![ppsun](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@ppsun](https://discourse.mitmproxy.org/u/ppsun)\
**Post date:** [February 28, 2017, 7:32pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/6 "2017-02-28T19:32:33Z")

</div>

I have tested the dup\_and\_replay script for mitmproxy version 2.0.0. However, I encountered a couple of problems.

Problem#1: This line of script “ctx.master.view.add(f)” leads to an error message:

“AttributeError: ‘DumpMaster’ object has no attributed ‘view’”

Problem#2: then I go ahead and comment out the line mentioned in Problem#1, but the main mitmproxy thread got stuck when setting “block” to be True in the ‘replay\_request’ function. But if I set ‘block’ to false, then I can see that the request is constantly replayed and response is received.

Do you know what is actually happening here?

---

<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:** [March 1, 2017, 9:16am UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/7 "2017-03-01T09:16:17Z")

</div>

> [@ppsun](#):
>
> Problem#1: This line of script “ctx.master.view.add(f)” leads to an error message:

Yes - that access mitmproxy-specific functionality I believe. I’d be happy to accept a PR that fixes that.

> [@ppsun](#):
>
> Problem#2: then I go ahead and comment out the line mentioned in Problem#1, but the main mitmproxy thread got stuck when setting “block” to be True in the ‘replay\_request’ function. But if I set ‘block’ to false, then I can see that the request is constantly replayed and response is received.

From the replay docs:

> ```
> block: If True, this function will wait for the replay to finish.
> 
> ```

```
            This causes a deadlock if activated in the main thread.

```

You can only block if you are in a separate thread, e.g. with `@concurrent`.

The constant refresh thing sounds like you call `.replay()` for the replayed flow again. If you replay a request, it goes through the event hooks like a normal request.

---

<div class="post-metadata">

**Author:** ![ppsun](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@ppsun](https://discourse.mitmproxy.org/u/ppsun)\
**Post date:** [March 1, 2017, 4:27pm UTC](https://discourse.mitmproxy.org/t/use-reverse-proxy-mode-to-do-real-time-replay/369/8 "2017-03-01T16:27:46Z")

</div>

Got it. Thanks. I see why it keeps replaying the request.
