Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daimajia/b3ac7687e77e6fade889 to your computer and use it in GitHub Desktop.
Save daimajia/b3ac7687e77e6fade889 to your computer and use it in GitHub Desktop.

平台:原生ubuntu14.04 x86 strongswan5.1.3 一:安装strongswan apt-get install gcc make strongswan (strongswan版本可能较低) 到https://gmplib.org/ 下载编译最新的The GNU Multiple Precision Arithmetic Library(编译strongswan过程依赖) 如编译出现错误“OpenSSL crypto library not found”,用 apt-get install openssl-dev 命令解决(ubuntu14.04本身已安装openssl)

ubuntu仓库中的strongswan经实验多次没有成功,遂重新自己下载编译 官网http://www.strongswan.org/, 5.1.3地址 http://download.strongswan.org/strongswan-5.1.3.tar.bz2 wget http://download.strongswan.org/strongswan-5.1.3.tar.bz2 tar -jxvf strongswan-5.1.3.tar.bz2 cd strongswan-5.1.3.tar.bz2 ./configure --prefix=/usr --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-ttls make && make install

二:生成、安装证书 1:win7和Android、wp8.1等平台的VPN客户端走ikev2协议,需要制作相应的证书,先生成ca证书 ipsec pki --gen --outform pem > caKey.pem ipsec pki --self --in caKey.pem --dn "C=CN, O=strongSwan, CN=strongSwan CA" --ca --outform pem > caCert.pem 2:然后是服务器端的证书 ipsec pki --gen --outform pem > serverKey.pem ipsec pki --pub --in serverKey.pem | ipsec pki --issue --cacert caCert.pem --cakey caKey.pem --dn "C=CN, O=strongSwan, CN=VPS的公网ip或域名" --san="VPS的公网ip或域名" --flag serverAuth --flag ikeIntermediate --outform pem > serverCert.pem 3:客户端的证书 ipsec pki --gen --outform pem > clientKey.pem ipsec pki --pub --in clientKey.pem | ipsec pki --issue --cacert caCert.pem --cakey caKey.pem --dn "C=CN, O=strongSwan, CN=client" --outform pem > clientCert.pem 生成的客户端证书 clientCert.pem 不能直接导入到win7或Anroid设备中,需先转换为.p12格式。执行后会提示要设置证书使用密码,可以设置一下密码也可以直接回车(密码为空)。 openssl pkcs12 -export -inkey clientKey.pem -in clientCert.pem -name "client" -certfile caCert.pem -caname "strongSwan CA" -out clientCert.p12 4安装证书 cp caCert.pem /etc/ipsec.d/cacerts/ cp serverCert.pem /etc/ipsec.d/certs/ cp serverKey.pem /etc/ipsec.d/private/

三:配置strongswan 1: #/etc/ipsec.conf config setup strictcrlpolicy=no uniqueids=no #允许多设备同时在线

conn windowsphone keyexchange=ikev2 ike=aes256-sha1-modp1024! esp=aes256-sha1! dpdaction=clear dpddelay=300s rekey=no left=%defaultroute leftsubnet=0.0.0.0/0 leftauth=pubkey leftcert=serverCert.pem leftid="C=CN, O=strongSwan, CN=X.X.X.X" #C=国家,CN=自己vps的公网ip right=%any rightsourceip=10.11.1.0/24 #为客户端分配的虚拟地址池 rightauth=eap-mschapv2 rightsendcert=never eap_identity=%any auto=add

2: #/etc/ipsec.secrets : RSA serverKey.pem 用户名1 : EAP "密码1" wp设备名称\用户名2 : EAP "密码2" #仅对windowsphone8.1设备 #windowsphone8.1,在客户端输入的用户名发送到服务器显示为“设备名称\用户名”的形式,故认证需加上设备名称,设备名限制15字符 #貌似只能全英文不能有标点、符号(这一点有待确认)

3: #/etc/strongswan.conf #加入分配的dns charon {

    dns1 = 8.8.8.8
    dns2 = 208.67.222.222

}

四:配置 Iptables 转发 iptables -A INPUT -p udp --dport 500 -j ACCEPT iptables -A INPUT -p udp --dport 4500 -j ACCEPT

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A POSTROUTING -s 10.11.1.0/24 -o eth0 -j MASQUERADE #地址与上面地址池对应 iptables -A FORWARD -s 10.11.1.0/24 -j ACCEPT #同上

最后,启动strongswan: ipsec start 滚动日志: ipsec start --nofork

参考链接: http://zh.opensuse.org/index.php?title=SDB:Setup_Ipsec_VPN_with_Strongswan&variant=zh http://si-you.com/?p=1167 http://blog.ltns.info/linux/pure_ipsec_multi-platform_vpn_client_debian_vps/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment