Squid如何搭建二级代理

Database and Ruby, Python, History


我想做一个代理,类似于科学上网,DeepSeek给我推荐Squid。但是,我没有玩过Squid,而且DeepSeek回复的也连续多次把我带到沟里。所谓的AI,如果你会这些,那么就如虎添翼;如果你不会,0到60很快,但是从60到80很可能会翻车。

客户端 -> Proxy 1 -> Proxy 2 -> 互联网

Proxy 2

首先创建二级代理,修改/etc/squid/squid.conf文件如下。这就暴露一个3128端口,自己可以通过export https_proxy=http://127.0.0.1:3128 http_proxy=http://127.0.0.1:3128,测试一下代理是否可用。

acl localnet src 0.0.0.1-0.255.255.255	# RFC 1122 "this" network (LAN)
acl localnet src 10.0.0.0/8		# RFC 1918 local private network (LAN)
acl localnet src 100.64.0.0/10		# RFC 6598 shared address space (CGN)
acl localnet src 169.254.0.0/16 	# RFC 3927 link-local (directly plugged) machines
acl localnet src 172.16.0.0/12		# RFC 1918 local private network (LAN)
acl localnet src 192.168.0.0/16		# RFC 1918 local private network (LAN)
acl localnet src fc00::/7       	# RFC 4193 local private network range
acl localnet src fe80::/10      	# RFC 4291 link-local (directly plugged) machines
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 deny !Safe_ports
http_access allow localnet manager
http_access deny manager
http_access allow localnet
http_port 3128
http_access deny all

Proxy 1

再创建一级代理,修改/etc/squid/squid.conf文件如下。

acl localnet src 10.0.0.0/8
acl target_domains dstdomain .googleapis.com .google.com
acl target_ips dst 142.250.217.74/32
acl target_ips dst 142.251.33.74/32
acl target_ips dst 142.250.217.106/32
acl target_ips dst 142.251.215.234/32
acl target_ips dst 172.217.14.234/32
acl target_ips dst 142.250.69.202/32
acl target_ips dst 142.251.33.106/32
acl target_ips dst 142.251.211.234/32

http_port 8080

cache_peer 127.0.0.1 parent 3128 0 no-query proxy-only
cache_peer_access 127.0.0.1 allow target_domains
cache_peer_access 127.0.0.1 allow target_ips
cache_peer_access 127.0.0.1 deny all

never_direct allow target_domains
never_direct allow target_ips
never_direct deny all

http_access allow localnet
http_access deny all

在第二个代理上,我把正向代理和反向代理搞混了。主要在Dify上也用了Squid,同时引入了正常代理和反向代理。 此外,第二个代理特别的地方在于,创建了cache_peer之后,还需要指定never_direct deny all,即禁止所有直连,强制要求走二级代理。中间因为没有配置这个,导致请求在一级代理就直接访问互联网了。

Dify

其实我做代理的目的是为了让Dify使用Gemini,但是默认情况下,Google用的GRPC方式去访问,无视我配置的HTTP_PROXY和HTTPS_PROXY。https://github.com/langgenius/dify/issues/4186提示可以修改transport="rest",但是又会遇到Squid不支持的HTTP/2协议的事情。 问了DeepSeek,Squid支持HTTP/2,但还没有试过,感觉还要掉到沟里一次。 由于默认用的GRPC,又可以通过all_proxy或者grpc_proxy去设置代理,这些也还没有尝试过。