🔗 在 Debian 上使用 Redirectors 和 Reporting 配置 Squid 拦截器
本文档(基于此文章,并进行了一些更新和补充)解释了如何在运行 Linux Debian 6 系统的 Bridge 设备上部署 Squid 拦截 Web 代理。由于代理执行透明拦截,LAN 用户无需在浏览器中手动设置代理地址即可浏览网页。
本文档还详细介绍了如何设置一些有用的功能,例如 Web 过滤(通过 Squirm)和使用情况监控(通过 SARG)。
首先,您需要一台具有两个网络接口的 Linux 机器,我们将将其配置为网桥。我们假设 eth0 连接到 LAN(下游),而 eth1 提供到 Internet 的上游访问。
🔗 设置 Linux 网桥
如果您没有安装所有必需的软件包,请获取它们
aptitude install ebtables bridge-utils
我们假设该机器位于 10.9.0.0/16 子网中,并为其分配 IP 地址 10.9.1.9。LAN 是一个 10.0.0.0/8 网络,通过路由器 10.9.2.2(通过 eth0 下游)访问,而路由器或防火墙 10.9.1.1 是提供到 Internet 的网关(通过 eth1 上游)。DNS 服务器的 IP 地址是 10.13.13.13。
现在我们将列出在机器上配置网络所需的所有命令。您可以在 shell 提示符下输入这些命令,但要使所有更改永久生效(即重启后),您还必须将它们放入 /etc/rc.local 中。
我们配置网络接口并将其设置为网桥
ifconfig eth0 0.0.0.0 promisc up
ifconfig eth1 0.0.0.0 promisc up
/usr/sbin/brctl addbr br0
/usr/sbin/brctl addif br0 eth0
/usr/sbin/brctl addif br0 eth1
ifconfig br0 10.9.1.9 netmask 255.255.0.0 up
我们定义路由表和 DNS
route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.2.2
route add default gw 10.9.1.1 dev br0
rm -f /etc/resolv.conf 2>/dev/null
echo "nameserver 10.13.13.13" >> /etc/resolv.conf
然后,我们指示所有发送到端口 80 的数据包(即来自 LAN 的 http 流量)不通过网桥,而是重定向到本地机器
ebtables -t broute -A BROUTING -p IPv4 --ip-protocol 6 --ip-destination-port 80 -j redirect --redirect-target ACCEPT
并将这些数据包重定向到端口 3128(即 Squid 正在监听的端口)
iptables -t nat -A PREROUTING -i br0 -p tcp --dport 80 -j REDIRECT --to-port 3128
🔗 配置 Squid
您现在必须配置 Squid。将所有以下行插入到文件 /etc/squid/squid.conf 中。
首先,您必须定义允许浏览的内部 IP 子网。在此示例中,我们允许从子网 10.0.0.0/8 浏览;如果您的 LAN 包含其他子网,请为每个子网重复此行。
acl localnet src 10.0.0.0/8
其余的主机和端口的 ACL 定义
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl localnet src fc00::/7
acl localnet src fe80::/10
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny to_localhost
http_access allow localnet
http_access allow localhost
http_access deny all
我们指定 Squid 必须在透明模式下运行在默认端口 3128 上
http_port 3128 intercept
Squid 将使用 10GB 的磁盘缓存
cache_dir ufs /var/cache 10000 16 256
我们决定保留最后 30 个每日日志文件
logfile_rotate 30
以下行很有用,因为它几乎立即启动关闭过程,而无需等待访问缓存的客户端。这允许 Squid 更快地重启。
shutdown_lifetime 2 seconds
最后,还有一些其他设置
hierarchy_stoplist cgi-bin ?
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
🔗 运行 Squid
编辑配置文件后,启动 squid
/etc/init.d/squid3 start
Squid 启动后,您应该可以从 LAN 浏览网页。请注意,Squid 负责向外部提供 HTTP 连接。如果 Squid 进程崩溃或停止,LAN 客户端将无法浏览网页。
要实时查看 Squid 提供的请求,请使用命令
root@squidbox:~# tail -f /var/logs/access.log
输出的第一个字段是自 UNIX 纪元(1970 年 1 月 1 日 00:00:00 UTC)以来的请求时间(以秒为单位)。为了获得更人性化的输出,请通过日志转换器将其管道化(您需要先安装 ccze 包)
root@squidbox:~# tail -f /var/logs/access.log | ccze -C
要重新加载 Squid 配置,请运行
root@squidbox:~# squid -k reconfigure
🔗 设置出站 IP
上游网关将 LAN 的所有 HTTP 请求视为来自一个唯一 IP:Squid 的地址,在本例中是 10.9.1.9。
您可能希望能够区分不同的客户端,也许是为了应用不同的策略,或者用于监控目的。例如,假设 LAN 包含三个子网
IT = 10.4.0.0/16,研发 = 10.5.0.0/16,管理 = 10.6.0.0/20
并且您希望根据客户端所在的子网分配不同的出站私有 IP 地址。只要出站地址与 Squid 位于同一子网中,您就可以这样做。例如
IT -> 10.9.1.4,研发 -> 10.9.1.5,管理 -> 10.9.1.6
首先,我们需要将这些 IP 地址分配给 Squid。每个地址将分配给一个桥接子接口。
将以下行添加到 /etc/rc.local
/usr/sbin/brctl addbr br0:4
/usr/sbin/brctl addbr br0:5
/usr/sbin/brctl addbr br0:6
ifconfig br0:4 10.9.1.4 netmask 255.255.0.0 up
ifconfig br0:5 10.9.1.5 netmask 255.255.0.0 up
ifconfig br0:6 10.9.1.6 netmask 255.255.0.0 up
然后将以下行添加到 /etc/squid/squid.conf
acl it_net src 10.4.0.0/16
acl rd_net src 10.5.0.0/16
acl admin_net src 10.6.0.0/20
tcp_outgoing_address 10.9.1.4 it_net
tcp_outgoing_address 10.9.1.5 rd_net
tcp_outgoing_address 10.9.1.6 admin_net
tcp_outgoing_address 10.9.1.9
最后一行指定了默认的出站地址 10.9.1.9。这是分配给不属于这三个子网中任何一个的客户端的地址。
重新启动网络服务和 Squid 以使更改生效。
🔗 设置 Web 重定向
我们现在将看到如何将可插拔的 Web 重定向器(如 Squirm)集成到代理中。Squirm 允许为 URL 重写定义规则,使其成为一个有效且轻量级的 Web 过滤器。
例如,可以通过将 &safe=active 添加到搜索 URL 来将 Google 搜索结果设置为最严格的 SafeSearch 级别。通过这样重写所有 Google 搜索查询的 URL,我们可以确保所有 LAN 用户只能获得安全内容。
(请注意,Google 正在逐步将所有搜索切换到 HTTPS。由于 Squid 仅处理 HTTP 流量,因此这不再有效。但是,您明白了其中的原理。)
下载最新版本的 Squirm (squirm-1.0betaB),解压缩,然后执行以下命令
root@squidbox:~# cd regex
root@squidbox:~# ./configure
root@squidbox:~# make clean
root@squidbox:~# make
root@squidbox:~# cp -p regex.o regex.h ..
获取 Squid 进程运行的用户名和组名
root@squidbox:~# ps -eo args,user,group | grep squid
它们应该是 nobody 和 nogroup,但如果不是,请记下它们。编辑 Makefile 并找到 install 指令。将安装的用户和组名更改为 Squid 执行的名称(很可能是 -o nobody -g nogroup)。
执行命令
root@squidbox:~# make
root@squidbox:~# make install
现在 Squirm 已安装并需要进行配置。
第一个配置文件是 /usr/local/squirm/etc/squirm.local,并且必须包含将由 Squirm 提供服务的 C 类网络。例如,在本例中,此文件可能以以下内容开始
10.4.1
10.4.2
10.4.128
10.5.64
10.5.65
依此类推。Squirm 不会为未在此文件中列出的任何网络中的客户端运行。
第二个配置文件是 /usr/local/squirm/etc/squirm.patterns,其中包含一个正则表达式列表,指示哪些 URL 以及如何重写它们。在本例中,我们希望它是
regexi ^(http://www\.google\..*/search\?.*) \1&safe=active
regexi ^(http://www\.google\..*/images\?.*) \1&safe=active
最后,将以下行添加到 Squid 配置文件
redirect_program /usr/local/squirm/bin/squirm
redirect_children 30
acl toSquirm url_regex ^http://www\.google\..*/(search|images)\?
url_rewrite_access allow toSquirm
url_rewrite_access deny all
前两行告诉 Squid 让 Squirm 处理重定向,并为其生成 30 个 Squirm 进程。接下来的几行是有用的性能优化。由于 Squirm 可能是瓶颈,这里我们告诉 Squid 只为那些最终将被重写的 URL 调用 Squirm,而不是为任何 URL 调用。在此处指定的正则表达式与 squirm.patterns 中指定的正则表达式完全匹配非常重要。
最后,重新启动 Squid,Squirm 就可以工作了。您可以通过文件 /usr/local/squirm/logs/squirm.match 来监控 Squirm 活动,该文件记录了所有正则表达式 URL 匹配。此文件可能会变得很大,因此最好设置一个 cron 作业来定期删除它。
🔗 生成使用报告
SARG (Squid Analysis Report Generator) 是一个不错的工具,可以生成关于客户端 IP、访问的网站、下载数据量等的统计信息。
SARG 可作为标准的 Debian 包提供
root@squidbox:~# apt-get install sarg
并且可以通过其配置文件 /etc/squid/sarg.conf 进行微调
SARG 根据 Squid 的 access.log 文件的内容生成报告。由于报告是 HTML 格式的,因此最好让 Apache 服务器运行在 Linux 机器上,并让 SARG 在 Document Root 目录中生成报告。对于最后这一点,请在 SARG 配置文件中将参数值设置为 output_dir /var/www。我们强烈建议您至少设置基本 HTTP 身份验证,以保护报告不被随意窥探。
当天的统计信息通过命令生成
root@squidbox:~# /usr/sbin/sarg-reports today
要自动生成每日报告,请向 crontab 文件添加一行(并记住之后重新启动 cron 守护程序)
30 23 * * * root /usr/sbin/sarg-reports today
请注意:报告的规模可能会非常大。对于一个拥有 2000 名客户端在中等程度上网(工作时间 8 AM - 5 PM)的网络,单个每日报告可能会产生 150,000 个文件,总大小为 1 GB。请务必通过以下命令监控您的磁盘空间和 inode 使用情况
root@squidbox:~# df -h; df -hi
因此,我们将安排我们的系统在 15 天后对报告进行 targzip 压缩,并在 3 个月后最终删除它们。为此,我们创建一个脚本 /etc/squid/tarsarg.sh
D_TAR=`date +%Y%b%d --date="15 days ago"`
D_DEL=`date +%Y%b%d --date="3 months ago"`
DAILY=/var/www/Daily
ARCHIVE=/var/www/Archive
LOGFILE=/etc/squid/tarsarg.log
mkdir -p $ARCHIVE/
if [ ! -d $DAILY/$D_TAR-$D_TAR/ ]
then
echo "`date`: error: report for $D_TAR not found" >> $LOGFILE
else
tar -czf $ARCHIVE/$D_TAR.tar.gz $DAILY/$D_TAR-$D_TAR/
rm -rf $DAILY/$D_TAR-$D_TAR/
echo "`date`: archived $D_TAR" >> $LOGFILE
fi
if [ ! -e $ARCHIVE/$D_DEL.tar.gz ]
then
echo "`date`: error: targzip $D_DEL not found" >> $LOGFILE
else
rm -f $ARCHIVE/$D_DEL.tar.gz
echo "`date`: deleted targzip $D_DEL" >> $LOGFILE
fi
然后,我们通过向 crontab 文件添加以下行来安排此脚本每天运行,在报告生成之后
0 1 * * * root /etc/squid/tarsarg.sh
⚠️ 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
导航:站点搜索、站点页面、分类、🔼 向上