TL;DR - I'm looking for a way to send all my external traffic through a proxy server but not send all my internal traffic through a proxy server. How can I achieve this?
My end goal is to forward all external traffic from a phantomjs node to a set of proxy servers on the internet AND to keep all traffic destined for one of my servers (10.X.1.X) from hitting the external proxy servers.
It is my understanding that I need to use a transparent proxy. I have attempted to use HAProxy and IPFire with no success.
My next attempt is using IPTables. I am using IPTables v1.4.7 on CentOS 6.8 on a virtual machine. The IP Address is 10.2.1.234. I have been successful in setting up IPTables to round robin ALL of the traffic to the 5 proxy servers on the internet with the following IPTables rules:
Table: nat
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
1 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 statistic mode nth every 5 tcp dpt:80 to:104.36.80.240:80
2 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 statistic mode nth every 4 tcp dpt:80 to:104.36.81.104:80
3 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 statistic mode nth every 3 tcp dpt:80 to:104.36.81.12:80
4 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 statistic mode nth every 2 tcp dpt:80 to:104.36.82.153:80
5 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:104.36.80.241:80
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 MASQUERADE all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
I set the following in /etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
/etc/sysconfig/iptables-config
# Load additional iptables modules (nat helpers)
# Default: -none-
# Space separated list of nat helpers (e.g. 'ip_nat_ftp ip_nat_irc'), which
# are loaded after the firewall rules are applied. Options for the helpers are
# stored in /etc/modprobe.conf.
IPTABLES_MODULES=""
# Unload modules on restart and stop
# Value: yes|no, default: yes
# This option has to be 'yes' to get to a sane state for a firewall
# restart or stop. Only set to 'no' if there are problems unloading netfilter
# modules.
IPTABLES_MODULES_UNLOAD="yes"
# Save current firewall rules on stop.
# Value: yes|no, default: no
# Saves all firewall rules to /etc/sysconfig/iptables if firewall gets stopped
# (e.g. on system shutdown).
IPTABLES_SAVE_ON_STOP="yes"
# Save current firewall rules on restart.
# Value: yes|no, default: no
# Saves all firewall rules to /etc/sysconfig/iptables if firewall gets
# restarted.
IPTABLES_SAVE_ON_RESTART="yes"
# Save (and restore) rule and chain counter.
# Value: yes|no, default: no
# Save counters for rules and chains to /etc/sysconfig/iptables if
# 'service iptables save' is called or on stop or restart if SAVE_ON_STOP or
# SAVE_ON_RESTART is enabled.
IPTABLES_SAVE_COUNTER="no"
# Numeric status output
# Value: yes|no, default: yes
# Print IP addresses and port numbers in numeric format in the status output.
IPTABLES_STATUS_NUMERIC="yes"
# Verbose status output
# Value: yes|no, default: yes
# Print info about the number of packets and bytes plus the "input-" and
# "outputdevice" in the status output.
IPTABLES_STATUS_VERBOSE="no"
# Status output with numbered lines
# Value: yes|no, default: yes
# Print a counter/number for every rule in the status output.
IPTABLES_STATUS_LINENUMBERS="yes"
# Reload sysctl settings on start and restart
# Default: -none-
# Space separated list of sysctl items which are to be reloaded on start.
# List items will be matched by fgrep.
#IPTABLES_SYSCTL_LOAD_LIST=".nf_conntrack .bridge-nf"
/proc/sys/net/ipv4/ip_forward
1
My problem is when I try to exempt my local addresses from being sent to the external proxy servers. I thought ACCEPT might work and added the following to to the nat PREROUTING table:
1 ACCEPT tcp -- 0.0.0.0/0 10.1.1.0/24
Then I send the following curl command from my desktop but it doesn't trigger that rule! It triggers one of the round robin rules.
curl -v -k -x 10.2.1.234:80 http://10.1.1.228/
I tried similar things with DNAT and FORWARD but for whatever reason, IPTables is not respecting the destination address. Am I missing something?