Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmfakecloud-proxy:  Add status subcommand #513

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions package/rmfakecloud-proxy/package
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pkgdesc="Connect Xochitl to a rmfakecloud server"
_url=https://github.com/ddvk/rmfakecloud-proxy
url="$_url"
_upver=0.0.3
pkgver="$_upver-1"
pkgver="$_upver-2"
timestamp=2021-09-26T20:38:44Z
section="utils"
maintainer="Mattéo Delabre <[email protected]>"
Expand Down Expand Up @@ -59,18 +59,7 @@ configure() {
uninstall-hosts
fi

if ! is-enabled; then
cat << MSG

Run the following commands to use rmfakecloud-proxy:

$ rmfakecloudctl set-upstream https://your-server.example.org
$ rmfakecloudctl enable

Replace your-server.example.org with the address of the server you want to use.

MSG
fi
rmfakecloudctl status
}

preremove() {
Expand Down
91 changes: 81 additions & 10 deletions package/rmfakecloud-proxy/rmfakecloudctl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ xochitl_conf_path=/home/root/.config/remarkable/xochitl.conf
# Path to Xochitl data
xochitl_data_dir=/home/root/.local/share/remarkable/xochitl

# ANSI escapes for colors
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
reset="\033[m"

# Disconnect Xochitl from the cloud
#
# This is a necessary step when switching cloud servers to prevent the client
Expand Down Expand Up @@ -140,6 +146,43 @@ addr: $proxy_listen:443
YAML
}

# Check if a given server is an rmfakecloud host
#
# Output: Details on the given host status
# Exit code:
#
# 0 - If the server seems to be a valid rmfakecloud host
# 1 - If it’s definitely not
check-upstream() {
local url="$upstream/service/json/1/test"
local contents
local wget_exit
contents="$(wget --output-document - --quiet "$url" 2>&1)" \
&& wget_exit=$? || wget_exit=$?

if [[ $wget_exit = 8 ]]; then
# HTTP error, the page probably doesn't exist
echo "invalid"
return 1
elif [[ $wget_exit = 4 ]]; then
# Network error, the server probably doesn't exist or is offline
echo "unreachable"
return 1
elif [[ $wget_exit != 0 ]]; then
# Other error
echo "error"
return 1
fi

if [[ $contents != '{"Host":"local.appspot.com","Status":"OK"}' ]]; then
echo "invalid"
return 1
fi

echo "working"
return 0
}

# Generate a local certificate authority, add it to the system trust,
# and create a self-signed proxy certificate
install-certificates() {
Expand Down Expand Up @@ -280,7 +323,6 @@ uninstall() {
disconnect-cloud
mark-disabled
systemctl disable --now rmfakecloud-proxy
rm -f "$conf_dir/config"
uninstall-hosts
uninstall-certificates
set -e
Expand All @@ -291,8 +333,9 @@ help() {
Manage rmfakecloud-proxy. Available commands:

help Show this help message.
status Check the current status of rmfakecloud-proxy.
enable Enable rmfakecloud-proxy.
set-upstream Define the rmfakecloud server.
set-upstream Set the upstream rmfakecloud server.
disable Disable rmfakecloud-proxy."
}

Expand All @@ -306,6 +349,42 @@ if [[ $0 = "${BASH_SOURCE[0]}" ]]; then
shift

case $action in
status)
is-enabled && state="${green}enabled" || state="${red}disabled"
service="$(systemctl is-active rmfakecloud-proxy.service)" || true

if [[ $service = active ]]; then
service="$green$service"
else
service="$yellow$service"
fi

if ! upstream="$(get-upstream)"; then
upstream="(${yellow}not set$reset)"
else
if ! upstream_status="$(check-upstream "$upstream")"; then
upstream="$upstream ($red$upstream_status$reset)"
else
upstream="$upstream ($green$upstream_status$reset)"
fi
fi

echo -e "Status: $state$reset ($service$reset)"
echo -e "Upstream server: $upstream"
echo

if is-enabled; then
echo "Run \`rmfakecloudctl disable\` to disable \
rmfakecloud-proxy."
else
echo "Run \`rmfakecloudctl enable\` to enable \
rmfakecloud-proxy."
fi

echo "Run \`rmfakecloudctl set-upstream https://<server>\` to \
set the upstream server."
;;

enable)
if is-enabled; then
echo "rmfakecloud-proxy is already enabled."
Expand Down Expand Up @@ -337,14 +416,6 @@ if [[ $0 = "${BASH_SOURCE[0]}" ]]; then
systemctl restart rmfakecloud-proxy
fi
fi

if ! is-enabled; then
cat << MSG
Run the following command to enable rmfakecloud-proxy:

$ rmfakecloudctl enable
MSG
fi
;;

help | -h | --help)
Expand Down