I am trying to write a simple proxy application on Android in Java to filter URLs. What it does is basically read everything on the incoming Socket, write everything to the server and vice versa. Everything works fine if I point my Opera mobile Browser to it. I want to make it transparent though, so I tried to redirect all traffic to it via these iptables rules:
iptables -t nat -A OUTPUT -m owner --uid-owner 10090 -j ACCEPT
iptables -t nat A OUTPUT -p tcp --dport 80 -j REDIRECT -p tcp --to-port 8081
I then get an error message, stating that “the server communication failed”.
Could it be that when using the proxy in transparent mode, it has to behave differently, e.g. like a webserver? I read somewhere that when using squid transparently, you have to configure it to be in “transparent mode”.
Or is there an entirely different reason?
Answering my own question.
The iptable rules work perfectly, so that was not the problem. The error message brought me on the right track. It states that “the server communication failed”, so there was something wrong with the way my proxy responded to the browser.
Turned out it did not send any HTTP Headers to the browser, because I used
URLConnectionto communicate with the real webserver.URLConnectiontakes care of the HTTP Protocol, that’s why the browser wasn’t seeing any headers in the response. I then switched to theSocketAPI to talk to the webserver, and just forwarded everything back to the browser, which included headers and everything, and it worked!