基于Open×××连接两个远程局域网段
2008-07-01 TsengYia#126.com
####################################################################
系统环境:
   服务端:RHEL5 [ 2.6.18-8.el5xen ]
目标功能:
   搭建Open×××服务器,跨越Internet连接两个异地的局域网段。
             |---------------| <-->   Internet  <--> |---------------|
   LAN2 <--> |Router2(Client)|                       |Router1(Server)| --> LAN1
             |---------------| <--> SSL ××× Tun <--> |---------------|
   (北京)Server(router1)网络参数:
       eth0 173.16.16.1/24
       eth1 192.168.20.1/24(本例中作为LAN1的网关)
       LAN1:192.168.20.0/24
   (广州)Client(router2)网络参数:
       eth0 211.20.20.1/24
       eth2 192.168.40.1/24(本例中作为LAN2的网关)
       LAN2:192.168.40.0/24
####################################################################
一、安装Open×××软件包 (在router1、router2上均执行以下操作,按默认配置安装到/usr/local目录下)
   1、安装lzo (为SSL数据提供压缩)
shell> tar zxvf lzo-2.03.tar.gz -C /usr/src
shell> cd /usr/src/lzo-2.03
shell> ./configure && make && make install
   2、安装open***
shell> tar zxvf open***-2.0.9.tar.gz -C /usr/src
shell> cd /usr/src/open***-2.0.9
shell> ./configure && make && make install

二、配置Open××× Server端(router1)
   1、制作证书和相关密钥文件 (可参考/usr/src/open***-2.0.9/easy-rsa/README)
     1)调整及预定义变量
shell> mkdir /etc/open***
shell> cd /usr/src/open***-2.0.9/easy-rsa
shell> vi vars
export D=`pwd`
export KER_CONFIG=$D/openssl.cnf
export KEY_DIR="/etc/open***/keys/"    #//修改生成的密钥等文件的保存位置
export KEY_SIZE=1024
export KEY_COUNTRY=CN    #//以下为用于各密钥中的预定义信息
export KEY_PROVINCE=BJ
export KEY_CITY=BJ
export KEY_ORG="BJ-GZ"
export KEY_EMAIL="TsengYiashell>126.com"
shell> . vars
shell> ./clean-all
     2)创建证书、密钥等文件
shell> ./build-ca    #//生成CA证书
shell> ./build-dh    #//生成dh(Diffie-Hellman)文件
shell> ./build-key-server router1    #//生成服务端密钥
shell> ./build-key router2    #//生成客户端密钥
shell> /usr/local/sbin/open*** --genkey --secret /etc/open***/keys/ta.key    #//生成tls-auth密钥
   2、建立Open×××服务配置文件
shell> cp /usr/src/open***-2.0.9/sample-config-files/server.conf /etc/open***/
shell> vi /etc/open***/server.conf
local 173.16.16.1    #//指定×××服务监听的接口地址(本例中eth0网卡的地址)
port 1194    #//指定×××服务监听的端口
proto udp
dev tun
ca keys/ca.crt
cert keys/router1.crt
key keys/router1.key
dh keys/dh1024.pem
server 10.8.8.0 255.255.255.0    #//指定***隧道的虚拟子网,*** server将自动使用第一个IP,如10.8.8.1
ifconfig-pool-persist ipp.txt
push "route 192.168.20.0 255.255.255.0"    #//向客户端通告服务器端LAN1网段
client-config-dir ccd    #//指定调用ccd子目录下的客户端配置文件,可在文件中指定对端的ip地址
route 192.168.40.0 255.255.255.0    #//为server端添加到client端LAN2网段的路由
client-to-client    #//允许各客户端之间的互相访问
duplicate-cn    #//允许client密钥被复用
keepalive 10 120
tls-auth keys/ta.key 0    #//指定tls认证密钥
cipher BF-CBC    #//指定cipher加密算法
comp-lzo
max-clients 100    #//指定最大并发连接数
user nobody
group nobody
persist-key
persist-tun
status /tmp/open***-status.log
verb 3
mute 20
shell> mkdir /etc/open***/ccd
shell> vi /etc/open***/ccd/router2    #//在client的独立配置文件中指定对端tun0的ip地址参数
iroute 192.168.40.0 255.255.255.0
ifconfig-push 10.8.8.2 10.8.8.1    #//依次为tun0本地地址,P-t-P对端地址
   3、准备启动脚本、启动Open×××
shell> cp -p /usr/src/open***-2.0.9/sample-scripts/open***.init /etc/init.d/
shell> vi /etc/init.d/open***
shell> chkconfig --add open***
shell> chkconfig --level 35 open*** on
shell> service open*** start
三、配置Open××× Client端(router2)
   1、下载证书和相关密钥文件
     1)下载在服务器生成的ca.crt、router2.crt、router2.key、ta.key文件,做好备份
     2)复制上述文件到router2的/etc/open***/keys目录
   2、修改Client配置文件
shell> cp /usr/src/open***-2.0.9/sample-config-files/client.conf /etc/open***/
shell> vi /etc/open***/client.conf
client
dev tun
proto udp
remote 173.16.16.1 1194
resolv-retry infinite
nobind
user nobody
group nobody
persist-key
persist-tun
ca keys/ca.crt
cert keys/router2.crt
key keys/router2.key
ns-cert-type server
tls-auth keys/ta.key 1
cipher BF-CBC
comp-lzo
verb 3
mute 20
四、准备启动脚本、启动Open××× (在router1、router2上均执行以下操作)
shell> cp -p /usr/src/open***-2.0.9/sample-scripts/open***.init /etc/init.d/
shell> vi /etc/init.d/open***
shell> chkconfig --add open***
shell> chkconfig --level 35 open*** on
shell> service open*** start

五、连通测试
   1、可以分别在router1、router2上查看tun0设备参数(ifconfig tun0)
router1的tun0信息:
   inet addr:10.8.8.1    P-t-P:10.8.8.2
router2的tun0信息:
   inet addr:10.8.8.2    P-t-P:10.8.8.1
   2、可以分别在router1、router2上查看路由记录(route -n)
router1的路由表信息中应有到LAN2网段的路由记录:
   Destination    Gateway    Genmask        Flags  Metric  Ref  Use  Iface
   192.168.40.0   10.8.8.2   255.255.255.0  UG     0        0   0    tun0
router2的路由表信息中应有到LAN1网段的路由记录:
   Destination    Gateway    Genmask        Flags  Metric  Ref  Use  Iface
   192.168.20.0   10.8.8.1   255.255.255.0  UG     0        0   0    tun0
   3、LAN1、LAN2两个网段的客户端互联测试,例如:
北京的192.168.20.20和广州的192.168.40.40能够相互ping通。