-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·92 lines (78 loc) · 3.4 KB
/
run.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
#!/bin/bash
DOMAIN_NAME=server.example.com
LEFT_ID="leftid="$DOMAIN_NAME
PWD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 1)
CREDENTIALS="user1 : EAP \""$PWD"\""
echo "Installing required packages..."
echo "------------------------------------"
apt-get update && apt-get -y upgrade
apt-get -y install mosh strongswan libcharon-extra-plugins letsencrypt
echo "...done!"
# Get certificates and copy them to destination
echo "Requesting certificates for $DOMAIN_NAME..."
echo "------------------------------------"
letsencrypt certonly --standalone -d $DOMAIN_NAME --non-interactive --agree-tos -m $ADMIN_EMAIL
echo "...done!"
echo "Copying certificates..."
echo "------------------------------------"
cp /etc/letsencrypt/live/$DOMAIN_NAME/fullchain.pem /etc/ipsec.d/certs
cp /etc/letsencrypt/live/$DOMAIN_NAME/privkey.pem /etc/ipsec.d/private
echo "...done!"
# Tuning iptables ...
echo "Setting iptables up..."
echo "------------------------------------"
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p udp -m udp --dport 4500 -j ACCEPT
iptables -A INPUT -p udp -m udp --dport 500 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p udp -m udp --dport 60000:60100 -j ACCEPT
iptables -P INPUT DROP
iptables -t nat -A POSTROUTING -s 10.11.12.0/24 -o eth0 -j MASQUERADE
iptables-save
echo "...done!"
# Tune kernel
echo "Tuning kernel parameters..."
echo "------------------------------------"
echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.all.accept_redirects = 0" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.all.send_redirects = 0" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.default.rp_filter = 0" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.default.accept_source_route = 0" | tee -a /etc/sysctl.conf
echo "net.ipv4.conf.default.send_redirects = 0" | tee -a /etc/sysctl.conf
echo "net.ipv4.icmp_ignore_bogus_error_responses = 1" | tee -a /etc/sysctl.conf
for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
sysctl -p
echo "...done!"
echo "Writing to rc.local..."
echo "------------------------------------"
sed -i '/^exit.*/i for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done' /etc/rc.local
echo "...done!"
#Setup ipsec
echo "Setting ipsec up..."
echo "------------------------------------"
sed -i 's,leftid=domain.*,'"$LEFT_ID"',g' ipsec.conf
echo "...done!"
#Save generated credentials
echo "Saving credentials to ipsec.secrets..."
echo $CREDENTIALS >> ipsec.secrets
echo "...done!"
echo "Copying ipsec files to etc..."
echo "------------------------------------"
cp ipsec.secrets /etc/
cp ipsec.conf /etc/
echo "...done!"
# Enable strongswan & restart ipsec
echo "Enabling strongswan..."
echo "------------------------------------"
systemctl enable strongswan
echo "...done!"
echo "Restarting ipsec..."
echo "------------------------------------"
ipsec restart
echo "...done!"
echo "Setting up crontab..."
echo "------------------------------------"
echo "$(echo "40 1 * * 1 iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT && letsencrypt renew && cp /etc/letsencrypt/live/$DOMAIN_NAME/fullchain.pem /etc/ipsec.d/certs && cp /etc/letsencrypt/live/$DOMAIN_NAME/privkey.pem /etc/ipsec.d/private && ipsec restart && iptables -D INPUT -p tcp --dport 443 -j ACCEPT" ; crontab -l)" | crontab -
echo "...done!"
echo "Finished!"