I’m using redirect_requests example in mitmproxy 3.0.1 but I have a problem: not all client requests end fine because mitmproxy returns 502 Bad Gateway error with detail HttpReadDisconnect('Server disconnected',)
.
I am try to redirect “site-to-redirect” to “localhost:8000”. Here mitmproxy log:
2018-02-23T11:52:47 DEBUG ::ffff:192.168.3.128:63265: request
→ Request(GET site-to-redirect:80/)
2018-02-23T11:52:47 DEBUG ::ffff:192.168.3.128:63265: Set new server address: localhost:8000
2018-02-23T11:52:47 DEBUG ::ffff:192.168.3.128:63265: serverconnect
→ (‘localhost’, 8000)
2018-02-23T11:52:47 DEBUG ::ffff:192.168.3.128:63265: response
→ Response(200 OK, text/html; charset=utf-8, 1.59k)
2018-02-23T11:52:47 192.168.3.128:63265: GET http://localhost:8000/
2018-02-23T11:52:47 Host: localhost
2018-02-23T11:52:47 Proxy-Connection: keep-alive
2018-02-23T11:52:47 Cache-Control: max-age=0
2018-02-23T11:52:47 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
2018-02-23T11:52:47 Upgrade-Insecure-Requests: 1
2018-02-23T11:52:47 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
2018-02-23T11:52:47 Accept-Encoding: gzip, deflate
2018-02-23T11:52:47 Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
2018-02-23T11:52:47 Cookie: PHPSESSID=f8avrseullsuhhc1p56f50rg86
2018-02-23T11:52:47 << 200 OK 1.59k
2018-02-23T11:52:47 Date: Fri, 23 Feb 2018 11:52:47 GMT
2018-02-23T11:52:47 Server: WSGIServer/0.2 CPython/3.6.3
2018-02-23T11:52:47 Content-Type: text/html; charset=utf-8
2018-02-23T11:52:47 X-Frame-Options: SAMEORIGIN
2018-02-23T11:52:47 Content-Length: 1626
2018-02-23T11:52:47 DEBUG ::ffff:192.168.3.128:63265: request
→ Request(GET site-to-redirect:80/static/bootstrap.min.css)
2018-02-23T11:52:47 ERROR Addon error: Traceback (most recent call last):
File “/root/.pyenv/versions/3.6.3/lib/python3.6/site-packages/mitmproxy/addons/termlog.py”, line 37, in log
err=(e.level == “error”)
File “/root/.pyenv/versions/3.6.3/lib/python3.6/site-packages/click/termui.py”, line 420, in secho
return echo(style(text, **styles), file=file, nl=nl, err=err, color=color)
File “/root/.pyenv/versions/3.6.3/lib/python3.6/site-packages/click/utils.py”, line 259, in echo
file.write(message)
UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xe0’ in position 51: ordinal not in range(128)
2018-02-23T11:52:47 192.168.3.128:63265: GET http://localhost:8000/static/bootstrap.min.css
2018-02-23T11:52:47 Host: localhost
2018-02-23T11:52:47 Proxy-Connection: keep-alive
2018-02-23T11:52:47 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
2018-02-23T11:52:47 Accept: text/css,/;q=0.1
2018-02-23T11:52:47 Referer: http://site-to-redirect/
2018-02-23T11:52:47 Accept-Encoding: gzip, deflate
2018-02-23T11:52:47 Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
2018-02-23T11:52:47 Cookie: PHPSESSID=f8avrseullsuhhc1p56f50rg86
2018-02-23T11:52:47 << Server disconnected
2018-02-23T11:52:47 DEBUG ::ffff:192.168.3.128:63265: serverdisconnect
→ (‘localhost’, 8000)
2018-02-23T11:52:47 INFO 192.168.3.128:63265: clientdisconnect
On the server side I see only the first call endend with 200 code and nothing about the second one.
I try to call directly the bootstrap resource and it works one time yes and one time no.
2018-02-23T12:16:04 INFO 192.168.3.128:64072: clientconnect
2018-02-23T12:16:04 DEBUG ::ffff:192.168.3.128:64072: request
→ Request(GET site-to-redirect:80/static/bootstrap.min.css)
2018-02-23T12:16:04 DEBUG ::ffff:192.168.3.128:64072: Set new server address: localhost:8000
2018-02-23T12:16:04 DEBUG ::ffff:192.168.3.128:64072: serverconnect
→ (‘localhost’, 8000)
2018-02-23T12:16:04 DEBUG ::ffff:192.168.3.128:64072: response
** → Response(200 OK, text/css, 118.36k)**
2018-02-23T12:16:04 192.168.3.128:64072: GET http://localhost:8000/static/bootstrap.min.css
2018-02-23T12:16:04 Host: localhost
2018-02-23T12:16:04 Proxy-Connection: keep-alive
2018-02-23T12:16:04 Cache-Control: max-age=0
2018-02-23T12:16:04 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
2018-02-23T12:16:04 Upgrade-Insecure-Requests: 1
2018-02-23T12:16:04 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
2018-02-23T12:16:04 Accept-Encoding: gzip, deflate
2018-02-23T12:16:04 Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
2018-02-23T12:16:04 Cookie: PHPSESSID=f8avrseullsuhhc1p56f50rg86
2018-02-23T12:16:04 << 200 OK 118.36k
2018-02-23T12:16:04 Date: Fri, 23 Feb 2018 12:16:04 GMT
2018-02-23T12:16:04 Server: WSGIServer/0.2 CPython/3.6.3
2018-02-23T12:16:04 Content-Type: text/css
2018-02-23T12:16:04 Last-Modified: Mon, 25 Jul 2016 14:53:28 GMT
2018-02-23T12:16:04 Content-Length: 121200
2018-02-23T12:16:05 DEBUG ::ffff:192.168.3.128:64072: request
→ Request(GET site-to-redirect:80/static/bootstrap.min.css)
2018-02-23T12:16:05 ERROR Addon error: Traceback (most recent call last):
File “/root/.pyenv/versions/3.6.3/lib/python3.6/site-packages/mitmproxy/addons/termlog.py”, line 37, in log
err=(e.level == “error”)
File “/root/.pyenv/versions/3.6.3/lib/python3.6/site-packages/click/termui.py”, line 420, in secho
return echo(style(text, **styles), file=file, nl=nl, err=err, color=color)
File “/root/.pyenv/versions/3.6.3/lib/python3.6/site-packages/click/utils.py”, line 259, in echo
file.write(message)
UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xe0’ in position 51: ordinal not in range(128)
2018-02-23T12:16:05 192.168.3.128:64072: GET http://localhost:8000/static/bootstrap.min.css
2018-02-23T12:16:05 Host: localhost
2018-02-23T12:16:05 Proxy-Connection: keep-alive
2018-02-23T12:16:05 Cache-Control: max-age=0
2018-02-23T12:16:05 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
2018-02-23T12:16:05 Upgrade-Insecure-Requests: 1
2018-02-23T12:16:05 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
2018-02-23T12:16:05 Accept-Encoding: gzip, deflate
2018-02-23T12:16:05 Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
2018-02-23T12:16:05 Cookie: PHPSESSID=f8avrseullsuhhc1p56f50rg86
2018-02-23T12:16:05 << Server disconnected
2018-02-23T12:16:05 DEBUG ::ffff:192.168.3.128:64072: serverdisconnect
→ (‘localhost’, 8000)
2018-02-23T12:16:05 INFO 192.168.3.128:64072: clientdisconnect
In my opinion it works only when I see the log Set new server address: localhost:8000
after the incoming request.
You will see timestamps because I edited addons/termlog.py file with this
import sys
import clickfrom mitmproxy import log
from mitmproxy import ctxfrom datetime import datetime
These get over-ridden by the save execution context. Keep them around so we
can log directly.
realstdout = sys.stdout
realstderr = sys.stderrclass TermLog:
def init(self, outfile=None):
self.outfile = outfiledef log(self, e): if log.log_tier(e.level) == log.log_tier("error"): outfile = self.outfile or realstderr else: outfile = self.outfile or realstdout if log.log_tier(ctx.options.verbosity) >= log.log_tier(e.level): #aggiunge un prefisso al messaggio ts = datetime.utcnow().isoformat("T", "seconds") level = "{:5s}".format(e.level.upper()) e.msg = ts + " " + level + " " + e.msg #quello che c'era prima click.secho( e.msg, file=outfile, fg=dict(error="red", warn="yellow", alert="magenta").get(e.level), dim=(e.level == "debug"), err=(e.level == "error") )