-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch-vpn-client
executable file
·188 lines (169 loc) · 4.95 KB
/
launch-vpn-client
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
181
182
183
184
185
186
187
#! /bin/bash
IMAGE_PATH= # singularity image to run. set with --image
NAME_MAX_TRIES=10 # number of attempts to find a unique name for the instance --max-tries
SINGULARITY=singularity # assume singularity is in path, otherwise specify with --singularity
VPN_SERVER= # vpn server to connect to. set with --vpn-server
SERVERPIN= # sha256 pin of the vpn server
VPN_MODE=auto # auto or ns or socks
START_CLIENT=yes # should start client connection automatically
SOCKS_PORT=11080
SIN_HOME=/srv
show_help () {
echo "$0 [options] -- CMD ARG ARG ..."
echo "required options are:"
echo " --image IMAGEPATH Path to the singularity image to execute. (Required.)"
echo " --server SERVER VPN server to connect to. (Required.)"
echo " --servercert SERVERPIN Server pin, of the form: pin-sha256:XXXX...= (Required)"
echo " --user USER Username for VPN server login. (Required.)"
echo " --passwd PASSWD Password for VPN server login. (Required.)"
echo ""
echo "other options are:"
echo " --max-tries N Maximum number of attempts to find a valid instance name. (Default: $NAME_MAX_TRIES)"
echo " --vpn-mode auto|ns|socks ns: create virtually network interface"
echo " socks: use a socks5 server"
echo " auto: try ns and fallback to socks. (Default: auto)"
echo " --singularity PATH Path to the singularity executable. (Default: $SINGULARITY)"
echo " --socks-port PORT Port for the socks5 server (Default $SOCKS_PORT)"
echo " --start-client yes|no Whether to start client on startup. If no, it can"
echo " be started with: source /etc/cms-vpn/vpn-client-start.sh"
echo ""
echo " CMD ARG ...: Command and args to execute inside the container."
echo " Must be specified last after: --"
}
start_instance () {
while [[ "${NAME_MAX_TRIES}" -gt 0 ]]
do
NAME_MAX_TRIES=$((NAME_MAX_TRIES-1))
CANDIDATE=ins-${RANDOM}
if ${SINGULARITY} instance start --home $(pwd):${SIN_HOME} --bind $(pwd):$(pwd) ${IMAGE_PATH} ${CANDIDATE}
then
INSTANCE_NAME=${CANDIDATE}
return 0
fi
echo $CANDIDATE
done
echo "Could not find a unique name for the instance."
exit 1
}
cleanup () {
if [[ -n "${INSTANCE_NAME}" ]]
then
${SINGULARITY} instance stop ${INSTANCE_NAME}
fi
}
trap cleanup EXIT
# parse arguments
while [[ $# -gt 0 ]]
do
arg="$1"
case "${arg}" in
--image)
shift
IMAGE_PATH="$1"
;;
--max-tries)
shift
NAME_MAX_TRIES="$1"
;;
--singularity)
shift
SINGULARITY="$1"
;;
--vpn-mode)
shift
VPN_MODE="$1"
;;
--start-client)
shift
START_CLIENT="$1"
;;
--server)
shift
VPN_SERVER="$1"
;;
--servercert)
shift
SERVERPIN="$1"
;;
--user)
shift
VPN_USER="$1"
;;
--passwd)
shift
VPN_PASSWD="$1"
;;
--socks-port)
shift
SOCKS_PORT="$1"
;;
--help)
show_help
exit 0
;;
--)
# Rest is taken as the command to execute.
shift
break
;;
*)
echo "Unrecognized option: $arg"
show_help
exit 1
;;
esac
shift
done
missing_arg=no
if [[ -z "${IMAGE_PATH}" ]]
then
echo "Missing --image option."
missing_arg=yes
fi
if [[ -z "${VPN_SERVER}" ]]
then
echo "Missing --server option."
missing_arg=yes
fi
if [[ -z "${SERVERPIN}" ]]
then
echo "Missing --servercert option."
missing_arg=yes
fi
if [[ -z "${VPN_USER}" ]]
then
echo "Missing --user option."
missing_arg=yes
fi
if [[ -z "${VPN_PASSWD}" ]]
then
echo "Missing --passwd option."
missing_arg=yes
fi
if [[ "$missing_arg" = yes ]]
then
show_help
exit 1
fi
if [[ $# = 0 ]]
then
# use /bin/bash as the default command to execute
set /bin/bash
fi
start_instance
export VPN_SERVER
export SERVERPIN
export VPN_MODE
export SOCKS_PORT
export SIN_HOME
# Warning: Only for testing. To do: Find better way to communicate credentials to clients.
export VPN_USER
export VPN_PASSWD
if [[ "${START_CLIENT}" = yes ]]
then
${SINGULARITY} run instance://${INSTANCE_NAME} /etc/cms-vpn/vpn-client-start.sh "$@"
else
echo "Not starting vpn client as requested. Ignoring COMMAND."
echo "To activate vpn client use: /etc/cms-vpn/vpn-client-start.sh COMMAND"
${SINGULARITY} run instance://${INSTANCE_NAME} /bin/bash
fi