Archive

Archive for the ‘Tools & Tips’ Category

在Android上走出天朝局域网

March 8th, 2011 西坪 3 comments

天朝的防火长城越来越牛逼了,我常用的一些网站如 Gmail, Goolgle Search,Google Docs 都很难访问。买了Android手机以后,一直在考虑如何访问这些网站。经过一番苦战,终于搞定,把相关经验写下来分享。

文章比较长,比较有价值的内容包括:

  • 第二部分介绍Opera mini,除了能实现访问抽疯的国外网站外,还能提高访问速度。
  • 第三部分主要介绍SSH Tunnel,能解决DNS污染的问题,并可实现浏览器以及其他非浏览器应用的翻墙访问。

我的手机是Huawei C8500, firmware:2.1-update1, kernel: 2.6.29-perf。

第一阶段:不给力的http_proxy和VPN

最早,我尝试用Android自身的HTTP代理方式。网上有不少帖子读在讨论给Android的一个系统设置表增加http_proxy记录来实现http代理,主要方式包括通过手动使用sqlite3修改data/data/com.google.android.providers.settings/databases/settings.db表,增加http_proxy代理记录(详细参考此文:Tips: Howto Connect Android Emulator behind proxy),如下所示:

./adb shell sqlite3 /DATA/DATA/com.google.android.providers.settings/DATABASES/settings.db
 “\”INSERT INTO system VALUES(99,’http_proxy’,[host_or_IP]:[port]);\”"

但经过验证,至少在我的机器上是不行的。

然后又照着这篇文章([Android] How to set proxy for android browser),写了一个插件直接设置浏览器代理。这个方法试了试,偶尔能行,偶尔不能行。

后来再尝试VPN,试验了几个VPN供应商提供的试用账户,都没有成功。

第二阶段:翻墙麻利、访问提速的Opera mini

Opera mini 是老资历的翻墙工具了,但自从前段时间出了Opera中国版的事情以后,官方版本不支持翻墙了,必须自己破解,并搭建一个IP位于国外的中转服务器。中转服务器其实很简单,只是接收请求,然后用CURL去Opera官方服务器去获取实际内容,再将内容发回。虽然弄一个使用自己的中转服务器的破解版Opera mini比较麻烦,但是搭建起来以后觉得速度相当快。尤其是访问国外几个大站如Google, facebook 和 twitter的移动版,那是相当流畅,远比 Andriod 自带的浏览器流畅。从服务器端Squid代理的日志来看,使用Opera mini访问同一个网页,貌似最终只有一个HTTP请求,图片和JS,CSS等都不用再单独请求了。从日志看,请求数明显减少,网页体积也缩小很多,流量也省不少。

详细参考:

搭建php版的中转服务器十分简单,就一个PHP文件,内容如下(文件内容参考自opera mini 翻墙大法提供的文件):

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
       //......
} else {
        $curlInterface = curl_init();
        $headers[] = 'Connection: Keep-Alive';
        $headers[] = 'content-type: application/xml';
        $headers[] = 'User-Agent: Java0';
        curl_setopt_array($curlInterface, array(
                CURLOPT_URL => 'http://server4.operamini.com',
                CURLOPT_HTTPHEADER => $headers,
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => @file_get_contents('php://input'))
    );
        $result = curl_exec($curlInterface);
        curl_close($curlInterface);
        header('Content-Type: application/octet-stream');
        header('Cache-Control: priavte, no-cache');
        echo $result;
}

顺带提一句,在你翻墙以后(前提是使用第三部分介绍的方式或者其他方式实现443/80端口翻墙以后),你可以使用最新的Opera Mobile 10翻墙。Opera Mobile 10支持手动设置代理,可直接使用国外IP的代理服务器。 Opera Mobile 10 有Android的原生版本,并且不经过Opera的中间服务器代理,你不需要担心安全问题。不过相比较经过中间服务器的Opera mini版本而言,速度慢了不少,且体积庞大,安装完成后有二十几兆大。

有关Opera Mobile 10的代理设置参考这里

第三阶段:使用SSH Tunnel为所有80/443端口的访问翻墙

但还有一个问题,就是GMS里的一部分应用被墙了,如Gmail,这必须使用能全局代理的代理软件才行。在桌面系统上,有很多工具实现全局代理。其中让系统以socks5代理协议通过SSH隧道就是一种常用方式。SSH隧道在本地服务器与SSH服务器之间建立起一个加密信道,本地系统数据经过这条隧道传递给目标服务器。 这种代理方式所有的内容都经过了加密,并会对流量进行适度压缩。其需要的资源也比较少,只要拥有一个ssh账户就可以实现。这篇文章 (A short guide to SSH port forwarding) 对通过SSH做端口转发做了详细介绍。

不过在Android系统上,因为系统本身不支持代理设置,尤其像Huawei C8500这样的手机,在Wifi/3G网络的设置中都不能设置代理,必须要有一种方式将本地数据包转发到SSH隧道的本地端口。因为Android是基于Linux的,通常我们可以用iptables来实现数据包的转发。有关iptables的内容,可以参阅一下这篇中文指南,了解一些基本知识。

除此以外,使用端口转发的方式,还需要在服务器端建立一个http代理服务器监听SSH隧道的另一端,接收请求并将请求转给目标服务器,并将处理结果再通过隧道传回给手机。常用的http代理服务器有 squid, nigix 等。

一开始我找到的是TransparentProxy, 但这个软件需要手机除了需要支持 iptables外, 内核还需支持 iptables 的 REDIRECT 表。我的手机不支持 redirect,只好作罢。

然后试了试 SSHTunnel,结果没报告错误,但就是访问不了。 因为这个项目是开源的,翻开代码一看,跟TransparentProxy一样,需要 iptables 的 redirect 支持。

尝试了用 connectBot 做动态代理,然后再写了个软件将所有端口的内容转发到这个connectBot。转发通过 iptables 的 DNAT 来实现。不过这样操作起来还是比较麻烦,要开启这两个软件,而且 connectbot 耗电太厉害了,开了一段时间以后,手机发热。

最后,签出 SSHTunnel 的代码,修改成在内核不支持 redirect 的手机上,自动使用 DNAT 实现转发。问题终于搞定。

完成后,向该项目提交了这个patch,该项目已经接受此patch,现在SSHTunnel支持更多的与华为C8500的固件类似的手机了。

SSH Tunnel 项目主页: 详细使用介绍请去该项目的帮助页面

My Vimrc File

December 24th, 2009 西坪 No comments

Apply the recommend vimrc file:

cp /usr/share/vim/vimcurrent/vimrc_example.vim  /usr/share/vim/vimrc

~/.vimrc: Keep it simple!

" 设置文件编码检测类型及支持格式
 set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
 
 "显示行号
 set nu!
 
 " 中文帮助
 if version > 603
 set helplang=cn
 endi
 
 " 设置代码自动缩进
 :set cindent
 
 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 " cscope setting (from http://easwy.com/blog/archives/advanced-vim-skills-cscope/)
 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 if has("cscope")
   set csprg=/usr/bin/cscope
   set csto=1
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
       cs add cscope.out
   endif
   set csverb
 endif
 
 nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>
 nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>
 nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>
 nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>
 nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>
 nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
 nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
 nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>
Categories: Tools & Tips Tags:

My Ubuntu Installation log

December 18th, 2009 西坪 No comments

业余使用Ubuntu好些年了,装了删删了装的,为了以后方便,把这次的新版本的安装配置过程都记录下来。

第一步,从官方下载 Ubuntu-desktop。

第二步,在VMWare上安装,并安装VMWare-tools。 在ubuntu-desktop-9.10上,不像以前需要手工安装一些包,安装linux头文件,这次一路回车就搞定了。

到这里系统就算安装完毕,发现已经用去了8G硬盘的 37%。

(update 2009-12-28): 事后发现根本没有装好,再次尝试安装,基本上各个模块编译时都会出现error日志,并报告模块没有成功编译。而原来在 ubuntu 8.10下是工作正常的。 为什么升级到 ubuntu 9.10就出错了呢?一开始在网络上搜索,看过 VMware workstation fails after 9.0.4 upgrade to 9.10, 根据该帖建议去打补丁, 但是都没有解决。

后来,去Vmware官方找了下是否有更新,结果发现 VMware Workstationfor Windows 出了新的6.5.3版本, 在 Release Notes 中发现 Ubuntu 9.04 is fully supported. 猜测也许是需要新版本。 尝试下载新版本安装后,重新安装和配置 vmware-tools, 一切顺利!

第三步,安装我需要的各种软件。

0. 设置source.list,我使用 mirrors.163.com.
设置网卡:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.126
netmask 255.255.255.0
gateway 192.168.1.1

#auto eth0
#iface eth0 inet dhcp

1. 安装 vim/ctags/cscope, 设置颜色,缩进等。 The details are noted at Here.

  • install: sudo apt-get install vim
  • Install Chinese vim help: download from http://vimcdoc.sourceforge.net/, tar and enter the directory, execute
  • sudo ./vimcdoc.sh -i
  • sudo apt-get install exuberant-ctags cscope

2. 安装 JDK/C++工具:

sudo apt-get install sun-java6-jdk sun-java6-source
sudo apt-get install build-essential

3. 安装manuals:

开发用的手册页:

sudo apt-get install manpages-dev

安装 Posix 相关的手册页。pthread 库相关的手册页就在这里面,而不在 Dev manual pages 中。

sudo apt-get install manpages-posix manpages-posix-dev

安装 STL library man pages:

sudo apt-get install libstdc++6-4.2-doc

4. 安装版本工具 Subversion 和 Git:

sudo apt-get install subversion subversion-tools git-doc git-core

5. Configure SSH server:

sudo apt-get install openssh-server

.
6. Install LAMP server by tasksel, and configure;
7. 中文支持。我安装系统时用的是英文,因此 LANG=’en_US.UTF-8′。中文输入遵循下面三步骤即可:

  1. Install Simple Chinese Language support.
  2. install: sudo apt-get install scim scim-pinyin
  3. System->Administration->Language Support->Input Method, select scim.
    1. Reboot system, 此时就可以使用智能拼音了。

Resource: SCIM Setup, SCIM Usage

中文问题的解决没有这么简单,还存在下面的问题:

  • gedit不能打开中文文件: 这个文件主要是gedit猜测编码的机制造成的,略加修改就可以。具体参考这里
  • vim不能打开中文文件:Here
  • pdf不能阅读某些中文文件: 问题的症状是能够阅读某些中文pdf,但是还有一些pdf则表现为乱码。问题的原因不太清楚,但是Google后安装了一个新包 poppler-data,就解决了。(sudo apt-get install poppler-data)

8. 安装 CHM 格式文档查看工具 kchmviewer.

sudo apt-get install kchmviewer

但是 kchmviewer 在查看 boost中文文档页时出现了编码错误。当然这不一定就是kchmviewer有问题,也可能是boost中文文档本身的兼容性等。于是另外安装了chmsee, chmsee的另外一个优点就是安装时体积很小,下载过程中才下了几百K。当然也可能是因为我提前把其他需要的库都已经装上了…
9. ruby gems:

sudo apt-get install rubygems1.9.1

10. 声音问题的解决:这里

Categories: Tools & Tips Tags:

Vim 快捷键

May 10th, 2009 西坪 No comments

All these stuff can be found in the manual. Put them here together to give me random review chance.

Some best resource about vim usage:
General tips
  • Ctrl-N: auto completion
  • Position the cursor on the name of the function in your file and type [I: Vim will show a list of all matches for the function name in included files.
Search & Replace

这里有篇介绍文章:VIM下,在文件及目录中查找字符串的方法 。例如: vimgrep /登录/g **/*.html

查找的结果会显示在 QuickList 列表中。对于Quicklist 的操纵,参考下面的文章 打造自己的VIM: QuickFix 編譯視窗 。 这篇文章还介绍了打造快捷键,因为我的Vim使用的版本比较多,而且直接切换到Qucik List然后使用行移动也很方便的,所以就不配置这些快捷键。 更详细的参考见官方手册 Quickfix 或者 :help quickfix

替换 

可以在 vim 中查看帮助 :help argdo

示例:

:args **/*.htm

:argdo %s/旧/新/ge  |  update

Move around quickly
  • Use % to jump from an open brace to its matching closing brace. Or from a "#if" to the matching "#endif". Actually, % can jump to many different matching items. It is very useful to check if () and {} constructs are balanced properly.
  • Use [{ to jump back to the "{" at the start of the current code block.
  • Use gd to jump from the use of a variable to its local declaration.
  • * command to search for other places
  • CTRL-], CTRL-t with ctags/cscope

整页翻页 ctrl-f ctrl-b
f就是forword b就是backward

翻半页
ctrl-d ctlr-u
d=down u=up

滚一行
ctrl-e ctrl-y

zz 让光标所杂的行居屏幕中央
zt 让光标所杂的行居屏幕最上一行 t=top
zb 让光标所杂的行居屏幕最下一行 b=bottom

Highlight and increment search set:
set hls is

Block Operation (Delete a column):
  • start mode with Ctrl-v
  • specify a motion, e.g. G (to the end of the file), or use up / down keys
  • for the selected block specify an action, e.g. ‘d’ for delete
Split Window

set splitright “put the new split window in the right)

(some content cited from The Vim/Cscope tutorial)

  • move between windows via ‘CTRL-W w’ (or CTRL-W arrow key, or CTRL-W h/j/k/l for left/up/down/right),
  • close a window via ‘CTRL-W c’ (or good old ‘:q’),
  • make the current window the only one via ‘CTRL-W o’,
  • split a window into two via ‘CTRL-W s’ (or ‘CTRL-W v’ for a vertical split),
  • open a file in a new window via ‘:spl[it] filename’,
  • close a window ‘CTRL-W c’
cscope & ctags

The most completed tutorial I got from Internet:
The Vim/Cscope tutorial

We can use ctags to navigate the java code by placing the cursor on a class or method and hitting CTRL-]. To jump back and forwards just use CTRL-O and CTRL-I. Type :jumps command to view the jump lists.

Cscope also can works well with other languages likes Java and C++. All we need to do is explicit specify the special file lists in the cscope.files or the command line.

Categories: Tools & Tips Tags:

Linux Command & Shell Tips (1)

March 22nd, 2009 西坪 No comments

此文用于记录使用 Linux 过程中的与 shell 和 commands 有关的有意思的小知识。

1. The problem of ftp url:
在写一个自动备份的脚本时发现,如果 username 中有 @ 符号时,使用 curl ftp://username:password@host/file 是不行的。这可能是 ftp url 地址格式的一个瑕疵。具体参考 Google Group上的一个讨论 (对于该讨论的正确性本人没有特别考证, 根据作者的观点,在整个URL中出现 “:”、”/”、”@” 都是不可行的。如果您发现此论断有问题请留言)。 还好 curl 可以使用 -n 指定使用 ~/.netrc 中的登录标识。

2. 直接使用 sed 命令批量替换文件中的内容

sed -i 's/\/var\/svn/d:\/data\/svn/g' `grep /var/svn -rl ./*/conf/trac.ini`

sed 命令还有很多用法,可惜平时用得不多,经常忘记了要查资料。

3. 使用 find 命令批量操作 Trac 项目同步SVN

find -maxdepth 1 -mindepth 1  -type d -print -exec trac-admin {} resync \;

对仅仅处理当前目录下的子目录的限定方法比较笨,也许有更好的方法。

find命令是最难用的,再提供一个统计代码行总数的例子。

$ find stat5.1projects/ ! -path '*.common/*' -a ! -path '*bin*' -a ! -path '*st
at.statuser*' -a \( -name *.java -or -name *.sql -or -name *.xml \) | xargs wc
-l
</pre lang="bash">
然后直接通过Shell计算出平均每天的代码行:
<pre lang="bash">
echo $((8888/22))

4. tar error message.

Removing leading `/’ from member names

It’s really annoying when it present in a crontab script. That is why (refer from here):

what do you want suggestions about? tar archives don’t contain absolute paths, only relative ones. this is correct behaviour, as the alternative is really nasty and illogical. this is the same way that other compression programs like winzip work. when you untar somethign you provide an destination directory, which implicity is ‘/’ in your case… but it should be explicit for logical operation

The resolution is using relative path. e.g.

cd /var/www
tar -cf /home/user/backup/wp-backup.tar web
# NOT below
# cd /home/user/backup
# tar -cf wp-backup.tar /var/www/web

5. svn: Delete unversioned files

svn status --no-ignore | grep '^[I?]' | sed 's/^[I?]//' | xargs rm -rf

Here are more frequent ask questions on subversion.

6. 防火墙后面的git通过代理访问:
设置GIT_PROXY_COMMAND系统变量即可。具体参考:Using Git with a SOCKS proxy

7. Ubuntu添加sudo用户:
Edit file /etc/sudoers, add line:

bob  ALL=(ALL) ALL

参考:http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch09_:_Linux_Users_and_Sudo

8. Unix timestamp 与 readable date 之间的转换:
shell:

date -d @1232144092 #timestamp to date
datedate=’1970-01-01 1000000000 sec GMT’
date -d today +%s #date to stamp

Perl:

date -d today +%s | perl -e 'print localtime(<>)."\n"' # to readable time

gawk:

date -d today +%s | gawk 'END{print strftime("%c",$1)}' # to readable time

9. AWK调用shell命令做一些计算

cat $f | awk '! /^NAI/ {cmd="date +%k%M -d @"$3; cmd | getline d; close(cmd); if (d>2357)print d}'

多行时可循环读取:

cmd_= ("cat "naiIdMappingFile);
while (cmd_ | getline d ) {
        split(d, naiIds," ");
        naiIdMaps[naiIds[1]" "]=naiIds[2];
}
close(cmd_)

其中要提醒一下的是,一定要记得 close() 的调用,不然很快会耗光文件描述符。

10. Perl 跟bash脚本配合,逐行对文件执行某些操作:

cat $f | perl -ane '$F[1] =~ s/\D+(\d+)\D+(\d+)\D+(\d+)/$1 $2/g; @cols = split(" ",$F[1]); $F[2] =~ s/\D+(\d+)\D+(\d+)\D+(\d+)/$3/g;print " @F ". scalar($F[2]-$cols[1]) ." \n";';
Categories: Tools & Tips Tags: ,