# 前言
科学上网的方法有多种,有很多第三方提供的免费方案,这些方案优缺点暂时不予讨论。实际工作生活中还是会有需要自己搭建的情况,这次介绍的是使用 squid+stunnel 方案进行搭建。
# 准备
一台可以访问外网的服务器,如香港的云主机并安装 Ubuntu 系统。
# squid 部分
- 1、安装
apt-get install -y squid |
- 2、配置
生成用户文件
apt-get install apache2-utils | |
htpasswd -c /etc/squid/squid_user.txt 用户名 |
修改 squid 配置
1、直接修改 /etc/squid/squid.conf 文件
2、修改 /etc/squid/conf.d/debian.conf 文件
两种方式都一样,在底部加入以下代码
#dns 服务器地址 | |
dns_nameservers 8.8.8.8 8.8.4.4 | |
dns_v4_first on | |
# 监听端口 | |
http_port 3128 | |
# 定义 squid 密码文件与 ncsa_auth 文件位置 | |
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/squid_user.txt | |
# 认证进程的数量 | |
auth_param basic children 15 | |
# 认证对话框显示提示信息 | |
auth_param basic realm Squid proxy-caching web server | |
# 认证有效期 | |
auth_param basic credentialsttl 24 hours | |
# 是否区分用户名大小,off 为不区分 | |
auth_param basic casesensitive off | |
# 对定义的 squid_user 文件内的用户开启认证访问 | |
acl 用户名 proxy_auth REQUIRED | |
# 允许 squid_user 文件内用户进行代理 | |
http_access allow 用户名 | |
# 顺序匹配,最后添加拒绝所有未允许的规则。不添加会发现,未匹配到的规则会被放行 | |
http_access deny all | |
# 缓存设置 | |
cache_dir ufs /var/spool/squid 100 16 256 read-only | |
cache_mem 0 MB | |
coredump_dir /var/spool/squid | |
# 配置高匿,不允许设置任何多余头信息,保持原请求 header。 | |
header_access Via deny all | |
header_access X-Forwarded-For deny all | |
header_access Server deny all | |
header_access X-Cache deny all | |
header_access X-Cache-Lookup deny all | |
forwarded_for off | |
via off | |
# logs 相关配置 | |
emulate_httpd_log on | |
logformat squid %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh | |
access_log /var/log/squid/access.log squid | |
cache_log /var/log/squid/cache.log | |
cache_store_log /var/log/squid/store.log | |
logfile_rotate 20 |
至次已经可以通过填写安装 squid 的服务器 ip 加端口 3128 加用户名密码进行代理访问了(通过访问 https://www.ip.cn/ 查看 ip 就会发现自己的出口 ip 已经变成了 squid 服务器的 ip 了)。但是要想科学上网还必须对代理的数据进行加密,否则访问外网还是会被我国的长城防火墙阻挡,所以还需要安装 stunnel 来实现此目的。
# stunnel 服务端部分
- 1、安装(和 squid 装在一起)
apt-get install -y stunnel |
- 2、生成 TLS/SSL 证书
openssl req -new -x509 -days 3650 -nodes -out stunnel.pem -keyout stunnel.pem |
3、将证书 stunnel.pem 放到 /etc/stunnel/ 目录下
4、修改 stunnel 配置 (/etc/stunnle/stunnle.conf)
; 设置stunnel的pid文件路径 | |
pid = /etc/stunnel/stunnel.pid | |
; 设置stunnel工作的用户(组) | |
setuid = root | |
setgid = root | |
; 开启日志等级:emerg (0), alert (1), crit (2), err (3), warning (4), notice (5), info (6), or debug (7) | |
debug = 7 | |
; 日志文件路径 | |
output = /etc/stunnel/stunnel.log | |
; 证书文件 | |
cert = /etc/stunnel/stunnel.pem | |
; 私钥文件 | |
key = /etc/stunnel/stunnel.pem | |
; 自定义服务名squid-proxy | |
[squid-proxy] | |
; 服务监听的端口,client要连接这个端口与server通信 | |
accept = 1234(自定义) | |
; 服务要连接的端口,连接到squid的3128端口,将数据发给squid | |
connect = 3128 |
# stunnel 客户端部分
可以安装在要代理的机器上,在需要代理的情况下再开启(代理地址填 127.0.0.1 加客户端监听端口)。也可以安装在国内的服务器上一直保持连接(代理信息填国内服务器 ip 加客户端监听端口)。本示例客户端为 windows 系统
- 1、下载地址
https://www.stunnel.org/downloads.html
- 2、修改配置(C:\Program Files (x86)\stunnel\config\stunnel.conf)
[squid-proxy] | |
client = yes | |
; 监听3128端口,那么用户浏览器的代理设置就是 stunnel-client-ip:3128 | |
accept = 3128 | |
; 要连接到的stunnel server的ip与端口 | |
connect = stunnel服务端ip:1234(服务端自定义端口) | |
; 需要验证对方发过来的证书 | |
verify = 2 | |
; 用来进行证书验证的文件(stunnel服务端生成的证书复制到以下目录并改名为stunnel-server.pem) | |
CAfile = C:\Program Files (x86)\stunnel\config\stunnel-server.pem |
至次配置好代理 ip 为 stunnel 客户端 ip 加端口 3128 就可以正式科学上网了。如果只想对需要科学的 url 进行代理,可以通过安装 Proxy SwitchyOmega 插件实现(规则地址可通过 https://github.com/gfwlist/gfwlist 获取)。