在 Linux 下使用你的网站主机作为加密代理服务器

曾经介绍过如何使用 Amazon EC2 建立自己的代理服务器来“翻墙“。用这个方法翻墙虽然网速很快,但还是有些缺点。Amazon EC2 是收费的,而且按小时算,所以不能随时都开着我的代理服务器。另外,每次想用代理的时候还得先等一两分钟,让服务器启动。

然而我今天看到了这个贴子,这才发现还可以使用一个普通的网站主机作为我的代理服务器。与 Amazon EC2 不同,普通的网站主机是 24/7 运行的,而且你本来就要用它做你的网站,不用额外花钱。以上帖子是专门写给使用 Dreamhost 的 Windows 用户。不过,凡是有支持 SSH 服务网站主机的 Linux 用户也可以享受翻墙的自由感。我为 Linux 用户编了一个小的 BASH 脚本,其主要功能为:

  • 与网站主机创建一个 SSH 隧道连接
  • SSH 隧道用完之后,安全地关闭 SSH 隧道连接

用这个脚本,我试了一下上平时被 GFW 封杀的 youtube.com… 结果 Firefox 差点让我晕倒了! 用这个代理,网速快极了… 感觉好像我还在英国上网似的 (那边一般都用 8M 以上的宽带) – 你可以看一下视频:

介绍一下脚本的内容

export SSH_HOST=admin@yoursite.com # username@host

这个需要你自己改一下… 就是你的 webhost 给你的 SSH 用户名和主机地址。 格式为 用户名@地址 (凡是用过 SSH 的人都应该知道写什么)

if [ ! -f /tmp/.tunnel ]

看看是否已经有 SSH 隧道开着。有的话就将其关闭。没有就创建新的 SSH 隧道。

ssh -f -D 9999 $SSH_HOST "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done" &

创建新的 SSH 隧道,设定本地 SOCKS 端口为 9999。隧道将会开着直到远程目录 “~/” 里有人创建 .tunnel 此文件为止。

touch /tmp/.tunnel

在你的电脑上创建 .tunnel 这个文件。这样脚本就知道正开着一个 SSH 隧道。

ssh $SSH_HOST "touch ~/.tunnel"

在远程目录 “~/” 里创建 .tunnel 此文件,这将会断开你电脑与代理服务器的连接

rm /tmp/.tunnel

删除 .tunnel 这个文件。这样脚本就知道 SSH 隧道已关闭。

下载并执行脚本
可以在此下载脚本

下载后放在你的首目录,比如在 ~/bin/。用一个文字编辑器修改脚本里的 ssh 变量并用 chmod 改一下文件权限

chmod u+x tunnel.sh

创建连接
在终端中执行脚本,执行后可以关闭终端。

open_tunnel.png

关闭连接
再次在终端中执行脚本并关闭 Firefox (如果 Firefox 还在用隧道的话,连接暂时无法关闭)

close_tunnel.png

用 SSH 密钥进行授权
你或许注意到了… 以上脚本里没有任何地方可以写下你的 SSH 密码。这就是因为我在用 SSH 密钥来进行电脑与服务器的授权工作,这样更自动化而且比将密码直接写入文本的文件里要安全多了。先打开一个终端,根据一下步骤进行密钥配置:

[jonolumb@jonoxps .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jonolumb/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jonolumb/.ssh/id_rsa
Your public key has been saved in /home/jonolumb/.ssh/id_rsa.pub
The key fingerprint is:
h7:10:49:46:ab:2t:3b:a3:36:2z:15:56:d4:f2:b7:3d jonolumb@jonoxps

请注意,配置时密码要留为空白。

将生成的 /home/jonolumb/.ssh/id_rsa.pub 的内容复制到远程服务器目录下,具体位置是:
~/.ssh/authorized_keys
要保证远程的 authorized_keys 和 本地的 id_rsa 文件的权限均为 700。
这样就可以自动登录到远程服务器了。

配置 Firefox 使用代理
创建了 SSH 隧道之后,就进入 Firefox 并打开:

首选项 --> 高级 --> 连接设置

选择“手动配置代理”并将其设定为:
HTTP 代理: localhost 端口: 9999

firefox_proxy.png

如果你用这个脚本遇到什么问题,请跟我取得联系。我很想听你们的意见。谢谢!

Tags: , , , , , , , , , , , , , ,

55 comments

  1. 谢谢你的脚本..很棒..
    呃..”变数”写成”变量”好点

  2. 我也一直在用ssh tunnel方式作为代理,不同的是我用plink(putty的一个组件)

    plink -C -D 127.0.0.1:7777 -N -pw 密码 用户名@服务器地址

    “下载后放在你的首目录。用一个文字编辑器修改前面的 SSH 变数并用 chmod 改一下权限” 这句话有点问题,你想说的应该是“根目录”而不是“首目录”,“修改前面的 SSH 变数”改为“修改脚本里的ssh 变量”

    另外,脚本里的
    export SSH_HOST=yourname@yoursite.com@yoursite.com
    为什么有两个@yoursite.com呢?

    建议用
    ssh -CfNg -D 127.0.0.1:7777 用户名@服务器地址

    没有看到你输入密码……疑惑ing

    • kukat 你好

      我的意思确实是要把脚本放在首目录里面,比如说我的放在 /home/jonolumb/bin/ 这个位置

      恩,应该写“修改脚本里的 ssh 变量“,我回完了就去改一下。

      我用的 webhost 是 Mediatemple,它的 SSH 用户名有点不同。

      用户名:username@host.com
      地址:host.com

      非 Mediatemple 用户应该是: username@host.com

      至于以上没有输入密码,为了更自动地创建 SSH 连接,我在使用 SSH 密匙来进行授权而不是用密码。详情请见这个帖子

      我而在去改一下帖子,写得有点不清楚…

  3. I have my own script to open a ssh tunnel:

    cat ~/bin/fuckgfw
    #!/bin/bash
    ssh -CfNg -D 9999 user@host.com

    but I have to `pkill ssh’ when I want to exit,, and i did’t got the way in your script quit ssh….

    I use an host provided by GFWBlog anyway, they only offer limited privilage access to the host.

    • When you use the -f flag in SSH it forks the process to the background – which means there is no way of killing it unless the remote host closes the connection or you simply pkill it. However, using the method mentioned, you are basically telling the remote server to establish a tunnel and run the command passed within the quotation marks. In this case the command tells the server to do nothing until an empty file called .tunnel is created in the ~/ (home) directory. When you want to break the connection, you simply use ssh to connect to the server and create this file and the tunnel will close on its own. All this is done automatically in the script – for more info you may want to look at this post which is where I got the idea in the first place!

      By the way, I love the name of your script!

  4. 没记错的话 tag 应该用逗号分开

  5. echo > /dev/null 改成 sleep 5 之类的?
    主机的名字、login 之类的可以写到 ~/.ssh/config 里面,我觉得那样方便。

    • 当然可以… 我自己也这么做。脚本里那样写只是为了方便那些不经常使用 SSH 的人。

      用 sleep 5 不是很合适… 过了5秒钟之后,只要没有别的软件用你的 SSH 隧道,它会自动关闭。如果你想一直开着的话,还是我那个方法好…

      • In your script, the short while loop will take much of your cpu resources (normally it will hog one whole core for this only). If you change your loop statement to “echo > /dev/null; sleep 5″, it will only be invoked every 5 seconds, which saves lot of ur cpu cycle.

        things like “用 sleep 5 不是很合适… 过了5秒钟之后,只要没有别的软件用你的 SSH 隧道,它会自动关闭。如果你想一直开着的话,还是我那个方法好…” will not happen. You whole loop won’t be broken by the sleep.

  6. Mate, excellent job! I am really impressed by your Chinese.I reckon that your Chinese is better than my English;-)

    I am using SSH tunnel on a daily basis to protect privacy. But I use putty on Linux (I am lazy) and a simple script on OS X (I don’t want to use the bloody ports).

    You may want to try SwitchProxy for Firefox if you need to switch proxies or stop using SSH tunnel from time to time.

    Terry
    a GNU/Linux and open source fan;-)

    • Thanks mate, still… my chinese has much room for improvement!

      Thanks for the recommendation (SwitchProxy), I’m going to have a look now. Up until now I have been using FoxyProxy to selectively redirect sites that I know are blocked over here.

      Now following you on twitter :-)

  7. 要去试试,网速快是个好东西啊。

  8. 实际上,ssh的config(~/.ssh/config)可以直接将你的连接信息配置成一个名称。具体查询man。而后,配合id_***.pub直接登录,可以将整个命令(包括-D)简化到一条指令。下面贴一个范例
    ———~/.ssh/config————–
    Host abc
    User username
    Port 22
    HostName remote ip
    DynamicForward your local port
    ————-end of file————
    而后,在命令行下执行应当是ssh abc,就可以自动完成登录。
    下面有机会我弄一下inetd,应该可以做到需要的时候自动连接,不需要了自动释放。

    • 我昨天看到了你的评论就试了一下用 xinetd 配置需要的时候自动连接,怎么弄都不好,折腾了好几个小时。好像出现了一个 Could not bind address 的错误… 你如果能做得更成功一定要告诉我啊!

  9. 甚好! 看来我要计划买个可以SSH的空间了。
    btw 知道了你汉化了keepassx。我相信把“母语译成外语”要比把“外语译成母语”难的多, You are so niubility!

    • 呵呵,谢谢你的夸奖!
      最近很多喜欢的网站都被 wall 了(facebook、twitter等)… 你还是早点买个 SSH 空间好~

  10. LDCN游客路过…
    虽然我一直用的是收费的VPN,但是还是要顶一下你。
    你真的是英国人么?汉语写得真不错。

  11. 请问你是用哪个空间服务商?我用hostmonster.com,速度非常慢

  12. Jono,感谢你的分享精神。另外推荐一个Ubuntu下的SSH Tunnel管理软件Gnome SSH Tunnel Manager,http://sourceforge.net/projects/gstm/ 。

  13. 如果ssh有流量限制或者速度不是很快时,可以在firefox里用proxy.pac(gnome的网络设置里好像也能用)来设置哪些网站使用代理。

    • It’s actually much easy to use the “FoxyProxy” plugin for Firefox and configure advanced rules determining which pages should be redirected through the proxy – sorry, can’t write in Chinese on this computer :-)

  14. 中文太好了点吧。。。。。佩服~~~

  15. 为啥搞这么复杂丫。。。 ssh -D 7070 user@host

    就可以搞了么?

  16. 我靠。这个世界真小啊。我以前在哪里看到过你的头像来着,这不,我们又用起了同一个theme。这个cleanr很不错的,另外我用wp-syntax插件显示代码。

    • 恩,我这博客还是以内容为主,博客的设计我就无所谓,越简单越好~
      我刚看了你的博客,你用的 wp-syntax 插件看起来不错,有时间我也要安装!

  17. Jono,现在那个shell下不了,麻烦您发到我邮箱吧。
    邮箱:
    kapenware@gmail.com
    还有我不经常用SSH,顺便问一下哪些免费的SSH服务器用起来比较爽啊。
    谢了。。

    • 我已经把脚本发到你邮箱了

      至于 SSH 的服务器,一般来说都是要收费的,我自己用的是 Media Temple,不过应该有比这便宜的。

  18. 谢谢啦,邮件已经收到了,正在努力翻。。。

  19. 你好,那个脚本现在下不了了。麻烦您发到我一份邮箱吧。多谢。hitacmclub@gmail.com

  20. 楼主你好,我在墙内无法下载文中提到的脚本,你给我发一个好吗?谢谢啦!

  21. 怎么建立了SSH链接之后,在终端看到的是“Connection timed out”这样的信息?

Leave a comment