You need to use shorewall, which in my experience is a highly capable and flexible firewall as well as Squid proxy to use this solution.