-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcloak-vnc.sh
More file actions
303 lines (257 loc) · 5.85 KB
/
cloak-vnc.sh
File metadata and controls
303 lines (257 loc) · 5.85 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
#
# remot3.it, Inc. : https://remote.it
#
# This script has the following format: script.sh $1 $2 $3 where
#
# $1 = jobid
# $2 = api server url
# $3 = short code
#
# r3_header, -l (Mebst|Choose Action|Cloak Service|Uncloak Service)
#### Settings #####
VERSION=0.0.9
MODIFIED="May 19, 2020"
#
# Config
#
TOOL_DIR="/usr/bin"
DEBUG_DIR="/tmp"
NOTIFIER="connectd_task_notify"
# turn on debug, set to 1 and will write to DEBUG_DIR/scriptname.log
DEBUG_ON=1
DEBUG=0
APIKEY="remote.it.developertoolsHW9iHnd"
# set -x
#
# API for short code
#
apiMethod="https://"
apiURI="/tiny/lookup/"
##### End Settings #####
########################################
# Support Functions #
########################################
#----------------------------------------------------------
# JSON parse (very simplistic): get value frome key $2 in buffer $1, values or keys must not have the characters {}[",
# and the key must not have : in it
#
# Example:
# value=$(jsonval "$json_buffer" "$key")
#
jsonval()
{
temp=$(echo "$1" | sed -e 's/[{}\"]//g' | sed -e 's/,/\n/g' | grep -w ${GREPFLAGS} "$2" | cut -d"[" -f2- | cut -d":" -f2-)
echo "${temp##*|}"
}
#
# Print Usage
#
usage()
{
echo "Usage: $0 <jobid> <api server> <shortcode>" >&2
echo "Version $VERSION Build $MODIFIED" >&2
return 0
}
#
# log if DEBUG_ON is set to /tmp
#
log()
{
if [ $DEBUG_ON -gt 0 ]; then
ts=$(date)
echo "$ts $1" >> ${DEBUG_DIR}/$(basename $0).log
fi
}
#
# Job Complete
#
Job_Complete()
{
ret=$(${TOOL_DIR}/$NOTIFIER 1 $jobid $api_server "Job Complete")
}
#
# Job Failed
#
Job_Failed()
{
ret=$(${TOOL_DIR}/$NOTIFIER 2 $jobid $api_server "Job Failed")
}
#
# Status column A
#
Status_A()
{
ret=$(${TOOL_DIR}/$NOTIFIER a $jobid $api_server "$1")
}
#
# Status column B
#
Status_B()
{
ret=$(${TOOL_DIR}/$NOTIFIER b $jobid $api_server "$1")
}
#
# Status column C
#
Status_C()
{
ret=$(${TOOL_DIR}/$NOTIFIER c $jobid $api_server "$1")
}
#
# Status column D
#
Status_D()
{
ret=$(${TOOL_DIR}/$NOTIFIER d $jobid $api_server "$1")
}
#
# translate short code
#
translate()
{
ret=0
# make api call
translate_url=${apiMethod}${api_server}${apiURI}${short_code}
log "Translate URL call using URL $translate_url"
resp=$(curl -s -S -X GET -H "content-type:application/json" -H "apikey:WeavedDeveloperToolsWy98ayxR" "$translate_url")
status=$(jsonval "$resp" "status")
log "return status $status"
if [ "$status" == "true" ]; then
#jsonvalx()
item=$(jsonval "$resp" "item")
#
# Convert from base64
decode=$(echo "$item" | base64 --decode)
log "item $item -> $decode"
echo -n "$decode"
else
printf "Fail"
ret=1
fi
return $ret
}
########################################
# Put Custom Support Functions Here #
########################################
cloakSSH()
{
sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 127.0.0.1/' /etc/ssh/sshd_config
listen=$(grep "ListenAddress" /etc/ssh/sshd_config)
systemctl restart sshd
log "remote.it cloak SSH $listen"
}
uncloakSSH()
{
sed -i 's/ListenAddress 127.0.0.1/#ListenAddress 0.0.0.0/' /etc/ssh/sshd_config
listen=$(grep "ListenAddress" /etc/ssh/sshd_config)
systemctl restart sshd
log "remote.it uncloak SSH $listen"
}
cloakVNC()
{
echo "localhost=1" >> /root/.vnc/config.d/vncserver-x11
systemctl restart vncserver-x11-serviced.service
log "remote.it cloak VNC 127.0.0.1"
}
uncloakVNC()
{
sed -i -e '/localhost=1/d' /root/.vnc/config.d/vncserver-x11
systemctl restart vncserver-x11-serviced.service
log "remote.it uncloak VNC 0.0.0.0"
}
########################################
# Main Customer Script Starts Here #
########################################
customer_main()
{
################################################
# parse the flag options (and their arguments) #
################################################
while getopts lvhmcr OPT; do
case "$OPT" in
v)
DEBUG_ON=$((DEBUG_ON+1)) ;;
h | [?])
# got invalid option
usage
exit 1
;;
esac
done
}
###############################
# Main program starts here #
###############################
#
# Must have 3 parameters
#
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
usage
exit 1
fi
jobid=$1
shift
api_server=$1
shift
short_code=$1
#
if [ $DEBUG_ON -gt 0 ]; then
echo "$0 called with jobid $jobid api_server $api_server and shortcode $short_code" >> $0.log
fi
command=$(translate $short_code)
if [ "$?" -gt 0 ]; then
echo "[Fail] translate short code $short_code failed"
exit 1
fi
if [ $DEBUG_ON -gt 0 ]; then
echo "Translated short code to $command" >> $0.log
fi
#
# Must use eval to correctly expand command
#
eval set -- ${command}
# keep track of first and second text parameter
parameter=0
################################################
# parse the flag options (and their arguments) #
################################################
while getopts l: OPT; do
case "$OPT" in
l)
# name of web package as shown above in r3-header
echo "-l"
echo "(l) $OPTARG"
if [ "$parameter" -eq 0 ]; then
action=$(echo "$OPTARG")
parameter=1
elif [ "$parameter" -eq 1 ]; then
server=$(echo "$OPTARG")
fi
;;
*)
echo "$OPT"
;;
esac
done
echo "Case done!"
# hardwire server to VNC for the moment
server="VNC"
set -x
Status_C "$action"
Status_D "$server"
if [ "$action" == "Cloak Service" ]; then
if [ "$server" == "SSH" ]; then
cloakSSH
elif [ "$server" == "VNC" ]; then
cloakVNC
fi
elif [ "$action" == "Uncloak Service" ]; then
if [ "$server" == "SSH" ]; then
uncloakSSH
elif [ "$server" == "VNC" ]; then
uncloakVNC
fi
fi
#-------------------------------------------------
Job_Complete