-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvps-setup.sh
277 lines (214 loc) · 7.93 KB
/
vps-setup.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash -e
# https://github.com/diev/Always-VPN
# based on github.com/jawj/IKEv2-setup
# Copyright (c) 2015 – 2018 George MacKerron
# Released under the MIT licence: http://opensource.org/licenses/mit-license
# also uses github.com/ValdikSS/easy-rsa-ipsec
# forked from github.com/OpenVPN/easy-rsa
#DE edition 2021-04-07 Add Ubuntu 20.04
#DE edition 2019-12-23
echo
echo "=== diev/Always-VPN ==="
echo
function exit_badly {
echo $1
exit 1
}
[[ $(lsb_release -rs) == "18.04" ]] || [[ $(lsb_release -rs) == "20.04" ]] || exit_badly "This script is for Ubuntu 20.04 or 18.04 only: aborting (if you know what you're doing, try deleting this check)"
[[ $(id -u) -eq 0 ]] || exit_badly "Please re-run as root (e.g. sudo ./$0)"
echo "--- Updating and installing software ---"
echo
export DEBIAN_FRONTEND=noninteractive
apt-get -o Acquire::ForceIPv4=true update
apt-get -o Acquire::ForceIPv4=true install -y software-properties-common
add-apt-repository universe
add-apt-repository restricted
add-apt-repository multiverse
apt-get -o Acquire::ForceIPv4=true --with-new-pkgs upgrade -y
apt autoremove -y
apt-get -o Acquire::ForceIPv4=true install -y language-pack-en strongswan libstrongswan-standard-plugins strongswan-libcharon libcharon-standard-plugins libcharon-extra-plugins moreutils iptables-persistent unattended-upgrades dnsutils uuid-runtime git mc fail2ban
echo
echo "--- Configuration: VPN settings ---"
echo
ETH0ORSIMILAR=$(ip route get 1.1.1.1 | awk -- '{printf $5}')
IP=$(ifdata -pa $ETH0ORSIMILAR)
echo "Network interface: ${ETH0ORSIMILAR}"
echo "External IP: ${IP}"
echo
VPNDNS="1.1.1.1,1.0.0.1"
echo
echo "--- Configuration: general server settings ---"
echo
TZONE="Europe/Moscow"
SSHPORT=22
VPNIPPOOL="192.168.103.0/24"
echo
echo "--- Configuring firewall ---"
echo
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -t nat -F
iptables -t mangle -F
# INPUT
# accept anything already accepted
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# accept anything on the loopback interface
iptables -A INPUT -i lo -j ACCEPT
# drop invalid packets
iptables -A INPUT -m state --state INVALID -j DROP
# rate-limit repeated new requests from same IP to any ports
iptables -I INPUT -i $ETH0ORSIMILAR -m state --state NEW -m recent --set
iptables -I INPUT -i $ETH0ORSIMILAR -m state --state NEW -m recent --update --seconds 300 --hitcount 60 -j DROP
# accept (non-standard) SSH
iptables -A INPUT -p tcp --dport $SSHPORT -j ACCEPT
# VPN
# accept IPSec/NAT-T for VPN (ESP not needed with forceencaps, as ESP goes inside UDP)
iptables -A INPUT -p udp --dport 500 -j ACCEPT
iptables -A INPUT -p udp --dport 4500 -j ACCEPT
# forward VPN traffic anywhere
iptables -A FORWARD --match policy --pol ipsec --dir in --proto esp -s $VPNIPPOOL -j ACCEPT
iptables -A FORWARD --match policy --pol ipsec --dir out --proto esp -d $VPNIPPOOL -j ACCEPT
# reduce MTU/MSS values for dumb VPN clients
iptables -t mangle -A FORWARD --match policy --pol ipsec --dir in -s $VPNIPPOOL -o $ETH0ORSIMILAR -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
# masquerade VPN traffic over eth0 etc.
iptables -t nat -A POSTROUTING -s $VPNIPPOOL -o $ETH0ORSIMILAR -m policy --pol ipsec --dir out -j ACCEPT # exempt IPsec traffic from masquerading
iptables -t nat -A POSTROUTING -s $VPNIPPOOL -o $ETH0ORSIMILAR -j MASQUERADE
# fall through to drop any other input and forward traffic
iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP
iptables -L
debconf-set-selections <<< "iptables-persistent iptables-persistent/autosave_v4 boolean true"
debconf-set-selections <<< "iptables-persistent iptables-persistent/autosave_v6 boolean true"
dpkg-reconfigure iptables-persistent
echo
echo "--- Configuring RSA certificates ---"
echo
cd /tmp
git clone https://github.com/ValdikSS/easy-rsa-ipsec.git
cd easy-rsa-ipsec/easyrsa3
./easyrsa init-pki
./easyrsa --batch --req-cn="${IP} Root CA" build-ca nopass
./easyrsa build-server-full ${IP} nopass
./easyrsa build-client-full ${IP}-user1 nopass
./easyrsa build-client-full ${IP}-user2 nopass
./easyrsa build-client-full ${IP}-user3 nopass
./easyrsa build-client-full ${IP}-user4 nopass
./easyrsa build-client-full ${IP}-user5 nopass
./easyrsa build-client-full ${IP}-user6 nopass
./easyrsa build-client-full ${IP}-user7 nopass
./easyrsa build-client-full ${IP}-user8 nopass
./easyrsa build-client-full ${IP}-user9 nopass
./easyrsa export-p12 ${IP}-user1 nopass
./easyrsa export-p12 ${IP}-user2 nopass
./easyrsa export-p12 ${IP}-user3 nopass
./easyrsa export-p12 ${IP}-user4 nopass
./easyrsa export-p12 ${IP}-user5 nopass
./easyrsa export-p12 ${IP}-user6 nopass
./easyrsa export-p12 ${IP}-user7 nopass
./easyrsa export-p12 ${IP}-user8 nopass
./easyrsa export-p12 ${IP}-user9 nopass
cp pki/ca.crt /etc/ipsec.d/cacerts/
cp pki/issued/${IP}.crt /etc/ipsec.d/certs/
cp pki/private/${IP}.key /etc/ipsec.d/private/
echo
echo "--- Configuring VPN ---"
echo
# ip_forward is for VPN
# ip_no_pmtu_disc is for UDP fragmentation
# others are for security
grep -Fq 'diev/Always-VPN' /etc/sysctl.conf || echo '
# https://github.com/diev/Always-VPN
net.ipv4.ip_forward = 1
net.ipv4.ip_no_pmtu_disc = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
' >> /etc/sysctl.conf
sysctl -p
# ipsec.conf - strongSwan IPsec configuration file
echo "config setup
uniqueids=never
conn %default
dpdaction=clear
dpddelay=35s
dpdtimeout=300s
fragmentation=yes
rekey=no
left=%any
leftauth=pubkey
leftcert=${IP}.crt
leftsendcert=always
leftsubnet=0.0.0.0/0
right=%any
rightauth=pubkey
rightdns=${VPNDNS}
rightsourceip=${VPNIPPOOL}
rightsendcert=never
conn ikev2-pubkey
keyexchange=ikev2
auto=add
conn ikev2-eap-tls
also=\"ikev2-pubkey\"
rightauth=eap-tls
eap_identity=%identity
" > /etc/ipsec.conf
# This file holds shared secrets or RSA private keys for authentication.
echo "${IP} : RSA \"${IP}.key\"
" > /etc/ipsec.secrets
ipsec restart
echo
echo "--- User ---"
echo
# user + SSH
#DE id -u $LOGINUSERNAME &>/dev/null || adduser --disabled-password --gecos "" $LOGINUSERNAME
#DE echo "${LOGINUSERNAME}:${LOGINPASSWORD}" | chpasswd
#DE adduser ${LOGINUSERNAME} sudo
#DE
#DE sed -r \
#DE -e "s/^#?Port 22$/Port ${SSHPORT}/" \
#DE -e 's/^#?LoginGraceTime (120|2m)$/LoginGraceTime 30/' \
#DE -e 's/^#?PermitRootLogin yes$/PermitRootLogin no/' \
#DE -e 's/^#?X11Forwarding yes$/X11Forwarding no/' \
#DE -e 's/^#?UsePAM yes$/UsePAM no/' \
#DE -i.original /etc/ssh/sshd_config
#DE
#DE grep -Fq 'diev/Always-VPN' /etc/ssh/sshd_config || echo "
#DE # https://github.com/diev/Always-VPN
#DE MaxStartups 1
#DE MaxAuthTries 2
#DE UseDNS no" >> /etc/ssh/sshd_config
#DE
#DE service ssh restart
echo
echo "--- Timezone, unattended upgrades ---"
echo
timedatectl set-timezone $TZONE
/usr/sbin/update-locale LANG=en_US.UTF-8
sed -r \
-e 's|^//Unattended-Upgrade::MinimalSteps "true";$|Unattended-Upgrade::MinimalSteps "true";|' \
-e 's|^//Unattended-Upgrade::Mail "root";$|Unattended-Upgrade::Mail "root";|' \
-e 's|^//Unattended-Upgrade::Automatic-Reboot "false";$|Unattended-Upgrade::Automatic-Reboot "true";|' \
-e 's|^//Unattended-Upgrade::Remove-Unused-Dependencies "false";|Unattended-Upgrade::Remove-Unused-Dependencies "true";|' \
-e 's|^//Unattended-Upgrade::Automatic-Reboot-Time "02:00";$|Unattended-Upgrade::Automatic-Reboot-Time "03:00";|' \
-i /etc/apt/apt.conf.d/50unattended-upgrades
echo 'APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
' > /etc/apt/apt.conf.d/10periodic
service unattended-upgrades restart
echo
echo "--- Creating configuration files ---"
echo
cp pki/ca.crt /tmp/${IP}-ca.crt
cp pki/issued/${IP}.crt /tmp
cp pki/private/*.p12 /tmp
echo
echo "--- How to connect ---"
echo
echo "Connection files take from /tmp"