technology

申请一个 bing translator api 玩玩

想补充下我的 DrawSthHelper 的翻译功能,google translate 的 api 收费了,于是转而投奔 bing translate ,然后发现,真 TMD 麻烦。很少用 MS 的 online 服务,好像是 bing app ID 的模式 deprecated 了,以后要用 Azure Marketplace 的了。然后就有了下面这个麻烦至极的过程。。。

api doc 在这里:http://msdn.microsoft.com/en-us/library/ff512421.aspx

其中,因为 bing app id 已经不再维护了(但还是可用的),还要注册一个 Azure Marketplace ,还要获取一个 access token 。

获取 access token 在这里:http://msdn.microsoft.com/en-us/library/hh454950.aspx

我简单罗列一下步骤,也就是翻译一下啦 @@

1,来这里免费注册个账号 https://datamarket.azure.com/developer/applications/

2,然后通过这个链接:http://go.microsoft.com/?linkid=9782667 ,来订阅(subscribe)Microsoft Translate API 的数据,每个月可以免费翻译 200w 个字符,我相信。。。一般人都够用了。

3,然后注册一个自己的应用,以下需要填的信息:

客户端ID(client_id,等会需要用),名称(随意),客户端机密(这翻译烂的。。。client secret ,等会也需要用),重定向 URL (其实是随意的,就是如果你的应用对外使用了,会显示你的名称,而你的名称的链接就是填的这个)

4,然后,POST 数据去获取 access token

POST 的参数如下:

client_id , 就是刚才那个客户端ID

client_secret ,URL encode 一下,例如,”=” 是 %3D

scope,定值,http://api.microsofttranslator.com/

grant_type,也是定值,client_credentials

然后就 POST 吧

curl -d ‘grant_type=client_credentials&client_id={your client id}&client_secret={your client secret after url encode}&scope=http://api.microsofttranslator.com’ https://datamarket.accesscontrol.windows.net/v2/OAuth2-13 | python -m json.tool

返回的是一个 json ,我用 python 去解析了一下,我们只需要其中的 access token 。

5,翻译一个词看看,这回是 GET ,GET 的参数如下:

text , 需要翻译的文本啊,字符串啊

to ,这个不用解释了吧。。。

from ,这个也不用

不过,需要注意的一点。。。就是要加一个 Authorization 的header ,值是 bearer + ” ” + 刚才的 access token

翻译开始:

curl -H “Authorization: bearer {刚才那一大串access token}”  ”http://api.microsofttranslator.com/V2/Http.svc/Translate?text=hello&from=en&to=zh-CH”

bing 返回:

<string xmlns=”http://schemas.microsoft.com/2003/10/Serialization/”>你好</string>

(这个时代还用 XML 。。。)

 

附 language codes :http://msdn.microsoft.com/en-us/library/hh456380.aspx

npm 安装hashlib 失败

呃。。。这篇东西趟在我的草稿箱一段时间了。。。

当时在摆弄 hubot 。我本机 MacOSX 要装几个 lib

==================

有个script 需要hashlib 的,但我发现npm 安装不上hashlib ,报错

[1/6] cc: libhash/md4c.c -> build/Release/libhash/md4c_1.o
[2/6] cc: libhash/md5c.c -> build/Release/libhash/md5c_1.o
[3/6] cc: libhash/sha0c.c -> build/Release/libhash/sha0c_1.o
[4/6] cxx: hashlib.cc -> build/Release/hashlib_2.o
../hashlib.cc:14:16: error: ev.h: No such file or directory
../hashlib.cc:15:17: error: eio.h: No such file or directory
../hashlib.cc:311: error: ‘eio_req’ was not declared in this scope
../hashlib.cc:311: error: ‘req’ was not declared in this scope
../hashlib.cc:312: error: expected ‘,’ or ‘;’ before ‘{’ token
Waf: Leaving directory `/usr/local/lib/node_modules/npm/node_modules/hashlib/build’
Build failed:  -> task failed (err #1):
{task: cxx hashlib.cc -> hashlib_2.o}

解决办法也简单,根据 https://github.com/brainfucker/hashlib/issues/25

npm install https://github.com/ovaillancourt/hashlib/tarball/compat_fix_node_0_6_X 即可

DrawSth

这几天都在玩这个 app,苦于词汇量真的太少。。。真的看出来对方画的是什么,也不懂拼。

随便写了个小程序,原理很简单,将所有的字母全排列一次,然后 spell check 。全排列原来已经有 lib 实现了,spell check 也有 pyenchant ,真是没有技术含量啊。。。

日后考虑加入 Google Translate ,不知道会不会因为太频繁访问被 Google ban 掉。

https://github.com/hellosa/draw-something-helper

当然,一般不用这东西,要不就破坏了游戏性了。

—————

update 2012-03-20:

今天想加入 google translate 的功能。。。发现貌似收费了。。。

zhihubot 搭建

zhihubot 是一个基于GitHub 开源的Hubot [1],长期寄居于Campfire 某个room 的机器人。那说回来,Hubot 又是啥呢?Hubot 是GitHub 内部的机器人,它能做很多事情,例如自动化部署,时不时贴一下有趣的图片,为他们播放音乐,翻译,你能想到的,都可以,只需要在Campfire 里的一个有Hubot 的chat room 里,和Hubot 说一句话就可以了,剩下就交给Hubot 吧。原生支持shell 和campfire ,当然,还有更多的接口,如IRC, gtalk。它之前的内部版本是用什么写的就不知道了,现在开源的版本是用node.js 的CoffeeScript 写的,想到自己的Hubot 需要什么功能,自行动手写吧。

还是说如何搭建吧。Hubot 有两个项目:主项目 https://github.com/github/hubot 和收集回来的脚本 https://github.com/github/hubot-scripts,下载源码包吧,以后它更新了你又跟进不了,git clone 一个readonly 的吧,差不多效果。所以,fork 吧,以后说不定要加入自己的很多scripts呢。fork 出来以后,在自己本地clone 一份吧,然后就可以开始了。

我一般在自己的Mac 下用shell 测试,在线上的Linux 连接Campfire 。因为包管理工具不同而略有不同而已。

Mac 下,我用的是homebrew

brew install node.js ,安装node.js

curl http://npmjs.org/install.sh | sh ,安装npm — node package manager

进入hubot 目录,npm install ,然后所有的依赖都帮你安装完成了

进行测试前,要export 一个系统变量,export PATH=”node_modules/.bin:$PATH”

./bin/hubot ,测试,尽情玩吧

Hubot>       ,见到这个命令行提示符,我最初是完成不知道如何操作的。。。万事开头难啊,哈哈

输入 hubot pug me 测试下

Linux 下,这次我用的是Debian

# apt-get install build-essential libssl-dev git-core redis-server libexpat1-dev ,安装相关的包

debian 既然还没有node.js 。。。自己下源码装吧

# wget http://nodejs.org/dist/v0.6.9/node-v0.6.9.tar.gz
# tar xf node-v0.6.9.tar.gz -C /usr/local/src && cd /usr/local/src/node-v0.6.9
# ./configure && make && make install
安装完node.js 后,安装 CoffeeScript ,npm install -g coffee-script

同样,现在也可以用shell 测试了。

现在来尝试一下连接Campfire 。首先去新建一个账号给hubot 使用。然后去邀请hubot 加入campfire 。

在hubot 目录下新建一个文件run ,内容为:

=========================================

#!/bin/bash

##

## Wrapper for Hubot startup

##

HUBOT=”/path/to/your/hubot/”

NAME=”zhihubot”

ADAPTER=”campfire”

HUBOT_CAMPFIRE_TOKEN=”your bot’s api”

HUBOT_CAMPFIRE_ACCOUNT=”subdomain”

HUBOT_CAMPFIRE_ROOMS=”room number 1, room number 2″

OPTS=”–name ${NAME} –adapter ${ADAPTER}”

export HUBOT_CAMPFIRE_TOKEN

export HUBOT_CAMPFIRE_ACCOUNT

export HUBOT_CAMPFIRE_ROOMS
until ${HUBOT} ${OPTS}; do

echo “Hubot crashed with exit code $?. Restarting.” >&2

sleep 5

done

================================================

NAME ,hubot 的名字,每次发命令基本都要带上,robot.hear 的不用

HUBOT_CAMPFIRE_TOKEN ,你申请那个hubot 用的账号的api token,在账号的info 里有

HUBOT_CAMPFIRE_ACCOUNT ,这里的account 有点误导,其实是$subdomain.campfirenow.com 的subdomain ,而不是hubot 的账号

HUBOT_CAMPFIRE_ROOMS ,聊天室的id ,用逗号分隔

填上以上,再赋个权,chmod +x run ,在screen or tmux 下,./run 即可。

OK. Let’s rock !

如果成功了,hubot 会作为你刚才为它申请那个账号的身份,进入聊天室,你会见到它在线的

在聊天室中输入 zhihubot pug me (格式一般为$hubotname $command ,可以 zhihubot help 查看帮助)看看效果??(爱死那狗狗了!)

hubot-scripts 里还有很多有趣的scripts ,可以自行玩玩,但由于写的人很多,很不完善,未必每个scripts 都能顺利跑起来噢

由于其默认读取的是两个目录:hubot/scripts & hubot/src/scripts ,后者是built-in 脚本,那么我就把自己觉得好玩的或者自己写的脚本放hubot/scripts 里。

========================================

Campfire 本身是可以免费的,但免费版实在太弱了,只能同时容纳4 个人聊天,想让你的Hubot 活起来,果断购买吧!!一个月10+ 刀的消费对于一个公司甚至个人来说都不算什么,何况它能极大提高团队的活力、幸福感、积极性、xxxx

Hubot ,很明显来自于GitHub 的Hub ,加上robot 的bot,而zhihubot ,很明显又是利用了hubot 的hu ,直接将zhihu 串上去,很是山寨。所以呢,zhihubot 是有它自己的名字的,它叫Marvin ,它长这个样子的

Reference:

[1] http://hubot.github.com/

HAproxy 代理redis

转眼2012 的1 月要过去了。。。还是不要空着一个月吧,随便写点什么记录一下。

1 月实践了一下haproxy  作为redis 的代理,统一了所有的redis 入口,对于应用来说,其实最好还是有一个全局的队列,这个中间层完全接管了mysql 和nosql 的写入读取。大工程啊,这里还是说一下简单的。

直接上配置吧:

global
log 127.0.0.1  local3 notice
ulimit-n 40960
maxconn 10240
user haproxy
group haproxy
nbproc  4
daemon
quiet
defaults
log     global
mode    http
option tcplog
balance roundrobin
stats uri /redis-proxy
stats auth hello:123
contimeout 5000
clitimeout 30000
srvtimeout 30000
listen monitor
mode http
bind 0.0.0.0:2000
listen redis
bind 0.0.0.0:6379
mode   tcp
option tcpka
server redis1 192.168.100.1:6379 check inter 5000 fall 3
server redis2 192.168.100.2:6379 check inter 5000 fall 3 backup

我这里两个redis ,一个是active 状态对外服务,另一个是热备。

用redis-benchmark 来随便打了一下压力,和redis-cli 直连性能上差不多。

timeout 一定要配置,之前就是忘了配置,其中一个down 了以后很久都不切。

velocity 2011

总的来说,有点失望,含金量不高。

老外的好几个都是在卖广告。。。门票的大头就是他们吧。。。还有一些是说自己的产品如何好如何好,连技术原理都不说,就说你使用了我们的产品呢,能怎么样怎么样的。

简单理一下我的一些笔记吧。略去广告。

======================

第一天,含金量最高,而且都在下午。

Steve Souders 的《高性能移动互联网》。这个Steve 大有来历,简单用熟悉一点的东西去介绍,就是 Yslow 和 Firebug 的开创者。可惜。。。我迟到了。。。前面那部分没听到。后面大概就是说一些与移动互联网相关的参数,Yahoo! 的14 条铁律在移动互联网已经不再完全适用,更多的是依靠app 自身,例如cache 。

章文嵩的《低功耗服务器定制与绿色计算》。上一年SACC 上分享淘宝CDN 的时候就已经提及的话题,今年就展开来说一下。大概就是,在最外层的squid cache 层,cpu 完全不是瓶颈,磁盘IO 和网络IO 才是,在这些地方用低功耗服务器可以节省很大一笔钱呢。然后我惊奇地发现,他们在定制低功耗服务器中测试过的三款CPU ,其中两个intel E5620 和 Atom D525 都是我们正在用或者将会试用的cpu。到现在,他们已经优化到cpu 快成为瓶颈了,真可怕。除了低功耗服务器,淘宝CDN 他就简单带过了,无非就是上一年的内容。混合使用各类不同的磁盘再次提到,无非就是SAS , SATA , SSD 三种不同成本的硬盘用来存放大中小文件,及冷热数据。然后我发现。。。他每次的ppt 介绍的都是这些内容啊。。。是不是每次分享都是一两年前的东西。。。现在内部用的已经是很成熟的一套了??

Percona 的季海东(http://www.haidongji.com/ )的《Innodb/Xtradb 性能优化与诊断》。他喜欢配合着DEMO 来说ppt ,这个其实很好。。。但是当时间不足的时候。。。他连Xtradb 都完全没有提及就已经没有时间了。其中演示了一些MySQL 的操作,技巧等。。。重点的调优说得不多,尤其是,innodb 的最重要参数 buffer_pool_size ,竟然武断地说,一般设置为物理内存的80% 。。。没有考虑到NUMA 架构的机器??

米聊陈臻的《开源工具选型》。这个分享其实不错。米聊同样是一年左右的创业时间,但他们在开源软件的经验上,对比我们还是多了不少。由于他们也是创业公司,注重点还是在产品上面,所以一般都是用开源软件。他们的原则:用大公司正在用的工具,用大公司不用但自己能在代码层级搞掂的工具。他们对于一些软件的选型,其实也没有太多的道理,例如java 容器的选型,resin 和tomcat ,没什么原因,因为初始的那一批人就是用resin 的,而且也没什么毛病,就一直选用resin 了。

奇异李刚的《NoSQL 选型与实践》。也蛮不错的,虽然Sean 一直不推荐去听这个分享@@ 奇异用mongodb 相对于redis 多一点,他们甚至用mongodb 来存放图片。。。这个我实在不能理解。。。不过既然他们用了,而且用得挺欢,那也就参考一下吧。或者是有点数量级了吧,他们对细节的优化很到位,没办法,mongodb 对内存是很贪婪的,他们不优化不行。他们在使用中发现了一些bug ,还主动hack 了。

淘宝的tengine 。其实这个也是不错的。包括两部分,第一部分是朱照远说说淘宝正在如何使用nginx 和tengine 的一些特性吧。第二部分是chaoslawful 的分享,其实就是这个ppt :https://docs.google.com/present/view?id=dddqrph4_23gmctkmcg&pli=1 。tengine 的很多特性还是挺不错的,但是不知道为啥。。。就是不想用。。。唉。。。难道是洁癖作怪??我之前一直徘徊于nginx module 和 nginx_lua 之间挣扎,听完chaoslawful 的分享,不矛盾了,luajit 完胜啊!

第二天,早上第一场据说已经很少人去听了。。。我前一天晚上工作到3 点多,也没能起来。。。也就算了。。。迟到吧

整个早上。。。也是要工作的。。。结果错过了几个分享,只听到半个分享,就是 来自OmniTI 的 Theo Schlossnagle 的运维生涯。分享了一些有趣的故事吧,由于我进场的时候已经说到一半了,就不用同声传译了,直接英语听力吧,所以一些地方可能没有catch 到他的本意。facebook 的infrastructure team 与业务没有关系,专门从事架构类的一些工作,而且还是允许错误发生的,不过不能在同一个错误上犯错两次而已。也说到了所谓的 DevOps ,不过我认为,如果不懂开发的运维,就会沦为操作者,使用着一些别人开发好的工具,严格依照着操作文档去操作而已,在腾讯,叫做一线运维,地位是比较低下的。所以一个合格的运维必须一定懂编程。

百度的《大规模集群控制系统与自动运维》。意义不大,其实就是自己开发的一套运维系统嘛。与sohu 的sagent 差不多,各自都根据自己的业务自己完全开发了一套。其实原理就那些,实现细节虽然可能会麻烦,但也就那些,重点还是有没有人力去做这回事。在我当前无法做到的情况下,我只能用用开源的puppet 了。所以这个分享我听了一半就去听 Yahoo! 的《大型网站性能监控、测量和故障排除》

Yahoo! 的是一个可爱的mm 分享的,主要介绍了yslow 这个command line tool . http://developer.yahoo.com/yslow/commandline/ ,貌似是最近才开源的。反正就是根据Yahoo! 的14 条铁律写出来的一个命令行工具,还可以生成漂亮的web 报表之类的。和 http://www.showslow.com/ 配合就能很好的分析网站的瓶颈了。虽然这个工具对web1.0 网站似乎用途会大一点,但如果能加入 cookie 的话,那也可以适用我们的网站了。据说是支持的。。。空了试用下。

去哪儿分享的《机票实时搜索引擎的优化》。这个是完全偏题的分享!!。。。但内容还ok 。。。根本没有实时搜索引擎相关的什么优化。。。只是说了去哪儿在前端和后端的优化。他们的前端开始的时候使用了 trimpath 这个框架,后来实在有性能问题了,就自己写了一套,我的前端很烂,就不说了。后端的优化说了几个实例。有一个trick , 就是xml 的解析,如果只是需要一个xml 的某些标签的content ,可以不用xml 解析器去把整个xml 解析出来,而是直接用正则把需要的字段抽出来即可,效率相差数量级啊!!还有一个就是memcache 的使用经验,我也曾经犯过这个错,就是分配给memcache 的内存,并不是都可以用于存储的,例如100k data block 会分配一定数目的桶,200k 300k 亦然,当大量小object 占满了100k 的桶子时,memcache 就会根据算法置换一些旧object 出去,也就会出现,分配的512M 内存都没有用完,但就经常miss cache 的情况了。他们说了一点:优化重视细节,但切忌过早过度优化,一开始设计很大,但其实最先出现问题的往往没有考虑过的地方。我也曾经一度陷入这个怪圈,但其实真的没有必要一开始考虑得太多,有时候船到桥头自然直。我现在多数关注的是瓶颈的方面了。

Configure FreeRadius with MySQL on Gentoo

安装支持MySQL 的FreeRadius ,默认是不支持的:

USE=”mysql” emerge freeradius

或者

echo “=net-dialup/freeradius-2.0.5 mysql” >> /etc/portage/package.use

emerge freeradius

radius 的配置都在 /etc/raddb/ 里,我们需要修改的有 radiusd.conf , clients.conf , users , sql.conf , /site-enabled/default,分别对应服务器端,客户端,用户,数据库的配置,验证方式。其实由于这里使用了MySQL 记录用户的密码,所以其实不用修改users 也行。

我尽量在默认配置的基础上修改配置,可以不修改就不修改默认的配置。

radiusd.conf :

这里可以不修改,但最好修改一下,listen 的ipaddr 修改为内网吧。log 方面也可以适当开启一下。

——————–

clients.conf :

客户机的配置,我这里其实是Airport Extreme。

client 192.168.1.10 {
secret = testing
shortname = AP
nastype = other
}
————-
sql.conf:
先准备好MySQL 环境。
mysql> create database radius ;
mysql> grant all on radius.* to radius@localhost identfied by ‘testing’ ;
$ mysql -uradius -ptesting radius < /etc/raddb/sql/mysql/schema.sql

添加一个用户:

INSERT INTO radcheck (UserName, Attribute, Value) VALUES (‘test’, ‘Password’, ’123456′);

修改一下sql.conf 相应的配置:

login=”radius”

passsword=”testing”

radius_db=”radius”

———–

/etc/raddb/sites-enabled/default:

把 authorize 里的sql uncomment 了,这样才能启用sql 验证。

当然,有洁癖的话可以把其它没用的comment 了。

————–

测试下:

进入debug 模式:radiusd -X

测试命令:

语法:Usage: radtest user passwd radius-server[:port] nas-port-number secret

radtest test 123456 192.168.1.1 0 testing

如果是Access-Reject 就是失败了,Access-Accept 就是成功了。

===============

配置到此完毕,算是可用了,但还不完善。不过我在想,用MySQL 还不如用LDAP 吧。。。下回再弄弄。

Reference:

http://en.gentoo-wiki.com/wiki/Chillispot_with_FreeRadius_and_MySQL

http://www.gentoo-wiki.info/HOWTO_WPA_Enterprise_with_MySQL

在PowerEdge R410 下安装Debian6

昨天第一次在一台 R410 上装了Debian 6 ,想不到还出问题了。。。竟然不能直接就用netinst 安装好Debian,居然还是缺驱动。

安装的过程中,它会检测硬件,但是竟然出错了,“load missing firmware file” ,驱动也指出来了,是这个:bnx2/bnx2-mips-09-5.0.0.j3.fw 。原因在于,这个网卡驱动在Debian6 时变成了non-free 的驱动了,Debian installer 也告诉我了,需要把这个firmware 下载到一个removable media ,然后插入即可。问题就在这里开始解决吧。

弄来一个U 盘,格式化为FAT 吧,然后到这里下载两个firmware :

http://194.95.9.96/debian-installer/amd64/temp/lib/firmware/bnx2/

bnx2-mips-09-5.0.0.j3.fw
bnx2-rv2p-09-5.0.0.j3.fw

下载回来,放在U 盘的根目录的 bnx2/ 目录里面。

在出现load missing firmware file ,让你插入removable media 的时候,ctrl + alt + F2 跳转到另一个终端,然后把U 盘挂载上去。我当时是/dev/sdb4 ,mount /dev/sdb4 /mnt 。

这个时候,那两个firmware 应该在

/mnt/bnx2/bnx2-mips-09-5.0.0.j3.fw

/mnt/bnx2/bnx2-rv2p-09-5.0.0.j3.fw

然后ctrl + alt + F1 回到安装的终端,按 YES 吧。

然后就过去了,继续如常安装吧。

我参考自这个页面:http://bbs.chinaunix.net/archiver/tid-2312139.html ,只是记录一下自己的经历,和他的做法稍有不同。

Debian 6 刚发布的时候,在我的老东家,机房的同事就说装Debian 6 有点问题,结果帮我们装了个Debian 5 ,让我们自己升,够坑爹的。我就在想,会不会就是因为这个驱动的问题导致的呢?或者,在FAI 下还真不好解决,下回我也试试FAI 安装Debian6 .

gource is so cool !!

我竟然今天才发现这个cool 极了的东西!!!

http://code.google.com/p/gource/

项目主页在这里,里面有视频。

读取版本管理的日志,然后通过很炫的动画,将版本的变动表现出来,cool 啊!!支持多种版本管理软件,包括我熟悉的svn 和不算太熟悉的git 。

homebrew 下很方便,brew install gource ,安装完成后,在代码的根目录,直接运行 gource 就行了!!

然后,就欣赏很炫的动画吧!!

HOWTO Configure L2TP VPN server on Linode Gentoo VPS

Linode announced a new facility in Tokyo, Japan at September 20, 2011. It’s a big good news for us inside the wall. The download speed, and latency, is pretty awesome!!

I migrated my linode(actually it belongs to my boss) from USA to Tokyo with no doubt. And rebuild it from Ubuntu to Gentoo. As a desktop, Ubuntu plays so good so far.But as a server, Ubuntu sucks !!

OK, everything is ready, all we need is a L2TP VPN server.

Here is my L2TP VPN configuration.

First of all, choose a faster mirror server, this is my selection: gentoo.channelx.biz, chosen from mirrorselect (emerge mirrorselect). And add it to /etc/make.conf, replace the original one.

U can run this command if u don’t have an original GENTOO_MIRRORS config in /etc/make.conf.

echo 'GENTOO_MIRRORS="http://gentoo.channelx.biz/" ' >> /etc/make.conf

The software we need: openswan , xl2tpd , ppp. We can install them all with the ‘emerge’ command, but I found there’s something wrong with the openswan (v2.4.15-r2), we must upgrade openswan to version 2.6.31, even though this version is masked.

Solve the masked problem:

echo 'EMERGE_DEFAULT_OPTS="--autounmask=n" '  >> /etc/make.conf
 
echo '=net-misc/openswan-2.6.31 ' >> /etc/portage/package.accept_keywords

OK, install it:

emerge =net-misc/openswan-2.6.31

Let’s do some copy and paste.

Configure the ipsec.

copy the content of /etc/ipsec.d/examples/sysctl.conf to /etc/sysctl.conf ,and make sure the rp_filter options are commented.

# Enables source route verification
#net.ipv4.conf.default.rp_filter = 1
# Enable reverse path
#net.ipv4.conf.all.rp_filter = 1

u can run this command:

cat /etc/ipsec.d/examples/sysctl.conf >> /etc/sysctl.conf

and active the sysctl.conf:

sysctl -p

Edit the /etc/ipsec.conf :

echo 'include /etc/ipsec.d/examples/l2tp-psk.conf' >> /etc/ipsec.conf

when ignore the commented line:

#cat /etc/ipsec.conf | egrep -v "^[[:space:]]*#" | grep -v "^$"
 
conn L2TP-PSK-NAT
	rightsubnet=vhost:%priv
	also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
	authby=secret
	pfs=no
	auto=add
	keyingtries=3
	rekey=no
	ikelifetime=8h
	keylife=1h
	type=transport
	left=YourPublicIP 
	leftprotoport=17/1701
	right=%any
	rightprotoport=17/0
conn passthrough-for-non-l2tp
        type=passthrough
        left=YourPublicIP
        leftnexthop=YourGatewayIP
        right=0.0.0.0
        rightsubnet=0.0.0.0/0
        auto=route

and remember to replace the “YourPublicIP” , “YourGatewayIP” to the correct value.

edit /etc/ipsec.secrets :

YourIPAddress  %any: PSK "sharedsecret"

do the same replacement.

Configure the iptables.

iptables -t nat -A POSTROUTING -j MASQUERADE
 
/etc/init.d/iptables save
 
rc-updat add iptables default

Configure xl2tpd :

# cat /etc/xl2tpd/xl2tpd.conf
 
[global] 
ipsec saref = yes
 
[lns default] 
ip range = 172.16.80.128-172.16.80.254 
local ip = 172.16.80.1
require chap = yes 
refuse pap = yes 
require authentication = yes 
name = xl2tpd
ppp debug = yes 
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

Configure PPP :

# cat /etc/ppp/options.xl2tpd
 
ipcp-accept-local
ipcp-accept-remote
ms-dns  8.8.8.8
ms-dns  8.8.4.4
noccp
auth
crtscts
idle 1800
mtu 1410
mru 1410
nodefaultroute
debug
lock
proxyarp
connect-delay 5000
# cat /etc/ppp/chap-secrets
 
Username        xl2tpd      Password        *

do some replacement here.

start the services:

/etc/init.d/ipsec start
/etc/init.d/xl2tpd start

Have fun .

PS: ipsec verify failed ?

Pluto listening for IKE on udp 500                          	[FAILED]
  Cannot execute command "lsof -i UDP:500": No such file or directory
Pluto listening for NAT-T on udp 4500                       	[FAILED]
  Cannot execute command "lsof -i UDP:4500": No such file or directory

it’s all right, because the command ‘lsof‘ is missing, just ‘emerge lsof

Reference:

http://riobard.com/blog/2010-04-30-l2tp-over-ipsec-ubuntu/
http://apple4.us/2010/05/setting-up-l2tp-vpn-on-debian-ubuntu.html
http://forums.gentoo.org/viewtopic-t-324500-highlight-openswan.html (the ppp configuration part, it’s different between gentoo and ubuntu)

——————————
update: 2011-09-26
If u can connect successfully at the first time, but failed at the second. Here’s the solution.
Add the following lines to your L2TP-PSK-noNAT connection and restart ipsec (/etc/init.d/ipsec restart )

dpddelay=40
dpdtimeout=130
dpdaction=clear

Reference:
http://lists.openswan.org/pipermail/users/2011-January/019945.html