-
Notifications
You must be signed in to change notification settings - Fork 2
/
provision-vpn-device.sh
96 lines (79 loc) · 2.01 KB
/
provision-vpn-device.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
set -eux
domain=$(hostname --fqdn)
if [ "$(hostname)" = 'moon' ]; then
local='moon'
local_ts='10.1.0.0/16'
remote='sun'
remote_ts='10.2.0.0/16'
remote_ip='10.2.0.2'
else
local='sun'
local_ts='10.2.0.0/16'
remote='moon'
remote_ts='10.1.0.0/16'
remote_ip='10.1.0.2'
fi
#
# enable IPv4 forwarding.
sed -i -E 's,^\s*#?\s*(net.ipv4.ip_forward=).+,\11,g' /etc/sysctl.conf
sysctl net.ipv4.ip_forward=1
#
# install the strongswan charon daemon (has native systemd integration).
# NB do not install the strongswan package as it will use the legacy stuff (e.g. ipsec.conf).
apt-get install -y charon-systemd
systemctl status strongswan-swanctl
swanctl --version
#
# configure.
cp /vagrant/shared/example-ca/example-ca-crt.pem /etc/swanctl/x509ca
cp /vagrant/shared/example-ca/$local.vpn.example.com-crt.pem /etc/swanctl/x509
cp /vagrant/shared/example-ca/$local.vpn.example.com-key.pem /etc/swanctl/private
mv /etc/swanctl/swanctl.conf ~/swanctl.conf.orig
# see https://wiki.strongswan.org/projects/strongswan/wiki/StrongswanConf
cat >/etc/swanctl/swanctl.conf <<EOF
connections {
net-net {
local {
auth = pubkey
certs = $local.vpn.example.com-crt.pem
}
remote {
auth = pubkey
id = "CN=$remote.vpn.example.com"
}
remote_addrs = $remote.vpn.example.com
children {
net-net {
local_ts = $local_ts
remote_ts = $remote_ts
dpd_action = restart
start_action = trap
}
}
}
}
EOF
swanctl --load-all
#
# kick the tires.
ping -n -c 4 $remote_ip || true
swanctl --list-conns
swanctl --list-sas
swanctl --stats
ip xfrm state
ip xfrm policy
#
# add useful commands to the shell history.
cat >>~/.bash_history <<EOF
ping -n $remote_ip
swanctl --list-conns
swanctl --list-sas
swanctl --stats
ip xfrm state
ip xfrm policy
systemctl status strongswan-swanctl
systemctl restart strongswan-swanctl
journalctl -u charon-systemd
journalctl -u strongswan-swanctl
EOF