# shellcheck shell=sh
# guide.sh — launch the bundled offline HTML handbook.
guide__page() {
case ${1:-index} in
index|setup|home) printf 'index' ;;
tmux) printf 'tmux' ;;
pe|workspace) printf 'pe' ;;
htb|workflow|vpn) printf 'htb-workflow' ;;
zsh|terminal|shell) printf 'zsh-terminal' ;;
recon|nmap) printf 'recon' ;;
web) printf 'web' ;;
ad|windows|active-directory) printf 'ad-windows' ;;
privesc|privilege-escalation) printf 'privesc' ;;
transfer|files) printf 'transfer' ;;
tools|toolbox|apps) printf 'toolbox' ;;
*) return 1 ;;
esac
}
guide__list() {
cat <<'PAGES'
setup tmux pe htb zsh recon web ad privesc transfer toolbox
PAGES
}
guide__url() {
command -v python3 >/dev/null 2>&1 || return 1
command -v curl >/dev/null 2>&1 || return 1
_base_port=${PE_GUIDE_PORT:-8765}
_attempt=0
while [ "$_attempt" -lt 10 ]; do
_port=$((_base_port + _attempt))
_url="http://127.0.0.1:$_port/"
if curl -fsS --max-time 1 "${_url}index.html" 2>/dev/null \
| grep -Fq 'Voidwalker Field Manual'; then
printf '%s%s.html\n' "$_url" "$_page"
return 0
fi
# Never mistake an unrelated loopback service for this handbook.
if curl -fsS --max-time 1 "${_url}index.html" >/dev/null 2>&1; then
_attempt=$((_attempt + 1))
continue
fi
mkdir -p "$PE_STATE_DIR" 2>/dev/null || true
chmod 700 "$PE_STATE_DIR" 2>/dev/null || true
# Redirect the whole detached subshell, not only Python. guide__url is
# called through command substitution; if the background shell keeps
# that stdout pipe open, `pe guide` waits instead of returning the URL.
(
cd "$PE_GUIDE_DIR" || exit 1
exec nohup python3 -m http.server "$_port" --bind 127.0.0.1
) </dev/null >"$PE_STATE_DIR/guide-http-$_port.log" 2>&1 &
sleep 0.3
if curl -fsS --max-time 1 "${_url}index.html" 2>/dev/null \
| grep -Fq 'Voidwalker Field Manual'; then
printf '%s%s.html\n' "$_url" "$_page"
return 0
fi
_attempt=$((_attempt + 1))
done
return 1
}
guide_main() {
case ${1:-} in
list|ls) guide__list; return 0 ;;
path) printf '%s\n' "$PE_GUIDE_DIR/index.html"; return 0 ;;
esac
_page=$(guide__page "${1:-index}") || {
pe_die "unknown guide page: $1 (run: pe guide list)"
}
_file="$PE_GUIDE_DIR/$_page.html"
[ -r "$_file" ] || pe_die "HTML handbook is missing: $_file"
_url=$(guide__url 2>/dev/null || true)
if [ -n "$_url" ] && [ "$(uname -s)" = Darwin ] && command -v open >/dev/null 2>&1; then
open "$_url"
elif [ -n "$_url" ] && command -v xdg-open >/dev/null 2>&1 && \
{ [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; }; then
xdg-open "$_url" >/dev/null 2>&1 &
pe_ok "opened the offline handbook: $_page"
elif [ -n "$_url" ]; then
printf '%s\n' "$_url"
pe_info "open this local handbook URL in a browser"
else
printf '%s\n' "$_file"
pe_info "open this file in a browser (the local handbook server is unavailable)"
fi
}