Well....looks like this is my first post to stackoverflow. Totally stumped and wasted hours working on this little project and I'm about ready to give up. Hopefully someone can help me.
I've been working with mitmproxy. I'm writing a python script to replace the body tag in a http response with custom content when the user loads a page.
from libmproxy import controller, proxy
import re
import os
import sys
class BodyInjector(controller.Master):
def __init__(self, server):
controller.Master.__init__(self, server)
def run(self):
try:
return controller.Master.run(self)
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, msg):
print 'New request detected.'
if 'Accept-Encoding' in msg.headers:
msg.headers["Accept-Encoding"] = ["none"]
print 'Changing encoding.'
msg.reply()
def handle_response(self, msg):
if 'Content-Type' in msg.headers:
if msg.headers["Content-Type"] == ["text/html"]:
print 'Webpage detected. Injecting response.'
if msg.content:
c = msg.replace('/<body itemscope itemtype=\"http:\/\/schema\.org\/WebPage\">.*?<\/body>/', '<body><p>Replacement text.</p></body>')
print 'Num replacements: ' + str(c)
if c > 0:
print 'Injection successful.'
else:
print 'Injection unsuccessful.'
msg.reply()
def main(argv):
config = proxy.ProxyConfig(cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem"))
server = proxy.ProxyServer(config, 1080)
print 'Starting Body Replace Server'
m = BodyInjector(server)
m.run()
main(sys.argv)
I think the problem is the line:
c = msg.replace('/<body itemscope itemtype=\"http:\/\/schema\.org\/WebPage\">.*?<\/body>/', '<body><p>Replacement text.</p></body>')
I've tested the regular expression with an online regex tester. I think my problem is the HTML is multi-line. I need to somehow set the re.DOTALL flag but I'm not sure if that's possible with the msg.replace method the developer wrote.
Here is a link to the documentation of the replace method.
Does anyone have any idea how to do this? I'm desperate. :'(