-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvpn-open
executable file
·84 lines (72 loc) · 1.88 KB
/
vpn-open
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
#!/bin/bash
# variables
LOCAL_PROXY_PORT=61000
SOCKS5=false
# functions
usage() {
echo "Open a VPN connection to the given host gateway url.
Usage: vpn-open [options...] <url>
-u The VPN username
-p The VPN password
-s The server hostname or IP to open a proxy tunnel to
-P The server port to connect & create a proxy tunnel to
-L The local proxy port (default: 61000)
-5 Use a SOCKS5 proxy instead of a proxy port tunnel"
exit 0;
}
while getopts ":u:p:s:P:L:5h" arg; do
case $arg in
u)
USERNAME=${OPTARG}
;;
p)
PASSWORD=${OPTARG}
;;
s)
SERVER=${OPTARG}
;;
P)
SERVER_PORT=${OPTARG}
;;
L)
LOCAL_PROXY_PORT=${OPTARG}
;;
5)
SOCKS5=true
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
HOST=$1
if [[ -z ${HOST} ]]; then
echo "Failure: no VPN gateway url provided"
exit 1
fi
if [[ -z ${USERNAME} ]]; then
echo "Failure: no VPN username provided"
exit 1
fi
if [[ -z ${PASSWORD} ]]; then
echo "Failure: no VPN password provided"
exit 1
fi
if [[ ${SOCKS5} = false && -z ${SERVER} ]]; then
echo "Failure: no server hostname/ip provided"
exit 1
fi
if [[ ${SOCKS5} = false && -z ${SERVER_PORT} ]]; then
echo "Failure: no server port provided"
exit 1
fi
if [[ ${SOCKS5} = true ]]; then
echo "Creating SOCKS5 proxy"
bash -c "echo -n ${PASSWORD} | openconnect -u${USERNAME} --passwd-on-stdin --script-tun --script 'ocproxy -k 2 -D${LOCAL_PROXY_PORT}' ${HOST}" &
else
echo "Creating proxy port tunnel"
bash -c "echo -n ${PASSWORD} | openconnect -u${USERNAME} --passwd-on-stdin --script-tun --script 'ocproxy -k 2 -L${LOCAL_PROXY_PORT}:${SERVER}:${SERVER_PORT}' ${HOST}" &
fi
sleep 5
echo "Opened proxy port on :${LOCAL_PROXY_PORT}"