-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathopkg-install_strongswan.sh
233 lines (204 loc) · 6.91 KB
/
opkg-install_strongswan.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/sh
#
FILE_PATH=$(readlink -f $(dirname $0)) #/root
FILE_NAME=$(basename $0) #opkg-install.sh
FILE_NAME=${FILE_NAME%.*} #opkg-install
FILE_DATE=$(date +'%Y%m%d-%H%M%S')
FILE_LOG="/var/log/$FILE_NAME.log"
###############################################################################
### Functions
###############################################################################
function fCmd() {
local cmd=$@
$cmd > /dev/null
if [ $? -ne 0 ]; then
echo "* "
echo "* $cmd" | xargs
echo -n "* Do you want to retry? [Y/n] "
read answer
[ -n "$(echo $answer | grep -i '^y')" ] || [ -z "$answer" ] && fCmd $cmd
fi
}
###############################################################################
### Environment Variables
###############################################################################
# Do not interprate sapce in variable
SAVEIFS=$IFS
IFS=$'\n'
# Source under this script directory
cd $(readlink -f $(dirname $0))
source ./.env
LOCAL_DOMAIN="${DOMAIN%%.*}"
NETADDR=${IPADDR%.*}
###############################################################################
### Script
###############################################################################
echo "* UCI config firewall for IKEv2/IPsec VPN server"
uci add firewall rule
uci set firewall.@rule[-1]=rule
uci set firewall.@rule[-1].name='Allow-IPsec-ESP'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].proto='esp'
uci set firewall.@rule[-1].target='ACCEPT'
uci add firewall rule
uci set firewall.@rule[-1]=rule
uci set firewall.@rule[-1].name='Allow-IPsec-AH'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].proto='ah'
uci set firewall.@rule[-1].target='ACCEPT'
uci add firewall rule
uci set firewall.@rule[-1]=rule
uci set firewall.@rule[-1].name='Allow-IPsec-IKE'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].dest_port='500'
uci set firewall.@rule[-1].proto='udp'
uci set firewall.@rule[-1].target='ACCEPT'
uci add firewall rule
uci set firewall.@rule[-1]=rule
uci set firewall.@rule[-1].name='Allow-IPsec-NAT-T'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].proto='udp'
uci set firewall.@rule[-1].dest_port='4500'
uci set firewall.@rule[-1].target='ACCEPT'
uci commit firewall
echo "* UCI config network/interface for IKEv2/IPsec VPN server"
# Add vpn interface
uci set network.ipsec_server=interface
uci set network.ipsec_server.proto='none'
uci set network.ipsec_server.device='ipsec0'
uci set network.ipsec_server.auto='1'
uci commit network
echo "* UCI config network/zone for IKEv2/IPsec VPN server"
# Add vpn zone
# firewall.@zone[3].name='vpn'
I=$(echo "$(uci show firewall | grep ".name='vpn'")" | awk -F'[][]' '{print $2}')
if [ -z "$I" ]; then
# Add new zone
uci add firewall zone
uci set firewall.@zone[-1]=zone
uci set firewall.@zone[-1].name='vpn'
uci set firewall.@zone[-1].network='ipsec_server'
uci set firewall.@zone[-1].input='ACCEPT'
uci set firewall.@zone[-1].output='ACCEPT'
uci set firewall.@zone[-1].forward='ACCEPT'
uci set firewall.@zone[-1].masq='1'
else
# Add network on existing zone
uci add_list firewall.@zone[$I].network='ipsec_server'
fi
uci commit firewall
echo "* UCI config network/route for IKEv2/IPsec VPN server"
# Add route
uci add network route
uci set network.@route[-1].interface='ipsec_server'
uci set network.@route[-1].target='10.10.3.0'
uci set network.@route[-1].netmask='255.255.255.0'
uci set network.@route[-1].gateway="$IPADDR"
uci commit network
echo "* UCI config dhcp/dnsmasq for IKEv2/IPsec VPN server"
# Accept DNS queries from others hosts
uci set dhcp.@dnsmasq[0].localservice='0'
uci commit dhcp
if [ -f /etc/acme/$DOMAIN/ca.cer ]; then
echo "* Link ACME cetificates for IKEv2/IPsec VPN server"
[ -d /etc/ipsec.d ] && rm -f /etc/ipsec.d/cacerts/acme.ca.cer
[ -d /etc/ipsec.d ] && find /etc/ipsec.d -name *.pem -print | xargs rm > /dev/null 2>&1
mkdir -p /etc/ipsec.d/cacerts && ln -sf /etc/acme/$DOMAIN/ca.cer /etc/ipsec.d/cacerts/acme.ca.cer
mkdir -p /etc/ipsec.d/certs && ln -sf /etc/acme/$DOMAIN/fullchain.cer /etc/ipsec.d/certs/$DOMAIN.cert.pem
mkdir -p /etc/ipsec.d/private && ln -sf /etc/acme/$DOMAIN/$DOMAIN.key /etc/ipsec.d/private/$DOMAIN.pem
fi
echo "* Package IKEv2/IPsec VPN server with strongSwan"
fCmd opkg install strongswan-full
# Fix missing package in OpenWrt 21.02.0:
# [KNL] received netlink error: Function not implemented (89)
# [KNL] unable to add SAD entry with SPI ccc321fa
# [IKE] unable to install inbound and outbound IPsec SA (SAD) in kernel
fCmd opkg install strongswan-mod-kernel-libipsec
echo "* Set config files for IKEv2/IPsec VPN server with strongSwan"
cat << EOF > /etc/strongswan.conf
# strongswan.conf - strongSwan configuration file
#
# Refer to the strongswan.conf(5) manpage for details
#
# Configuration changes should be made in the included files
charon {
threads = 16
load_modular = yes
plugins {
include strongswan.d/charon/*.conf
}
}
include strongswan.d/*.conf
EOF
cat << EOF > /etc/ipsec.conf
# ipsec.conf - strongSwan IPsec configuration file
config setup
# Increase debug level
#charondebug = "all"
charondebug = "ike 1, knl 1, cfg 0"
# Allows few simultaneous connections with one user account
uniqueids=no
conn %default
# Dead peer detection will ping clients and terminate sessions after timeout
dpdaction=clear
dpdtimeout=90s
dpddelay=30s
fragmentation=yes
forceencaps=yes
conn ikev2-eap
# Create IKEv2 VPN tunnel
auto=add
compress=no
type=tunnel
keyexchange=ikev2
reauth=no
rekey=no
ike=3des-sha1-modp1024,3des-sha256-modp1024,aes256gcm16-sha256-modp2048!
esp=3des-sha1,3des-sha256,aes256gcm16-sha256!
# left - local (server) side
left=%any
leftid=@$DOMAIN
leftauth=pubkey
leftcert=$DOMAIN.cert.pem # Filename of certificate located at /etc/ipsec.d/certs/
leftsendcert=always
leftsubnet=0.0.0.0/0
# right - remote (client) side
right=%any
rightid=%any
rightauth=eap-mschapv2
rightsourceip=10.10.3.0/24
rightdns=$NETADDR.1
rightsendcert=never
eap_identity=%identity
EOF
cat << EOF > /etc/ipsec.secrets
# /etc/ipsec.secrets - strongSwan IPsec secrets file
: RSA $DOMAIN.pem
EOF
# Add automatically vpn user
# VPN_USER="username|password"
for L in $(cat .env | grep "^VPN_USER="); do
# Get the value after =
V=${L#*=}
# Evaluate variable inside the line
V=$(eval echo $V)
# Remove " from string
#V=${V//\"}
# username : EAP "password"
echo "$(echo $V | cut -d'|' -f1) : EAP \"$(echo $V | cut -d'|' -f2)\"" >> /etc/ipsec.secrets
done
echo "* UCI config remove default firewall - Traffic Rules for IKEv2/IPsec VPN server"
L=$(uci show firewall | grep 'Allow-IPSec-ESP')
if [ -n "$L" ]; then
I=$(echo "$L" | awk -F'[][]' '{print $2}')
uci -q del firewall.@rule[$I]
fi
L=$(uci show firewall | grep 'Allow-ISAKMP')
if [ -n "$L" ]; then
I=$(echo "$L" | awk -F'[][]' '{print $2}')
uci -q del firewall.@rule[$I]
fi
uci commit firewall
# Rollback Internal Field Separator
IFS=$SAVEIFS
exit 0