Like slack_master said, you're gonna want to look into
www.iptables.org/ if you want to write your own. There are some good tutorials there, and some example scripts (and other links) here:
www.linuxguruz.org/iptables/
I believe that you have to get different software for the proxy stuff. Check out squid.... It's the only one that I can think of at the moment.
What distro are you using? Most of them come with iptables. Many times however, the kernel version and/or the firewall patches are a little outdated. You may want to look into patching your kernel w/ patch-o-matic, but then again you may not need any of the newer features.