利用Raspberry Pi作为GAE翻墙代理服务器
因为Raspberry Pi耗电量小,所以它非常适合用于作为不关机服务器。
1. 环境介绍
服务器:Raspberry Pi
操作系统:Raspbian(Raspberry Pi官方提供基于Debian的Linux系统)
安装软件:WallProxy 2.1.14
网络环境:联通宽带,动态分配IP,使用路由器接入互联网。
2. 搭建代理
本文假定Raspberry Pi的操作系统已经安装完毕,Raspbian系统自带有Python 2.7和Python 3.2,而WallProxy本身是使用Python编写的,因此可以直接将WallProxy复制到Raspberry Pi即可。
因为我一直偏好使用WallProxy而非GoAgent,WallProxy使用更方便而且兼容GoAgent,一直是我的首选。本文假定你已有一个正常运行的WallProxy或GoAgent,至于如何在GAE上搭建WallProxy或GoAgent的服务端则不是本文描述的范畴内,请自行Google教程。
2.1. 安装WallProxy
在Linux直接使用ssh命令访问Raspberry Pi,在Windows下可以使用SecureCRT等工具访问。将WallProxy目录下的local文件夹复制到Raspberry Pi中即可,例如我的安装路径为:~/isoft/WallProxy_2.1.14/local。
2.2. 设置开机启动WallProxy
编辑 /etc/rc.local 文件:
|
1
|
sudo nano /etc/rc.local |
在 exit 0 之前插入以下代码:
|
1
|
setsid python /home/pi/isoft/WallProxy_2.1.14/local/startup.py &> /dev/null |
2.3. 为Raspberry Pi设置局域网静态ip
作为服务器,Pi需要一个固定的ip来响应客户端的请求,由于Pi是通过路由器接入宽带,而宽带的ip也是动态分配的,因此内外网ip都是动态的,所以首先要配置静态的内网ip,然后监视动态的外网ip。
Pi自带了一个网卡,同时也支持USB无线网卡,下面分别介绍这两种网卡的静态ip配置方法。
Pi自带网卡
编辑 /etc/network/interfaces 文件:
|
1
2
3
4
5
6
7
8
9
|
auto loiface lo inet loopbackauto eth0iface eth0 inet staticaddress 192.168.1.5gateway 192.168.1.1netmask 255.255.255.0network 192.168.1.0broadcast 192.168.1.255 |
以上代码就是将Pi设置为192.168.1.5的静态IP,如果要自动获取IP,只需要将 iface eth0 inet static 修改为 iface eh0 inet dhcp,然后将其下方代码全部注释掉即可(注释符号:#)。
Pi外接USB无线网卡
使用无线网卡可以使用 lsusb 命令来查看设备是否正常:
|
1
|
lsusb |
如在列表中看到插入的网卡设备,则说明网卡可用。
然后同样编辑 /etc/network/interfaces 文件:
|
01
02
03
04
05
06
07
08
09
10
11
12
|
auto loiface lo inet loopbackallow-hotplug wlan0auto wlan0iface wlan0 inet staticwpa-ssid "SSID"wpa-psk "PASSWORD"address 192.168.1.5gateway 192.168.1.1netmask 255.255.255.0network 192.168.1.0broadcast 192.168.1.255 |
以上就将无线网络配置好了。(我使用的是Mercury MW150U无线迷你网卡)
在编辑interfaces文件之前,最好将interfaces备份一下。
编辑interfaces文件需要root权限,因此需要:
|
1
|
sudo nano /etc/network/interfaces |
设置静态IP的时候,只有 address 和 netmask 是必须的。而 netmask 总是 255.255.255.0 ,而 gateway 通常来说都是 192.168.1.1 ,根据路由器不同而有可能不同。
配置完成之后,需要重启网络:
|
1
|
sudo /etc/init.d/networking restart |
重启网络后,ssh连接会断开,再次连接就可以使用刚才配置的静态IP。但是使用 restart 命令有可能会发生错误而无法连接到Pi,因为 restart 命令可能没有激活某些接口,因此 restart 命令不推荐使用。
你可以使用 sudo reboot 来重启 Pi,从而重启网络。或者使用 reload 命令代替 restart:
|
1
|
sudo /etc/init.d/networking reload |
(参考链接:http://elinux.org/RPi_Setting_up_a_static_IP_in_Debian)
2.4. 监控外网IP
由于没有静态IP,所以只要网络中断,外网IP就会发生变化,要想随时随地为其他终端提供代理,既然不能固定IP,那就监视IP,随时随地为其他终端通知当前实际IP地址。
Python是Raspberry Pi官方编程语言,而且WallProxy也是由Python编写而成,所以用Python写个监视程序再理所当然不过了。
以下为负责监控的py脚本:
|
01
02
03
04
05
06
07
08
09
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#!/urs/bin/env python# -*- coding: utf-8 -*-# proxy_monitor.py# author = 'iFantasticMe'import urllib2import smtplibimport timeimport osfrom email.mime.text import MIMEText# 收信人地址列表,支持多个。#设置服务器,用户名、口令以及邮箱的后缀mail_host = "smtp.host.com"mail_user = "user"mail_pass = "password"mail_postfix = "host.com"def send_mail(sub, content, to_list=None): """ 发送邮件 sub: 邮件标题 content: 邮件内容 to_list: 收信人列表 """if not to_list:to_list = mail_to_listme = mail_user + "<" + mail_user + "@" + mail_postfix + ">"msg = MIMEText(content)msg['Subject'] = submsg['From'] = memsg['To'] = ";".join(to_list)try:s = smtplib.SMTP()s.connect(mail_host)s.login(mail_user, mail_pass)s.sendmail(me, to_list, msg.as_string())s.close()return Trueexcept Exception, e:print str(e)return Falsedef get_router_ip():"""获取电信运营商动态分配的互联网ip."""feedback_url = r"https://my-bottle-app.appspot.com/ip"opener = urllib2.build_opener(proxy, urllib2.HTTPHandler, urllib2.HTTPSHandler)urllib2.install_opener(opener)f = urllib2.urlopen(feedback_url, timeout=10)ip = f.read()return ipdef monitor_ip():"""监控当前使用的外网ip是否有效。"""ip_txt = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ip.txt")f = open(ip_txt, 'a+')used_ip = f.read()ip = get_router_ip()if used_ip != ip:if send_mail("New ip for RaspberryPi", ip):print "New ip has been sent out."f.seek(0)f.truncate()f.write(ip)f.close()return Trueelse:print "Sorry, mail has not been sent."return Falseelse:print "The ip doesn't change."if __name__ == "__main__":retry_times = 5 # 如有故障,尝试5遍。while retry_times:retry_times -= 1 time.sleep(15)if monitor_ip():retry_times = 0 |
以上脚本中,请自行配置收发邮件人和服务器,即以下内容:
|
1
2
3
4
5
6
7
8
|
# 收信人地址列表,支持多个。#设置服务器,用户名、口令以及邮箱的后缀mail_host = "smtp.host.com"mail_user = "user"mail_pass = "password"mail_postfix = "host.com" |
将此脚本可以放置到任何位置,但建议放到WallProxy目录下,以便管理。
为了密切监视IP,将监视脚本加入到crontab中,设置为每小时扫描IP一次。
|
1
|
00 * * * * setsid python /home/pi/isoft/WallProxy_2.1.14/local/proxy_monitor.py |
全部设置完毕后,IP发生改变后,新IP地址会在一小时之内发送到指定的邮箱

共有 0 条评论