-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovpn-generate
executable file
·119 lines (98 loc) · 2.51 KB
/
ovpn-generate
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
#!/bin/bash
# write a new line
nl() {
echo $'\n'
}
writeMessage() {
echo "==== $1"
}
writeOkMessage() {
# Bash colors - http://misc.flogisoft.com/bash/tip_colors_and_formatting
echo -e "\e[32m[ok]\e[0m $1"
}
writeFailMessage() {
echo -e "\e[31m[fail]\e[0m $1"
exit 1
}
# message with green background
writeSuccessMessage() {
echo -e "\e[30;42;5;82m $1 \e[0m"
}
openvpnDir=/etc/openvpn
scriptsDir=$openvpnDir/easy-rsa
keysDir=$scriptsDir/keys
ovpnDownloadDir=/var/www/ovpn
profileName=''
while [ -z $profileName ]; do
read -p "Enter profile name: " profileName
done
caCertName=ca.crt
clientKeyName="${profileName}.key"
clientCertName="${profileName}.crt"
clientExampleProfile=client_example.ovpn
clientProfile="${profileName}.ovpn"
if [ ! -f $keysDir/$clientExampleProfile ];
then
serverIP=''
while [ -z $serverIP ]; do
read -p "Enter server hostname or IP: " serverIP
done
cat > $keysDir/$clientExampleProfile << END
client
;dev tap
dev tun
;dev-node MyTap
;proto tcp
proto udp
remote $serverIP 1194
;remote my-server-2 1194
;remote-random
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
;http-proxy-retry
;http-proxy [proxy server] [proxy port #]
;mute-replay-warnings
;ca ca.crt
;cert client.crt
;key client.key
ns-cert-type server
;tls-auth ta.key 1
;cipher x
comp-lzo
verb 3
;mute 20
END
fi
if [ ! -f $keysDir/$clientProfile ];
then
source $scriptsDir/vars
$scriptsDir/build-key $profileName
cp $keysDir/$clientExampleProfile $keysDir/$clientProfile
nl
# Add the Certificate Authority
echo '<ca>' >> $keysDir/$clientProfile
cat $openvpnDir/$caCertName >> $keysDir/$clientProfile
echo '</ca>' >> $keysDir/$clientProfile
writeOkMessage "Certificate Authority has been added to client profile"
# Add the certificate
echo '<cert>' >> $keysDir/$clientProfile
cat $keysDir/$clientCertName >> $keysDir/$clientProfile
echo '</cert>' >> $keysDir/$clientProfile
writeOkMessage "Certificate has been added to client profile"
# Add the key
echo '<key>' >> $keysDir/$clientProfile
cat $keysDir/$clientKeyName >> $keysDir/$clientProfile
echo '</key>' >> $keysDir/$clientProfile
writeOkMessage "Key has been added to client profile"
cp $keysDir/$clientProfile $ovpnDownloadDir/$clientProfile
nl
writeMessage "File copied in ${ovpnDownloadDir} folder"
nl
writeSuccessMessage "Done."
else
writeFailMessage "Profile ${profileName} already created"
fi