My Raspberry acts as a Gateway between a local LAN and the Internet. So it has IP forwarding activated and does NAT for the devices on the LAN. Things work fine.

The devices on the LAN are connecting to a server via HTTP and since all the traffic goes through the Raspberry, you could call it a "man in the middle".

Our HTTP server has a bug regarding encoding of XML, which will eventually be fixed but it may take some time. However, the fact that my Raspberry is in between, I wonder whether I could modify the response of the server and correct the invalid data in advance.

From an answer I don't expect a full configuration, just a list of packages to be installed with a short hint of the purpose. Once I know the package, I can lookup the configuration myself.

It's ok if there's some programming required. I just don't want to implement a full proxy.

In pseudo code, it should work like this:

byte[] OnIncomingData(byte[] httpHeader, byte[] data) 
    if (isHttpForMyNetwork(httpHeader))
        if (containsXmlEncodingBug(data))
             data = fixEncoding(data);
    return data;

Clarification from comments:

the content is dynamic, i.e. it depends on the GET parameters of the HTTP request. So it's not as simple as scarping a website into files on disk and then hosting it with Apache.

  • It would be faster and easier if you where to simpily scrape the data off the site and fix the Xml on the Pi then host on. The Pi. Commented Sep 7, 2016 at 16:17
  • @MohammadAli: it's dynamic content which can't really be scraped Commented Sep 8, 2016 at 6:20
  • Have you thought about something like on demand scraping where a user accesses the pi's IP then the Pi goes and fetches the content and makes the required changes? Commented Sep 8, 2016 at 11:54
  • @MohammadAli: that would be ok. Which components do you suggest to do that? I would need a web server that pauses the request, then scrapes and then resumes the request Commented Sep 8, 2016 at 11:57
  • I personally would use nodejs but you can use whatever language you are most comfortable with, you don't need something that pauses the request that is done automatically by all web servers, if you are using something like nodejs it will be slightly faster and I can help write sample code if you are a bite more descriptive on the edit you want and provide me with a demo HTML page. Be aware that this is pretty slow and it's gonna add like 1-3 seconds per page load Commented Sep 8, 2016 at 12:00

1 Answer 1


Main functionality of a man in the middle for HTTP is provided by https://mitmproxy.org/

iptables is already available if you enabled NAT, that’s fine. You can use it for PREROUTING (a parameter to iptables) and forwarding a port (port 80 probably) to another port (choose whatever you like). Let mitmproxy listen on that port and you can write Python scripts to perform replacements.

Major packages needed:

apt-get install python-dev libxml2-dev libxslt-dev pip
pip install mitmproxy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.