🔗 使用 DNAT 拦截 Linux 流量
🔗 目录
无需任何客户端配置即可透明地拦截 IPv4 Web 请求。当 Web 流量到达运行 Squid 的机器时。
NAT 配置仅在 **在 Squid 服务器上** 使用时才有效。这是精确安全地执行拦截所必需的。要从网关机器拦截并将流量定向到单独的 Squid 服务器,请使用 策略路由。

🔗 iptables 配置
将 **SQUIDIP** 替换为 Squid 可用于其监听端口和出站连接的公共 IP。将 **SQUIDPORT** 替换为 Squid.conf 中使用 **intercept** 标志设置的端口。
由于 NAT 的安全漏洞,**强烈建议** 阻止外部访问内部接收端口。这必须在 iptables 的 **mangle** 部分进行,在 DNAT 发生之前,以防止拦截的流量被丢弃。
如果这里的第一个 iptables 行不是第一个,您的设置可能会遇到转发循环问题。
# your proxy IP
SQUIDIP=192.168.0.2
# your proxy listening port
SQUIDPORT=3129
iptables -t nat -A PREROUTING -s $SQUIDIP -p tcp --dport 80 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination $SQUIDIP:$SQUIDPORT
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t mangle -A PREROUTING -p tcp --dport $SQUIDPORT -j DROP
DNAT 仅在较旧的内核版本上适用于 IPv4 流量。IPv6 拦截请使用 TPROXY 版本 4。
🔗 /etc/sysctl.conf 配置
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
# Controls source route verification
net.ipv4.conf.default.rp_filter = 0
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
🔗 Squid 配置文件
您需要像这样配置 Squid 以便它知道 IP 地址正在被拦截:
http_port 3129 transparent
在 Squid 3.1+ 中,transparent 选项已被拆分。使用 ‘intercept 来捕获 DNAT 数据包。
http_port 3129 intercept
⚠️ Disclaimer: Any example presented here is provided "as-is" with no support
or guarantee of suitability. If you have any further questions about
these examples please email the squid-users mailing list.
类别: ConfigExample
导航:站点搜索、站点页面、分类、🔼 向上