日志分类:Web与移动平台

Featureless Wireless Router + Linux Machine = VPN Wireless Router

2010/08/29 | 21:56 | 分类:Web与移动平台 | 标签: | 98次阅读

When I tried to access some oversea services on my Milestone, the connection was often reset by the "firewall". I have an OpenVPN account but the Android system only provides PPTP and L2TP connectivity (I do not want it rooted yet). Then I tried to make my laptop PC with VPN connection to be a wireless access point. It was unfortunate that my old Intel wireless network adaptor does not support the Master (Access Point) mode, while the unrooted Android system cannot connect to an Ad-hoc wireless network. I had to configure a VPN enabled wireless router as the access point...

Lots of articles teach you how to configure a VPN wireless router that can break the "firewall" transparently. These techniques are all based on some powerful firmware like OpenWrt. If your featureless router does not support these firmware, it cannot run as a VPN based proxy. But that is not a problem if you can dedicate a Linux machine to running the VPN client. An old desktop or laptop PC is enough.

1. Install the VPN client and the iptables service on the Linux machine. Connect it to the LAN of the wireless router, either by wire or wireless.

2. Configure the VPN client on the Linux machine and ensure it works well. You will see a VPN tunnel device created, like "tun0" for my OpenVPN connection.

3. Make the Linux machine to be a gateway that forwards the LAN traffic to the VPN tunnel. You should write correct IP range and tunnel device name in the command,

  1. echo 1 > /proc/sys/net/ipv4/ip_forward
  2. iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o tun0 -j MASQUERADE

and the verify them after executing.

  1. iptables -t nat -L

If you want the it to be a regular service, you can insert the VPN connecting and IP forwarding commands into /etc/rc.local or other starting-up scripts.

4. Modify the DHCP settings of the wireless router. For most routers, these settings are supplied via a web portal. The gateway should be the IP of the Linux machine that runs the VPN client, and the DNS should be an unpolluted one.
If you do not have the administrative permission to the router, you have to ask the wireless users to perform these settings on their client devices manually.

5. Try to connect to the wireless router from a client device. After connected, check the auto-allocated gateway and DNS. Then try to browse a website showing the client's IP (e.g. ifconfig.me), if it shows the VPN's IP, you made it.

使用智器 V5 实现山寨 MiFi

2010/05/04 | 23:30 | 分类:Web与移动平台 | 标签: | 915次阅读

  上个月 iPad 上市后,我就常听到 idealeeheqian 同学鼓吹 MiFi 及其类似设备。说白了,那就是一个 3G-to-WiFi Adaptor。只要有一台同时支持 3G 和 WiFi 的设备,并且其系统相对开放,实现 MiFi 的功能就不难。笔记本电脑当然可以,不过体积大了点。正好我年前购入一台智器 V5,就用它来山寨一台 MiFi 吧。
  智器 V5 的三个系统中,只有 Ubuntu 预装了 3G 驱动和拨号程序,那就在 Ubuntu 中实现。首先使用 USB OTG 连接 3G Modem,我这里是华为的 WCDMA Modem。智器最新版的固件不需要显式拨号,Modem 插入之后自动联网。然后使用 NetworkMananger 创建新的无线网络,为其设定名称(“miffy”如何?)和密钥,这样一来智器摇身一变成了无线 AP。现在试用笔记本电脑搜索周围的无线网络,很快就会发现智器的信号。连接之,密钥校验通过之后笔记本和智器便构成了局域网,使用 /sbin/ifconfig 可查看各自的 IP(在我这里为 10.42.43.*)。Ping 一下,连接正常。

使用智器 V5 实现山寨 MiFi

  下一步理应在智器上配置 iptables,将来自笔记本的数据包转发到 WCDMA 网络。智器的 Ubuntu 中虽然有 iptables 命令,然而运行后才发现这个 Linux Kernel 中的 ip_tables 模块已被裁减。用不了 iptables,就先找点简单的办法连接外网,比如 SSH 转发。智器的 Ubuntu 中预装了 OpenSSH 服务端,使用 sudo /etc/init.d/ssh start 命令启动即可。在笔记本上使用 ssh user@10.42.43.1 -D 7474 登录智器(不知道密码?那就先用 sudo passwd user 设置一下密码),然后将浏览器的 Socks 代理服务器设置为本地的 7474 端口。实验一下,访问网页正常。
  智器电力相当有限,特别是同时打开 WiFi 和 3G 的时候。要把它当 MiFi 用的,最好加一个移动电源,比如我用的这块 4400mAh 锂电池。最终三个设备加其来的体积比 MiFi 大不少,但比起笔记本电脑还是可以接受的。其续航能力与笔记本相当,当然比不过专业的 MiFi。

使用智器 V5 实现山寨 MiFi

  哪位朋友有空研究着重新编译一下智器的 Linux Kernel,加入 ip_tables 模块?这样我们山寨的 MiFi 就更加完美易用了。从智器粉丝团的相关帖子看应该不难,有个 Mer-SmartQ 可用,需要的可能只是经验和时间。
  附,iptables 可用之后,数据包转发所需要的配置:

  1. sudo su
  2. iptables -F
  3. iptables -P INPUT ACCEPT
  4. iptables -P FORWARD ACCEPT
  5. iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
  6. echo 1 > /proc/sys/net/ipv4/ip_forward

使用 rsync 或 unison 备份或同步支持 ssh 的 web 主机

2010/01/07 | 15:11 | 分类:Web与移动平台 | 标签: | 1,450次阅读

  使用 web 主机而非 VPS 的站长,站点的备份或同步常常是一个问题。很多站点只能使用 ftp 做单向备份,基于较弱的元信息来判定文件是否需要重新下载,缺乏校验、压缩、增量传输等高级特性。有的服务商在 web 控制面板中提供备份功能,或允许上传简单的 cron 脚本,但这些途径通常只适合备份整站或指定目录,而不方便以增量方式传输更新过的内容。Linux 下成熟的镜像同步工具是 rsync,如果你的 web 主机允许 ssh 登录,则可以考虑使用 rsync 或其它类似工具。
  网上有很多说法认为 web 主机不允许跑 daemon、不允许开自定义端口,故而无法使用 rsync。其实不然,rsync 可以仅通过 ssh 连接而不需要开放额外的端口,服务端也可以借助 sshd 来启动而不需要作为 daemon 运行。我们需要的只是 ssh 登录以及运行自己上传的程序的权限。我使用的 Godaddy Economy Plan Hosting (Linux) 即属于这种情况。
  Godaddy 的 web 主机不提供 rsync 程序,我们可以从本地上传一个。需要注意上传的 rsync 二进制文件应该与你的 web 主机的平台一致,连接 libc、libpopt 等库的版本一致。对于使用 x86 版 CentOS 5.2 的 Godaddy 主机,我也直接在相同版本的 CentOS 中提取了一个 rsync 上传。这时,在客户端使用“-e”参数指定连接方式为 ssh,用“--rsync-path”参数指定服务端 rsync 所在的位置,即可借助 ssh 连接传输数据了。

  1. rsync -vzrtopgl --progress --delete -e ssh --rsync-path=/[remote_home_dir]/bin/rsync [username]@[hostname]:/[remote_home_dir]/html/ /[local_home_dir]/

  如果客户端为 Windows,则可以使用 Win32 下移植的 cwRsync,这个工具基于 cygwin 库但不需要安装整个 cygwin 环境,它同时包含了 Win32 版的 OpenSSH 客户端。上述命令无须修改即可在 Windows 下运行。
  rsync 解决了文件的单向的备份或镜像功能,但如果需要双向同步,更适合的工具是 unison。unison 使用 OCaml 语言开发,基于 rsync 算法对两端文件进行比较,将它们更新到一致的状态(最新的、不冲突的版本)。unison 可借助 socket、ssh 等连接方式,并支持多种操作系统。与 rsync 类似,我们需要向 web 主机上传一个 unison 二进制文件。官方只提供了最新版的源代码,需要自行下载到本地编译(事先安装 OCaml 编译器及 etags 工具)。服务端部署之后,客户端配置文件([config_name].prf)为:

  1. root = /[local_home_dir]/html/
  2. root = ssh://[username]@[hostname]//[remote_home_dir]/html/
  3. servercmd = /[remote_home_dir]/bin/unison

  在客户端执行 ./unison [config_name] 即可完成双向同步。注意 unison 要求服务端和客户端的主次版本号一致。
  如果客户端为 Windows,同样可以使用来自 cwRsync 的 ssh 命令。如果嫌这个 ssh 外加 cygwin 库的体积太大(~5M),另一种替代的方案是使用 Putty 提供的 Plink 工具。这是一个小巧的 Win32 ssh 客户端(276K),由于运行参数与 OpenSSH 不同,因此需要写一个批处理文件(ssh.bat)来封装:

  1. @Plink.exe [hostname] -l [username] -pw [password] "/[remote_home_dir]/bin/unison -server"

  并在 unicon 配置文件中指定 ssh 命令:

  1. sshcmd = ssh.bat

  此时运行 unicon,即可使用 Plink 进行 ssh 连接。
  无论使用 OpenSSH 的 ssh 还是 Putty 的 Plink,都可以借助公钥认证方式避免密码的输入。这样有利于定时备份和同步的自动化执行。具体方法不再赘述。

基于手机遥控的远程打印

2009/11/19 | 23:23 | 分类:Web与移动平台 | 标签: | 1,029次阅读

  场景:假设实验室的打印机距离你的工位比较远,现需要打印一些文章,但不知道打印机里有没有放纸、是不是合适的纸(A4/B5;新纸/旧纸反面)。贸然打印有可能造成浪费,而如果把合适的纸拿过去放进打印机再回来发送打印命令则需要跑两个来回,中途别人还有可能发送打印命令把你的纸给用了。懒人有懒道,要是能在打印机旁遥控电脑就好了!想想用什么做遥控器呢?身边最好找的远程通信器件应该是手机了。于是,我们来实现基于手机遥控的远程打印(以 Windows 环境为例)——
  我的手机可以使用语音、短信、红外、蓝牙、GPRS 等通信手段,软件支持 Email、MSN、Gtalk、Fetion 等应用。考虑到成本,排除语音和短信;考虑到距离,排除红外和蓝牙;Email 没有实时性,也排除;剩下三个 IM,其中基于 Jabber 的 Gtalk 由于其简单性和开放性,成为了我的首选。
  接着考虑如何操作应用程序进行打印。对于 Windows GUI 的自动化操作,AutoHotkey (AHK) 大神当然是不二之选。
  现在可以动手了。首先建立一个 Jabber Message Handler 来接收手机 Gtalk 客户端发来的消息。如果收到事先定义好的某个消息,则通知特定的应用程序执行打印操作。这种简易的 Handler 用 Python + xmpppy 实现即可,代码如下(gthandler.py):

  1. import os
  2. import xmpp
  3.  
  4. def msgHndl(clnt, mess):
  5.     text = mess.getBody()
  6.     user = mess.getFrom()
  7.     if user.getNode() == 'xxx' and user.getDomain() == 'linjian.org' and text.startswith('p'):
  8.         os.system('doprint.ahk')
  9.  
  10. if __name__ == '__main__':
  11.     login = 'yyy'
  12.     pwd = 'zzz'
  13.     clnt = xmpp.Client('linjian.org')
  14.     clnt.connect(server = ('talk.google.com', 5223))
  15.     clnt.auth(login, pwd, 'botty')
  16.     clnt.RegisterHandler('message', msgHndl)
  17.     clnt.sendInitPresence()
  18.     while clnt.Process(1):
  19.         pass

  这段代码很好理解。Handler 端使用“yyy@linjian.org”账号登录 Gtalk,如果收到来自手机端联系人“xxx@linjian.org”发来的“p”消息,则执行 doprint.ahk 脚本。该 AHK 脚本负责向应用程序发出打印命令(这里是以 Microsoft Word 为例的,向 Word 发送 Ctrl-P 并回车,即可使用默认打印机来打印文档),内容如下:

  1. IfWinExist MyDoc.doc
  2. {
  3.     WinActivate
  4.     Send ^{p}{Enter}
  5. }

  好了,现在打开 MyDoc.doc,再运行 gthandler.py,然后在手机上登录 Gtalk,从“xxx@linjian.org”向“yyy@linjian.org”发送一个“p”,看看有没有效果?当然,实验时可以先用 Ctrl-G 之类不浪费纸的命令做测试。
  这就是程序员,本来可以通过内线电话或免费的 VPMN、利用社会工程学手段解决的问题,也要设法把它程序化了。这到底是折腾呢,还是不折腾呢?但至少有一点是肯定的:脚本的优点是一劳永逸。

Create your private bit.ly and twitpic

2009/10/15 | 23:45 | 分类:Web与移动平台 | 标签: | 762次阅读

As you know that some web 2.0 services, such as twitter web clients, URL shorteners, and picture hosting services cannot be accessed steadily in Mainland China. Perhaps you know how to break through the “wall”, but as information promulgators, we would better make our resources accessible for masses without special network skills or the lazy ones. A few months ago, I wrote Imagoxy, a picasa proxy for my blog readers. And these days a private URL shortener and a private picture hosting service are built for my twitter followers.

I chose a free PHP hosting service with advertisements to host these web applications. For one thing, I do not want my paid host be punished for being related to them accidentally, while free spaces are easily replaceable. For another, advertisements are only appended to htmls, and yet URL forwarding and picture files are not affected.

There are lots of free URL shortener scripts based on PHP and MySQL. I prefer TightURL, which is relatively simpler than the others, since web pages with rich features such as AJAX may go wrong for appended advertisements. Apache's RewriteEngine should be enabled to make the short URL more graceful. And you can also modify “tighturl.*.tmpl” to make it more friendly to users and external caller scripts. Like most of public URL shorteners, TightURL can be dragged into browser's bookmarks for convenience.

I had not found a free and simple enough PHP application to host pictures for twitter. So I wrote a tiny script for myself. It saves uploaded picture and send a tweet via twitter API (with shortened URL). I employed PHP Twitter to call twitter API for its simplicity. The script is accessible from my cellphone, so I can upload the taken photos instantly. I will release the script after more security measure taken.

More services can be deployed on free / advertisement web hosting sites. However, these sites are also usually banned by the “wall”. You should backup your data periodically and be ready to migrate at all times. The only unaltered entrance for your twitter followers is your domain name – unless you have too much influence infuriating somebody.

At last, my private services' domain name is “Jian.me”. It originates from my given name. It's interesting that “Jian” can be thought as “简” or “减” for URL shortener using.

页面存档: 上页 1 2 3 4 5 6 下页