-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua-vpn
executable file
·128 lines (94 loc) · 2.74 KB
/
ua-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
#!/bin/bash
# Exit on error
set -e
function _ {
[ -x "$(command -v $1)" ] || {
echo "Error: '$1' is not installed." >&2
exit 1
}
}
# You must have these binaries in your system.
_ docker
_ curl
_ systemd-resolve # likely to be installed
# ---------------
IMAGE_NAME="ua-snx-vpn"
function update {
echo ""
echo "Copying ua-vpn script to /usr/local/bin [with sudo].."
curl -Lks https://raw.githubusercontent.com/eupedrosa/ua-snx-vpn/master/ua-vpn > /tmp/ua-vpn
chmod +x /tmp/ua-vpn
sudo mv /tmp/ua-vpn /usr/local/bin
} # end install
function status {
container=$(docker ps -f 'name=ua-vpn' -f 'status=running' -q)
test -z "$container" && {
echo "Disconnected"
return
}
echo "VPN Container is running.."
test ! -d /sys/class/net/tunsnx && {
echo "The VPN tunnel does not exist, something is wrong.."
exit 1
}
rx_bytes=`cat /sys/class/net/tunsnx/statistics/rx_bytes`
rx_dropped=`cat /sys/class/net/tunsnx/statistics/rx_dropped`
tx_bytes=`cat /sys/class/net/tunsnx/statistics/tx_bytes`
tx_dropped=`cat /sys/class/net/tunsnx/statistics/tx_dropped`
echo -e "TX Bytes: $tx_bytes \tDropped: $tx_dropped"
echo -e "RX Bytes: $rx_bytes \tDropped: $rx_dropped"
}
function up {
container=$(docker ps -f 'name=ua-vpn' -f 'status=running' -q)
test ! -z "$container" && {
echo "VPN container already running.."
return
}
read -p 'UA username: ' VPN_USER
read -sp 'Password: ' VPN_PWD
echo ""
docker run --rm --name ua-vpn --network host --cap-add=ALL -v /lib/modules:/lib/modules \
-e VPN_USER=$VPN_USER -e VPN_PWD=$VPN_PWD -td $IMAGE_NAME > /dev/null
# Give it time to create the connection
sleep 2
# Make sure it is running
container=$(docker ps -f 'name=ua-vpn' -f 'status=running' -q)
test -z "$container" && {
echo "VPN connection failed.."
return
}
echo "VPN connected.."
echo "Setting DNS server [with sudo].."
# DNS servers 193.136.172.{20,21}
# DNS suffix ua.pt
sudo systemd-resolve -i tunsnx --set-dns=193.136.172.20 --set-dns=193.136.172.21 --set-domain=ua.pt
echo "Done"
} # end up
function down {
container=$(docker ps -f 'name=ua-vpn' -f 'status=running' -q)
test -z "$container" && {
echo "VPN container is not running.."
return
}
echo "Terminating VPN connection.."
docker exec ua-vpn snx -d > /dev/null
docker stop -t 1 ua-vpn > /dev/null
echo "Done"
} # end down
function usage {
echo "usage: `basename $0` [command]"
echo ""
echo "Commands"
echo " up Create the VPN connection."
echo " down Terminate ther VPN connection."
echo " update Update the UA's VPN docker image and this script."
echo " status Show connection status."
} # end status
[ $# -eq 0 ] && { usage; exit; }
case $1 in
up) up ;;
down) down ;;
update) update ;;
status) status ;;
*) usage ;;
esac