strongswan 搭建ikev1 for Android and iOS

来源:互联网 发布:蜜桃诱导充值视频源码 编辑:程序博客网 时间:2024/05/29 11:55

1.关于strongswan 的介绍

 srongswan 是基于ipsec 的开源 vpn 服务器,目前已经实现了ikev1 和 ikev2的vpn 服务器,其官网上有相应的示例,但如果是初学者来说,可能会比较难,本人就是一个菜鸟,刚开始做的时候,一头雾水,网上的资源也相对较少,因此本人整理关于如何搭建ikev1 模式的vpn 服务器,并且测试成功


 2.  安装strongswan 

Centos安装依赖:yum  install  -y  gmp-devel 

Debian/ubuntu安装依赖:

apt-get  –y  install  gcc

apt-get  –y  install  make

apt-ge  -y  install  libgmp10 libgmp3-dev libssl-dev pkg-config libpcsclite-dev libpam0g-dev

下载:

wget http://download.strongswan.org/strongswan.tar.gz

tar xzf strongswan.tar.gcd cd strongswan-*

./configure  --sysconfdir=/etc  --enable-openssl --enable-nat-transport --disable-mysql --disable-ldap  --disable-static --enable-shared --enable-md4 --enable-eap-mschapv2 --enable-eap-aka --enable-eap-aka-3gpp2  --enable-eap-gtc --enable-eap-identity --enable-eap-md5 --enable-eap-peap --enable-eap-radius --enable-eap-sim --enable-eap-sim-file --enable-eap-simaka-pseudonym --enable-eap-simaka-reauth --enable-eap-simaka-sql --enable-eap-tls --enable-eap-tnc --enable-eap-ttlsmake > make install


3.与strogswan 相关的配置文件及其配置

 stongswan 的相关配置文件在/etc 目录下,需要修改的配置文件有/etc/ipsec.conf , /etc/strongswan.conf , /etc/ipse.secrets ,/etc/strongswan.d/charon-logging.conf

1)首先修改ipsec.conf 配置文,如下所示:

conn android_xauth_psk       keyexchange=ikev1       fragmentation=yes       keyingtries=3       left=%defaultroute       leftauth=psk       right=%any       rightsourceip = %config       rightsubnet=0.0.0.0/0       rightauth=psk       rightauth2=xauth       auto=add


说明

Conn位于配置文件的全局区,后面紧紧跟的是该VPN服务器的

名称

keyexchange=ikev1:指定使用的密钥交换协议是ikev1

left=%defaultroute:远端vpn服务器的ip地址是任意的

leftauth=psk :表示远程服务器数据加密的方式是预共享密钥

right=psk 表示本地客户端的数据加密方式的预共享密钥

right=%any:表示客户端的ip地址是任意的

rightsourceip=%config为客户端分配ip地址池,%config表示是ipv4的任意地址

rightsubnet=0.0.0.0/0表示客客户端的子网是任意的

rightauth2=xauth表示客户端的用户认证方式是XAUTH

fragmentation=yes开启对 iOS 拆包的重组支持。

auto 定义 strongswan 启动时该连接的行为start是启动; route是添加路由表,有数据通过就启动; add是添加连接类型但不启动; ignore是当它不存在。默认是 ignore。看起来似乎是 route比较好,但问题是我们服务器端不能预分配虚拟 IP,所以服务器端一般用的都是 add。而客户端文本配置可以选择 start

2)配置strongswa.conf

charon {        load_modular = yes        duplicheck.enable = no         compress = yes        dns1 = 8.8.8.8        dns2 = 8.8.4.4     filelog {            /var/log/strongswan.charon.log {                time_format = %b %e %T                    default = 2                    append = no                    flush_line = yes            }}


说明:其他的保持不变

strongswan5以后,无论是ikev1还是ikev2协议都使用ike2daemoncharon来实现

duplicheck.enable = no是为了同时连接多个设备,所以要把冗余检查关闭

 /var/log/strongswan.charon.log配置的strongswan的日志的格式

 default = 2定义的是日志的级别,默认日志级别为:-1,0,1,2,3,4-1是完全没有日志,0只告诉你建立连接,连接关闭;1只输出错误提示,2会输出错误,警告和调试信息;3会把连接传输的数据也打印;4则会把密钥内容这些敏感数据也打印。一般情况下,12都可以

append = no是相当你重启strogswan后,是按上次日志写,还是新建一个日志(上次的会删除)。因为strogswan的日志太多了,所以用no 

flush_line = yes是每产生一行日志,就写入到磁盘一次,防止突然断电,磁盘缓存数据丢失。

3)修改ipse.secrets配置文件

该文件主要保存的客户端和服务器之间的数据加密方式及用户的信息

:   PSK  0sZ3l0MTIzNDU2

jianq : XAUTH "gyt123456"

说明:PSK即为共享密钥,其数据格式为0s+base64编码的数据,

  XAUTH左右两端的分别为用户名和用户登录密码(字符串)

修改/etc/strongswwan.d/Charon-logging.cong配置文件:

该文件主要是用于控制输出的调试信息

filelog作用域加上如下内容:

 append=nodefault=2flush_line = yesike_name=yes time_format = %b %e %T  stderr {          ike =2          knl=3        }


 4)修改 charon-logging.conf 配置文件

charon {    # Section to define file loggers, see LOGGER CONFIGURATION in    # strongswan.conf(5).    filelog {        # <filename> is the full path to the log file.        # <filename> {            # Loglevel for a specific subsystem.            # <subsystem> = <default>            # If this option is enabled log entries are appended to the existing            # file.            append = yes            # Default loglevel.             default = 1            # Enabling this option disables block buffering and enables line            # buffering.            # flush_line = no         flush_line=yes            # Prefix each log entry with the connection name and a unique            # numerical identifier for each IKE_SA.            # ike_name = no             ike_name=yes            # Prefix each log entry with a timestamp. The option accepts a            # format string as passed to strftime(3).            # time_format =               time_format=%b %e %T        # }    }    # Section to define syslog loggers, see LOGGER CONFIGURATION in    # strongswan.conf(5).    syslog {        # Identifier for use with openlog(3).        # identifier =        # <facility> is one of the supported syslog facilities, see LOGGER        # CONFIGURATION in strongswan.conf(5).        # <facility> {            # Loglevel for a specific subsystem.            # <subsystem> = <default>            # Default loglevel.            # default = 1              default=1            # Prefix each log entry with the connection name and a unique            # numerical identifier for each IKE_SA.            # ike_name = no                ike_name=yes        # }    }


4.修改防火墙

sudo iptables -A INPUT -p udp --dport 500 -j ACCEPTsudo iptables -A INPUT -p udp --dport 4500 -j ACCEPTsudo iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADEsudo iptables -A FORWARD -s 10.0.0.0/24 -j ACCEPTsudo echo 1 > /proc/sys/net/ipv4/ip_forward

做完上述步骤之后启动strongswan 

调试的形式启动:strongsan : ipsec  start  --nofork

查看状态 ipsec statusall


5.配置客户端

说明:

Android端类型选择IPSec  Xauth  PSK

IOS端类型选择  ipsec 

预共享密钥:填写PSK后面base64位解码的字符串

用户名和密码填写XAUTH的左右的内容


说明:

Android端类型选择IPSec  Xauth  PSK

IOS端类型选择  ipsec 

预共享密钥:填写PSK后面base64位解码的字符串

用户名和密码填写XAUTH的左右的内容






0 0