-
Notifications
You must be signed in to change notification settings - Fork 120
/
AppRun
executable file
·133 lines (112 loc) · 4.54 KB
/
AppRun
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
#!/bin/bash
# Copyright (C) 2020 HiddenVM <https://github.com/aforensics/HiddenVM>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
set -m # Enable background job control (for fg to work)
# This is the dir where we were launched from
LAUNCH_DIR=$(pwd)
# This is where we are within the AppImage mount
HERE="$(dirname "$(readlink -f "${0}")")"
while [ -n "${1}" ]; do
case "${1}" in
-version)
cat "${HERE}/HVM_VERSION"
exit
;;
*)
echo "Invalid option: ${1}"
echo "Available HiddenVM options:"
echo -e "\t-version\tPrints the HiddenVM version"
exit
;;
esac
shift
done
LOG_FILE="${LAUNCH_DIR}/HiddenVM.log"
# We need common functions and CLEARNET_VBOX_ENV_FILE
. "${HERE}/lib/common.sh"
enforce_amnesia
# Prevent running multiple instances of this script
CMD="$(basename "${0}")"
exec 9>"/var/lock/HiddenVM-${CMD}"
if ! flock -x -n 9; then
error "Another instance of HiddenVM is already running"
warn_box "HiddenVM" "HiddenVM is already running!"
exit 1
fi
log "Launch dir: ${LAUNCH_DIR}"
log "HERE: ${HERE}"
# If we have a local env file (relative to LAUNCH_DIR),
# save its path so we can pass it to launch-log-progress.sh
ENV_FILE="${LAUNCH_DIR}/HiddenVM/env"
ENV_FILE_ARG=
if [ -f "${ENV_FILE}" ]; then
log "Found local env file: ${ENV_FILE}"
ENV_FILE_ARG=${ENV_FILE}
else
log "Did not find local env file in ${LAUNCH_DIR}/HiddenVM/"
fi
# gnome-terminal uses a client-server architecture, so a server is reused and
# new windows are created, rather than separate processes. This doesn't work
# for us, because we need to make sure AppRun waits for launch-log-progress.sh
# to complete before terminating. Otherwise the AppImage will be unmounted
# before launch-log-progress.sh finishes. So the solution is to start a custom
# terminal server and open a client terminal on it.
SERVER_APP_ID="hidden.vm.id"
/usr/libexec/gnome-terminal-server --app-id "${SERVER_APP_ID}" &
# Try to launch a terminal on the server. We have 10s before the server goes
# away! The server may take some time to come up, so we have to retry on error.
# gnome-terminal always returns 0, so we need to check the output for Error.
while
gnome-terminal --app-id "${SERVER_APP_ID}" --working-directory "${HERE}" -- \
bash -c "./launch-log-progress.sh \"${ENV_FILE_ARG}\" \"${LOG_FILE}\"" 2>&1 | \
grep "Error creating terminal"
do
error "gnome-terminal-server '${SERVER_APP_ID}' is not ready, trying again..."
sleep 1
done
# Re-attach to the gnome-terminal server. The server
# will terminate when the terminal is closed)
fg
log "gnome-terminal was closed"
# If launch-log-progress.sh successfully installed the env file in
# CLEARNET_VBOX_LIB_HOME, source it to get the HVM_HOME
if [ -f "${CLEARNET_VBOX_ENV_FILE}" ]; then
. "${CLEARNET_VBOX_ENV_FILE}"
log "HVM_HOME is: ${HVM_HOME}"
# Make sure the logs dir exists in HVM_HOME
LOG_DIR="${HVM_HOME}/logs"
mkdir -p "${LOG_DIR}"
# If we're running from the HVM_HOME's parent dir (where the AppImage lives),
# delete old app images and move the log file.
HVM_HOME_PARENT_DIR="${HVM_HOME}/../"
if [ "${LAUNCH_DIR}" -ef "${HVM_HOME_PARENT_DIR}" ]; then
shopt -s extglob
VERSION_EXCLUSION="!(${HVM_VERSION})"
rm -f "${HVM_HOME_PARENT_DIR}"/HiddenVM-${VERSION_EXCLUSION}-*.AppImage
shopt -u extglob
log "Moving log file to ${LOG_DIR}"
mv "${LOG_FILE}" "${LOG_DIR}"
else
# Otherwise remove the old AppImage and copy the new AppImage + log file
rm -f "${HVM_HOME_PARENT_DIR}"/HiddenVM*.AppImage
APP_IMAGE_FILE="${LAUNCH_DIR}/HiddenVM-${HVM_VERSION}-x86_64.AppImage"
if [ -f "${APP_IMAGE_FILE}" ]; then
log "Copying AppImage file to ${HVM_HOME_PARENT_DIR}"
cp "${APP_IMAGE_FILE}" "${HVM_HOME_PARENT_DIR}"
fi
log "Copying log file to ${LOG_DIR}"
cp "${LOG_FILE}" "${LOG_DIR}"
fi
fi