Total Pageviews

Saturday 24 August 2013

利用squid搭建https proxy+chrome翻墙(我测试失败)

Squid是一个大名鼎鼎老牌软件,用于做HTTP缓存代理。它非常完善的支持HTTP/1.1 proxy协议,所以我的方法就是:让squid以https的方式提供forward proxy service,然后在客户端使用chrome直接访问。
首先,我在usa有一个virtual private server(VPS),操作系统是debian/ubuntu,我要在它上面安装squid。最简单的方式是直接apt-get install squid,但是我想要新的squid 3.3版本,这个在官方的仓库中没有。所以我就采用了源代码安装。
   wget http://www.squid-cache.org/Versions/v3/3.3/squid-3.3.8.tar.gz
   tar zxvf squid-3.3.8.tar.gz
   cd squid-3.3.8
   ./configure --prefix=/usr --sysconfdir=/etc/squid --libdir=/usr/lib  
  --with-openssl 
  --enable-auth-basic=DB --enable-auth-ntlm=none  
  --enable-auth-negotiate=none 
  --enable-auth-digest=none 
  --with-logdir=/var/logs 
  --with-pidfile=/var/run/squid.pid --with-swapdir=/var/spool/squid 
  --libexecdir=/usr/lib/squid --with-default-user=squid 
  make
  make install
(运行configure之前,需先安装perl和g++: apt-get install perl g++ -y)
然后修改Squid的配置文件。先是修改监听的端口号,把http_port变成https_port
https_port 4433 cert=/etc/mycert/server.crt key=/etc/mycert/server.key cafile=/etc/mycert/allca.pem
其中cert和key分别是网站的HTTPS证书和私钥。注意证书的common name一定要和服务器的域名相匹配。cafile是用于签署cert的CA链。
然后配置身份认证。squid默认是按照IP进行权限控制,但是这个对我不适用,因为我是要从公网访问它,client没有一个固定的IP。所以最简单的办法就是通过http basic身份认证。
auth_param basic program /usr/lib/squid/basic_db_auth
auth_param basic children 5 startup=5 idle=1
auth_param basic realm Squid myRealm
auth_param basic credentialsttl 2 hours
acl pauth proxy_auth REQUIRED
http_access allow pauth
#http_access allow localnet
http_access allow localhost
# And finally deny all other access to this proxy
http_access deny all
basic_db_auth这个程序是用perl写的,从数据库中读取用户名密码。更简单的方式是用basic_ncsa_auth,搭配apache的htpasswd程序使用。
client端很简单,和使用SPDY proxy一样,从命令行启动chrome,在末尾加上--proxy-server=https://urdomain.com:4433即可。注意,把这里的域名换成你的服务器域名。
假如浏览器(比如IE)不支持https的proxy怎么办呢?这时可以请stunnel这个程序来帮忙。https://www.stunnel.org/
装完stunnel后给它写一个简单的配置文件:
client=yes
fips=no
[proxy]
accept = 127.0.0.1:50000
connect = urdomain.com:4433
CAfile = /etc/allca.pem
如果是Linux或Mac OS X,把上述内容以文本文件方式保存为/etc/stunnel/stunnel.conf
然后启动 stunnel 程序。直接在命令行上执行 stunnel 回车。
然后测试一下:
curl -L -v  --proxy-digest -Usnnn:xxxxx -x http://localhost:50000 http://twitter.com/
其中-U后面是proxy的用户名密码,以冒号分割。
------------------
相关帖子:
https://briteming.blogspot.com/2015/08/squidchrome.html
https://briteming.blogspot.com/2013/03/linux-vpssquidstunnel.html