Squid

来自 ArchWiki

此条目或章节需要改进语言、wiki 语法或风格。参见 Help:Style 以获取参考。

原因: 风格不良 (在 Talk:Squid 中讨论)

Squid 是 HTTP、HTTPS 和 FTP 的缓存代理,提供广泛的访问控制。

安装

注意
  • HTTPS 已用于 ~80%(并且还在增加)的互联网流量,Squid 无法缓存这些流量,除非启用 #SSL 拦截
  • “您的客户端将能够识别代理的存在。如果您正在寻找一种完全秘密地进行此操作的方法,请不要使用 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,因此如果您的 RAM 很大,您可能需要增加它。
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
注意: 在定义新的 cache_dir 后,可能有必要使用此命令初始化缓存目录结构:squid -zN -z 用于创建缺失的交换目录,-N 用于无守护进程模式。
  • cache_dir - 这是您的缓存目录,所有缓存文件都存储在此处。这里有很多选项,但格式通常应如下所示
cache_dir <storage type> <directory> <size in MB> 16 256

因此,在学校的互联网代理的情况下

cache_dir diskd /cache0 200000 16 256

如果您更改了默认缓存目录,则必须在启动 Squid 之前设置缓存目录的正确权限,否则它将无法创建其缓存目录并将启动失败。

访问本地主机名上的服务

如果您计划使用未完全定义的主机名访问 LAN 上的 Web 服务器(例如 http://mywebapp),您可能需要启用 dns_defnames 选项。如果没有此选项,Squid 将按字面意思对主机名 (mywebapp) 发出 DNS 请求,这可能会失败,具体取决于您的 LAN 的 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参见问题 #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。

注意: 如果您正在使用内容过滤解决方案,您应该放置它的端口,而不是 Squid 端口,并且您需要删除 http_port 行中的 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 可以配置为要求用户和密码才能使用它。我们将使用 摘要 HTTP 认证

首先使用 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

警告: 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 拦截

参考: 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 拦截将工作。

重启 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.

您可以

禁用 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

确保它是可执行的

附加资源