This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn
executable file
·180 lines (158 loc) · 4.36 KB
/
vpn
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
#!/bin/bash
source helpers
source vpn.conf
startup() {
# get permission to sudo stuff
echo "Please enter your user password (Machine User)"
sudo echo "Thank you"
sudo chmod u+x "$BASEDIR/vpnc-split-traffic"
start_vpn
if [ $? -ne 0 ]; then
echo "Could not start VPN tunnel"
return $?
fi
sleep 5
setup_routes
if [ $? -ne 0 ]; then
echo "Could not setup routes"
return $?
fi
start_proxy
setup_mac_env
setup_env
}
shutdown() {
echo "Please enter your user password (Mac)"
sudo echo "Thank you"
reset_env
reset_mac_env
stop_proxy
remove_routes
stop_vpn
if [ ! -z "$POST_VPN_DOWN" ]; then
echo "Executing POST shutdown hook"
for ((i=0; i<${#POST_VPN_DOWN[@]}; ++i)); do
eval "sudo ${POST_VPN_DOWN[$i]}"
done
fi
}
setup_routes() {
# add route for remote proxy
sudo route add -host "$REMOTE_PROXY_HOST" -interface "$TUN_IF"
return $?
}
remove_routes() {
sudo route delete -host "$REMOTE_PROXY_HOST" -interface "$TUN_IF" >/dev/null 2>&1
return $?
}
start_vpn() {
echo "Digging VPN tunnel"
sudo openconnect "$VPN_HOST" \
--useragent='Cisco AnyConnect VPN Agent for Windows 3.1.04072' \
--local-hostname=FooBars-PC \
--os=win \
--user="$VPN_USER" \
--authgroup="$VPN_AUTH_GROUP"∏\
--script="$BASEDIR/vpnc-split-traffic" \
--pid-file="$OPENCONNECT_PIDFILE" \
--printcookie \
-b \
$VPN_EXTRA
return $?
}
stop_vpn() {
echo "Collapsing VPN tunnel"
sudo pkill -F "$OPENCONNECT_PIDFILE"
# echo Waiting for openconnect to terminate
# anywait $(cat $OPENCONNECT_PIDFILE)
}
start_proxy() {
echo "Generating Proxy Configuration"
URL_USR=$(urlencode "$REMOTE_PROXY_USERNAME")
URL_PWD=$(urlencode "$REMOTE_PROXY_PASSWORD")
SEDFILE=$(mktemp -t 'sed.XXXXXXXXXX')
{
echo "s~\${PROXY_PORT}~$PROXY_PORT~g"
echo "s~\${PROXY_PIDFILE}~$PROXY_PIDFILE~g"
echo "s~\${REMOTE_PROXY_DOMAINS}~$REMOTE_PROXY_DOMAINS~g"
echo "s~\${REMOTE_PROXY_EXCLUDE}~$REMOTE_PROXY_EXCLUDE~g"
echo "s~\${REMOTE_PROXY_HOST}~$REMOTE_PROXY_HOST~g"
echo "s~\${REMOTE_PROXY_HOST}~$REMOTE_PROXY_HOST~g"
echo "s~\${REMOTE_PROXY_PORT}~$REMOTE_PROXY_PORT~g"
echo "s~\${REMOTE_PROXY_USERNAME}~$URL_USR~g"
echo "s~\${REMOTE_PROXY_PASSWORD}~$URL_PWD~g"
echo "s~\${BASEDIR}~$BASEDIR~g"
} >> "$SEDFILE"
sed -f "$SEDFILE" "$BASEDIR/squid.template.conf" > "$BASEDIR/squid.conf"
rm "$SEDFILE"
echo "Creating folders for squid logs & cache"
mkdir -p "$BASEDIR/squid/logs"
mkdir -p "$BASEDIR/squid/cache"
echo "Starting Proxy"
/usr/local/sbin/squid -f "$BASEDIR/squid.conf"
}
stop_proxy() {
echo "Stopping Proxy"
/usr/local/sbin/squid -f "$BASEDIR/squid.conf" -k shutdown 2> /dev/null
echo Waiting for proxy to terminate
anywait "$(cat "$PROXY_PIDFILE")"
}
setup_env() {
PROXY="$PROXY_HOST:$PROXY_PORT"
printf "######################################################\n"
printf "# Copy these lines into the terminal you wish to use #\n"
printf "######################################################\n"
echo export https_proxy="http://$PROXY"
echo export http_proxy="http://$PROXY"
echo export HTTPS_PROXY="http://$PROXY"
echo export HTTP_PROXY="http://$PROXY"
printf "######################################################\n"
}
reset_env() {
printf "######################################################\n"
printf "# Copy these lines into the terminal you wish to use #\n"
printf "######################################################\n"
echo unset https_proxy
echo unset http_proxy
echo unset HTTPS_PROXY
echo unset HTTP_PROXY
printf "######################################################\n"
}
setup_mac_env() {
echo "Configuring Mac OS network settings"
SERVICE="$(mac_find_active_service)"
sudo networksetup -setwebproxy "$SERVICE" "$PROXY_HOST" "$PROXY_PORT"
sudo networksetup -setwebproxystate "$SERVICE" on
sudo networksetup -setsecurewebproxy "$SERVICE" "$PROXY_HOST" "$PROXY_PORT"
sudo networksetup -setsecurewebproxystate "$SERVICE" on
}
reset_mac_env() {
echo "Restoring Mac OS network settings"
SERVICE="$(mac_find_active_service)"
echo "Disableing Proxy for $SERVICE"
sudo networksetup -setwebproxystate "$SERVICE" off
echo "Disableing Secure Proxy for $SERVICE"
sudo networksetup -setsecurewebproxystate "$SERVICE" off
}
if [ -n "$1" ]; then
case "$1" in
start)
startup
;;
stop)
shutdown
;;
re)
remove_routes
stop_vpn
start_vpn
setup_routes
;;
encode)
urlencode "$2"
;;
*)
echo "usage: $0 <start|stop>"
;;
esac
fi