让OpenVPN支持IPv6隧道,让客户端访问IPv6网络

来源:https://humou.net/blog/201102041307.html

tunnelbroker.net申请到了免费的IPv6地址, 当然不能浪费着,目前天朝的IPv6太遥远的,所以我要想办法让在天朝的电脑访问IPv6的网络,鉴于翻墙必备openvpn,所在打算在openvpn 上改造,想法是让openvpn分配ipv6地址给客户端电脑,Google了一下,果真找到有相同想法的网友,并且成功实现。

本人的系统配置环境为:Debian 5(服务器),Windows 7(客户机)

首先在服务器上配置好openvpn及he.net的IPv6隧道,openvpn配置见:https://humou.net/index.php?s=openvpn
IPv6隧道配置方法如下:
首先在:http://tunnelbroker.net注册账号,使用你的服务器的IPv4地址申请一块IPv6地址,申请非常的容易,这里不再介绍,Google一下也大把的。

申请成功后你会得到以下几个IP地址:
服务端的IPv4地址,服务端的IPv6地址,你的IPv4地址,你的IPv6地址,你得到的IPv6地址块

下面以Debian 5为例进行配置网络:
编辑:

1
/etc/network/interfaces

添加:

1
2
3
4
5
6
7
8
auto hetunnel
iface hetunnel inet6 v4tunnel
address 2001:470:1f0e:990::2 ##你的ipv6地址
netmask 64
ttl 64
gateway 2001:470:1f0e:990::1 ##服务器的ipv6地址
endpoint 216.218.224.42 ##服务器的ipv4地址
local 69.XXX.XXX.XXX ##你的ipv4地址,即你自已服务器的ipv4地址

不明白者请参考图片添加。

添加好之后重启网络:

1
/etc/init.d/networking restart

或重启系统。

修改openvpn配置文件:

编缉:

1
/etc/sysctl.conf

添加:

1
net.ipv6.conf.all.forwarding = 1

编缉:

1
/etc/openvpn/server.conf

在最后添加:

1
2
3
script-security 2
client-connect /etc/openvpn/client-connect.sh
client-disconnect /etc/openvpn/client-disconnect.sh

其中/etc/openvpn/client-connect.sh这个文件的内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
 
# This is a script that is run each time a remote client connects
# to this openvpn server.
# it will setup the ipv6 tunnel depending on the ip address that was
# given to the client
 
BASERANGE="2001:470:baa2" ##你的48位的ipv6地址块前缀,见图
# v6net is the last section of the ipv4 address that openvpn allocated
V6NET=$(echo ${ifconfig_pool_remote_ip} | awk -F. '{print $NF}')
 
SITID="sit${V6NET}"
 
# setup the sit between the local and remote openvpn addresses
/sbin/ip tunnel add ${SITID} mode sit ttl 64 remote ${ifconfig_pool_remote_ip} local ${ifconfig_local}
/sbin/ip link set dev ${SITID} up
 
# config routing for the new network
/sbin/ip -6 addr add ${BASERANGE}:${V6NET}::1/64 dev ${SITID}
/sbin/ip -6 route add ${BASERANGE}:${V6NET}::/64 via ${BASERANGE}:${V6NET}::2 dev ${SITID} metric 1
 
# log to syslog
echo "${script_type} client_ip:${trusted_ip} common_name:${common_name} local_ip:${ifconfig_local} \
remote_ip:${ifconfig_pool_remote_ip} sit:${SITID} ipv6net:${V6NET}" | /usr/bin/logger -t ovpn

表示openvpn客户端连接成功后,会自动添加一条ipv6隧道,并在openvpn服务器上生成一块名为sitX的网卡, X为客户端的IP地址最后一位数字。

比方:客户端IP为10.168.1.6,openvpn服务器IP为10.168.1.1,那么,openvpn服务器上自动添加网卡sit6,网卡的IP地址为:2001:470:baa2::6:1,而客户端的ipv6地址为:2001:470:baa2::6:2

然后配置客户端Windows 7,以管理员身份运行cmd.exe,运行以下命令:

1
2
3
netsh interface ipv6 add v6v4tunnel interface=IP6Tunnel 10.168.1.6 10.168.1.1
netsh interface ipv6 add address IP6Tunnel 2001:470:baa2:6::2/64
netsh interface ipv6 add route ::/0 IP6Tunnel 2001:470:baa2:6::1

然后连上openvpn,即可访问ipv6。

而/etc/openvpn/client-disconnect.sh文件即为客户端断开openvpn连接时删除相应的隧道及网卡。

内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
 
# This is a script that is run each time a remote client disconnects
# to this openvpn server.
 
BASERANGE="2001:470:baa2"
# v6net is the last section of the ipv4 address that openvpn allocated
V6NET=$(echo ${ifconfig_pool_remote_ip} | awk -F. '{print $NF}')
 
SITID="sit${V6NET}"
 
/sbin/ip -6 addr del ${BASERANGE}:${V6NET}::1/64 dev ${SITID}
 
# remove the sit between the local and:q
 
#remote openvpn addresses
 
/sbin/ip link set dev ${SITID} down
/sbin/ip tunnel del ${SITID} mode sit ttl 64 remote ${ifconfig_pool_remote_ip} local ${ifconfig_local}
 
# log to syslog
echo "${script_type} client_ip:${trusted_ip} common_name:${common_name} local_ip:${ifconfig_local} \
remote_ip:${ifconfig_pool_remote_ip} sit:${SITID} ipv6net:${V6NET} duration:${time_duration} \
received:${bytes_received} sent:${bytes_sent}" | /usr/bin/logger -t ovpn

—————————————————————————————————————————

需要翻墙利器赛风? 请阅读和关注中国数字时代

推特用户请点击这里免翻墙上推特

请点击这里下载翻墙软件

更多翻墙方法请发电邮(最好用Gmail)到:fanqiang70ma@gmail.com

请阅读和关注中国数字时代、翻墙技术博客GFW BLOG(免翻墙)

请使用Google Reader订阅中国数字时代中文版http://chinadigitaltimes.net/chinese/feed),阅读最有价值的中文信息;以及GFW BLOG(功夫网与翻墙)http://feeds2.feedburner.com/chinagfwblog,获取最新翻墙工具和翻墙技巧信息。


没有评论: