mitmproxy icon indicating copy to clipboard operation
mitmproxy copied to clipboard

Help: Proxy traffic from localhost:<port> and passing through to <remote_ip>:<their_port>

Open rafajafar opened this issue 10 years ago • 1 comments

I popped in IRC and dwfreed said this was the place to post this question.

I created code which sets up a proxy each time it runs. This proxy delegates information from selenium (for firefox browser testing) to the proxy over localhost: and sends the request. This worked! It went great. However, I have gotten a new requirement. I need to be able to send this request as follows:

Selenium -> local proxy : some port -> username:password@remote proxy : some port

Here is my trimmed-down code for that: https://gist.github.com/rafajafar/945b2fe9e0d0c58281ebc20d7461879a

You can see line 63 of "selenium_proxy.py" has mode=upstream set. I had to implement this fix manually to make this work:

https://github.com/mitmproxy/mitmproxy/commit/02ba76ea0904b15f01956587a6ebbde43ee97909

As this fix is not in the current release on pip.

In this script I actually one once without the proxy first and then create the proxy on the fly, then run again with the proxy on the same url.

Now I am getting the following errors on the url-with-the-proxy step:

> Traceback (most recent call last):
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/proxy/server.py", line 121, in handle
>     root_layer()
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/proxy/modes/http_proxy.py", line 25, in __call__
>     layer()
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/protocol/tls.py", line 358, in __call__
>     layer()
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/protocol/http1.py", line 67, in __call__
>     layer()
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/protocol/http.py", line 192, in __call__
>     self.get_response_from_server(flow)
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/protocol/http.py", line 294, in get_response_from_server
>     get_response()
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/protocol/http.py", line 290, in get_response
>     self.send_request(flow.request)
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/mitmproxy/protocol/http1.py", line 25, in send_request
>     self.server_conn.wfile.write(http1.assemble_request(request))
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/netlib/http/http1/assemble.py", line 10, in assemble_request
>     head = assemble_request_head(request)
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/netlib/http/http1/assemble.py", line 17, in assemble_request_head
>     headers = _assemble_request_headers(request.data)
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/netlib/http/http1/assemble.py", line 83, in _assemble_request_headers
>     headers = request_data.headers.copy()
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/netlib/utils.py", line 45, in copy
>     return self.from_state(self.get_state())
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/netlib/http/headers.py", line 203, in from_state
>     return cls([list(field) for field in state])
>   File "/home/ccusce/dev/selenium_src/seleniumenv/local/lib/python2.7/site-packages/netlib/http/headers.py", line 96, in __init__
>     raise ValueError("Headers passed as fields must be bytes.")
> ValueError: Headers passed as fields must be bytes.

I'm not sure how to grok this but I'm super under the gun and need help.

Also quick question: Assuming I can manage unique ports to prevent conflicts, can I create multiple instances of SeleniumProxy (as seen in my code) to run in multiple threads or is there one flowmaster this way the same way there is one flow master in the CLI for mitmproxy ?

Thanks much, this last minute customer request is killing me, but they have a hard deadline of Monday.

rafajafar avatar May 14 '16 20:05 rafajafar

The issue here was that upstream_auth needed to be a single string.

mhils avatar May 16 '16 03:05 mhils