2013-04-07 17:16:01 +00:00
|
|
|
"""
|
|
|
|
This example shows two ways to redirect flows to other destinations.
|
|
|
|
"""
|
2015-09-03 22:46:42 +00:00
|
|
|
from libmproxy.models import HTTPResponse
|
2015-09-05 18:45:58 +00:00
|
|
|
from netlib.http import Headers
|
2014-05-15 12:37:05 +00:00
|
|
|
|
2014-09-08 14:02:31 +00:00
|
|
|
def request(context, flow):
|
2014-09-05 13:16:20 +00:00
|
|
|
# pretty_host(hostheader=True) takes the Host: header of the request into account,
|
2015-05-30 00:03:28 +00:00
|
|
|
# which is useful in transparent mode where we usually only have the IP
|
|
|
|
# otherwise.
|
2014-09-08 14:02:31 +00:00
|
|
|
|
|
|
|
# Method 1: Answer with a locally generated response
|
2014-09-03 21:44:54 +00:00
|
|
|
if flow.request.pretty_host(hostheader=True).endswith("example.com"):
|
2014-05-15 12:37:05 +00:00
|
|
|
resp = HTTPResponse(
|
|
|
|
[1, 1], 200, "OK",
|
2015-09-05 18:45:58 +00:00
|
|
|
Headers(Content_Type="text/html"),
|
2014-05-15 12:37:05 +00:00
|
|
|
"helloworld")
|
2014-09-03 22:10:01 +00:00
|
|
|
flow.reply(resp)
|
2014-09-08 14:02:31 +00:00
|
|
|
|
|
|
|
# Method 2: Redirect the request to a different server
|
2014-09-03 21:44:54 +00:00
|
|
|
if flow.request.pretty_host(hostheader=True).endswith("example.org"):
|
2013-04-07 17:16:01 +00:00
|
|
|
flow.request.host = "mitmproxy.org"
|
2014-09-03 21:44:54 +00:00
|
|
|
flow.request.update_host_header()
|