-
Notifications
You must be signed in to change notification settings - Fork 0
/
startVpn.sh
executable file
·185 lines (150 loc) · 4.21 KB
/
startVpn.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
#!/usr/bin/env bash
# Author: Tim Neumann
# License: Mozilla Public License
# Project Page: https://github.com/c-hack/openvpnClientScripts
dir="$(dirname "$(realpath "$0")")"
function printHelp {
echo "Usage:"
echo ""
echo "$0 [options] <server name>"
echo ""
echo "Options:"
echo "-h --help print this help"
echo "-n --not invert next option. Only works for options below"
echo "-d --default-route add default route through vpn"
echo "-t --tmux use tmux"
echo ""
echo "Use the -n flag to override settings from the config"
echo ""
echo "Config:"
echo "You can configure some of these options in the file startVpn.conf"
echo "The following options are supported: default-route, tmux"
}
if ! which "xargs" > /dev/null 2>&1 ;then
echo "Need xargs"
fi
args=($@)
if [ $# -lt 1 ] ;then
printHelp
exit
fi
serverName="${@: -1}"
if [ "$serverName" == "-h" ] ;then
printHelp
exit
elif [[ "$serverName" == "-"* ]] ;then
echo "Missing server name"
printHelp
exit
fi
defaultRoute=false
tmux=false
while read line ; do
option=$(echo "$line" | xargs)
if [[ "$option" == "#"* ]] || [ "$option" == "" ] ;then
: #Is a comment
elif [ "$option" == "default-route" ] ;then
defaultRoute=true
elif [ "$option" == "tmux" ] ;then
tmux=true
else
echo "Unknown config option: $line"; printHelp; exit
fi
done < "$dir/startVpn.conf"
invert=false
optionCount=`expr $# - 1`
counter=0
while [ $counter -lt $optionCount ] ;do
o="${args[$counter]}"
if [ "$o" == "-h" ] || [ "$o" == "--help" ] ;then printHelp; exit
elif [ "$o" == "-n" ] || [ "$o" == "--not" ] ;then if $invert ;then echo "Cannot invert $o" ;exit ;else invert=true ;fi
elif [ "$o" == "-d" ] || [ "$o" == "--default-route" ] ;then if $invert ;then defaultRoute=false ;invert=false ;else defaultRoute=true ;fi
elif [ "$o" == "-t" ] || [ "$o" == "--tmux" ] ;then if $invert ;then tmux=false ;else tmux=true ;invert=false ;fi
else echo "Unknown option: $o";printHelp; exit
fi
counter=`expr $counter + 1`
done
if $tmux && ! which "tmux" > /dev/null 2>&1 ;then
echo "Need tmux"
fi
if $tmux && ! which "sudo" > /dev/null 2>&1 ;then
echo "Need sudo"
fi
if $tmux ;then
if [ "$EUID" -eq 0 ]
then echo "No need to run as root when using tmux. Will use sudo inside the tmux session."
fi
sessionName="VPN-$serverName"
tmux="tmux -2"
if $tmux has-session -t $sessionName ;then
echo "Session $sessionName already exists. Attaching."
sleep 1
$tmux attach -t $sessionName
exit 0;
fi
#create new session with the name and detach from it for now
$tmux new-session -d -s $sessionName
$tmux send-keys "sudo $0 -n -t"
if $defaultRoute ;then
$tmux send-keys " -d"
else
$tmux send-keys " -n -d"
fi
$tmux send-keys " $serverName" Enter
$tmux attach -t $sessionName:0
exit
fi
### Start of internal logic
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
serverDir="$dir/servers/$serverName"
if ! [ -d "$serverDir" ] ;then
echo "Cannot find the directory for the server $serverName"
exit
fi
openVpnConfFile="$serverDir/client.ovpn"
if ! [ -e "$openVpnConfFile" ] ;then
echo "Cannot find the client config file servers/$serverName/client.ovpn"
exit
fi
startupHook="$serverDir/startup.sh"
shutdownHook="$serverDir/shutdown.sh"
if ! [ -e "$startupHook" ] ;then
echo "Cannot find the startupHook servers/$serverName/startup.sh"
exit
fi
if ! [ -e "$shutdownHook" ] ;then
echo "Cannot find the shutdownHook servers/$serverName/shutdown.sh"
exit
fi
cleanup() {
err=$?
echo "Cleaning stuff up..."
trap '' EXIT INT TERM
bash "$shutdownHook" "$defaultRoute"
exit $err
}
sig_cleanup() {
trap '' EXIT # some shells will call EXIT after the INT handler
false # sets $?
cleanup
}
PIPE="$dir/.pipe_$serverName"
function readAndWorkPipe {
while read line ;do
if [[ "$line" == *"Initialization Sequence Completed"* ]] ;then
echo "Getting address..."
bash "$1" "$2"
echo "Done getting address."
trap cleanup EXIT
trap sig_cleanup INT QUIT TERM
fi
done < $3
}
rm -f "$PIPE"
mkfifo "$PIPE"
cd "$serverDir"
readAndWorkPipe "$startupHook" "$defaultRoute" "$PIPE" &
openvpn "$openVpnConfFile" | tee "$PIPE"