使用加密的squid配合stunnel实现HTTP代理

现在大部分人都是用ssh tunnel来搭建socks5代理,其实这种方式效率并不高,ssh tunnel并不是为了做代理而存在的。一个比较好的方法是加密squid配合stunnel实现http代理。下面介绍在Archlinux下配置https squid和windows下配置stunnel的方法。

1.首先是Archlinux下安装squid。注意现在squid在AUR里,需要先用makepkg生成yaourt的安装包然后安装。此步骤不在本文讨论范围内。

2. 配置squid,主要包括添加访问权限,以及配置https监听模式。
2.1 创建证明书文件

openssl req -new > yourdomain.com.csr
openssl rsa -in privkey.pem -out yourdomain.com.key
openssl x509 -in yourdomain.com.csr -out yourdomain.com.crt -req -signkey yourdomain.com.key -days 3650

然后将yourdomain.com.crt和yourdomain.com.key拷贝到/etc/squid目录下(或其他目录)。

2.2 添加squid用户
这一步是生成用户名密码对,同时将密码用hash函数处理。用于squid的用户认证。

htdigest -c /etc/squid/users yourdomain.com yourusername

2.3 配置squid.conf
下面给出与本文要实现功能有关的配置参数:

auth_param digest program /usr/lib/squid/digest_file_auth -c /etc/squid/users
auth_param digest children 3 startup=1 idle=2
auth_param digest realm MyRealm
acl users proxy_auth REQUIRED

http_access allow users
http_access deny all

https_port XXXXX cert=/etc/squid/yourdomain.com.crt key=/etc/squid/yourdomain.com.key

其中,MyRealm可以随便起名,XXXXX是你要squid监听的端口号。同时要注意”http_access deny all”这行要放在所有其他的http_access语句之后。

2.4 启动squid
Archlinux下可以用下面命令启动squid:

systemctl start squid.service

然后运行下面命令检查squid的配置是否正确,如果正确则无任何返回信息:

squid -k check

3. 配置stunnel
下面开始配置Windows端(客户端)的stunnel,将https流量转换成http,实现http代理功能。
编辑stunnel.conf,注释掉所有行,然后加入下面配置参数:

client = yes
[ssl2http]
accept = 127.0.0.1:8080
connect = xxx.xxx.xxx.xxx:XXXXX

其中,xxx.xxx.xxx.xxx是运行squid程序的机器IP,XXXXX是之前配置的监听端口号。

4. 测试
现在大功告成,将浏览器配置成使用地址为“127.0.0.1”,端口为8080的HTTP代理,然后就去twitter、facebook测试一下吧!

如有疑问,欢迎回复讨论。
本博客中文章皆为原创,如转载请注明出处,谢谢!

本文为悠然居(https://wordpress.youran.me/)的原创文章,转载请注明出处!

5 thoughts on “使用加密的squid配合stunnel实现HTTP代理”

  1. 你好,按照你上面的配置,我不能够访问google和fackbook等敏感网站,使用的是美国的vps

    Google Chrome 28.0.1500.95 Google Chrome 28.0.1500.95 Windows 7 x64 Edition Windows 7 x64 Edition
    1. 难道高墙又升级了……我之前用这个方法是没问题的,现在在墙外也没法测试

      Firefox 18.0 Firefox 18.0 Windows XP Windows XP
    2. 另外检查下各步骤是否有报错或者遗漏的。DNS也要设成google的或者opendns,不能用国内ISP的。HTTP代理本身并不负责解析域名

      Firefox 18.0 Firefox 18.0 Windows XP Windows XP
  2. 我电脑DNS 设置的是8.8.8.8配置也都成功了,stunnel也有和代理通信的信息

    Google Chrome 28.0.1500.95 Google Chrome 28.0.1500.95 Windows 7 x64 Edition Windows 7 x64 Edition

Leave a Reply to youran Cancel reply

Your email address will not be published. Required fields are marked *