我正在使用带有 Tomato USB 的路由器将端口 80 请求转发到透明模式下带有 Squid 3 的 Ubuntu 服务器。Tomato 论坛上的某个人改编了这些指示番茄:
#!/bin/sh
INTERNAL_NETWORK="192.168.1.0/24"
ROUTER_IP="192.168.1.1"
PROXY_SERVER="192.168.1.3"
PROXY_PORT="3128"
/usr/sbin/iptables -t nat -A PREROUTING -i br0 -s $INTERNAL_NETWORK -d $INTERNAL_NETWORK -p tcp --dport 80 -j ACCEPT
/usr/sbin/iptables -t nat -A PREROUTING -i br0 -s ! $PROXY_SERVER -p tcp --dport 80 -j DNAT --to $PROXY_SERVER:$PROXY_PORT
/usr/sbin/iptables -t nat -A POSTROUTING -o br0 -s $INTERNAL_NETWORK -p tcp -d $PROXY_SERVER -j SNAT --to $ROUTER_IP
/usr/sbin/iptables -t filter -I FORWARD -s $INTERNAL_NETWORK -d $PROXY_SERVER -i br0 -o br0 -p tcp --dport $PROXY_PORT -j ACCEPT
转发工作正常,请求由 Squid 处理。说明显示了绕过网络上某些计算机的规则。我的问题是我需要绕过一些存在代理问题的站点,而不是特定的计算机。我尝试添加以下内容:
/usr/sbin/iptables -t nat -A PREROUTING -d caixa.gov.br -j ACCEPT
此规则不起作用。我根本不想将 caixa.gov.br(以及其他一些网站)转发到代理。但 Tomato 仍在转发所有网站。
是否可以为每个我不想转发的站点添加一条规则?
答案1
iptables -A ...
将规则置于链的末尾。因此,您的一个规则永远不会匹配(或至少没有任何效果),因为第二个规则 ( -s ! $PROXY_SERVER
) 已经获得了这些数据包/连接。
而不iptables -A PREROUTING
需要iptables -I PREROUTING 2
。或者你可以创建链,让结构更容易理解:
#!/bin/bash
INTERNAL_NETWORK="192.168.1.0/24"
ROUTER_IP="192.168.1.1"
PROXY_SERVER="192.168.1.3"
PROXY_PORT="3128"
if iptables -L prerouting_exceptions -n &>/dev/null; then
iptables -t nat -F prerouting_exceptions
else
iptables -t nat -N prerouting_exceptions
fi
# this prevents the same rule being inserted with each script call
if ! iptables -L FORWARD -n | grep -q proxy; then
iptables -t filter -I FORWARD -s $INTERNAL_NETWORK -d $PROXY_SERVER -i br0 \
-o br0 -p tcp --dport $PROXY_PORT -j ACCEPT -m comment --comment proxy
fi
iptables -t nat -F PREROUTING
iptables -t nat -A PREROUTING -j prerouting_exceptions
iptables -t nat -A PREROUTING -i br0 -s ! $PROXY_SERVER -p tcp \
--dport 80 -j DNAT --to $PROXY_SERVER:$PROXY_PORT
iptables -t nat -A prerouting_exceptions -i br0 -s $INTERNAL_NETWORK \
-d $INTERNAL_NETWORK -p tcp --dport 80 -j ACCEPT
iptables -t nat -A prerouting_exceptions -d caixa.gov.br -j ACCEPT
答案2
我尝试了您的答案。除了 caixa.gov.br 之外,我还添加了 www1.caixa.gov.br,因为它也显示在 Squid 日志中。
鱿鱼日志:
1367165802.899 151455 192.168.1.1 TCP_MISS/503 4261 GET http://www.caixa.gov.br/ - DIRECT/www.caixa.gov.br text/html
Tomato USB 上 iptables -t nat -L -nv 的结果:
Chain PREROUTING (policy ACCEPT 117 packets, 10457 bytes)
pkts bytes target prot opt in out source destination
155 12613 prerouting_exceptions all -- * * 0.0.0.0/0 0.0.0.0/0
12 660 DNAT tcp -- br0 * !192.168.1.103 0.0.0.0/0 tcp dpt:80 to:192.168.1.103:3128
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
169 12403 MASQUERADE all -- * ppp0 0.0.0.0/0 0.0.0.0/0
12 660 SNAT all -- * br0 192.168.1.0/24 192.168.1.0/24 to:192.168.1.1
Chain OUTPUT (policy ACCEPT 73 packets, 5279 bytes)
pkts bytes target prot opt in out source destination
Chain WANPREROUTING (0 references)
pkts bytes target prot opt in out source destination
0 0 DNAT icmp -- * * 0.0.0.0/0 0.0.0.0/0 to:192.168.1.1
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9740 to:192.168.1.117:9740
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5740 to:192.168.1.101:5740
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:34184 to:192.168.1.117:34184
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:14983 to:192.168.1.117:14983
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:54184 to:192.168.1.101:54184
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:51413 to:192.168.1.103:51413
0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:42020 to:192.168.1.100:42020
Chain prerouting_exceptions (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT tcp -- br0 * 192.168.1.0/24 192.168.1.0/24 tcp dpt:80
14 800 ACCEPT all -- * * 0.0.0.0/0 200.201.166.106
12 696 ACCEPT all -- * * 0.0.0.0/0 200.201.166.240
caixa.gov.br 的 IP 地址:
#host caixa.gov.br
aixa.gov.br has address 200.201.166.106
caixa.gov.br mail is handled by 0 bootes1.caixa.gov.br.
caixa.gov.br mail is handled by 0 bootes.caixa.gov.br.