使OpenVPN服务器推送下发chnroutes路由表

IT民工都知道chnroutes,不过这个路由表每过一段时间就要更新,在Windows上面还弄个VBS+批处理 是在是讨厌。
好在OpenVPN除了能推送VPN子网内确定的路由设置外,还能推送客户端子网内以客户端子网为网关的路由。这样我们就可以实现chnroutes路由表的远程下发。然后服务器上弄个定时更新就可以了,这样路由的推送和更新可以实现全自动。

问题的关键在于,一般我们推送路由都是使用vpn_gateway这个变量来指示VPN网关,其实,我们也可以使用net_gateway来指示客户端所在子网网关。OpwnVPN客户端能理解这个变量(除了在2003上面可能有点问题)为自己本地子网网关,然后执行相应路由添加操作。

我们可以利用client-config-dir的DEFAULT配置来避免每次更新路由表后都要重启OpenVPN才能下发的问题。

--client-config-dir dir Specify a directory dir for custom client config files. After a connecting client has been authenticated, OpenVPN will look in this directory for a file having the same name as the client's X509 common name. If a matching file exists, it will be opened and parsed for client-specific configuration options. If no matching file is found, OpenVPN will instead try to open and parse a default file called "DEFAULT", which may be provided but is not required.
The following options are legal in a client-specific context: --push, --push-reset, --iroute, --ifconfig-push, and --config.

我们定义好CCD目录以后 新建一个名字叫DEFAULT的配置文件 把要推送的路由写成如下格式就可以了
比如 我想让教育网的某些IP从我的本地网关走,但是219.230.159.XXX例外,还是从VPN走

#校内路由推送
push "route 219.230.0.0 255.255.0.0 net_gateway"
push "route 211.65.0.0 255.255.0.0 net_gateway"
push "route 10.49.0.0 255.255.0.0 net_gateway"
push "route 172.0.0.0 255.0.0.0 net_gateway"
push "route 219.230.159.XXX 255.255.255.255 vpn_gateway"

要实现chnroutes的下发也非常容易,我们只要用一行脚本就可以把原来给OpenVPN客户端用的配置文件转换成服务器用的。

[bash] wget http://chnroutes.googlecode.com/files/routes.txt -O - -q|sed 's/route/push \"route/' |sed 's/net_gateway 5/net_gateway\"/'[/bash]

不过这个routes.txt不是经常更新,对这个Generating script 稍作修改就可以了,大家自己思维发散~~~ XDDDD

需要注意的是,由于chnroutes路由表长度巨长,接近1000条,而OpenVPN默认只能接受100条路由规则,在旧版本的OpenVPN中,最多只能添加100条自定义路由表。但是从2.1-rc20版本开始,可以通过max-routes xxx选项来更改这个限制。前提:该选项必须添加到所有的路由设置项目之前,包括那个redirect-gateway def1 bypass-dhcp(添加默认路由的那个行为),so "max-routes 1000"应该够用了

Author Info :
  • From:使OpenVPN服务器推送下发chnroutes路由表
  • URL:https://blog.ihipop.com/2011/06/2504.html
  • Please Reserve This Link,Thanks!
  • 《使OpenVPN服务器推送下发chnroutes路由表》上有4条评论

    1. 服务器端openvpn 2.2.2, push 'max-routes 1000' ,客户端 tunnelblick 3.2.3, 不管使用ovpn 2.1.4 or 2.2.1都:option 'max-routes' cannot be used in this context。

      无从下手诊断了..... 求助。

    回复 @caoxg 取消回复

    您的电子邮箱地址不会被公开。 必填项已用*标注