# 前言科学上网的方法有多种,有很多第三方提供的免费方案,这些方案优缺点暂时不予讨论。实际工作生活中还是会有需要自己搭建的情况,这次介绍的是使用 squid+stunnel 方案进行搭建。
# 准备一台可以访问外网的服务器,如香港的云主机并安装 Ubuntu 系统。
# squid 部分
1 apt-get install -y squid
1 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 文件 两种方式都一样,在底部加入以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 dns_nameservers 8.8 .8 .8 8.8 .4 .4 dns_v4_first on http_port 3128 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 auth_param basic casesensitive off acl 用户名 proxy_auth REQUIRED 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_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 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 apt-get install -y stunnel
1 openssl req -new -x509 -days 3650 -nodes -out stunnel.pem -keyout stunnel.pem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ; 设置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 系统
https://www.stunnel.org/downloads.html
2、修改配置(C:\Program Files (x86)\stunnel\config\stunnel.conf)
1 2 3 4 5 6 7 8 9 10 11 [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 获取)。