Squid
Squid 是一款适用于 HTTP、HTTPS 和 FTP 的缓存代理,提供广泛的访问控制。
安装
- 目前互联网流量中已有 ~80% (且在增加) 使用 HTTPS,除非启用了 #SSL Bumping,否则 Squid 无法对其进行缓存。
- “您的客户端将能够识别代理的存在。如果您在寻找一种完全隐秘的方式,请不要使用 Squid。” - Squid 官方网站
配置
默认情况下,缓存目录将创建在 /var/cache/squid 中,并为这些目录设置相应的权限。然而,为了获得更大的控制权,我们需要深入研究 /etc/squid/squid.conf。
以下选项可能对您有用。如果您的配置文件中没有该选项,请将其添加进去!
http_port- 设置 Squid 在本地机器上绑定的端口。您可以通过指定多行http_port让 Squid 绑定到多个端口。默认情况下,Squid 绑定到端口 3128。
http_port 3128 http_port 3129
http_access- 这是允许谁使用代理的访问控制列表。默认情况下,仅允许 localhost 访问代理。为了测试目的,您可能希望将选项http_access deny all更改为http_access allow all,这将允许任何人连接到您的代理。如果您只想允许访问您的子网,可以这样做:
acl ip_acl src 192.168.1.0/24 http_access allow ip_acl http_access deny all
cache_mgr- 缓存管理者的电子邮件地址。
cache_mgr squid.admin@example.com
shutdown_lifetime- 指定当要求停止服务时,Squid 应该等待多长时间。如果您在桌面 PC 上运行 squid,可能希望将其设置为较短的时间。
shutdown_lifetime 10 seconds
cache_mem- 您希望 Squid 使用多少内存来保留对象,而不是将它们写入磁盘。Squid 的总内存使用量将超过此值!默认值为 8MB,如果您有大量可用内存,可能需要增加它。
cache_mem 64 MB
visible_hostname- 在状态/错误消息中显示的主机名
visible_hostname cerberus
cache_peer- 如果您希望您的 Squid 通过另一个代理服务器而不是直接连接到互联网,您需要在此处指定。login- 如果上级代理需要身份验证,请使用此选项。never_direct- 告知缓存永远不要直接连接互联网以检索页面。如果您设置了上述选项,则需要此项。
cache_peer 10.1.1.100 parent 8080 0 no-query default login=user:password never_direct allow all
maximum_object_size- 缓存对象的最大尺寸。默认值为 4 MB,如果您有大量磁盘空间,可能需要将其增加到一个合理的大小。
maximum_object_size 10 MB
squid -zN(-z 用于创建缺失的交换目录,-N 表示非守护进程模式)。cache_dir- 这是您的缓存目录,所有缓存文件都存储在这里。这里有很多选项,但格式通常如下:
cache_dir <storage type> <directory> <size in MB> 16 256
以学校的互联网代理为例:
cache_dir diskd /cache0 200000 16 256
如果您更改了默认的缓存目录,在启动 Squid 之前必须在缓存目录上设置正确的权限,否则它将无法创建缓存目录并导致启动失败。
访问本地主机名上的服务
如果您计划使用非完整定义的主机名(例如 http://mywebapp)在局域网中访问 Web 服务器,您可能需要启用 dns_defnames 选项。如果没有此选项,Squid 将原样(mywebapp)发送 DNS 请求,这可能会根据您局域网的 DNS 设置而失败。启用此选项后,Squid 在发出请求时将附加 /etc/resolv.conf 中配置的任何域名(例如 mywebapp.company.local)。
dns_defnames on
启动
完成配置后,您应该检查配置文件是否正确:
# squid -k check
然后创建您的缓存目录:
# squid -z
然后您可以 启动/启用 squid.service。
内容过滤
如果您在寻找内容过滤解决方案,请参阅 Privoxy。
前端
如果您想要一个基于 Web 的前端来管理 Squid,Webmin 是您的最佳选择。
Webmin 不支持 Squid 4.x
如果您收到错误提示显示您的 Webmin 版本不受支持:
Your version of Squid is not supported by Webmin. Only versions from 1.1 to 3.4 are supported by this module.
您需要修改文件 /opt/webmin/squid/index.cgi(见 issue #952)
使用 adzapper 拦截广告
Adzapper 是 Squid 的一个插件。它可以拦截各类广告(甚至是 Flash 动画)并将其替换为您选择的图像,因此页面的布局不会被过多地改变。
安装
AdZapper 目前不在官方仓库或 AUR 中。脚本本身以及详细的配置和使用信息可以在 https://adzapper.sourceforge.net 找到。
配置
echo "redirect_program /usr/bin/adzapper.wrapper" >> /etc/squid/squid.conf
(squid 2.6.STABLE13-1)
echo "url_rewrite_program /usr/bin/adzapper.wrapper" >> /etc/squid/squid.conf echo "url_rewrite_children 10" >> /etc/squid/squid.conf
如果您愿意,可以编辑 /etc/adzapper/adzapper.conf 以根据您的喜好配置 adzapper。不过,开箱即用的配置已经运行得非常出色。
透明 Web 代理
透明代理是通过将 eth0 接收到的所有 www 请求重定向到 Squid 来实现的。您需要添加一个带有 intercept(适用于 squid 3.2)参数的端口。请注意,至少必须有一个端口在没有 intercept 参数的情况下可用。
http_port 3128 http_port 3129 intercept
对于 TLS:
https_port 3130 intercept ssl-bump generate-host-certificates=on dynamic_cert_mem_cache_size=4MB cert=/etc/squid/squidCA.pem acl step1 at_step SslBump1 ssl_bump peek step1 ssl_bump splice all # workaround for some sites client_persistent_connections off server_persistent_connections off
iptables
在具有 root 权限的终端中运行:
# gid=`id -g proxy` # iptables -t nat -A OUTPUT -p tcp --dport 80 -m owner --gid-owner $gid -j ACCEPT # iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination SQUIDIP:3129 # iptables -t nat -A OUTPUT -p tcp --dport 443 -j DNAT --to-destination SQUIDIP:3130 # iptables-save > /etc/iptables/iptables.rules
然后 启动 iptables.service systemd 单元。
将 SQUIDIP 替换为 Squid 用于监听端口和出站连接的公网 IP。
intercept 选项。Shorewall
编辑 /etc/shorewall/rules 并添加:
REDIRECT loc 3129 tcp www # redirect to Squid on port 3128 ACCEPT $FW net tcp www # allow Squid to fetch the www content
重启 shorewall systemd 单元。
HTTP 身份验证
Squid 可以配置为要求用户和密码才能使用。我们将使用 digest http auth。
首先使用 htdigest -c /etc/squid/users MyRealm username 创建用户文件。在提示时输入密码。
然后在您的 squid.conf 中添加这些行:
auth_param digest program /usr/lib/squid/digest_file_auth -c /etc/squid/users auth_param digest children 5 auth_param digest realm MyRealm acl users proxy_auth REQUIRED http_access allow users
并重启 squid。现在,在访问代理时,系统会提示您输入用户名和密码。
您可以使用 htdigest /etc/squid/users MyRealm newuser 添加更多用户。您可能需要安装 Apache 软件包,其中包含了 htdigest 工具。
http_access 规则是级联的,因此您需要按照所需的顺序设置它们。NTLM
设置 samba 和 winbindd 并通过以下命令测试:
ntlm_auth --username=DOMAIN\\user
为 squid 用户/组授予 /var/cache/samba/winbindd_privileged/ 目录的 r-x 访问权限。
然后在 squid.conf 中添加类似以下内容:
auth_param ntlm program /usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp auth_param ntlm children 5 auth_param ntlm max_challenge_reuses 0 auth_param ntlm max_challenge_lifetime 2 minutes auth_param ntlm keep_alive off
acl ntlm_users proxy_auth REQUIRED http_access allow ntlm_users http_access deny all
隐藏浏览器的真实 IP 地址
参考:Squid Proxy Hide System’s Real IP Address
/etc/squid/squid.conf
# Hide client ip forwarded_for delete # Turn off via header via off # Deny request for original source of a request follow_x_forwarded_for deny all request_header_access X-Forwarded-For deny all
SSL Bumping (SSL 拦截)
参考:Intercept HTTPS CONNECT messages with SSL-Bump
创建自签名根 CA 证书
cd /etc/squid
openssl req -new -newkey rsa:2048 -sha256 -days 3650 -nodes -x509 -extensions v3_ca -keyout myCA.pem -out myCA.pem
Generating a 2048 bit RSA private key .....+++ .............................................................................................................................................+++ writing new private key to 'myCA.pem'
You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:Illinois Locality Name (eg, city) []:Chicago Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Company LTD. Organizational Unit Name (eg, section) []:Information Technology Common Name (e.g. server FQDN or YOUR name) []:Example Company LTD. Email Address []:
创建用于导入用户浏览器的 DER 编码证书
openssl x509 -in myCA.pem -outform DER -out myCA.der
结果文件 (myCA.der) 应被导入到用户浏览器的“证书颁发机构”部分。例如在 FireFox 中:
Open 'Preferences' Go to the 'Privacy and Security' section Press the 'View Certificates' button and go to the 'Authorities' tab Press the 'Import' button, select the .der file that was created previously and pres 'OK'
修改 Squid 配置文件
/etc/squid/squid.conf
http_port 3128 ssl-bump tls-cert=/etc/squid/myCA.pem generate-host-certificates=on dynamic_cert_mem_cache_size=4MB options=NO_SSLv3,NO_TLSv1,NO_TLSv1_1,SINGLE_DH_USE,SINGLE_ECDH_USE ssl_bump stare all ssl_bump bump all
创建并初始化 TLS 证书缓存目录
/usr/lib/squid/security_file_certgen -c -s /var/cache/squid/ssl_db -M 4MB
最后,重启 Squid 后 SSL Bump 即可生效
重启 squid.service。
故障排除
Squid 需要在启动后重启
如果您同时使用 squid 和 NetworkManager,以下错误意味着 squid 在 NetworkManager 启用 Wi-Fi 连接之前启动了(/etc/resolv.conf 为空)。
/var/log/squid/cache.log
Warning: Could not find any nameservers. Trying to use localhost Please check your /etc/resolv.conf file or use the 'dns_nameservers' option in squid.conf.
您可以:
- 启用 NetworkManager-wait-online.service systemd 单元。
- 使用 NetworkManager dispatcher 代替 systemd 来启动 squid。
使用以下脚本 禁用 squid.service systemd 单元:
/etc/NetworkManager/dispatcher.d/10_squid
if test "$1" = 'wlp2s0'
then
if test "$2" = 'up'
then
systemctl start squid
else
systemctl stop squid
fi
fi
确保它是 可执行的。