'How to make a network request inside a Mitmproxy addon
Let's say I have an addon that modifies a response from specific endpoints, like the one provided in the documentation example: https://docs.mitmproxy.org/stable/api/events.html#event-hooks
Within that response
function I want to make an http(s) request to an unrelated endpoint. To make http(s) requests I use the requests library. Problem is, if I specify nothing but the url in the requests get/post method, I get ValueError: check_hostname requires server_hostname
error. After researching this I got the impression that the underlying urllib3
library requires extra proxy information. But if I try to provide proxies object that looks like:
proxies={
'http': f'http://127.0.0.1:{port}',
'https': f'https://127.0.0.1:{port}',
}
I get the following error instead: requests.exceptions.ProxyError: HTTPSConnectionPool(host='<hostname>', port=443): Max retries exceeded with url: <endpoint> (Caused by ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1114: The handshake operation timed out')))
I assume that any request made inside addon scripts goes through mitmproxy itself, where it successfully dies. Hence, I was wondering how to properly make http requests within mitmproxy addons?
OS: Windows 10
Mitmproxy version: 6.0.2
Solution 1:[1]
Make a file Named: multiproxy.py
from mitmproxy import http
from mitmproxy.connection import Server
from mitmproxy.net.server_spec import ServerSpec
class multiproxy:
def __init__(self):
self.num = 0
def request(self, flow) -> None:
address = ("target-ip-2", [target-port-2])
flow.server_conn = Server(flow.server_conn.address)
flow.server_conn.via = ServerSpec("http", address)
addons = [multiproxy()]
Run mitmproxy with this addon:
./mitmproxy --set block_global=false --ssl-insecure -s multiproxy.py
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |