2.0.2 : receiving wrong request from mitmproxy


#1

I am using mitmdump with python script to intercept https request and manipulate flow.request.host, flow.request.scheme, flow.request.port and flow.request.path with in def request(flow):. I have been able to achieve the same and get a response for the manipulated request. After first get response for first request, my application send a second request. In the second request, instead of the host and port set by my application, I get the same previously manipulated host and port that is flow.request.host and flow.request.port are from my last manipulated request but flow.request.scheme and flow.request.path are same as my application sets in second request.
Is there any way I can reset the flow.request.host and flow.request.port after every response, so that my next request is captured in flow.request correctly?

from mitmproxy import http
import sys
    
def request(flow):
    	print("\nOriginal Request : " + str(flow.request)+"\n")
    	if flow.request.pretty_host.endswith("original.host.com"):
    		flow.request.host = "mock.host.com"
    		flow.request.scheme = "http"
    		flow.request.port = 8081
    		flow.request.path = re.sub(r"billing",r"moquer-0.0.2/mockbillingBinding",flow.request.path)
    		print("Manipulated Request : " + str(flow.request)+"\n")
    def response(flow):
    	print("Response : " +str(flow.response)+"\n")

#2

Hi,
I don’t quite get what you’re trying to achieve here. Do you only want to manipulate the first request?
If your script has the request method defined every request will go through it. If you want it to manipulate only a specific request you can do something like:

if flow.request.url == "my.url":
     ...

#4

I actually want to manipulate all the requests made by my application. I am able to get the correct request for first time in http.HTTPFlow. But for the second time I am getting wrong request in http.HTTPFlow. I am getting the manipulated host/port from last request.