-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvpnbook_wrapper
executable file
·105 lines (91 loc) · 2.58 KB
/
vpnbook_wrapper
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
#!/bin/sh
# Begin Functions
__read_property(){
input="$@"
if [ ! -f ${config_file} ]; then
echo "${config_file} does not exist... Aborting..."
exit 1
else
temp=`cat $config_file | sed 's/\"//g' | grep -w $input | cut -d ":" -f2`
echo ${temp##*|}
fi
}
__open_xterm_terminal(){
local l_server="$1"
local l_interactive="$2"
if [ ${l_interactive} = "1" ]; then
xterm -title "VPN Book" -hold -e "cd ${default_dir} && ./vpnbook -s ${l_server} -i"
else
xterm -title "VPN Book" -hold -e "cd ${default_dir} && ./vpnbook -s ${l_server}"
fi
}
__is_installed(){
echo "Checking if $1 is installed..."
if ! [ -x "$(command -v $1)" ]; then
echo "Error: $1 is not installed." >&2
exit 1
else
echo "$1 is installed." >&2
fi
}
__check_requirements(){
echo "Checking general Requirements..."
# Check if the terminal emulator is installed
__is_installed ${terminal_emulator}
}
__wait_for_seconds(){
local seconds="$@"
echo "waiting ${seconds} seconds before continuing..."
sleep ${seconds}
}
__check_if_up(){
__wait_for_seconds 10
local if_id="$@"
local if_result=$(ifconfig ${if_id} | grep "${if_id}: flags" | cut -d \: -f1)
if [ "${if_result}" == "${if_id}" ]; then
return 1
else
return 0
fi
}
# End Functions
default_dir=${HOME}/.local/scripts
config_file=${default_dir}/vpnbook.config
# Start Script
while [ "$1" != "" ]; do
case $1 in
-s | --server ) shift
server=$1
interactive=0
;;
-i | --interactive ) interactive=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# Variable Declaration
terminal_emulator=$(__read_property terminal_emulator)
vpnbook_if_name=$(__read_property vpnbook_if_name)
# Check if the 'tun1' interface is up and running
__check_if_up ${vpnbook_if_name}
# The Tunnel is established if the tun1 interface exists
if [ $? -eq 1 ]; then
echo "Nothing to do here. Vpn Tunnel already established."
exit 0
fi
# Check if the terminal emulator is installed
__is_installed ${terminal_emulator}
echo "Starting VPN connection to ${server} and interactive ${interactive}..."
case ${terminal_emulator} in
xterm | XTERM ) echo "Terminal emulator ${terminal_emulator} is supported."
__open_xterm_terminal ${server} ${interactive}
;;
* ) echo "Terminal emulator ${terminal_emulator} not supported."
exit 1
esac
exit 0