IPTables DNAT 豁免

IPTables DNAT 豁免

TL;DR - 我正在寻找一种方法,通过代理服务器发送所有外部流量,但不通过代理服务器发送所有内部流量。我该如何实现这一点?

我的最终目标是将所有外部流量从 phantomjs 节点转发到互联网上的一组代理服务器,并阻止所有发往我的一台服务器(10.X.1.X)的流量到达外部代理服务器。

据我所知,我需要使用透明代理。我尝试过使用 HAProxy 和 IPFire,但没有成功。

我的下一次尝试是使用 IPTables。我在虚拟机上的 CentOS 6.8 上使用 IPTables v1.4.7。IP 地址是 10.2.1.234。我已成功设置 IPTables,使用以下 IPTables 规则将所有流量轮询到互联网上的 5 个代理服务器:

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

我在 /etc/sysctl.conf 中设置了以下内容

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

/etc/sysconfig/iptables 配置

# 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

我的问题是当我尝试免除将本地地址发送到外部代理服务器时。我认为 ACCEPT 可能会起作用,并将以下内容添加到 nat PREROUTING 表中:

1    ACCEPT     tcp  --  0.0.0.0/0            10.1.1.0/24

然后我从我的桌面发送以下 curl 命令,但它没有触发该规则!它触发了其中一条循环规则。

curl -v -k -x 10.2.1.234:80 http://10.1.1.228/

我尝试使用 DNAT 和 FORWARD 进行类似操作,但无论出于什么原因,IPTables 都不尊重目标地址。我是不是漏掉了什么?

相关内容