-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtunnel-manager.sh
More file actions
executable file
·113 lines (101 loc) · 3.24 KB
/
tunnel-manager.sh
File metadata and controls
executable file
·113 lines (101 loc) · 3.24 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
#!/bin/sh
vscodeCliBinFile="vscode_cli_alpine_x64_cli.tar.gz"
vscodeCliFile="code"
vscodeCliPath="/usr/bin"
vscodeLogFile="vscode-server.log"
downloadUrl="https://update.code.visualstudio.com/latest/cli-alpine-x64/stable"
action=$1
tunnelName=$2
# functions
log() {
logfile=$vscodeLogFile
timestamp=$(date +"%Y-%m-%d %T")
echo "$timestamp: $1" >>$logfile
}
killProcessByName() {
processName=$1
pids=$(ps -ef | grep $processName | grep -v grep | awk '{print $2}')
for pid in $pids; do
kill -9 $pid
log "Process $processName with PID $pid was killed."
done
if [ -z "$pids" ]; then
log "No instances of process $processName were found."
fi
}
downloadLatestVersion() {
log "Downloading latest version from $downloadUrl"
if command -v curl >/dev/null 2>&1; then
curl -L $downloadUrl -o $vscodeCliBinFile
elif command -v wget >/dev/null 2>&1; then
wget -O $vscodeCliBinFile $downloadUrl
else
log "Neither curl nor wget is installed. Please install one of them."
exit 1
fi
if [ $? -ne 0 ]; then
log "Download failed"
exit 1
fi
log "Download completed successfully"
}
startTunnel() {
local tunnelName=$1
# check and download latest version
if [ ! -f "$vscodeCliBinFile" ] || [ -n "$3" ] && [ "$3" = "--update" ]; then
downloadLatestVersion
fi
# verify downloaded file exists
if [ ! -f "$vscodeCliBinFile" ]; then
log "$vscodeCliBinFile not found, exit now"
exit 1
fi
# extract binary
if [ ! -f "$vscodeCliPath/$vscodeCliFile" ] || [ -n "$3" ] && [ "$3" = "--update" ]; then
log "Extracting $vscodeCliBinFile to $vscodeCliPath"
sudo tar zxf $vscodeCliBinFile -C $vscodeCliPath
if [ $? -ne 0 ]; then
log "Extraction failed"
exit 1
fi
sudo chmod +x $vscodeCliPath/$vscodeCliFile
log "Extraction completed successfully"
fi
# check tunnel name
if [ -z "$tunnelName" ]; then
tunnelName="tunnel-$(cat /proc/sys/kernel/random/uuid | md5sum | cut -c 1-8)"
log "Tunnel name not specified, using random name: $tunnelName"
fi
# start tunnel
log "Starting tunnel with name: $tunnelName"
nohup $vscodeCliPath/$vscodeCliFile tunnel --name $tunnelName --accept-server-license-terms 2>&1 | while read line; do log "$line"; done &
log "Tunnel service started"
}
stopTunnel() {
log "Stopping VS Code tunnel service"
killProcessByName $vscodeCliFile
log "VS Code tunnel service stopped"
}
showUsage() {
echo "Usage:"
echo " $0 start [tunnel-name] [--update] - Start tunnel service with optional name and update flag"
echo " $0 stop - Stop tunnel service"
echo "Examples:"
echo " $0 start - Start with random tunnel name"
echo " $0 start my-tunnel - Start with specific tunnel name"
echo " $0 start my-tunnel --update - Start with specific name and force update"
echo " $0 stop - Stop all running tunnels"
}
# Main logic
case "$action" in
"start")
startTunnel "$tunnelName" "$3"
;;
"stop")
stopTunnel
;;
*)
showUsage
exit 1
;;
esac