-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn.sh
executable file
·153 lines (136 loc) · 4.67 KB
/
vpn.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
#!/bin/bash
set -e
show_help () {
cat << EOF
Usage for$(basename "${0}")
Global commands
$(basename "${0}") [ls|upgrade]
ls List active VPN
upgrade Pulls the :latest for all docker images and eliminates the dangling ones
$(basename "${0}") all stop
Will stop all active VPN containers
VPN instance commands
$(basename "${0}") [-u] <vpn name> [start|stop|restart|shell|exec <command>|log]
-u Will call for pulling the latest version of the container
start|stop|restart
Will start, stop, or restart the VPN container
shell Connects to the shell inside the VPN's container
exec <command>
Excecutes the <command> inside the VPN's container
log Shows the output of the main process runnin in the container
VPN docker machine commands
$(basename "${0}") vm [start|stop|restart|shell|status]
start|stop|restart
Will start, stop, or restart the VPN container
shell Connects to the shell inside the VPN's container
status Shows the status of the vagrant machine hosting the docker machine
EOF
}
params="$(getopt -o ":hu" -- "$@")"
eval set -- "$params"
while [ "$#" -gt 0 ]; do
case "$1" in
-u) update="1"; shift; restart_args="-u" ;;
-h) show_help; exit ;;
--) shift; break ;;
*) ;;
esac
done
# must stay before the .settings is sourced
name="${1:?vpn name}"
shift
VPN_BASE="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
# shellcheck source=/dev/null
[ -f "${VPN_BASE}/.settings" ] && . "${VPN_BASE}/.settings"
# global commands
case "${name}" in
ls) docker container ls -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"; exit ;;
upgrade)
docker images --format "{{.Repository}}" -f "reference=*/*latest" | xargs -n1 docker pull
dangling="$(docker images -f "dangling=true" -q)"
if [ -n "${dangling}" ]; then
echo "${dangling}" | xargs docker rmi
fi
exit
;;
esac
op="${1:-start}"
shift || true
if [ "${name}" = "vm" ]; then
case "${op}" in
restart) cmd="reload" ;;
start) cmd="up" ;;
stop) cmd="halt" ;;
shell) cmd="ssh" ;;
status) cmd="status" ;;
*)
echo "Usage: $0 vm {start|stop|shell|status|reload|up|halt|ssh|ls}"
exit 1
;;
esac
cd "${VPN_BASE}" && vagrant "${cmd}"
exit
fi
VPN_HOME="${VPN_BASE}/conf/${name}"
VPN_MOUNT="${VPN_MOUNT:-$VPN_HOME}"
PROXY_PORT="${PROXY_PORT:-8443}"
PROXYPAC_PORT="${PROXYPAC_PORT:-8088}"
PROXY_BIND="${PROXY_BIND:-127.0.0.1}"
PROXY_ENDPOINT="${PROXY_ENDPOINT:-${PROXY_BIND}:${PROXY_PORT}}"
if [ "${name}" != "all" ] && [ ! -d "${VPN_HOME}" ]; then
echo "Unknown VPN ${name}"
exit 1
fi
running="$(docker container ls -f "name=${name}" -q | wc -l)"
case "${op}" in
restart) $0 "${name}" stop || true; $0 "${name}" start ${restart_args} ;;
start)
if [ "${running}" -ne 0 ]; then
echo "VPN ${name} already running"
exit 2
fi
ensure_proxy=1
d_args=( run --rm -it "--cap-add=NET_ADMIN" -v "${VPN_MOUNT}:/conf" --name "${name}" --hostname "${name}" --network vpn "$@" )
[ "${update}" = "1" ] && d_args+=( "--pull=always" )
# shellcheck source=/dev/null
[ -f "${VPN_HOME}/.container_args" ] && . "${VPN_HOME}/.container_args"
if [ "${ensure_proxy}" = "1" ] && [ "$(docker container ls -f 'name=proxy' -q | wc -l)" = "0" ]; then
echo "proxy not running run ${0} proxy"
fi
docker network inspect vpn >/dev/null 2>&1 || docker network create --driver bridge --subnet 192.168.253.0/24 --gateway 192.168.253.1 vpn
docker "${d_args[@]}"
;;
stop)
if [ "${name}" = "all" ]; then
docker container ls -a --format "table {{.Names}}\t{{.Image}}" | grep asharlohmar/glider | awk '{print $1}' | xargs docker stop -t1
else
if [ "${running}" = "0" ]; then
echo "VPN ${name} is not running"
exit 2
else
docker stop -t1 "${name}"
fi
fi
;;
log)
if [ "${running}" = "0" ]; then
echo "VPN ${name} is not running"
exit 2
fi
docker logs -f --since 10m "${name}"
;;
shell)
if [ "${running}" = "0" ]; then
echo "VPN ${name} is not running"
exit 2
fi
docker exec -it "${name}" /bin/sh -l
;;
exec)
docker exec -it "${name}" "${@}"
;;
*)
echo "Usage: ${0} {start|stop|log|shell|ls}"
exit 1
;;
esac