From 01ba6e535da369ac908f1206c1344c0fcf93dc03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Wed, 15 Dec 2021 18:49:22 -0500 Subject: [PATCH 1/2] Add `rmfakecloudctl status` subcommand This subcommand reports whether rmfakecloud-proxy is enabled, currently active, and whether the upstream server has been set. If the upstream server is set, it also checks whether it is reachable and a valid rmfakecloud server. --- package/rmfakecloud-proxy/package | 15 +--- package/rmfakecloud-proxy/rmfakecloudctl | 90 +++++++++++++++++++++--- 2 files changed, 83 insertions(+), 22 deletions(-) diff --git a/package/rmfakecloud-proxy/package b/package/rmfakecloud-proxy/package index 9bdfbda4d..5e3cd0955 100644 --- a/package/rmfakecloud-proxy/package +++ b/package/rmfakecloud-proxy/package @@ -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 " @@ -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() { diff --git a/package/rmfakecloud-proxy/rmfakecloudctl b/package/rmfakecloud-proxy/rmfakecloudctl index 852427c2a..8f6f2f2db 100644 --- a/package/rmfakecloud-proxy/rmfakecloudctl +++ b/package/rmfakecloud-proxy/rmfakecloudctl @@ -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 @@ -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() { @@ -291,8 +334,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." } @@ -306,6 +350,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://\` to \ +set the upstream server." + ;; + enable) if is-enabled; then echo "rmfakecloud-proxy is already enabled." @@ -337,14 +417,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) From 632735a6712fcf5fc8e87862a3d1f5d42405fd2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Wed, 15 Dec 2021 18:55:43 -0500 Subject: [PATCH 2/2] Keep config on uninstall --- package/rmfakecloud-proxy/rmfakecloudctl | 1 - 1 file changed, 1 deletion(-) diff --git a/package/rmfakecloud-proxy/rmfakecloudctl b/package/rmfakecloud-proxy/rmfakecloudctl index 8f6f2f2db..1e418d3e7 100644 --- a/package/rmfakecloud-proxy/rmfakecloudctl +++ b/package/rmfakecloud-proxy/rmfakecloudctl @@ -323,7 +323,6 @@ uninstall() { disconnect-cloud mark-disabled systemctl disable --now rmfakecloud-proxy - rm -f "$conf_dir/config" uninstall-hosts uninstall-certificates set -e