-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc.sh
300 lines (267 loc) · 10.5 KB
/
oc.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
# Version : 2.0.2
# Build : Passing
# Author : Amir Masoud Noohi
# Language : Bash Script
# Check whether the root user
if [[ $(id -u) != "0" ]]; then
printf "\e[42m\e[31mError: You must be root to run this install script.\e[0m\n"
exit 1
fi
# Check whether CentOS 7 or RHEL 7 is detected
if [[ $(grep "release 7." /etc/redhat-release 2>/dev/null | wc -l) -eq 0 ]]; then
printf "\e[42m\e[31mError: Your OS is NOT CentOS 7 or RHEL 7.\e[0m\n"
printf "\e[42m\e[31mThis install script is ONLY for CentOS 7 and RHEL 7.\e[0m\n"
exit 1
fi
basepath=$(dirname $0)
cd ${basepath}
function ConfigEnvironmentVariable {
# Variable settings
# Single IP maximum number of connections, the default is 2
maxsameclients=4
# The maximum number of connections, the default is 16
maxclients=1024
# Server certificate and key file, placed in the same directory with the script, the key file permissions should be 600 or 400
servercert=${1-server-cert.pem}
serverkey=${2-server-key.pem}
# VPN Intranet IP segment
vpnnetwork="172.16.24.0/24"
# DNS
dns1="8.8.8.8"
dns2="8.8.4.4"
# Configuration directory
confdir="/etc/ocserv"
# Obtain the network card interface name
systemctl start NetworkManager.service
ethlist=$(nmcli --nocheck d | grep -v -E "(^(DEVICE|lo)|unavailable|^[^e])" | awk '{print $1}')
eth=$(printf "${ethlist}\n" | head -n 1)
if [[ $(printf "${ethlist}\n" | wc -l) -gt 1 ]]; then
echo ======================================
echo "Network Interface list:"
printf "\e[33m${ethlist}\e[0m\n"
echo ======================================
echo "Which network interface you want to listen for ocserv?"
printf "Default network interface is \e[33m${eth}\e[0m, let it blank to use this network interface: "
read ethtmp
if [[ -n "${ethtmp}" ]]; then
eth=${ethtmp}
fi
fi
# Port, the default is 443
port=443
echo -e "\nPlease input the port ocserv listen to."
printf "Default port is \e[33m${port}\e[0m, let it blank to use this port: "
read porttmp
if [[ -n "${porttmp}" ]]; then
port=${porttmp}
fi
# User name, default is user
username=user
echo -e "\nPlease input ocserv user name."
printf "Default user name is \e[33m${username}\e[0m, let it blank to use this user name: "
read usernametmp
if [[ -n "${usernametmp}" ]]; then
username=${usernametmp}
fi
# random code
randstr() {
index=0
str=""
for i in {a..z}; do arr[index]=$i; index=$(expr ${index} + 1); done
for i in {A..Z}; do arr[index]=$i; index=$(expr ${index} + 1); done
for i in {0..9}; do arr[index]=$i; index=$(expr ${index} + 1); done
for i in {1..10}; do str="$str${arr[$RANDOM%$index]}"; done
echo ${str}
}
password=$(randstr)
printf "\nPlease input \e[33m${username}\e[0m's password.\n"
printf "Random password is \e[33m${password}\e[0m, let it blank to use this password: "
read passwordtmp
if [[ -n "${passwordtmp}" ]]; then
password=${passwordtmp}
fi
}
function PrintEnvironmentVariable {
# Print the configuration parameters
clear
ipv4=$(ip -4 -f inet addr show ${eth} | grep 'inet' | sed 's/.*inet \([0-9\.]\+\).*/\1/')
ipv6=$(ip -6 -f inet6 addr show ${eth} | grep -v -P "(::1\/128|fe80)" | grep -o -P "([a-z\d]+:[a-z\d:]+)")
echo -e "IPv4:\t\t\e[34m$(echo ${ipv4})\e[0m"
if [ ! "$ipv6" = "" ]; then
echo -e "IPv6:\t\t\e[34m$(echo ${ipv6})\e[0m"
fi
echo -e "Port:\t\t\e[34m${port}\e[0m"
echo -e "Username:\t\e[34m${username}\e[0m"
echo -e "Password:\t\e[34m${password}\e[0m"
echo
echo "Press any key to start install ocserv."
get_char() {
SAVEDSTTY=$(stty -g)
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty ${SAVEDSTTY}
}
char=$(get_char)
clear
}
function InstallOcserv {
# Upgrading the system
#yum update -y -q
# Install epel-release
if [ $(grep epel /etc/yum.repos.d/*.repo | wc -l) -eq 0 ]; then
yum install -y -q epel-release && yum clean all && yum makecache fast
fi
# Install ocserv
yum install -y ocserv
}
function ConfigOcserv {
# Detects whether there is a certificate and a key file
if [[ ! -f "${servercert}" ]] || [[ ! -f "${serverkey}" ]]; then
# Create a ca certificate and a server certificate (refer to http://www.infradead.org/ocserv/manual.html#heading5)
certtool --generate-privkey --outfile ca-key.pem
cat << _EOF_ >ca.tmpl
cn = "AMIRMNOOHI"
organization = "AMIRMNOOHI"
serial = 1
expiration_days = 3650
ca
signing_key
cert_signing_key
crl_signing_key
_EOF_
certtool --generate-self-signed --load-privkey ca-key.pem \
--template ca.tmpl --outfile ca-cert.pem
certtool --generate-privkey --outfile ${serverkey}
cat << _EOF_ >server.tmpl
cn = "IP"
organization = "AMIRMNOOHI"
serial = 2
expiration_days = 3650
signing_key
encryption_key #only if the generated key is an RSA one
tls_www_server
_EOF_
certtool --generate-certificate --load-privkey ${serverkey} \
--load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem \
--template server.tmpl --outfile ${servercert}
fi
# Copy the certificate
cp "${servercert}" /etc/pki/ocserv/public/server.crt
cp "${serverkey}" /etc/pki/ocserv/private/server.key
# Edit the configuration file
(echo "${password}"; sleep 1; echo "${password}") | ocpasswd -c "${confdir}/ocpasswd" ${username}
sed -i 's@auth = "pam"@#auth = "pam"\nauth = "plain[passwd=/etc/ocserv/ocpasswd]"@g' "${confdir}/ocserv.conf"
sed -i "s/max-same-clients = 2/max-same-clients = ${maxsameclients}/g" "${confdir}/ocserv.conf"
sed -i "s/max-clients = 16/max-clients = ${maxclients}/g" "${confdir}/ocserv.conf"
sed -i "s/tcp-port = 443/tcp-port = ${port}/g" "${confdir}/ocserv.conf"
sed -i "s/udp-port = 443/udp-port = ${port}/g" "${confdir}/ocserv.conf"
sed -i 's/^ca-cert = /#ca-cert = /g' "${confdir}/ocserv.conf"
sed -i 's/^cert-user-oid = /#cert-user-oid = /g' "${confdir}/ocserv.conf"
sed -i "s/default-domain = example.com/#default-domain = example.com/g" "${confdir}/ocserv.conf"
sed -i "s@#ipv4-network = 192.168.1.0/24@ipv4-network = ${vpnnetwork}@g" "${confdir}/ocserv.conf"
sed -i "s/#dns = 192.168.1.2/dns = ${dns1}\ndns = ${dns2}/g" "${confdir}/ocserv.conf"
sed -i "s/cookie-timeout = 300/cookie-timeout = 86400/g" "${confdir}/ocserv.conf"
sed -i 's/user-profile = profile.xml/#user-profile = profile.xml/g' "${confdir}/ocserv.conf"
}
function ConfigFirewall {
firewalldisactive=$(systemctl is-active firewalld.service)
iptablesisactive=$(systemctl is-active iptables.service)
# Add a firewall permission list
if [[ ${firewalldisactive} = 'active' ]]; then
echo "Adding firewall ports."
firewall-cmd --permanent --add-port=${port}/tcp
firewall-cmd --permanent --add-port=${port}/udp
echo "Allow firewall to forward."
firewall-cmd --permanent --add-masquerade
echo "Reload firewall configure."
firewall-cmd --reload
elif [[ ${iptablesisactive} = 'active' ]]; then
iptables -I INPUT -p tcp --dport ${port} -j ACCEPT
iptables -I INPUT -p udp --dport ${port} -j ACCEPT
iptables -I FORWARD -s ${vpnnetwork} -j ACCEPT
iptables -I FORWARD -d ${vpnnetwork} -j ACCEPT
iptables -t nat -A POSTROUTING -s ${vpnnetwork} -o ${eth} -j MASQUERADE
#iptables -t nat -A POSTROUTING -j MASQUERADE
service iptables save
else
printf "\e[33mWARNING!!! Either firewalld or iptables is NOT Running! \e[0m\n"
fi
}
function Install-http-parser {
if [[ $(rpm -q http-parser | grep -c "http-parser-2.0") = 0 ]]; then
mkdir -p /tmp/http-parser-2.0 /opt/lib
cd /tmp/http-parser-2.0
wget "http://mirrors.aliyun.com/epel/7/x86_64/h/http-parser-2.0-5.20121128gitcd01361.el7.x86_64.rpm"
rpm2cpio http-parser-2.0-5.20121128gitcd01361.el7.x86_64.rpm | cpio -div
mv usr/lib64/libhttp_parser.so.2* /opt/lib
sed -i 'N;/Type=forking/a\Environment=LD_LIBRARY_PATH=/opt/lib' /lib/systemd/system/ocserv.service
sed -i 'N;/Type=forking/a\ExecStartPost=/bin/sleep 0.1' /lib/systemd/system/ocserv.service
systemctl daemon-reload
cd ~
rm -rf /tmp/http-parser-2.0
fi
}
function ConfigSystem {
#Disabled selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#Modify the system
echo "Enable IP forward."
sysctl -w net.ipv4.ip_forward=1
echo net.ipv4.ip_forward = 1 >> "/etc/sysctl.conf"
systemctl daemon-reload
echo "Enable ocserv service to start during bootup."
systemctl enable ocserv.service
#Start the ocserv service
systemctl start ocserv.service
echo
}
function PrintResult {
#Detects whether the firewall and the ocserv service are working properly
clear
printf "\e[36mChecking Firewall status...\e[0m\n"
iptables -L -n | grep --color=auto -E "(${port}|${vpnnetwork})"
line=$(iptables -L -n | grep -c -E "(${port}|${vpnnetwork})")
if [[ ${line} -ge 2 ]]
then
printf "\e[34mFirewall is Fine! \e[0m\n"
else
printf "\e[33mWARNING!!! Firewall is Something Wrong! \e[0m\n"
fi
echo
printf "\e[36mChenking ocserv service status...\e[0m\n"
netstat -anptu | grep ":${port}" | grep ocserv-main | grep --color=auto -E "(${port}|ocserv-main|tcp|udp)"
linetcp=$(netstat -anp | grep ":${port}" | grep ocserv | grep tcp | wc -l)
lineudp=$(netstat -anp | grep ":${port}" | grep ocserv | grep udp | wc -l)
if [[ ${linetcp} -ge 1 && ${lineudp} -ge 1 ]]
then
printf "\e[34mocserv service is Fine! \e[0m\n"
else
printf "\e[33mWARNING!!! ocserv service is NOT Running! \e[0m\n"
fi
#Print VPN parameters
printf "
if there are NO WARNING above, then you can connect to
your ocserv VPN Server with the user and password below:
======================================\n\n"
echo -e "IPv4:\t\t\e[34m$(echo ${ipv4})\e[0m"
if [ ! "$ipv6" = "" ]; then
echo -e "IPv6:\t\t\e[34m$(echo ${ipv6})\e[0m"
fi
echo -e "Port:\t\t\e[34m${port}\e[0m"
echo -e "Username:\t\e[34m${username}\e[0m"
echo -e "Password:\t\e[34m${password}\e[0m"
}
ConfigEnvironmentVariable $@
PrintEnvironmentVariable
InstallOcserv
ConfigOcserv
ConfigFirewall
#Install-http-parser
ConfigSystem
PrintResult
exit 0