Pages

Monday, 14 September 2015

用Squid在SSH开启个Tunnel代理

这是icyboy自己想出来的想法,并在google和一些国外大神的帮助下,总算搞清楚了如何操作。
总所周知,squid是一个非常出色的HTTP代理软件,常用于用于CDN分发、Load Balance等高端服务器操作。现在,我们大材小用,把它用来钻洞翻墙。据身在国内的icyboy说,他测试下来的结果,squid使用效率比ssh动态转发高效得多,当然这得归功于squid本身了。
首先,我们需要一台能在国内访问的国外VPS或者独立主机,日美韩三国的速度比较不错。博客里有很多卖vps的商家推荐,建议自己去看看。
第二,VPS需是Linux系统,Windows配置这里不讲。原理是通过SSH映射本机一个端口到服务器上squid的一个端口,这样,我们的代理就直接通过了SSH的加密,省下了不少GFW关键字重置的麻烦。
配置过程:
使用SSH命令安装squid,本例只拿Debian做一个例子。
apt-get install squid
安装好后直接修改/etc/squid/squid.conf的配置文件,如下内容:

# Recommended minimum configuration:
#
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
acl all src 0.0.0.0/0.0.0.0
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
#acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
#acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
#acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
#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
#
# Recommended minimum Access Permission configuration:
#
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager
# Deny requests to certain unsafe ports
#http_access deny !Safe_ports
# Deny CONNECT to other than secure SSL ports
#http_access deny CONNECT !SSL_ports
# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on “localhost” is a local user
#http_access deny to_localhost
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
#http_access allow localnet
http_access allow localhost
acl INTRANET dstdomain .intranet.example.com critical.example.com
http_access allow INTRANET
append_domain .domainname.com
# And finally deny all other access to this proxy
http_access deny all
# Squid normally listens to port 3128
http_port 3128
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /var/spool/squid 800 16 256
cache_mem 500 MB
# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid
# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
其实最复杂的部分就是配置squid的过程,现在已经简化好了。这个配置里需要重点注意的是三行:
acl INTRANET dstdomain .intranet.example.com critical.example.com
http_access allow INTRANET
http_access deny all
除了本机,拒绝其他任何形式的连接。小老外在第三行这里吃过亏,deny all机器认不出来。我就在上面的配置文件里加上了一行acl all src 0.0.0.0/0.0.0.0就能用了。icyboy的配置并没有这一行。如果你最后的测试结果有问题,可以试着把这行去掉。Squid的端口可以自己改,不一定要默认的3128。记得修改完配置后/etc/init.d/squid restart重启一下squid。
好了之后,下载plink.exe,在相同文件夹下创建一个bat批处理,命令行如下:
plink.exe -2 -P 22 -pw sshpassword -L localhost:9090:localhost:3128 username@serverIPaddress
命令中-p是端口号,如果你的SSHd端口被修改过,请自己更改。sshpassword是你的ssh密码,最后一个username@serverIPaddress我不多讲了吧。9090是本机映射的端口,3128是服务器squid端口。
然后是本机设置。
Firefox可以通过插件或者直接设置,通过HTTP代理来获得翻墙功能。Chrome建议大家装个Proxy SwitchySharp,在里面新建一个squid的配置。具体也不多讲了吧,这步是最简单的了。
其实Squid除了用SSH加密之外,还可以用SSL加密,具体操作类似,配置不同而已。在服务器443端口开个squid,把所有流量都代理出去。想试试的就直接Google吧。
from https://www.d0z.net/archives/5583
------------------
经过测试,上面那段squid.conf内容不适合squid3.我用apt-get install squid3来安装squid3,然后用此方法翻墙失败。改装squid(即squid2.x,命令为apt-get install squid)后,翻墙成功。而且可以访问xyz.blogspot.com网站,不过访问blogger.com,youtube.com,却打不开页面,不知为何?所以不要舍近求远了,还是用ssh -D 1081 root@vps-ip -p ssh_port_no更好!