StrongSwan with PureVPN (IKEv2/IPsec)

配置StrongSwan作为客户端,连接到VPN服务器。本来应该是很简单的操作,但是StrongSwan的奇怪的软件设计和安装包的设计会带来一些奇怪的问题。

几个常见问题的处理

1 Ca 必须被手动导入

默认情况下Strongswan是不信任所有证书的,包括所有公共CA的Root证书,也就是说当你连接一个网络上公开服务器时,服务器证书一般是由Public CA Root证书签发的,你必须手动安装相应的Root证书。

https://wiki.strongswan.org/issues/1540

#no trusted RSA public key found for
curl "https://support.comodo.com/index.php?/Knowledgebase/Article/GetAttachment/970/821027" | tee /etc/ipsec.d/cacerts/comodo-rsa-domain-validation-secure-server-ca.pem
curl "https://support.comodo.com/index.php?/Knowledgebase/Article/GetAttachment/969/821026" | tee /etc/ipsec.d/cacerts/comodo-rsa-certification-authority.pem

2 一些插件必须手动安装

Strongswan安装完成后,并没有自动安装一些必备的辅助工具,这些工具有时需要你自己安装,否则连接时就会报错。包括:

  • curl – 用于直接下载和验证服务器的公共证书
  • eap – 用于EAP验证 包含在 :libcharon-extra-plugins 中

https://forum.mikrotik.com/viewtopic.php?t=131681

3 匹配自动连接

如何让一个配置好的连接自动建立连接?auto=start 仅仅时在ipsec启动时进行自动连接,一旦连接中断,不会进行自动重连,很多资料指出应该使用 auto=route,以确保连接按需启动。

事实上,auto=route并不适应于常用的连接公共服务器的情况(https://wiki.strongswan.org/issues/2162) ,正确的方式是配置:

I guess for roadwarrior clients you can also use auto=startkeyingtries=%forever and dpdaction=restart and even closeaction=restart since you use uniqueids=never on the server.

配置示例

以下是配置Strongswan作为PureVPN客户端使用,使用iKEV2协议建立连接的一个典型示例:

# /etc/ipsec.conf - strongSwan IPsec configuration file
# basic configuration
config setup
        # strictcrlpolicy=yes
        # uniqueids = no
# PureVPN connections
conn pure
        keyexchange=ikev2
        right=sg2-cn.sslwonders.com
        rightid=%any
        rightsubnet=0.0.0.0/0
        rightauth=pubkey
        leftsourceip=%config4
        leftauth=eap-mschapv2
        eap_identity=vpn_username 
        auto = start
        keyingtries=%forever
        dpdaction=restart
        closeaction=restart
conn passthrough-1
        leftsubnet=10.5.0.0/16
        rightsubnet=10.5.0.0/16
        type=passthrough
        auto=route
# /etc/ipsec.secret
# This file holds shared secrets or RSA private keys for authentication.
# this file is managed with debconf and will contain the automatically created private key
vpn_username : EAP "password"

在Docker中使用Strongswan

在Docker中使用Strongswan会遇到一个DNS方面的问题,一旦StrongSwan成功建立连接,然后又断开连接后,Resolve.cof文件会被清空。分析发现,Strongswan 经常需要对/etc/resolve.conf进行的修改,以确保使用tunnel对端的DNS。而resole.conf在Docker环境下是以mount的方式挂接主机的resove.conf,这种状况下Strongswan无法正确处理resolve.conf,往往会造成resolve.conf被清空。

解决的方法是在Docker container 建立时 umount  relolve.conf,同时还有注意 umount 之后必须给 resolve.conf 配置正确的初始值。如下:

umount /etc/resolv.conf
echo 'nameserver 8.8.8.8 
options ndots:0' | tee /etc/resolv.conf

可用的Docker Image

如果你准备在Docker中使用StrongSwan,这里有一个很好的Docker Image ( https://hub.docker.com/r/emericlee/stronggate ),它把StrongSwan / Squid Proxy / v2ray 进行了捆绑,可以方便的通过它建立一个基于IPSEC VPN的本地代理,并且提供v2ray server。

它的源代码在这里 https://github.com/EmericLee/stronggate

几个常用命令

ipsec statusall
ipsec restart 
ipsec up link-name

参考

发表评论